Inside my strategy I need to set an OCO order with Stoploss and Target immediately after the system has opened a position.
I NEED to set the Stoploss only at the close of the candle where the entry order has been filled, because i need some time for the exact calculation of the stop loss value (in my case the TRADEstopLoss value ).
So I thought to simply put the condition " if barstatus = 2 " before the stop order and this worked just fine in the backtest, but TODAY in realtime (with real money) testing when the system entered a position it was able to set up the target easily but the STOP loss order keeping to be set and then rejected/canceled at the end of every bar.
The system is running over a 100tick chart and really at the end of every bar , when market position is 1 or -1, it continued to set the stop loss and cancel it the second later.
So basically I only had a target on the chart, the stop loss appear for a millisecond and then disappear the second after and by this way I cannot protect myself from sudden movement.
This is the code I'm using ( short position example ):
Code: Select all
if marketposition = -1 then
begin
if currentcontracts = 1 then
begin
buytocover ( " Target SE ")1 contract next bar at entryprice - (TargetPoints * TickSize ) limit;
if barstatus = 2 then buytocover ( " Stop SE ")1 contract next bar at TRADEstopLoss + StopOffSet stop;
if time >= Time_CLOSE_SESSION - 10 and time < Time_CLOSE_SESSION - 1 then buytocover("SE CLOSE SESSION") 1 ontract next bar at market;
end;
......................................
...............................
.............
Thanks
David