a strategy close all open position in a preset time

Questions about MultiCharts and user contributed studies.
User avatar
johnmok
Posts: 78
Joined: 18 Oct 2011
Has thanked: 26 times
Been thanked: 7 times

a strategy close all open position in a preset time

Postby johnmok » 24 Oct 2011

hi all,

i want to day trade futures and therefore i need to close all open positions when each session end.

therefore it comes up the following script, but it does not work, i wonder is there any error in the code, thanks all.

p.s. i use 5 min chart

inputs: xtimeam(1155), xtimepm(1610);

if time = xtimeam or time = xtimepm then
begin
buytocover this bar;
sell this bar;
end;

User avatar
Henry MultiСharts
Posts: 8520
Joined: 25 Aug 2011
Has thanked: 1213 times
Been thanked: 2741 times

Re: a strategy close all open position in a preset time

Postby Henry MultiСharts » 24 Oct 2011

Please have a look at the following discussion.

User avatar
johnmok
Posts: 78
Joined: 18 Oct 2011
Has thanked: 26 times
Been thanked: 7 times

Re: a strategy close all open position in a preset time

Postby johnmok » 24 Oct 2011

thanks a lot, i think i've got the concept, and for my case, i've made a little bit modification as follows, but it seems doesn't work, i add it as a separate strategy, as my testing strategy is not the "IOG true" type, shall i make a strategy that is IOG true to match the code, or my code have some problem actually?

after applying the two strategies, on the historical chart, the position is still carry over session, i don't know whether the strategy can only work in real time and can't show on the historical chart or there is actually some problem in my code, thanks for your help.

[intrabarordergeneration = true]
input: CloseTimeAM(115930),AMCloseReal(120000),CloseTimePM(161430);
if ((currenttime_s >= CloseTimeAM and currenttime_s <= AMCloseReal) or currenttime_s >=CloseTimePM) then
Begin
RecalcLastBarAfter(1);
sell ("EOD_LX") next bar market;
buytocover("EOD_SX") next bar market;
End;

User avatar
johnmok
Posts: 78
Joined: 18 Oct 2011
Has thanked: 26 times
Been thanked: 7 times

Re: a strategy close all open position in a preset time

Postby johnmok » 24 Oct 2011

more info....

i test the code provided in the link and my modified code as follows.

i applied three strategies to test it, include the MACD LE, MovAvg Cross LX and the exit strategy in the link and my mod. ver.. using it on the es-mini 5 min chart.

strategies code provided in the link
-------------------------------------------------

Code: Select all

[intrabarordergeneration = true]
input: TimeClosePosition(041430);
if (currenttime_s >= TimeClosePosition) then
Begin
    RecalcLastBarAfter(1);
    sell ("EOD_LX") next bar market;
    buytocover("EOD_SX") next bar market;
End;

----------------------------------------------------
my modified version,
-----------------------------------------------------

Code: Select all

[intrabarordergeneration = true]
input: CloseTimeAM(235930),AMCloseReal(240000),CloseTimePM(041430),PMCloseReal(041430);
if (currenttime_s >= CloseTimeAM and currenttime_s <= AMCloseReal) then
Begin
    RecalcLastBarAfter(1);
    sell ("EOD_LX_AM") next bar market;
    buytocover("EOD_SX_AM") next bar market;
End;

if (currenttime_s >= CloseTimePM and currenttime_s <= PMCloseReal) then
Begin
    RecalcLastBarAfter(1);
    sell ("EOD_LX_PM") next bar market;
    buytocover("EOD_SX_PM") next bar market;
End;

-----------------------------------------------------
the results are as follows,

when only two strategies applied, the MACD LE and the MovAvg Cross LX, i get the following

Image

when add the provided exit strategy, the position close at bar end after entry, like this...

Image

and if i use my own modified version exit strategy, nothing happen, you can see that the position will still carry over session....

Image

need help... thank you all
Last edited by johnmok on 24 Oct 2011, edited 2 times in total.

User avatar
TJ
Posts: 6598
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1909 times

Re: a strategy close all open position in a preset time

Postby TJ » 24 Oct 2011

note:

1. Please use code tag when posting codes. It makes reading easier.
(I have tagged them for you in the above post.)

2. Please reduce the photo to 800 pixel width before posting.

User avatar
TJ
Posts: 6598
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1909 times

Re: a strategy close all open position in a preset time

Postby TJ » 24 Oct 2011

johnmok wrote:...

when only two strategies applied, the MACD LE and the MovAvg Cross LX, i get the following
...
need help... thank you all

