How to get a 'day counter' to reset when a new trade is entered in the same direction as the previous trade

Questions about MultiCharts and user contributed studies.
tonyt
Posts: 48
Joined: 21 May 2021
Has thanked: 20 times
Been thanked: 1 time

How to get a 'day counter' to reset when a new trade is entered in the same direction as the previous trade

Postby tonyt » 22 Mar 2022

Hello everyone,

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;
Is there a way to get the counter to reset when a trade signal in generated in the same direction (long/short) as the last trade and in the same bar?

Thank you

Tony
Attachments
Day Counter Issue.png
(158.7 KiB) Not downloaded yet
Day Counter Issue Trade List.png
(32.2 KiB) Not downloaded yet

User avatar
Tammy MultiCharts
Posts: 200
Joined: 06 Aug 2020
Has thanked: 6 times
Been thanked: 65 times

Re: How to get a 'day counter' to reset when a new trade is entered in the same direction as the previous trade

Postby Tammy MultiCharts » 01 Apr 2022

Hello Tony,

Please have a look at TotalTrades keyword, it might help you achieve your goal.


Return to “MultiCharts”