Closing at end of day with Daily Data

The strategy I am testing longs/shorts at the open. The data it is operating on is daily.
I also apply stop losses based on previous day ranges.

Now I want to square off the positions at the end of day. I have looked into using TimeNum() function to do that but it won't work in my case since the data is day ranged like 1/1/18, 2/2/18 and not intraday.

So in such a case how should I proceed with closing at the end of the day all open positions?

How about:

Sell = True;
SellPrice = C;
2 Likes

I see. This seems to work. I have an another related question since this piece of code brough it to my mind and I don't want to waste an another thread on it.

I have seen in some code samples, these:
For ex: https://www.amibroker.com/guide/h_portfolio.html

Short=Cross( MA(C,p1) , MA(C,p2) );
Buy=Cross( MA(C,p2) , MA(C,p1) );
// always in the market 
Sell=Short;  
Cover=Buy;

I don't understand what the part Sell=Short; Cover=Buy; does as I am new to AFL.
Can someone explain?

@xterminator,

As that system is trying to Always be in the Market, it must reverse from a Long position to a Short Position.

So it must Sell it's Long position, then it must Short to get in the Short position. To exit the Short position it must Cover (or Buy).

Essentially it is the terminology difference of the Entry and Exit of Long and Short positions.

1 Like

When someone posts an answer that solves your problem, you should mark it as the solution and/or "like" it with the heart icon.

Thanks a ton for the help to all.

I am using the following code now:

SetTradeDelays(0, 0, 0, 0)
// code to compute Buy and Short signals
//...
//...
Sell = Ref(Buy, 0); // only sell it back if already long
SellPrice = Close;
Cover = Ref(Short, 0);
CoverPrice = Close; // only buy it back if already short

// Setting Stop Loss
StopAmount = Ref(Close, -1);
StopAmount = Max(TickSize, StopAmount);
IsLong = Ref(Buy, 0); // since entering position on the same bar using ref 0, correct?
ApplyStop(stopTypeLoss, stopModePoint, IIf(IsLong, BuyPrice - StopAmount, StopAmount - BuyPrice), 1);

I am using Sell=Ref(Buy, 0) because I think it will only Sell when I am already Long rather than simply using Sell=True which would have caused it to Sell even when not in position.
Hope my logic is correct?

AmiBroker will not Sell unless you're in a long position, but your new code will work fine. You could simply use:

Sell = Buy

as Ref(...,0) just returns the original array.

1 Like