dudemanguy
New member
I'm trying to use FPL in order to determine how many shares to order in strategies. But for some reason when FPL is plotted in strategies it returns a different number than when plotted as a study, and the slippage makes the backtesting inaccurate. Either that or I'm doing something wrong with the code. Here's an example for a simple MA200 strategy:
I'ved tested it on different tickers, ETFs, timeframes, and there is always some slippage. Any insight appreciated!
Just to clarify, it seems that if you use
def portfolio = initial + FPL();
to determine bet sizing it seems to start its own FPL calculation as opposed to using the internal FPL() calculation. Changing tickers seems to fix the problem, but if the study parameters are updated then the behavior starts happening again.
Code:
input length = 200;
input price = close;
input aType = AverageType.SIMPLE;
plot MA = MovingAverage(aType, price, length);
ma.SetDefaultColor(Color.GREEN);
def buy = price > MA and price[1] < MA[1];
def sell = price < MA and price[1] > MA[1];
plot FPL = FPL();
input initial = 10000;
def portfolio = initial + FPL();
input kelly = 1.0;
def orderShares = RoundDown(kelly * portfolio/open[-1],0);
AddOrder(OrderType.BUY_TO_OPEN, buy, tradesize = orderShares, tickColor = Color.GREEN, arrowColor = Color.GREEN, name = "");
AddOrder(OrderType.SELL_TO_CLOSE, sell, tickColor = Color.RED, arrowColor = Color.RED, name = "");
I'ved tested it on different tickers, ETFs, timeframes, and there is always some slippage. Any insight appreciated!
Just to clarify, it seems that if you use
def portfolio = initial + FPL();
to determine bet sizing it seems to start its own FPL calculation as opposed to using the internal FPL() calculation. Changing tickers seems to fix the problem, but if the study parameters are updated then the behavior starts happening again.
Last edited by a moderator: