Limiting trades per session

Questions about MultiCharts and user contributed studies.
User avatar
ZaphodB
Posts: 64
Joined: 12 Feb 2019
Has thanked: 20 times
Been thanked: 2 times

Limiting trades per session

Postby ZaphodB » 09 Mar 2022

I've searched and haven't found a great solution for limiting the number of trades per session. I made the below code but it's giving me an error message "Wrong session index: -1". Does anyone have a better way of doing this? I don't think I can use the EntriesToday function because it's a futures chart where the session spans two different calendar days. I am using it on a minute chart but would like to be able to use it on others. This is what I have so far:

Code: Select all

Variables: ThisSessStartTime (0) , BarsBackSessStarted (0) , GoodToTrade (False) ; ThisSessStartTime = SessionStartTime ( 0, CurrentSession(0) ) ; //Finds time when the current session started BarsBackSessStarted = MRO (Time = ThisSessStartTime, 1500, 1) ; //Finds number bars back where the session started If TotalTrades = TotalTrades[BarsBackSessStarted] then GoodToTrade = True else GoodToTrade = False ;

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

Re: Limiting trades per session

Postby Tammy MultiCharts » 24 Mar 2022

Hello Zaphod,

The wrongSessionIndex error appears in sessionStartTime(0, CurrentSession(0)) function when CurrentSession(0) = -1
Please add a condition so that the function is only called when CurrentSessions(0) is equal to 0.

Code: Select all

if CurrentSession(0) <> -1 then begin sessionStartTime(0, CurrentSession(0)) end;
Also please have a look at another sample to achieve what you are looking for:

Code: Select all

once cleardebug; var: BarsBackSessStarted (0); var: trade_count(0); if SessionLastBar then begin BarsBackSessStarted = Symbol_CurrentBar + 1; trade_count = 0; end; var: intrabarpersist tr(0); tr = totaltrades; print(datetimetostring(datetime), "; totaltrades = ", totaltrades, "; endSession = ", SessionLastBar, "; TRADE_COUNT_IN_SESSION = ", trade_count); if trade_count <= 4 then begin if mod(symbol_currentbar, 22) = 0 then buy next bar at market; if mod(symbol_currentbar, 23) = 0 then sellshort next bar at market; end; if tr[0] <> tr[1] then begin trade_count = trade_count + 2; //Buy and Sellshort end;

User avatar
ZaphodB
Posts: 64
Joined: 12 Feb 2019
Has thanked: 20 times
Been thanked: 2 times

Re: Limiting trades per session

Postby ZaphodB » 24 Mar 2022

Hello Zaphod,

The wrongSessionIndex error appears in sessionStartTime(0, CurrentSession(0)) function when CurrentSession(0) = -1
Please add a condition so that the function is only called when CurrentSessions(0) is equal to 0.

Code: Select all

if CurrentSession(0) <> -1 then begin sessionStartTime(0, CurrentSession(0)) end;
Also please have a look at another sample to achieve what you are looking for:

Code: Select all

once cleardebug; var: BarsBackSessStarted (0); var: trade_count(0); if SessionLastBar then begin BarsBackSessStarted = Symbol_CurrentBar + 1; trade_count = 0; end; var: intrabarpersist tr(0); tr = totaltrades; print(datetimetostring(datetime), "; totaltrades = ", totaltrades, "; endSession = ", SessionLastBar, "; TRADE_COUNT_IN_SESSION = ", trade_count); if trade_count <= 4 then begin if mod(symbol_currentbar, 22) = 0 then buy next bar at market; if mod(symbol_currentbar, 23) = 0 then sellshort next bar at market; end; if tr[0] <> tr[1] then begin trade_count = trade_count + 2; //Buy and Sellshort end;
Excellent, thank you!! I will have to try these out and look through them further later.

User avatar
ZaphodB
Posts: 64
Joined: 12 Feb 2019
Has thanked: 20 times
Been thanked: 2 times

Re: Limiting trades per session

Postby ZaphodB » 30 Mar 2022

There is now other information I'm trying to get about a previous session and wish I could use built in function like HighD (but HighSession) instead. To be less resource intensive (than the code above), I thought about changing the time zone so that when the session starts at 17:00 Central Time in Chicago, my chart in Multicharts has a time of 00:00. I tried to accomplish this by setting the CME Exchange Time Zone in Quote Manager to GMT+2 and then editing the symbol so the sessions line up with that. I also made sure to edit the settings for the symbol on the chart so it is using Exchange time zone instead of local. However, it's not displaying GMT+2 on the chart, it's still displaying Central Time, even after using ctrl+R to reload data...


Return to “MultiCharts”