# NEOTICKER DATA BEGIN ScriptType=Indicator Description=Example Delphi Pair System Name=ex_del_pair_sys Language=DelphiScript Links=4 MinBars=0 TimerInterval=100 EarlyBinding=0 MetaStyle=Normal ValueRange=Same as Source Placement=New Pane Multiplot_num_plots=4 Multiplot_color_0=255 Multiplot_style_0=Line Multiplot_width_0=1 Multiplot_enabled_0=1 Multiplot_name_0=system equity Multiplot_breakstyle_0=0 Multiplot_color_1=255 Multiplot_style_1=Line Multiplot_width_1=1 Multiplot_enabled_1=0 Multiplot_name_1=spread calculation Multiplot_breakstyle_1=0 Multiplot_color_2=255 Multiplot_style_2=HLine Multiplot_width_2=1 Multiplot_enabled_2=0 Multiplot_name_2=+vb Multiplot_breakstyle_2=0 Multiplot_color_3=255 Multiplot_style_3=HLine Multiplot_width_3=1 Multiplot_enabled_3=0 Multiplot_name_3=-vb Multiplot_breakstyle_3=0 UpdateByTick=0 FloatMarker=1 DepthData=0 TradingSystemUI=1 PrimaryLinkOnly=0 NotifyOnRemoval=0 Param_count=2 Param_name_0=period Param_inuse_0=1 Param_type_0=integer.gte.1 Param_default_0=30 Param_name_1=Volatility Factor Param_inuse_1=1 Param_type_1=real Param_default_1=1.5 Explanation_Lines=0 # NEOTICKER DATA END function ex_del_pair_sys : double; var hv1, hv2, cr : variant; xbelow_posvb, xabove_negvb, xzero : variant; vb, sp : double; begin if not (data1.valid[0] and data2.valid[0]) then begin itself.successall := false; exit; end; //daily data series for link1 and link2 to calculate volatility band. itself.compressseries('OneDaily', '1', ppDaily, 1); itself.compressseries('TwoDaily', '2', ppDaily, 1); hv1 := itself.updateindicatorex(1); if hv1 = nil then hv1 := itself.makeindicatorex('hv1', 1, 'historical_vola', ['$OneDaily'], [params.items['period'].str,'1']); hv2 := itself.updateindicatorex(2); if hv2 = nil then hv2 := itself.makeindicatorex('hv2', 2, 'historical_vola', ['$TwoDaily'], [params.items['period'].str,'1']); cr := itself.updateindicatorex(3); if cr = nil then cr := itself.makeindicatorex('cr', 3, 'correl', ['$OneDaily','$TwoDaily'], [params.items['period'].str]); if hv1.valid[1] and hv2.valid[1] and cr.valid[1] then begin vb := (hv1.value[1]+hv2.value[1])*ntlib.sqrt(1/252)*(1-cr.value[1]); itself.setseries('nvb', -1*vb, true); itself.setseries('pvb', vb, true); itself.plot[3] := vb; itself.plot[4] := -1*vb; end else begin vb := 0; itself.setseries('nvb', vb, false); itself.setseries('pvb', vb, false); itself.successex[3] := false; itself.successex[4] := false; end; if (itself.series('OneDaily').close[1]>0) and (itself.series('TwoDaily').close[1]>0) then begin sp := (data1.value[0]/itself.series('OneDaily').close[1])- (data2.value[0]/itself.series('TwoDaily').close[1]); itself.setseries('xsp', sp, true); end else itself.setseries('xsp', 0, false); xabove_negvb := itself.updateindicatorex(4); if xabove_negvb = nil then xabove_negvb := itself.makeindicatorex('xab', 4, 'xabove', ['$xsp','$nvb'], ['']); xbelow_posvb := itself.updateindicatorex(5); if xbelow_posvb = nil then xbelow_posvb := itself.makeindicatorex('xbl', 5, 'xbelow', ['$xsp','$pvb'], ['']); xzero := itself.updateindicatorex(6); if xzero = nil then xzero := itself.makeindicatorex('xz', 6, 'xcrossconst', ['$xsp'], ['0']); if (xabove_negvb.value[0]>0) and itself.series('nvb').valid[0] and Trade.OpenPositionFlatEx[1] and Trade.OpenPositionFlatEx[2] then Trade.LongStopEx(1, data1.high[0], trade.defaultordersize, otfFillorKill, 'Long Pair A'); if trade.Openpositionlongex[1] and trade.openpositionflatex[2] then trade.shortatmarketex(2, trade.defaultordersize, 'Short Pair B'); if (xbelow_posvb.value[0]>0) and itself.series('pvb').valid[0] and Trade.OpenPositionFlatEx[1] and Trade.OpenPositionFlatEx[2] then Trade.ShortStopEx(1, data1.low[0], trade.defaultordersize, otfFillorKill, 'Short Pair A'); if trade.Openpositionshortex[1] and trade.openpositionflatex[2] then trade.longatmarketex(2, trade.defaultordersize, 'Long Pair B'); if trade.openpositionlongex[1] and trade.openpositionshortex[2] then begin if xzero.value[0]>0 then trade.exitallcurrentpositions('Long Exit at Target'); if spparams.items['Volatility Factor'].real*vb then trade.exitallcurrentpositions('Short Exit Stop'); end; itself.plot[1] := trade.currentequity; itself.plot[2] := sp; end;