SetExitOnClose

Questions about MultiCharts and user contributed studies.
faraz
Posts: 144
Joined: 25 Feb 2011
Has thanked: 26 times
Been thanked: 57 times

SetExitOnClose

Postby faraz » 20 Jan 2014

Hello waveslider,

The closing tick of the bar is determined by the opening tick of the next bar. So there is no 100% solution to send an order exactly on the closing tick of the bar, but you can get close to it. Here is what I can suggest:
Enable IOG for your signal. Start Recalculating your code two seconds before the bar close. On the next calculation or next tick (depending on the market activity) send the order you need. Example for closing a position:

Code: Select all

[intrabarordergeneration = true]
input: TimeClosePosition(0);
if (currenttime_s >= TimeClosePosition) then
Begin
RecalcLastBarAfter(1);
sell ("EOD_LX") next bar market;
buytocover("EOD_SX") next bar market;
End;
As an input you need to specify the time when you need to close the position, for example 152958 – is 15:29:58.

Henry,

I have a strategy that uses "Open Tomorrow" and "SetExitOnClose" When I used your above IOG code to make sure exit my trades before close, MC gave error. That with Open Tomorrow can't use IOG featured strategy.

Further more I tried to add 1 minute as data2 to issue order 1 minute before close. but again error as "Open tomorrow" can't work with "this bar close" orders as well.

Code: Select all

if t data2 = 1514 then begin sell this bar c; buytocover this bar c; end;
Now I have these issues;
a) need a CONFIRM way to exit my trades before market close.
b) need a way to enter Early close days on holidays for early exit my trades.
c) Strategy is using "Open Tomorrow" order so i) Can't use "IOG" feature ii) or "This bar on close" iii) Or Data2

Please suggest.....

Awaiting

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

Re: closing status of a bar sometimes takes too long

Postby TJ » 20 Jan 2014

Re: "Open Tomorrow"

You don't know the "Open Price" of Tomorrow, so the order is invalid.

You can only trade "Next Bar at Market".

faraz
Posts: 144
Joined: 25 Feb 2011
Has thanked: 26 times
Been thanked: 57 times

Re: closing status of a bar sometimes takes too long

Postby faraz » 20 Jan 2014

"Open Tomorrow" should be replaced by "Next Bar at Market"

Not possible.
Below code will give an idea what we are dealing with.

Code: Select all

if open tomorrow = oPrice then buy next bar at open tomorrow limit;
Thanks

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

Re: closing status of a bar sometimes takes too long

Postby TJ » 20 Jan 2014

Re: Sell this bar c

see post #6 and post #7
viewtopic.php?f=16&t=10811

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

Re: closing status of a bar sometimes takes too long

Postby TJ » 20 Jan 2014

"Open Tomorrow" should be replaced by "Next Bar at Market"
Not possible.
Below code will give an idea what we are dealing with.

Code: Select all

if open tomorrow = oPrice then buy next bar at open tomorrow limit;
Thanks
Please post your complete code for debugging.

faraz
Posts: 144
Joined: 25 Feb 2011
Has thanked: 26 times
Been thanked: 57 times

Re: closing status of a bar sometimes takes too long

Postby faraz » 20 Jan 2014

Re: "Open Tomorrow"

You don't know the "Open Price" of Tomorrow, so the order is invalid.

You can only trade "Next Bar at Market".

Usually MC run the code at 2 times of bar.
a) when the bar is closed
b) when the bar is opening NEXT day after session close.

so in situation of (B) we get the "Open Tomorrow" price.
if you can run this on any chart you will get open tomorrow price.

Code: Select all

Print(d, " ", t, " ",open, " ", Open tomorrow);

See if you can debug this complete Code. Run it on 30 minute E-Mini S&P (ES) ONLY. Your task is to simply make sure you exit your trade at end of US session Every day, With out using "SetExitOnClose" as you know SetExitOnClose require 1 tick before 2 seconds of session closing time and it is good for back testing but not workable in Real Trading.

Code: Select all

vars: oP(0);
oP=open tomorrow;
if open tomorrow = oP then buy next bar open tomorrow limit;
setexitonclose;
Thanks

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

Re: closing status of a bar sometimes takes too long

Postby TJ » 20 Jan 2014

