I need to get my 'day counter', that counts the number of days a trade has been open, to reset whenever a new trade is entered. I thought I had it working but it appears that the counter is not resetting properly, and is continuing to 'count' after a trade has been exited. What ends up happening is that when a trade signal is generated in the same direction as the previous / exited trade, the 'day counter' thinks the previous trade is still on and exits the CURRENT TRADE as though it was the original trade and had been open past the day counter timed exit instructions. This appears to happen to a single trade, and then the counter correctly resets. That trade is entered in the same bar as the previous trade is exited.
Any idea why this is happening? Is it because the exited trade is followed immediately by a new entry, in the same bar, and so the counter cannot see it / has not had a chance to increment? Is there a way to correct this issue?
I have tried to address all scenarios when the 'day count' should be reset, namely, when a new trade is entered or exited, with the following code:
Code: Select all
var:
MP ( 0 ),
SLB ( false ),
dayCounterLX ( 0 ),
daycounterSX ( 0 );
MP = marketposition;
SLB = sessionlastbar;
if SLB[1] then dayCounterLX = daycounterLX + 1; // for long trade entries
if SLB[1] then dayCounterSX = dayCounterSX + 1; // for short trade entries
if MP <> MP [1] and MP <> 0 or (mp[1] = 1 and mp = 1 and barssinceentry = 0) then daycounterLX = 0;
if MP <> MP [1] and MP <> 0 or (mp[1] = -1 and mp = -1 and barssinceentry = 0) then daycounterSX = 0;
I tried adding 'exit name' as a qualifier but the issue persists.
Code: Select all
if MP <> MP [1] and MP <> 0 or (mp[1] = 1 and mp = 1 and barssinceentry = 0) or (exitname(1) = "TradeCut_LX") then daycounterLX = 0;
if MP <> MP [1] and MP <> 0 or (mp[1] = -1 and mp = -1 and barssinceentry = 0) or (exitname(1) = "TradeCut_SX") then daycounterSX = 0;
Thank you
Tony