Short Selling Limit Price

Hi Guys,

Been playing around more with AFL. And I noticed something not working as expected. So, I wrote a simple script which buys if price is above previous candle high (and stoploss at previous candle low, and a takeprofit of 2*risk). This is working as expected.

On the other hand, for shorts, I am selling short when price is below previous candle low (and stoploss at previous candle high and a takeprofit of 2*risk). The problem I am seeing here is that the entries are not happening at the previous candle low! The entries are happening at the open of the current bar. For longs, its opening correctly.

Can someone look at my code and tell me where the problem is please? In the screenshot below, I also included the trades.. and the last 2 columns are the previous low price and previous high price.. The entries for longs are exactly the same as the previous high price.. whereas entries for shorts are not the same as previous low price..

TotalRiskPercentage = 2;
tp = 2;

NewDay = Day()!= Ref(Day(), -1);
EndDay = (Day()!= Ref(Day(), 1));

tn = TimeNum();
startTime = 93000; // start in HHMMSS format
endTime = 153000;

BuyPrice = Ref(High, -1);
SellPrice = Ref(Low, -1);
RiskPerTrade = abs(BuyPrice-SellPrice);

PctSize = BuyPrice*TotalRiskPercentage / RiskPerTrade;
SetPositionSize( PctSize, spsPercentOfEquity );

Buy =  Low<=BuyPrice AND High>=BuyPrice;
Sell = EndDay;

Short = Low<=SellPrice AND High>=SellPrice;
Cover = EndDay;

ApplyStop(stopTypeLoss, stopModePoint, abs(BuyPrice-SellPrice), ExitAtStop=1);
ApplyStop(stopTypeProfit, stopModePoint, tp*abs(BuyPrice-SellPrice), ExitAtStop=1);

StaticVarSet(Name() + "Prev Low", SellPrice);
StaticVarSet(Name() + "Prev High", BuyPrice);

// SET OPTIONS
SetBacktestMode( backtestRegular);

//https://www.amibroker.com/guide/afl/setoption.html
SetOption("NoDefaultColumns", False);
SetOption("InitialEquity", 100000);
SetOption("AllowSameBarExit", True);
SetOption("ActivateStopsImmediately", True);
SetOption("AllowPositionShrinking", True);
SetOption("FuturesMode", False);
SetOption("InterestRate", 0);
SetOption("MaxOpenPositions", 1000);
SetOption("MinShares", 1);
SetOption("PriceBoundChecking", True);
SetOption("CommissionMode", 2); //  $ per trade
SetOption("CommissionAmount", 1);
SetOption("AccountMargin", 50);
SetOption("ReverseSignalForcesExit", False);
SetOption("UsePrevBarEquityForPosSizing", False);
SetOption("PortfolioReportMode", 0);
SetOption("DisableRuinStop", False);
SetOption("GenerateReport", 1);
SetOption("RefreshWhenCompleted", True);
SetOption("SettlementDelay", 0);


SetCustomBacktestProc("");

if( Status("action") == actionPortfolio )
{
    bo = GetBacktesterObject();
    bo.Backtest(1); // run default backtest procedure

   // iterate through closed trades first
   for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
   {
		x = StaticVarGet( trade.Symbol + "Prev Low" );
		y = StaticVarGet( trade.Symbol + "Prev High" );
		
		trade.AddCustomMetric( "Prev Low", Lookup( x, trade.EntryDateTime ) );
		trade.AddCustomMetric( "Prev High", Lookup( y, trade.EntryDateTime ) );
   }

    bo.ListTrades();
}

image

1 Like

You never set the ShortPrice and CoverPrice variables, so your entry and exit prices for Short trades will be controlled by the Analysis Settings.

1 Like

Thanks @mradtke .. I didn't realize there was a ShortPrice variable. I thought SellPrice was the price to short.. All makes sense now!!

You should at minimum read the Tutorial section of the Users' Guide
https://www.amibroker.com/guide/tutorial.html

As to backtesting and ShortPrice it is of course described in tutorial
https://www.amibroker.com/guide/h_backtest.html

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.