Extended Floating Profit & Loss - Backtesting data utility For ThinkOrSwim

Status
Not open for further replies.
@TMB70 Hands down, ToS is the BEST for charting analysis. But it is horrible at portfolio management and at providing documentation.
Most of the members seem to find the best method for learning the use of functions to be reading through the many indicators using that function and comparing its use in the many other threads. And then cutting and pasting what might work best in what they are building.

You are best off searching the word 'fold' yourself. But if you are looking for technical definitions here are a few links to get you started:
https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/fold
https://jshingler.github.io/TOS-and-Thinkscript-Snippet-Collection/TOS & Thinkscript Collection.html#the-fold-function-explained
https://usethinkscript.com/threads/conditional-triggers-using-the-fold-function.5920/
https://stackoverflow.com/questions/55679224/how-to-use-fold-statement-index-in-function-call
 
Last edited:
Thank you! I’ve tried the cutting and pasting idea, but it hasn’t “clicked” yet for me on how it (fold) functions and how best to implement it. I’ll have a look at these references. Thanks for sharing.
 
Are there any good references on how to use fold or loops? I’ve been thinking that might be a way to solve the problem, but do not have much experience using the fold function.
can you explain some of what you are trying to do? maybe we can come up with something to get you started?

fold can,
...process a series of formulas and end up with 1 number.
... look at a group of bars (index) and read values of variables.
...use every # between a start and a stop, in formulas. usually used as a bar offset in getvalue(), to retrieve a value of a variable.
...if while is used, then loop runs as long as while is true

fold can't,
...plot

this may help. i explain a fold statement to someone.
https://usethinkscript.com/threads/...onfirming-a-thinkscript-line.6662/#post-64428


