Double the quantity of shares

Is there any procedure to reverse the double quantity of stock.

For example, i want to buy 100 quantity of some script wherever 5Day EMA crosses from below the 22Day EMA. Again after some time, whevever 5D EMA crosses the 22D EMA from above, then there will be sell of 200 (so that 100 buy position would be squared off and new 100 short position will be created) quantity of same script. Again after sometime, whenever the 5D EMA crosses the 22D EMA from below, there will be again buy of 200 shares (by squaring off the 100 short positions and initiate 100 buy positions). In this way its going to move further and there will be auto squared off when the time will pass 3.00PM.
Please suggest the AFL code to implement the same. Thanks and regards.

You mean to Exit an Open position before taking a new one.

  1. Using SetOption:
    image
    (Source: https://www.amibroker.com/guide/afl/setoption.html)

  2. And this guide - How to close open positions at the end of the day (for daytraders)

As per the option no.2. The open position can only closed if the last order was a buy order, if the last order is sell order, then how can it be closed as per the below mentioned condition?

Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() ) OR TimeNum() > 155000

if the last order before square off was a sell order, then when the time crosses 3.50PM, then another short position will be taken by the system as per the above mentioned code, then how can be open position will be squared off?

Sell variable does not mean short but just means Buy exit!
If you want to short then you have to use Short variable.

SetPositionSize(100, spsShares);
SetOption( "ReverseSignalForcesExit", True);

// https://www.amibroker.com/kb/2006/10/26/how-to-close-open-positions-at-the-end-of-the-day-for-daytraders/
tn = TimeNum();
end_time = 155000;
is_session_end = tn > end_time;
is_session = tn >= 090000 AND tn <= end_time;

Buy_sig = Cross( MACD(), Signal() ) AND is_session;
Sell_sig = Cross( Signal(), MACD() ) AND is_session;
 
Buy = Buy_sig; // Buy entry
Sell = Sell_sig OR is_session_end;// Buy exit

Short = Sell_sig; // Short entry
Cover = Buy_sig OR is_session_end;// Short exit

40

2 Likes

Thank you very much.