How to close open positions at the end of the day (for daytraders)

I used the code in the below link for MACD crossover Intraday:
https://www.amibroker.com/kb/2006/10/26/how-to-close-open-positions-at-the-end-of-the-day-for-daytraders/
Code as below:
Buy = Cross( MACD(), Signal() ) OR TimeNum() > 151500;
Sell = Cross( Signal(), MACD() ) OR TimeNum() > 151500;
Sell=ExRem(Sell,Buy) ;
Buy=ExRem(Buy,Sell);
Short =ExRem(Sell,Buy);
Cover=ExRem(Buy,Sell);

But exit is not happening at 151500 in the same day for the stock 'INFY'.
Please refer the screen shots below and kindly advise:

SS_01

SS_02

Without going into any other detail, you have to be aware that the stated "But exit is not happening AT 151500" is not consistent with the formula.

Try:

Sell = Cross( Signal(), MACD() ) OR TimeNum()  == 151500;

Note the equals sign.

I have modified the code as below:
Buy = Cross( MACD(), Signal() ) OR TimeNum() == 151500;
Sell = Cross( Signal(), MACD() ) OR TimeNum() == 151500;
Sell=ExRem(Sell,Buy) ;
Buy=ExRem(Buy,Sell);
Short =ExRem(Sell,Buy);
Cover=ExRem(Buy,Sell);

But still exit is not happening at 151500 in the same day of trading, please refer the screen shot below:

SS_03

@av.ragavan,

Please use Code Tags.

See

Write down your intention (in plain english). What are your Buy and Sell conditions?

To ensure buy and sell at same day every day you have to read this article here
https://www.amibroker.com/kb/2014/11/28/how-to-restrict-trading-to-certain-hours-of-the-day/

2nd example there

It is also possible to force an exit signal after 11:00 to avoid overnight positions:

/// @link https://www.amibroker.com/kb/2014/11/28/how-to-restrict-trading-to-certain-hours-of-the-day/
tn = TimeNum();
startTime = 93000; // start in HHMMSS format
endTime = 151000;  // end in HHMMSS format
timeOK = tn >= startTime AND tn <= endTime;

Buy = Cross( MACD(), Signal() ) AND timeOK;
Sell = (Cross( Signal(), MACD() ) AND timeOK) OR Cross( tn, endTime ); 
3 Likes

For Intraday, Buying should happen when MACD() crosses signal between 9:45:00 AM and 3:00:00 PM and Selling should happen when signal crosses MACD() between 9:45:00 AM and 3:00:00 PM. Finally, Trade should be exited at 03:20:00 PM.

Check if this helps:

Buy = Cross( MACD(), Signal() ) AND TimeNum() >= 94500 AND TimeNum() < 150000;
Sell = TimeNum() >= 151500;

Short = Cross( Signal(), MACD() ) AND TimeNum() >= 94500 AND TimeNum() < 150000;
Cover = TimeNum() >= 151500;

It's a classic one. @fxshrat has basically given you the solution. I guess you want to sell before markets are closed :wink: so be careful with you selling condition/time. Imo it's better to distinguish 3 periods of the day: opening, closing and end of day so you can test various options e.g

TN = TimeNum();
MAC = MACD();
SIG = Signal();

opening_start = ParamTime( "Opening Start Time", "09:45");
opening_end = ParamTime( "Opening End Time", "15:00" );
Opening = (TN >= opening_start) AND (TN <= opening_end);

Buy = Opening AND Cross(MAC, SIG);
 

similarly for Selling (closing_start/end) but add a condition for end of day e.g

closing_start = ParamTime( "Closing Start Time", "09:45");
closing_end = ParamTime( "Closing End Time", "15:00" );
Closing = (TN >= closing_start) AND (TN <= closing_end);

eod_start =  ParamTime( "End Of Day Start Time", "15:19"); // I would set at 15:15 or 15:19
eod_end = ParamTime( "End Of Day End Time", "15:20"); 
EOD = (TN >= eod_start) AND (TN < eod_end); // '<' prevents selling after RTH

Sell = EOD OR (Closing AND Cross(SIG, MAC));

Don't forget to remove excessive Buy/Sell signals and you're done :slight_smile:

Sell = ExRem(Sell, Buy);
Buy = ExRem(Buy, Sell);

As a general strategy, decompose your problem first and write a piece of code for each subproblem.

1 Like

@av.ragavan (& @bysoaa),

did you bother to read the posted KB article (four posts above)?
It is all there already.
Why re-inventing wheel?

You just need a second endtime just for tn cross. That's all.
The rest is the same as of KB article.
Please do yourself a favour and please read the KB article.

/// @link https://www.amibroker.com/kb/2014/11/28/how-to-restrict-trading-to-certain-hours-of-the-day/
/// just adding second exit timenum, nothing more and nothing less
tn = TimeNum();
startTime = 94500; // start in HHMMSS format
endTime = 145959;  // end in HHMMSS format
endTime2 = 151500;  // end2 in HHMMSS format for session exit
timeOK = tn >= startTime AND tn <= endTime;

Buy = Cross(MACD(), Signal()) AND timeOK;
regular_sell = Cross(Signal(), MACD()) AND timeOK;
session_exit = Cross(tn, endTime2);
Sell = regular_sell OR session_exit; 

Since you use 15-min TF in first post and now you want to exit at 15:20 (session exit)... in addition you would have to use time frame functions for MACD crosses and going to shorter intervals such as 5-min, 1-min. Because 15:20 does not exist in 15-min time frame.


@av.ragavan, please also read here on how to ask good question.
You've constantly changed requirements throughout this thread.
First you mention exit at 15:15, then 15:20, then also having different session for MACD cross,...
It would be great to mention everything in initial post.

2 Likes

Thanks, exit worked after changing the endTime2 from 151500 to 151400 in your code:

tn = TimeNum();
startTime = 94500; // start in HHMMSS format
endTime = 145959;  // end in HHMMSS format
endTime2 = 151400;  // end2 in HHMMSS format for session exit
timeOK = tn >= startTime AND tn <= endTime;

Buy = Cross(MACD(), Signal()) AND timeOK;
regular_sell = Cross(Signal(), MACD()) AND timeOK;
session_exit = Cross(tn, endTime2);
Sell = regular_sell OR session_exit; 

Short = Cross(Signal(), MACD()) AND timeOK;
regular_buy = Cross(MACD(), Signal()) AND timeOK;
session_exit = Cross(tn, endTime2);
Cover = regular_buy OR session_exit; 

Could you please share if there is any link which covers coding for beginners?