Close at end of day (again, I know)  [SOLVED]

Questions about MultiCharts and user contributed studies.
quod_erat
Posts: 33
Joined: 14 Mar 2020
Has thanked: 4 times
Been thanked: 1 time

Close at end of day (again, I know)

Postby quod_erat » 23 Mar 2020

New to the forum, and really enjoying Multicharts. But there’s one thing I can’t quite figure out, and I hope someone can help.

I’m using the Portfolio Trader, and want to close a strategy before close of RTH session (4 pm EST). I’ve read through just about every thread on it, and I’m using the code below. But because my strategy runs on 15 min. candles, the order to close gets sent at exactly 4 pm – and hence into the after-hours market. Which wouldn’t be a big deal (the securities I trade have a lot of volume during AH), but it makes it impossible to close on time on Fridays.

My thinking here is that I could use a data2 feed with a lower time resolution (let’s say, 1 min.), and use that feed as the time trigger for closing all positions. But I’m unsure how to do that, or if it’s even a possibility.

Code:

Code: Select all

[intrabarordergeneration = False] input: TimeClosePosition(155800); if (currenttime_s > TimeClosePosition) then Begin sell ("EOD_LX") this bar on close; %is this where specifying data2 feed would make sense? buytocover("EOD_SX") this bar on close; End;

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Close at end of day (again, I know)

Postby TJ » 23 Mar 2020


User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Close at end of day (again, I know)

Postby TJ » 23 Mar 2020

You can use this code:

Code: Select all

Begin sell ("EOD_LX") Next Bar at Market; buytocover("EOD_SX") Next Bar at Market; End;

ps. Many brokers suggest you should allow 3 min for end of day orders.

quod_erat
Posts: 33
Joined: 14 Mar 2020
Has thanked: 4 times
Been thanked: 1 time

Re: Close at end of day (again, I know)

Postby quod_erat » 23 Mar 2020

Thanks, you're right -- I had `Next Bar at Market;` initially, and changed for testing purposes.

But it doesn't address my problem yet, because on a 15 min candle, I'd have to close at 3:45 to be out of the trade at 4:00. Is it possible to use a data2 stream with 1-min resolution to trigger the sell/buytocover signals on a strategy that runs on 15 min. resolution?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Close at end of day (again, I know)

Postby TJ » 23 Mar 2020

Yes, use multi-data, that's how you control the timing.

You need to put the fastest fractal as data1.

see post #18
viewtopic.php?t=10811

quod_erat
Posts: 33
Joined: 14 Mar 2020
Has thanked: 4 times
Been thanked: 1 time

Re: Close at end of day (again, I know)

Postby quod_erat » 23 Mar 2020

But can I enter a position from data2? If I ran data1 as 1-min, I'd have to run my strategy on data2, e.g.:

Code: Select all

Buy ( "MomLE" ) next bar at High of data2
I was under the impression that you can't submit orders from data2. But it's possible that I remember that from a different language framework ...

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Close at end of day (again, I know)

Postby TJ » 23 Mar 2020

You can only trade from Data1.
That's why you always put the fastest fractal on data1.

You can use any data series to trigger your analysis.

quod_erat
Posts: 33
Joined: 14 Mar 2020
Has thanked: 4 times
Been thanked: 1 time

Re: Close at end of day (again, I know)

Postby quod_erat » 23 Mar 2020

Ok, I think I got it. So if my signals come from 15-minute bars, I would put 15min on data2. Then, for example, if I wanted to exit five 15-min bars after entering a position (just using one of the pre-made examples), I'd write:

Code: Select all

[IntrabarOrderGeneration = false] inputs: BarToExitOn( 5 ); if BarsSinceEntry = BarToExitOn of data2 then Buy To Cover ( "TimeBarsSX" ) next bar at market ;
Is that about right? Sorry, I don't see a lot of resources about how to include dataX feeds in your syntax.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Close at end of day (again, I know)

