How to include Short and Cover into my trading formula

Hi Everyone, I am new and don't really understand how to write code for short/cover. Can you provide an example of how I would fit it into my simple code below? Every time I write anything in for Short and Cover, the backtesting brings back no information, as if I didn't include and coding for it.

SetOption("InitialEquity",100000);    
SetOption("MaxOpenPositions",20);    
SetOption("AccountMargin",100);        
SetOption("CommissionMode",1);        
SetOption("CommissionAmount",0.25);    
SetOption("usecustombacktestproc",False);
SetBacktestMode(backtestRegular);
SetTradeDelays(1,1,1,1);               

BuyPrice = Open;                       
SellPrice = Open;                      

//Simulation Rules
MinimumTurnover     =500000;
LiquidityFilter     =LLV(C*V,20) > MinimumTurnover;     
VolatilityFilter    =ATR(14)/C<0.05;


BreakoutBars        =Optimize("HHV Bars",50,20,400,20); 
EntryTrigger        =Cross(C,Ref(HHV(C,BreakoutBars),-1));

ExitRule            =C<EMA(C,200);                      

Buy                 =LiquidityFilter AND VolatilityFilter AND EntryTrigger;
Sell                =ExitRule;


SetPositionSize(5,spsPercentOfEquity);

Moderator: added code tags

  1. You have to read here please on how to use code tags for inserting code to a post. It's mandatory.

  2. Then (as for your code) you need to add Short and Cover variables into your formula file. There aren't any in your upper code.

    e.g.

    Short = Sell;
    Cover = Buy;
    
  3. Then for backtest you need to make sure that short is enabled in analysis settings.

    E.g. select "Long and short" at "Positions" drop down menu
    19

1 Like

Sorry about not having it in the right format.

I'm sill lost. Can you either provide me example code that would work within the formula I provided so that I can break the code down to see how it works and experiment with it until I get satisfactory results and/or point me to an article which covers short/cover coding that would work within what I provided.

I've tried putting in all types of short/cover coding and nothing works. Did I write myself into a whole and no short/cover code works within this formula? The example you provided didn't work and I'm unable to see how to build off of it to make it work. However, thanks for the changing the Analysis Settings advice.

The example I provided is an incomplete snippet that is supposed to be added to your code of post #1 below of Buy Sell variables. And it does work. I don't post things not working.

Also there are examples in manual (also read ranking via PositionScore and SeparateLongShortRank of Setoption function there) and in knowledge base involving short and cover.

SetOption("InitialEquity",100000);    
SetOption("MaxOpenPositions",20);    
SetOption("AccountMargin",100);        
SetOption("CommissionMode",1);        
SetOption("CommissionAmount",0.25);    
SetOption("usecustombacktestproc",False);
SetBacktestMode(backtestRegular);
SetTradeDelays(1,1,1,1);               

BuyPrice = Open;                       
SellPrice = Open;        

ShortPrice = O;
CoverPrice = O;              

//Simulation Rules
MinimumTurnover     =500000;
LiquidityFilter     =LLV(C*V,20) > MinimumTurnover;     
VolatilityFilter    =ATR(14)/C<0.05;


BreakoutBars        =Optimize("HHV Bars",50,20,400,20); 
EntryTrigger        =Cross(C,Ref(HHV(C,BreakoutBars),-1));

ExitRule            =C<EMA(C,200);                      

Buy                 =LiquidityFilter AND VolatilityFilter AND EntryTrigger;
Sell                =ExitRule;

m = MA( Close, 20 ); // simple moving average

Short = Sell; // Cross( m, Close );// etc.
Cover = Buy; // Cross( Close, m );// etc.

SetPositionSize(5,spsPercentOfEquity);

As you can see I do get Long & Short results with single symbol...

20


... as well as if code being applied on portfolio of symbols.

21


In addition you might use (separate) ranking

/// @link https://www.amibroker.com/guide/h_portfolio.html
/// To enable separate long/short ranking use:
SetOption("SeparateLongShortRank", True );

PositionScore = 100-RSI();

And/or enabling/disabling exit forced by reverse signal.

// enable or disable depending on 
// how you want BT to handle existing trades
// if there are reverse signals occuring
SetOption("ReverseSignalForcesExit", True);

If you still don't get short results then double check in detailed log.
22

3 Likes

I tried explorer, why is it empty in Indonesia Stock ? .Thanks

It is backtesting code but not exploration code.
It has zero exploration code in there.