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:
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.
It's a classic one. @fxshrat has basically given you the solution. I guess you want to sell before markets are closed 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
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.
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.