Postby TJ » 23 Mar 2020

I think you have got the idea.

see Post #5
viewtopic.php?t=6929

quod_erat
Posts: 33
Joined: 14 Mar 2020
Has thanked: 4 times
Been thanked: 1 time

Re: Close at end of day (again, I know)

Postby quod_erat » 24 Mar 2020

I'll need to think more about this, because it's not quite working yet. I understand the logic you describe, but here's the problem. Let's say this is the logic to open a position:

Code: Select all

if condition1 then Buy next bar at High + 1 point stop ;
The code generates a signal based on some vars, and then sends a buy/sell order at the beginning of the next 15-min bar. The 15-min bar part is key to avoid overtrading. If data1 is a 1-min resolution, `next bar` is the next 1-minute bar, and I overtrade (this happened to me when I tried out the new code today).

So here's what I'm wondering. Is it possible to send a Close All Positions signal out through the Money Management Signal functionality to *all* my strategies? I think -- based on the manual -- that the PMMS uses the resolution of the first instrument in data1. If, as describe above, my data1 needs to be 15-min, could I create a dummy strategy with a 1-minute resolution, which PMMS would then use for the end-of-day code I have in the first post?

[Edit: nevermind. If I read this correctly (https://www.multicharts.com/trading-sof ... nt_Signals) the MMS takes its cue from the first data1 row of each strategy. So a dummy strategy approach wouldn't work.

Salzburg
Posts: 140
Joined: 19 Sep 2018
Has thanked: 9 times
Been thanked: 35 times

Re: Close at end of day (again, I know)

Postby Salzburg » 25 Mar 2020

An idea, why would you not just create a custom session template, to end 1minute before real RTH ?
This is how i have solved same problem using daily bars thought... but still should work solving your problem.

I just end my session at 15 59, since real session ends at 16 00... then my orders get executed 1 minute before close, and problem solved.

Regards

quod_erat
Posts: 33
Joined: 14 Mar 2020
Has thanked: 4 times
Been thanked: 1 time

Re: Close at end of day (again, I know)

Postby quod_erat » 27 Mar 2020

An idea, why would you not just create a custom session template, to end 1minute before real RTH ?
This is how i have solved same problem using daily bars thought... but still should work solving your problem.

I just end my session at 15 59, since real session ends at 16 00... then my orders get executed 1 minute before close, and problem solved.

Regards
[EDIT] But a custom session template doesn't generate closing orders at end of session -- unless there's code for that I'm not aware of?

I solved the problem differently, but I'm intrigued.

Salzburg
Posts: 140
Joined: 19 Sep 2018
Has thanked: 9 times
Been thanked: 35 times

Re: Close at end of day (again, I know)  [SOLVED]

Postby Salzburg » 08 Apr 2020

Quod, with custom session ending 1 minute before real close.... i am able to open position end of day, and close positions end of day..
So its working great..
If my condition is met, i exit end of my session, and orders are sent 1 minute before real close.

Regards

User avatar
Vlada MultiCharts
Posts: 293
Joined: 22 Apr 2020
Has thanked: 8 times
Been thanked: 76 times

Re: Close at end of day (again, I know)

Postby Vlada MultiCharts » 21 Jul 2020

Hello quod_erat,

Here is the suggestion from our engineer team:

1) Create the first data series with 15 Minutes resolution;
2) Create the second data series with 1 Minute resolution;
3) Apply the signal:

Code: Select all

[intrabarordergeneration = true] once cleardebug; input: TimeClosePosition(155800); input: TimeSession(160000); if (time_s data2 >= TimeClosePosition and time_s data2 <= TimeSession) then Begin RecalcLastBarAfter(1); sell ("EOD_LX") next bar market; buytocover("EOD_SX") next bar market; End;
4) Disable the Realtime-history Matching option by going Format -> Signal -> Properties -> Backtesting tab.

Also, we suggest you check this forum thread.


Return to “MultiCharts”