Re: "Open Tomorrow"
You don't know the "Open Price" of Tomorrow, so the order is invalid.
You can only trade "Next Bar at Market".
Usually MC run the code at 2 times of bar.
a) when the bar is closed
b) when the bar is opening NEXT day after session close.
so in situation of (B) we get the "Open Tomorrow" price.
if you can run this on any chart you will get open tomorrow price.

Code: Select all

Print(d, " ", t, " ",open, " ", Open tomorrow);
See if you can debug this complete Code. Run it on 30 minute E-Mini S&P (ES) ONLY. Your task is to simply make sure you exit your trade at end of US session Every day, With out using "SetExitOnClose" as you know SetExitOnClose require 1 tick before 2 seconds of session closing time and it is good for back testing but not workable in Real Trading.

Code: Select all

vars: oP(0);
oP=open tomorrow;
if open tomorrow = oP then buy next bar open tomorrow limit;
setexitonclose;
Thanks
Are you using MultiCharts .net version? or the EasyLanguage version?

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1542 times
Been thanked: 1565 times
Contact:

Re: closing status of a bar sometimes takes too long

Postby JoshM » 20 Jan 2014

Are you using MultiCharts .net version? or the EasyLanguage version?
His code examples show PowerLanguage, not MC .NET/C#. For example, SetExitOnClose in his code example does not exist in MC .NET (its called ExitOnClose there).

faraz
Posts: 144
Joined: 25 Feb 2011
Has thanked: 26 times
Been thanked: 57 times

Re: closing status of a bar sometimes takes too long

Postby faraz » 20 Jan 2014

Are you using MultiCharts .net version? or the EasyLanguage version?
PowerLanguage version

Tried, many possibilities but MC gave error, no luck. Lets see if you can do this.

Thanks

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

Re: closing status of a bar sometimes takes too long

Postby TJ » 20 Jan 2014

Are you using MultiCharts .net version? or the EasyLanguage version?
PowerLanguage version
Tried, many possibilities but MC gave error, no luck. Lets see if you can do this.
Thanks
Your syntax are incorrect.
I would suggest you to start from here:

[FAQ] Autotrade / Backtest / Optimization
viewtopic.php?f=16&t=10811

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

Re: closing status of a bar sometimes takes too long

Postby TJ » 20 Jan 2014

Re: "Open Tomorrow"
You don't know the "Open Price" of Tomorrow, so the order is invalid.
You can only trade "Next Bar at Market".
Usually MC run the code at 2 times of bar.
a) when the bar is closed
b) when the bar is opening NEXT day after session close.
::
Thanks
Please see this article:

How Signals are Calculated
https://www.multicharts.com/trading-sof ... Calculated

faraz
Posts: 144
Joined: 25 Feb 2011
Has thanked: 26 times
Been thanked: 57 times

Re: closing status of a bar sometimes takes too long

Postby faraz » 20 Jan 2014

Your syntax are incorrect.
I would suggest you to start from here:

[FAQ] Autotrade / Backtest / Optimization
viewtopic.php?f=16&t=10811
Please see this article:

How Signals are Calculated
https://www.multicharts.com/trading-soft ... Calculated
TJ,
I have very good experience in TS and Multicharts languages. If you can't solve it then leave it, let others reply. Syntax is also correct. May be you have never worked or seen such codes. Normally [1] is used to get back value for example open[1] but open tomorrow gives you [-1] value. I gave you print command, may be you need to learn from it.

Code: Select all

Print(d, " ", t, " ",open, " ", Open tomorrow);
Thanks

faraz
Posts: 144
Joined: 25 Feb 2011
Has thanked: 26 times
Been thanked: 57 times

Re: closing status of a bar sometimes takes too long

Postby faraz » 20 Jan 2014

Re: "Open Tomorrow"
You don't know the "Open Price" of Tomorrow, so the order is invalid.
You can only trade "Next Bar at Market".
Usually MC run the code at 2 times of bar.
a) when the bar is closed
b) when the bar is opening NEXT day after session close.
::
Thanks
Please see this article:

How Signals are Calculated
https://www.multicharts.com/trading-sof ... Calculated

TJ,

