Trade counter variable

Questions about MultiCharts and user contributed studies.
paulc
Posts: 59
Joined: 26 Sep 2012
Has thanked: 13 times
Been thanked: 2 times

Trade counter variable

Postby paulc » 05 Jan 2022

Hi all,

I am trying to create a variable to count the number of trades in a bar.

So far I have used (without success):
vars: tradescounter(0),MP(0);
If barssinceentry(1)<>barssinceentry(0) then tradescounter = 0;
MP=marketposition;
If ( MP <> 0 ) and (MP[1] <> MP ) then tradescounter = tradescounter+ 1;
The issue I have is that the counter recognises a trade only on the following bar and not the same bar; i.e. intrabar. I am trading realtime and have the 'Recalculate on market position change' at broker set to yes in Autotrading tab. I am using stop/limit orders which trigger intra bar as expected.

How do I recalculate the variable if not using the 'recalculate on market position change' variable (and yes, the market position does change).

Any ideas?

Thanks.

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

Re: Trade counter variable

Postby Tammy MultiCharts » 12 Jan 2022

Hello paulc,

The reason why the TradesCounter variable does not recognize a trade inside the bar is that it updates once per bar on the Close.

To make the variable update inside the bar, you need to declare it with IntraBarPersist.

Here's an example:

Code: Select all

vars: intrabarpersist tradescounter(0), intrabarpersist MP(0);

paulc
Posts: 59
Joined: 26 Sep 2012
Has thanked: 13 times
Been thanked: 2 times

Re: Trade counter variable

Postby paulc » 12 Jan 2022

Thanks Tammy. I will try that out.

But I thought the purpose of the setting: 'Recalculate on market position change at broker' in the Autotrading tab was intended to update intrabar? What is being recalculated using this setting if not the strategy?

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

Re: Trade counter variable

Postby Tammy MultiCharts » 12 Jan 2022

Hi,

You are right, "Recalculate on market position change at broker" option will call strategy calculation.
However, the value of the variable will not update upon strategy recalculation unless it's declared with IntraBarPersist keyword.


Return to “MultiCharts”