there is a function called script , that may help you. it allows you to create a sub program, within the study, that can be called many times. it can produce more than 1 number, from plot(s). ( it can't plot or use 2nd agg)
https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/script
 
Are there any good references on how to use fold or loops? I’ve been thinking that might be a way to solve the problem, but do not have much experience using the fold function.
here is an example using fold to count the candles that are smaller than the current one ( engulfing).
on each bar, it looks back 40 bars, comparing the previous candles to the current one. if they are smaller, then it counts +1. if one is bigger, then it stops the loop.
https://usethinkscript.com/threads/...st-65-most-recent-days.7483/page-2#post-72474
 
I really like this tool! Thanks for sharing it! I've been doing post-processing in Excel, which arrives at similar results, but is time consuming.

Curious... Is there a way to reset the FPL back to zero at the start of each new day? Maybe it would have to be named something different like FPL_Daily so that it would not impact the other code. Please let me know your thoughts.

Thanks!
i don't know what a fpl is.

if you want to track profits on a strategy, try this,
copy the code from the link to the end of your strategy.
change 4 formulas.
then add code to reset it on the start of a new day.
def newDay = if GetDay() != GetDay()[1] then 1 else 0;

https://usethinkscript.com/threads/label-with-information-related-to-strategy.7902/#post-75825
 
can you explain some of what you are trying to do? maybe we can come up with something to get you started?

fold can,
...process a series of formulas and end up with 1 number.
... look at a group of bars (index) and read values of variables.
...use every # between a start and a stop, in formulas. usually used as a bar offset in getvalue(), to retrieve a value of a variable.
...if while is used, then loop runs as long as while is true

fold can't,
...plot

this may help. i explain a fold statement to someone.
https://usethinkscript.com/threads/...onfirming-a-thinkscript-line.6662/#post-64428


there is a function called script , that may help you. it allows you to create a sub program, within the study, that can be called many times. it can produce more than 1 number, from plot(s). ( it can't plot or use 2nd agg)
https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/script
"can you explain some of what you are trying to do? maybe we can come up with something to get you started?"

Hello @halcyonguy,
Thanks for your reply and the useful info. I haven't had a chance to make use of it yet (or the info provided by @MerryDay), but will be digging in after this reply. To answer your question above, I am trying to find a way to reset the Floating Profit & Loss (FPL) back to zero at the start of each new day. It doesn't have to be the actual FPL. It could be another variable named like "FPL_Daily". Something like...

def FPL_Today = FPL() - FPL_at_Close_Yesterday;

Of course, "FPL_at_Close_Yesterday" would need to be determined at whatever time the strategy stops trading in the previous day.

Once the "FPL_Today" is available, I'd like to be able to use it for comparison against either a "DailyGoal" or "MaxAllowableDailyLoss" and then stop the strategy from trading until the start of the next day if either is met.

I would use this on a "30 D : 1 m" time frame setup. Then use the tool @eddielee394 created to see how well a given strategy does over the 30 days, or maybe export to csv and do additional postprocessing.

Let me know what you think of the idea.

Thanks again!
 
@eddielee394 great work on this one, very helpful.

I am wondering if there is a way to edit the code to count the actual trade type being produced by the strategy and not basing it off the entry/exit price? as im sure you know tos doesnt give the best data for backtesting regarding actual profitability of each trade, so if we could count the Entry and then Take profit/stop out trade type this may do a better job of tos in giving you a ratio
 
@eddielee394 great work on this one, very helpful.

I am wondering if there is a way to edit the code to count the actual trade type being produced by the strategy and not basing it off the entry/exit price? as im sure you know tos doesnt give the best data for backtesting regarding actual profitability of each trade, so if we could count the Entry and then Take profit/stop out trade type this may do a better job of tos in giving you a ratio
https://usethinkscript.com/threads/...tility-for-thinkorswim.1624/page-4#post-80167
 
Does anyone happen to know how to change the "AvgReturn" from displaying as $ to display as %? Would make it a lot easier to compare strategies. I tried for a long time to figure it out but could not, appreciate the help
 
Does anyone happen to know how to change the "AvgReturn" from displaying as $ to display as %? Would make it a lot easier to compare strategies. I tried for a long time to figure it out but could not, appreciate the help
The OP is longer on the forum. The indicator as written is all that is available.
 
Hello everyone,

Currently backtesting a moving avg crossover strategy, but would like to add a couple bubbles to the top of the charts. This is the code I current have, please advise if you can help.

Thank you


Bubbles I would like to add:
  • Win/Loss %
  • Current trade % return
  • Total trades
  • Count win
  • Count loss
  • Highest return
  • Avg return
  • Avg win
  • Avg loss
  • Total P/L

input longEntries = yes;
input shortEntries = no;

input fastMALength = 8;
input slowMALength = 21;
input averageTypeFast = AverageType.Exponential;
input averageTypeSlow = AverageType.Exponential;

input quantity = 100;

plot fastMA = MovingAverage(averageTypeFast, close, fastMALength);
plot slowMA = MovingAverage(averageTypeSlow, close, slowMALength);

#8 EMA x 21 EMA
AddLabel(yes, fastMALength + (if averageTypeFast == 0 then " Simple" else if averageTypeFast == 1 then " EMA" else if averageTypeFast == 2 then " Weighted" else if averageTypeFast == 3 then " Wilders" else " Hull") + " x " + slowMALength + (if averageTypeSlow == 0 then " Simple" else if averageTypeSlow == 1 then " EMA" else if averageTypeSlow == 2 then " Weighted" else if averageTypeSlow == 3 then " Wilders" else " Hull"), color.yellow);

AddOrder(OrderType.Buy_To_Open, (longEntries) and fastMA crosses above slowMA, close, quantity);
AddOrder(OrderType.Sell_To_Close,fastMA crosses below slowMA, close, quantity);


AddOrder(OrderType.Sell_To_Open, (shortEntries) and fastMA crosses below slowMA, close, quantity);
AddOrder(OrderType.Buy_To_Close, fastMA crosses above slowMA, close, quantity);
 
Hello everyone,

Currently backtesting a moving avg crossover strategy, but would like to add a couple bubbles to the top of the charts. This is the code I current have, please advise if you can help.

Thank you


Bubbles I would like to add:
  • Win/Loss %
  • Current trade % return
  • Total trades
  • Count win
  • Count loss
  • Highest return
  • Avg return
  • Avg win
  • Avg loss
  • Total P/L
See the beginning post of this thread for the extended P&L data available as labels. As the OP is longer on the forum, the indicator as written is all that is currently available on this forum. We do not have the ability to make the modifications you are looking for.
 
Hi I would like to also have an average percent return for the trade included in the code.
Is this something possible ?
 
Last edited:
Wondering if somebody can help me, want to add a label to the chart showing the current position is long or short. But something like:
Addlabel(yes, if Entry[1] == 0 then "LONG" else "SHORT"); does not seem to be working. Anybody have a workaround?
 
Status
Not open for further replies.

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
448 Online
Create Post

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