Short sell using Howard Bandy code

Hello, I am new to Amibroker but a long term trader and investor. I have managed to adapt Howard Bandy's AFL code for my own strategies and really enjoy the product, and the excellent support & network. I am trying to change Howard Bandy's code to short sell, and not succeeding yet. Could someone please assist. When I put it thru the Optimizer it produces no trades. (The long system works well). Thank you for your help.

SetOption( "ExtraColumnsLocation", 1 );
SetOption ( "CommissionMode", 2 ); // $ per trade
SetOption( "CommissionAmount", 0 );
SetOption( "InitialEquity", 100000 );
SetPositionSize( 10000, spsValue );
MaxPos = 1;
SetOption( "MaxOpenPositions", MaxPos );
SetTradeDelays( 0, 0, 0, 0 );

ShortPrice = Close;
CoverPrice = Close;

// Define a day as rising based on the closing price
Rising = C > Ref( C, -1 );
Falling = C < Ref( C, -1 );

// The number of days in the sequence
N = Optimize( "N", 2, 1, 7, 1 );

// Direction. 1 == Rising, 0 == Falling
Direction = Optimize( "Direction", 1, 0, 1, 1 );
//Direction = 0;

// Exit variables
// Maximum holding period
HoldDays = Optimize( "HoldDays", 7, 1, 7, 1 );
// Profit target
ProfitTarget = Optimize( "ProfitTarget", 0.6, 0.2, 4.0, 0.2 );

// Detect an N day sequence
if ( Direction == 1 )
{
    NDaySequence = Sum( Rising, N ) == N;
}
else
{
    NDaySequence = Sum( Falling, N ) == N;
}


Buy = 0;
Sell = 0;
Short = NDaySequence;
Cover = 0;

ApplyStop( stopTypeProfit, stopModePercent, ProfitTarget );
ApplyStop( stopTypeNBar, stopModeBars, HoldDays ); 

/////////////// end /////////////////
1 Like

Make sure to activate Long and Short in Analysis settings General tab -> Positions.

https://www.amibroker.com/guide/w_settings.html

1 Like

many thanks. ummm ... I knew I would feel a bit silly :slight_smile:

OK ... a follow on question to the community ... I now get trades in the analysis/backtest/optimization (duh), but the system that worked fine for longs doesn't enter on the right dates when using short & cover only. Is there another trap for young players here? My hunch is it is related to the interplay between the use of 'Short' and my custom indicator 'NDaySequence' ... many thanks.

@tdmurray in your code you use:

 SetTradeDelays( 0, 0, 0, 0 );

So the dates where you enter the trade should be the ones where (NDaySequence > 0).
I did a short test and did not see anything strange.

Please, explain why do you think the entering dates are the wrong ones?

(If needed re-post the actual code used to get the "wrong" results and a screenshot of the first lines of the backrest results) and let us know what dates you expected to see.

By the way, IMO, using SetTradeDelays( 1, 1, 1, 1 ) with prices set to Open is more realistic for EOD trading.

2 Likes