Some times Experience is above BOOK knowledge. For example This link you mentioned
https://www.multicharts.com/trading-sof ... Calculated
Realtime. Normal mode.
The script is calculated on the bar close. Order is placed on this bar close/ next bar open. Order is filled on any tick price within the next bar.

Note this word: The script is calculated on the bar close.

Now lets come to the REAL world. Once trading Session is closed at that moment. MC don't issue order as exchange won't accept them. So NEXT day when market OPEN then MC re-run the strategy and generate order. But BOOK information as above shown is missing this.
So BOOK is wrong (link you gave). I told you correct thing.

This how signals calculated in Real time. Normal mode.
a) when the bar is closed (in Back testing MC already got data so this is correct)
b) when the bar is opening NEXT day after session close. (But in REAL world MC wait for Next bar opening, Thats why "open tomorrow" [-1] is possible)

I hope this helps.

Thanks

faraz
Posts: 144
Joined: 25 Feb 2011
Has thanked: 26 times
Been thanked: 57 times

Re: closing status of a bar sometimes takes too long

Postby faraz » 23 Jan 2014

Hello Every one;

a) This code is suggested for early close days + normal close days, I added extra lines to be on safe side even if we don't get any tick in last 5 seconds;

Code: Select all

[intrabarordergeneration = true]
input: oEarlyDate1yyMMdd(1130113),oEarlyCloseTime24hrHHmmss(151455),oNormalCloseTime24hrHHmmss(151455);

//////For Early Close days/////////////////////////////////////////////////////////////////////////
{Begin calculation with 1 second delay before 2 minutes of Early Close time}
if lastbaronchart and currentdate=oEarlyDate1yyMMdd and (currenttime_s >=(oEarlyCloseTime24hrHHmmss-120)) then begin RecalcLastBarAfter(1);

//Exit trades if required
if lastbaronchart and currentdate=oEarlyDate1yyMMdd and (currenttime_s >=oEarlyCloseTime24hrHHmmss) then begin
RecalcLastBarAfter(1);
sell ("IOG EOD_LX1") next bar market;
buytocover("IOG EOD_SX1") next bar market;
End;
End;
//////END For Early Close days/////////////////////////////////////////////////////////////////////////


//For normal days//////////////////////////////////////////////////////////////////////////////////////
{Begin calculation with 1 second delay before 2 minutes of Normal Close time}
if lastbaronchart and (currenttime_s >= (oNormalCloseTime24hrHHmmss-120)) then Begin RecalcLastBarAfter(1);

//Exit trades if required
if lastbaronchart and (currenttime_s >= oNormalCloseTime24hrHHmmss) then Begin
RecalcLastBarAfter(1);
sell ("IOG EOD_LX2") next bar market;
buytocover("IOG EOD_SX2") next bar market;
End;
End;
//END For normal days//////////////////////////////////////////////////////////////////////////////////////

b) There is a problem with this code it uses PC Time. So if your chart is using EXCHANGE time then it is difficult to send order in time. You must update your PC clock all the time. Solution use this software which can update your clock to even 1 second
http://www.worldtimeserver.com/atomic-clock/


c) if your strategy is using "Open Tomorrow" then this code will not work as IOG is not compatible with "Open Tomorrow" strategies yet. Alternative Suggestion is to use "!From Broker To Strategy MP Synchronizer!" strategy which comes with MC.


d) Or best way is to have a new advance SetExitOnClose which can handle all these issues once and for all. Please vote for it
http://www.multicharts.com/pm/viewissue ... no=MC-1521


Thanks

faraz
Posts: 144
Joined: 25 Feb 2011
Has thanked: 26 times
Been thanked: 57 times

Re: closing status of a bar sometimes takes too long

Postby faraz » 10 May 2014

When using IOG exits 5 seconds before market, your computer clock must be accurate all the time, check this like to automatically update your computer clock.
http://www.multicharts.com/discussion/v ... =5&t=46553



further more if your code is using "This bar on close" then for real trading 2 solutions;
a) change End of day session close time to 1 minute earlier. For example if market closing to 1515 then make it 1514. One problem with it, for early close days you need to change manually for that specify day for one day and then change it back next day.

b) convert your code to IOG. Example IOG code attached.

Thanks
Attachments
code.pla
(8.99 KiB) Downloaded 607 times


Return to “MultiCharts”