Is there a bug in FPL() function when plotting in strategies vs studies?

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:

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:
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:

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.
I had similar experiences...
Check out this post it may assist you troubleshoot.
Thread 'GandalfProjectResearchSystem Dashboard For ThinkOrSwim' https://usethinkscript.com/threads/gandalfprojectresearchsystem-dashboard-for-thinkorswim.17429/
 
I had similar experiences...
Check out this post it may assist you troubleshoot.
Thread 'GandalfProjectResearchSystem Dashboard For ThinkOrSwim' https://usethinkscript.com/threads/gandalfprojectresearchsystem-dashboard-for-thinkorswim.17429/
I found it was related to memory. If I have too many charts open and make calculations over a really long time period, it will tend to get lazy and not calculate correctly. The easy fix is to just go to options, turn on extended hours, update, then turn off extended hours, update. It will force the correct calculations.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
319 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top