exit a trade before the close

Questions about MultiCharts and user contributed studies.
justme
Posts: 56
Joined: 08 Jan 2015
Has thanked: 9 times
Been thanked: 3 times

exit a trade before the close

Postby justme » 07 Dec 2015

Hi-

I need to exit a trade a few seconds before the close... so I am looking at this example

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;
I am not understanding the need for RecalcLastBarAfter(1), what exactly is that doing for the code?
What would happen if it wasn't there?

Are there other alternatives to sending an order out based on time?

Thanks. Gordon

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 » 07 Dec 2015

Hi-

I need to exit a trade a few seconds before the close... so I am looking at this example

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;
I am not understanding the need for RecalcLastBarAfter(1), what exactly is that doing for the code?
What would happen if it wasn't there?

::
The Wiki has full explanation and example of "RecalcLastBarAfter".

justme
Posts: 56
Joined: 08 Jan 2015
Has thanked: 9 times
Been thanked: 3 times

Re: exit a trade before the close

Postby justme » 07 Dec 2015

I have yet to find it, Can you give me a keyword or a link as to where I need to be look?

Thanks.

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

Re: exit a trade before the close

Postby TJ » 07 Dec 2015

I have yet to find it, Can you give me a keyword or a link as to where I need to be look?

Thanks.
Just go to the wiki page,

write whatever you want to know in the search box and press Enter!

The search box is located at the top right corner of your page.


In your case, type "RecalcLastBarAfter" in the search box.

tony
Posts: 420
Joined: 14 Jun 2013
Has thanked: 30 times
Been thanked: 81 times
Contact:

Re: exit a trade before the close

Postby tony » 07 Dec 2015

What time frame are you trading? For example if you are trading 5 minute bars, just have some logic that says

if time = 1555 and MP > 0 then sell next bar at market;
if time = 1555 and MP < 0 then buy to cover next bar at market;

Something to that affect will work if you are trading shorter than daily bars, which I assume you are based on your IOG setting.

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

Re: exit a trade before the close

Postby faraz » 08 Dec 2015

I am not understanding the need for RecalcLastBarAfter(1), what exactly is that doing for the code?
What would happen if it wasn't there?

Gordon

MC IOG run on every Tick. But if there is NO tick in the market strategy will not run and it is possible that you could miss those few seconds to exit before market close and your positions does not exit.

So RecalcLastBarAfter(1) make sure it run strategy after 1 seconds even if there is NO tick.

Thanks

justme
Posts: 56
Joined: 08 Jan 2015
Has thanked: 9 times
Been thanked: 3 times

Re: exit a trade before the close

Postby justme » 08 Dec 2015

Thank you both very much.

The code sample above is basically what I used. But there really needs to be a 2nd exit in place to emulate this "realtime" exit. If this order will exit the trade 15 seconds before the close today, when I open the charts of tomorrow, a 2nd exit will need to run the exit, so something along these lines

Code: Select all

If time=1600 then sell("LX.BarClose") this bar on close;
In realtime, this won't fire as the trade has already exited 14 seconds ago, but this will keep the system on track as the charts get refreshed.

Thanks. Gordon


Return to “MultiCharts”