KISS
I would not use multiple strategies on the same instrument.

To make multiple-strategies work, you have to include intelligence in the strategies so that your left hand knows what the right hand is doing.
These users thanked the author TJ for the post:
JoshM

User avatar
TJ
Posts: 6598
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1909 times

Re: a strategy close all open position in a preset time

Postby TJ » 24 Oct 2011

johnmok wrote:more info....
...
the results are as follows,

when only two strategies applied, the MACD LE and the MovAvg Cross LX, i get the following

when add the provided exit strategy, the position close at bar end after entry, like this...

and if i use my own modified version exit strategy, nothing happen, you can see that the position will still carry over session....

need help... thank you all


What instrument are you trading?
What time zone do you use for the chart? Local or Exchange time?
What is the time zone you are in?


PS.
I can't see the time on the chart, you need to describe what is wrong with the picture.

User avatar
johnmok
Posts: 78
Joined: 18 Oct 2011
Has thanked: 26 times
Been thanked: 7 times

Re: a strategy close all open position in a preset time

Postby johnmok » 24 Oct 2011

TJ wrote:note:

1. Please use code tag when posting codes. It makes reading easier.
(I have tagged them for you in the above post.)

2. Please reduce the photo to 800 pixel width before posting.



i am sorry... new to forum, photo resolution changed! :-)
These users thanked the author johnmok for the post:
TJ

User avatar
johnmok
Posts: 78
Joined: 18 Oct 2011
Has thanked: 26 times
Been thanked: 7 times

Re: a strategy close all open position in a preset time

Postby johnmok » 25 Oct 2011

TJ wrote:
johnmok wrote:more info....
...
the results are as follows,

when only two strategies applied, the MACD LE and the MovAvg Cross LX, i get the following

when add the provided exit strategy, the position close at bar end after entry, like this...

and if i use my own modified version exit strategy, nothing happen, you can see that the position will still carry over session....

need help... thank you all


What instrument are you trading?
What time zone do you use for the chart? Local or Exchange time?
What is the time zone you are in?


i am trading es-mini of the cme market, but from the setting in IB, it shows that the product belongs to GLOBEX exchange.

the time zone in the chart...i choose the local time.
i am in hong kong, the time zone should be gmt+8, so for me , the trading hour of the es-mini should be 21:30 - 04:15 i guess..

User avatar
TJ
Posts: 6598
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1909 times

Re: a strategy close all open position in a preset time

Postby TJ » 25 Oct 2011

johnmok wrote:
TJ wrote:
johnmok wrote:more info....
...
the results are as follows,

when only two strategies applied, the MACD LE and the MovAvg Cross LX, i get the following

when add the provided exit strategy, the position close at bar end after entry, like this...

and if i use my own modified version exit strategy, nothing happen, you can see that the position will still carry over session....

need help... thank you all


What instrument are you trading?
What time zone do you use for the chart? Local or Exchange time?
What is the time zone you are in?


i am trading es-mini of the cme market, but from the setting in IB, it shows that the product belongs to GLOBEX exchange.

the time zone in the chart...i choose the local time.
i am in hong kong, the time zone should be gmt+8, so for me , the trading hour of the es-mini should be 21:30 - 04:15 i guess..


1. You have to be careful with the Daylight Savings Time. There is no DST in HK, but in USA, the time moves ahead one hour in the spring and returns to regular time in the fall. If you use Local Time for your charts, your input time has to be adjusted twice a year.

2. I would not leave the exit to the last 30 seconds. It is too risky for autotrade.
Most brokers require the last order submitted 3 minutes before closing. You should make provision for delays/outages along the way.

3. It would be easier if you set the chart to Exchange time,
then set the code as follows:
(note TIME. It exits all your positions at the beginning of the last bar.
On a 5 min chart, it will give you 5 minutes of exit time.)

Code: Select all

[intrabarordergeneration = true]
input: TimeClosePosition(1615);

if (TIME = TimeClosePosition) then
Begin
    RecalcLastBarAfter(1);
    sell ("EOD_LX") next bar market;
    buytocover("EOD_SX") next bar market;
End;
These users thanked the author TJ for the post:
johnmok

User avatar
johnmok
Posts: 78
Joined: 18 Oct 2011
Has thanked: 26 times
Been thanked: 7 times

Re: a strategy close all open position in a preset time

Postby johnmok » 25 Oct 2011

oh my god! it really works!

tj, thank you so much, you are absolutely my star~! :-D
These users thanked the author johnmok for the post:
TJ


Return to “MultiCharts”