I am working on code which will scale in on lower liquidity. The basic objective is to buy lower liquidity shares over 3 tranches, whilst higher liquidity shares being bought once only.
When I run my code as an exploration I am getting the outcome I am expecting. But running it as a backtest I am getting results I cannot explain.
I would welcome guidance on what I am doing wrong.
My code...
_SECTION_BEGIN( "Signal Scale In work" );
//=================================================================================
//1. The "SetOptions" are management options & they are a feature of Amibroker
//=================================================================================
TradingFunds = Param( "Trading Funds - $", 60000, 1000, 10000000, 1000 );
SetOption( "InitialEquity", 1200000 );
SetOption( "PriceBoundChecking", 1 );
SetOption( "CommissionMode", 2 );
SetOption( "CommissionAmount", 9.50 );
SetOption( "UsePrevBarEquityForPosSizing", 1 );
SetOption( "AllowSameBarExit", False );
SetTradeDelays( 1, 1, 1, 1 );
//=================================================================================
//2. Liquidity
//=================================================================================
Liq = C * V; // Liquidity Filter
LowLiq = Liq <= 1000000;
MedLiq = Liq > 1000000 AND Liq <= 3000000;
NormalLiq = Liq > 3000000;
//=================================================================================
//3. Buy & Sell conditions
//=================================================================================
buysig= C>MA(C,100);
buysigLow = buysig AND LowLiq;
buysigMed = buysig AND MedLiq;
BuysigNormal = buysig AND NormalLiq;
sellsig1 = C<MA(C,100);
x = SumSince(sellsig1,buysigLow OR buysigMed OR buysigNormal) ;
xsigs= x<=3;
xsigsNorm= x <=1;
BuyLow = buysigLow AND xsigs;
BuyMed = buysigMed AND xsigs;
BuyNorm = buysigNormal AND xsigsNorm;
Buy = IIf(BuyLow OR BuyMed,sigScaleIn,IIf(BuyNorm,1,0));
Sell = Sellsig1;
//Buy = ExRem( Buy, Sell ); // Removes additional buy signals
Sell = ExRem( Sell, Buy ); // Removes additional sell signals
/*
PosQty = 20; // Position Quantity = Maximum 20 positions
PositionSize = IIf(buysigLow,20000,IIf(buysigMed,30000, 60000 ));
*/
PlotShapes(IIf(buy, shapeUpTriangle, shapeNone), colorWHITE, 0, L, -15);
PlotShapes(IIf(Sell, shapeDownTriangle, shapeNone), colorRed, 0, H, -15);
//=================================================================================
//Add "Filters for the Exploration Analysis"
//=================================================================================
Filter = Buy OR Sell; // Buy & Sell Filters
//=================================================================================
//Add Buy & Sell coding for use in trading the pre-auction
//=================================================================================
BuyOffered = Close * 1.03; // +3% Buy premium over the last closing price
BuyOffer = ceil( BuyOffered * 100 ) / 100; // The amount is rounded up no matter the price (ceil function used)
SellOffered = Close * 0.97; // -3% Sell premium below the last closing price
SellOffer = floor( SellOffered * 100 ) / 100; // The amount is rounded down no matter the price (floor function used)
//=================================================================================
//Add the Exploration code
//=================================================================================
ToBuyPosSize = floor( TradingFunds / BuyOffer ); // Trading Funds divided by buy offer of (+3%) buy premium over the last closing price
ToBuyPosCost = BuyOffer * ToBuyPosSize; // The cost of buying the amount of share
//=================================================================================
// Add columns to report & sort the Exploration Analysis results
//=================================================================================
BuyLowQty = IIf( BuyLow, ToBuyPosSize/3, Null );
BuyMedQty = IIf( BuyMed, ToBuyPosSize/2, Null );
BuyNormQty = IIf( BuyNorm, ToBuyPosSize, Null );
//=================================================================================
// Add "Position Sizing"
//=================================================================================
BuyPrice = Open; // Buy the next day at open
SellPrice = Open; // Sell the next day at open
//Buy = ExRem( Buy, Sell ); // Removes additional buy signals
Sell = ExRem( Sell, Buy ); // Removes additional sell signals
PosQty = 20; // Position Quantity = Maximum 20 positions
PositionSize = IIf(buysigLow,20000,IIf(buysigMed,30000, 60000 ));
/*
PosQty = 30; // Position Quantity = Maximum 20 positions
PosSizeLow = -33/posqty; // scalein for 3 positions
PosSizeMed = -50/posqty; // scalein for 2 positions
//PositionSize = IIf(BuyLow,-33/posqty,IIf(BuyMed,-50/posqty, -100 / posqty));
//PositionSize = IIf(BuyLow,BuyLowQty,IIf(BuyMed,BuyMedQty, BuyNormQty));
/*
SetPositionSize(IIf(BuyLowQty,BuyLowQty,0),spsShares);
SetPositionSize(IIf(BuyMedQty,BuyMedQty,0),spsShares);
SetPositionSize(IIf(BuyNormQty,BuyNormQty,0),spsShares);
*/
SetOption( "MaxOpenPositions", PosQty ); // Maximum number of open position
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
_SECTION_BEGIN("EMA");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 200, 1 );
Plot( eMA( c, 25 ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style", styleLine | styleNoLabel ) | styleNoRescale );
_SECTION_END();
_SECTION_END();
AddColumn( IIf( BuyLow, ToBuyPosSize/3,
IIf( BuyMed, BuyMedQty,
IIf( BuyNorm, BuyNormQty, Null ))),
"# shares", 1, colorWhite, colorDarkGreen, 90 ); // Exploration Analysis - this column displays quantity of shares to buy
AddColumn( IIf( Buy, BuyOffer, Null ), "$ Buy Offer", 1.2, colorWhite, colorDarkGreen, 110 ); // Exploration Analysis - this column displays pre-auction buy offer price (+3% premium added to the last closing price)
AddColumn( IIf( BuyLowQty, BuyLowQty* BuyOffer,
IIf( BuyMedQty, BuyMedQty* BuyOffer,
IIf(BuyNormQty, BuyNormQty* BuyOffer,
Null ))),
"$ Cost", 1.2, colorWhite, colorDarkGreen, 80 ); // Exploration Analysis - this column displays the total ($) you will pay for the qty of shares at the +3% primum
AddColumn( IIf(BuyNormQty, BuyNormQty* BuyOffer, Null ), "$ Cost", 1.2, colorWhite, colorDarkGreen, 80 ); // Exploration Analysis - this column displays the total ($) you will pay for the qty of shares at the +3% primum
AddColumn( IIf( Sell, SellOffer, Null ), "$ Sell Offer", 1.2, colorWhite, colorRed, 110 ); // Exploration Analysis - this column displays pre-auction sell offer price (-3% discount to the last closing price)
AddColumn(Liq,"Liquidity",1);
//-- Top Left Corner
GfxSetBkMode( 1 );
GfxSelectFont("COMIC SANS MS", 15, 400, False, False, 0);
//GfxSelectFont("COOPER BLACK", 17, 400, False, False, 0);
GfxSetTextColor(colorWhite);
GfxTextOut( "Signal Scale In test", 10, 30);
Firstly, using code CGS.au as an example
The exploration, which is what I am expecting:
The backtest trades:
The backtest detailed log:
In summary this are the trades, I have highlighted the trades which are "not correct"
Hopefully my question is clear, that is why is the backtest giving me these results rather than something in line with the exploration. If not please let me know and I will clarify.
Finally if I can get that right, I am also looking to change how the code works, so that the maximum trade size is limited to $60,000. I have not attempted to code this as the previous questions above are perplexing