It is not working properly because you are sending orders to buy or sell the next bar before you exit your position. TradeStation doesn't know if the trade you are in will close with profits or losses when you are sending your orders. A work around would be..... vars: tt(0),size(1),lasttrade(1); tt=totaltrades; if tt>tt[1] then begin if positionprofit(1)>=0 then size=1; if positionprofit(1)<0 then size=size+1; end; if marketposition=0 then begin buy size contracts next bar at highd(0) on stop; sell size contracts next bar at lowd(0) on stop; end; setexitonclose;
Protrader, thanks for helping me, but i don't see how the code can handle situations when a position is closed and reversed at the same time. in these situations, the if marketposition=0... condition will be false, so TS will not generate an entry signal.
Try this. The problem doing it this way is you're assuming that if openpositionprofit > 0 the trade will close with profits, when that is not neccessarily the case. You could maybe change the 0 to 100 to give yourself some room. Code: vars: tt(0),size(1),tradesize(1); tt=totaltrades; mp=marketposition; if tt>tt[1] then begin if positionprofit(1)>=0 then size=1; if positionprofit(1)<0 then size=size+1; end; if mp<>0 then begin if openpositionprofit>0 then tradesize=1 else tradesize=size+1; end; buy tradesize contracts next bar at highd(0) on stop; sell tradesize contracts next bar at lowd(0) on stop; setexitonclose;