//Global Variables Var Position: integer; EntryPrice: real; ExitPrice: real; ExitStop: real; ScalpSize: real; StopSize: real; TrailBE: real; TrailStoptoBE: real; TickSize: real; HH: real; LL: real; ScalpS, StopS, Trail, Longtrade, Shorttrade, Flat: integer; BuySignal: boolean; SellSignal: boolean; BuySH,SellSH: integer; ReverseSignal: boolean; ReverseTrades: boolean; TrendUp: boolean; TrendDn: boolean; Chartname: string; procedure InitializeVariables; var Commission: real; BuyArrow: integer; SellArrow: integer; OutArrow: integer; ShowBoxes: boolean; Variablesh: integer; w: integer; begin {Set these variables according to your needs} Commission:=6; {deduct $6 round trip for each trade} ChartName:=''; {if multiple charts are open in your workspace enter the chartname here} LongTrade:=1; ShortTrade:=-1; Flat:=0; Position:=Flat; ScalpS:=25; {scalp objective in Ticks} StopS:=10; {protective stop in Ticks} Trail:=20; {move Stop if Profit >= Trail} TrailStoptoBE:=5; {Trailing Stop moved to Entry +/- ticksize*TrailStoptoBE} ReverseTrades:=true; {Are ReverseTrades allowed or not} {program code starts here. Change with caution} BuySignal:=false; {flag to indicate Buy setup} SellSignal:=false; {flag to indicate Sell setup} ReverseSignal:=false; {flag to indicate Reverse setup} BuyArrow:=8; {show Up arrow and the word 'Long'} SellArrow:=13; {show Down arrow and the word 'Short'} OutArrow:=6; {show Left arrow and the word 'Out'} ShowBoxes:=true; {mark trade with a square box on the bar} ResetTrades(Commission,BuyArrow,SellArrow,OutArrow,ShowBoxes); FindWindow(eChart,Chartname); {locate the window with the chart} Variablesh:=FindStudyName(eDYO,'DYO: Variables'); {Use a DYO named Variables to transfer values from your chart to the Tradesystem} Ticksize:=GetStudy(Variablesh,210); {Line A contains the Ticksize as reported by the $Q variable} BuySH:=FindStudyName(eDYO,'DYO: BuySignal'); {Line J = True constitutes a BuySignal -> GetStudy (BuySH,219)} SellSH:=FindStudyName(eDYO,'DYO: SellSignal'); {Line J = True constitutes a SellSignal -> GetStudy (SellSH, 219)} TrailBE:=Trail*Ticksize; {calculate the Profit at which the Stop is moved to Breakeven +1} Scalpsize:=ScalpS*Ticksize; {calculate the Profittarget} StopSize:=StopS*Ticksize; {calculate the Stop} Remove(eLine); {Remove any Trademarkers from a previous run from the chart} {output(eClear);} {I don't want to clear the ESPL output window as that makes it easier to compare different Stop and Targetsettings} {I use a seperate subroutine to do that task instead} end; Procedure OutClear; begin output(eclear); FindWindow(eChart,Chartname); {locate the window with the chart} Remove(eLine); {Remove any Trademarkers from a previous run from the chart} end; procedure TestExit(w: integer); var bExit: boolean; begin bExit:=false; if Position=Longtrade then begin {currently long} if Low(w)<=ExitStop then begin {test for stopped out} if Open(w)ExitPrice then begin {test for scalp objective} if Open(w)>ExitPrice then ExitPrice:=Open(w); {check for gap across goal} bExit:=true; {set exit flag} end else if ReverseSignal then begin If Sellsignal then begin ExitPrice:=Open(w)-Ticksize; {we can not be sure to have a fill at the open, so we assume we have to pay the spread} bExit:=true; end; ReverseSignal:=false; end; end else if Position=Shorttrade then begin {currently short} if High(w)>=ExitStop then begin {test for stopped out} if Open(w)>ExitStop then ExitPrice:=Open(w) {check for gap across stop} else ExitPrice:=ExitStop; bExit:=true; {set exit flag} end else if Low(w)1 then begin If (Position = Shorttrade) then begin if EntryPrice-Low(w)>=TrailBE then ExitStop:=Entryprice-ticksize*TrailStoptoBE end; If (Position = Longtrade) then begin if High(w)-EntryPrice>=TrailBE then ExitStop:=Entryprice+ticksize*TrailStoptoBE end; end; {If Reverse Trades are allowed we need to test for the criteria before we exit the trade} If ReverseTrades then Begin If Position=Shorttrade then begin if Buysignal then Reversesignal:=true; end; If Position=Longtrade then begin if Sellsignal then Reversesignal:=true; end; end; TestExit(w); {did this bar trigger an exit} TestTrade(w); {did the previous bar trigger a trade} Buysignal:=GetStudy(BuySH,219,w); SellSignal:=GetStudy(SellSH,219,w); end; end; procedure Report; begin Trade(eOut); {close-out the last open trade} TradeReport(True); {print the results in the output window} end; procedure Tradesystem; begin InitializeVariables; Writeln(); Writeln ('Target: ',Scalps,' Stop: ',Stops,' TrailBE: ',Trail,'/',TrailStoptoBE, ' Tick: ',format('%2.4f',Ticksize)); Calculate; Report; end; {**********Main Program**********} begin InitializeVariables; if ESPL = 1 then OutClear; if ESPL = 2 then Tradesystem; end; =Last(w); {check for gap across goal} bExit:=true; {set exit flag} info:=' Market close'; end; if ReverseSignal then begin if BuySignal then begin ExitPrice:=Open(w)+Ticksize; {we can not be sure to have a fill at the open, so we assume we have to pay the spread} bExit:=true; info:='Reverse Tradesignal received'; end; reverseSignal:=false; end; end; if bExit then begin Trade(eOut,w,ExitPrice,ContractsOpen); {exit the position} If tradeinfo then Writeln(Tradenumber,'-',w,': Exit ',ContractsOpen,'# at ',format('%10.4f',Exitprice),info); If tradeinfo then Writeln(); if StopReverse=True then begin if bStop then begin {we will Reverse only if a Stop is hit} if Position=Longtrade then begin SellSignal:=true; BuySignal:=false; StopHit:= true; Stopprice:=ExitPrice; end else if Position=Shorttrade then begin BuySignal:=true; SellSignal:=false; StopHit:=true; Stopprice:=ExitPrice; end end; end; Position:=Flat; {position goes to neutral} ContractsOpen:=0; TrailAdjusted:=false; end; end; procedure TestTrade(w: integer); var bExit: boolean; info: string; begin info:=''; if Position=Flat then begin {do not trade if already in a position} if BuySignal then begin {did prior bar give buy signal} if not SellSignal then begin If Stophit=true then begin Entryprice:=Stopprice+ticksize; Info:=' StopReverse' end else Begin EntryPrice:=open(w); {trade at open price} end; Tradenumber:=Tradenumber+1; Trade(eBuy,w,EntryPrice,Contracts); ContractsOpen:=ContractsOpen+Contracts; Stophit:=false; Stopprice:=0; ExitPrice:=EntryPrice+ScalpSize; {set scalp objective} ExitStop:=EntryPrice-StopSize; {set protective stop} TrailBEprice:=EntryPrice+TrailBE; {move Stop to BE} AddPrice:=EntryPrice+AddSize; {calculate Add-On Price} If Longinfo=true then begin If tradeinfo then Writeln(Tradenumber,'-',w,': Long ',Contracts,'# at ',format('%10.4f',Entryprice),' Stop:',format('%10.4f',ExitStop),' Target:',format('%10.4f',Exitprice),' Add:',format('%10.4f',Addprice),' Trail:',format('%10.4f',TrailBEprice),info); end else begin If tradeinfo then Writeln(Tradenumber,'-',w,': Long ',Contracts,'# at ',format('%10.4f',Entryprice),info); end; Position:=Longtrade; {new position long} BuySignal:=false; {Reset Tradesystem} SellSignal:=false; ReverseSignal:=false; end; end; if SellSignal then begin {did prior bar give sell signal} if not BuySignal then begin If Stophit=true then begin Entryprice:=Stopprice-ticksize; Info:=' StopReverse' end else Begin EntryPrice:=open(w); {trade at open price} end; Tradenumber:=Tradenumber+1; Trade(eSell,w,EntryPrice,Contracts); ContractsOpen:=ContractsOpen+Contracts; Stophit:=false; Stopprice:=0; ExitPrice:=EntryPrice-ScalpSize; {set scalp objective} ExitStop:=EntryPrice+StopSize; {set protective stop} TrailBEprice:=EntryPrice-TrailBE; {move Stop to BE} AddPrice:=EntryPrice-AddSize; {calculate Add-On Price} If Longinfo=true then begin If tradeinfo then Writeln(Tradenumber,'-',w,': Short ',Contracts,'# at ',format('%10.4f',Entryprice),' Stop:',format('%10.4f',ExitStop),' Target:',format('%10.4f',Exitprice),' Add:',format('%10.4f',Addprice),' Trail:',format('%10.4f',TrailBEprice),info); end else begin If tradeinfo then Writeln(Tradenumber,'-',w,': Short ',Contracts,'# at ',format('%10.4f',Entryprice),info); end; Position:=Shorttrade; {new position short} BuySignal:=false; {Reset Tradesystem} SellSignal:=false; ReverseSignal:=false; end ; end; end; end; procedure Calculate; var w: integer; study: integer; stop: integer; begin {A common mistake when designing a Tradesystem is taking the trade at the open of the Bar generating the Tradesignal} {This is not possible in Realtime, as you don't know whether there is a Tradesignal present or not until a bar closes} {with the exception of Tradesignals calculated at the open of the bar of course} {Therefore our Tradesystem will first test a bar for a possible trade exit} {If we allow for Reverse Trades need to make sure, that we first close the position at the Open of the bar and then take the next trade} {Then we will look for a Tradesignal generated by the previous bar and take the trade at the open of this bar} {Last we will test at the close of the current bar whether a tradesignal was generated by our System} FindWindow(eChart,Chartname); {locate the window with the chart} for w:=1 to BarEnd do begin {calculate across all chart bars} {If Reverse Trades are allowed we need to test for the criteria before we exit the trade} If ReverseTrades then Begin If Position=Shorttrade then begin if Buysignal then Reversesignal:=true; end; If Position=Longtrade then begin if Sellsignal then Reversesignal:=true; end; end; TestExit(w); {did this bar trigger an exit} TestTrade(w); {did the previous bar trigger a trade} BuySignal:=GetStudy(BuySH,219,w); SellSignal:=GetStudy(SellSH,219,w); end; end; procedure Report; begin writeln(); Trade(eOut); {close-out the last open trade} TradeReport(True); {print the results in the output window} end; procedure Tradesystem; Var info:string; begin InitializeVariables; Writeln(); Writeln ('Target: ',Scalps,' Stop: ',Stops,' TrailBE: ',Trail,'/',TrailStoptoBE, ' Tick: ',format('%2.4f',Ticksize)); if Reversetrades then Writeln ('Tradesignal may reverse trade') else Writeln('No Reverse trades on new Signal'); if Stopreverse then Writeln('Stoploss reverse allowed') else Writeln('No Stop reverse trades'); If Addtrade then begin Writeln('Add to trades at '+format('%4.0f',AddS)); If AddadjustTarget then Writeln('Targetprice is adjusted based on Average Entry price') else Writeln('Targetprice remains fixed'); end else Writeln('No Add-On trades allowed'); Calculate; Report; end; {**********Main Program**********} begin InitializeVariables; if ESPL = 1 then OutClear; if ESPL = 2 then Tradesystem; end;