Extended Floating Profit & Loss - Backtesting data utility For ThinkOrSwim

Status
Not open for further replies.
Based on the metrics provided by this study addon... what would you guys consider a successful backtest strategy?
  • Easy to execute/manage in a real world environment
  • Doesn't incur significant drawdown risk per personal tolerance
  • Makes money ;)
What is the difference between WinRate and W/L %?
win rate is a ratio of winning trades to losing trades.
W/L is the percentage of wins relative to total number trades.
 

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

Thank you.

Would you consider this to be a successful strategy? 100 share trades. 2min chart.

str.jpg
 
@eddielee394 Absolutely brilliant study/strategy!

Just have a few questions:

1. Is the code able to be adapted so the Total Profit of a certain strategy can be added to a Watchlist (Just a dollar value)?

2. I am not able to add it on the end of a custom strategy. It only seems to work for me in two scenarios:
a. As a upper study (floating P&L hidden) when another study has been enabled
b. As a lower study (floating P&L enabled) when another study has been enabled
 
. Is the code able to be adapted so the Total Profit of a certain strategy can be added to a Watchlist (Just a dollar value)?
Not likely (although I could be wrong). It depends on the strategy order functions and I don't believe those are accessible as watchlist fields. But maybe someone thats a bit more adept with TS might have more insight.

I am not able to add it on the end of a custom strategy.
Thats odd. I'd have to see how you're implementing it with your strategy. Displaying it as an upper study on it's own shouldn't display any results as it requires the order functions from a strategy to perform the p&l calculations. Unless I'm misunderstanding you. Regardless, post your implementation with your strategy and I'll take a look.
 
Maybe there is an error in counting the number of trades, because it seems that the trades that end neutral (0.00) don't count. How can I fix that?
 
Maybe there is an error in counting the number of trades, because it seems that the trades that end neutral (0.00) don't count. How can I fix that?
The variable that's passed to the increment function used to count the number of trades is entryFPLAll. Would have to add some logic in the condition that that would include "breakeven" trades.

So the code allows you to see the Last entry price. how can i display what the last exit price is. Thanks
TOS doesn't have a built in function to get a strategy exit. So the only way you would be able to do this is to create a variable based on your strategy's exit conditions and then get the resulting price. It's not included in this script because it would couple the script too tightly with the strategy (making it difficult to port over to other strategies since exit conditions will differ per strategy).
 
Last edited:
The variable that's passed to the increment function used to count the number of trades is entryFPLAll. Would have to add some logic in the condition that that would include "breakeven" trades.

How can I add 'breakeven' trades counter in your code? It seems you count breakeven as winners (>=0)

def entryReturn = if isExitBar then exitBarPL - exitBarPL[1] else entryReturn[1];
def isWin = if isExitBar and entryReturn >= 0 then 1 else 0;
def isLoss = if isExitBar and entryReturn < 0 then 1 else 0;
def entryReturnWin = if isWin then entryReturn else entryReturnWin[1];
def entryReturnLoss = if isLoss then entryReturn else entryReturnLoss[1];
def entryFPLWins = if isWin then entryReturn else 0;
def entryFPLLosses = if isLoss then entryReturn else 0;
def entryFPLAll = if isLoss or isWin then entryReturn else 0;
 
How can I add 'breakeven' trades counter in your code? It seems you count breakeven as winners (>=0)


Could probably just add a variable to count the break-even trades and include that in the entryFPLAll condition. Maybe something like this (untested):
Code:
def isBreakEven = if isExitBar and entryReturn == 0 then 1 else 0;
def isWin = if isExitBar and entryReturn > 0 then 1 else 0;
...//other variables

def entryFPLAll = if isLoss or isWin or isBreakEven then entryReturn else 0;
 
Could probably just add a variable to count the break-even trades and include that in the entryFPLAll condition. Maybe something like this (untested):
Code:
def isBreakEven = if isExitBar and entryReturn == 0 then 1 else 0;
def isWin = if isExitBar and entryReturn > 0 then 1 else 0;
...//other variables

def entryFPLAll = if isLoss or isWin or isBreakEven then entryReturn else 0;
It doesn't work, maybe we need to fix 'entryreturn' variable, if it doesn't count zero increment...
 
The variable that's passed to the increment function used to count the number of trades is entryFPLAll. Would have to add some logic in the condition that that would include "breakeven" trades.


TOS doesn't have a built in function to get a strategy exit. So the only way you would be able to do this is to create a variable based on your strategy's exit conditions and then get the resulting price. It's not included in this script because it would couple the script too tightly with the strategy (making it difficult to port over to other strategies since exit conditions will differ per strategy).

@ eddielee39
The variable that's passed to the increment function used to count the number of trades is entryFPLAll. Would have to add some logic in the condition that that would include "breakeven" trades.


TOS doesn't have a built in function to get a strategy exit. So the only way you would be able to do this is to create a variable based on your strategy's exit conditions and then get the resulting price. It's not included in this script because it would couple the script too tightly with the strategy (making it difficult to port over to other strategies since exit conditions will differ per strategy).


Got ya but question is when i take this code

Addlabel (yes, text =“last exit: “ + ( entryreturn +entry price) hit apply and it shows the exit price for a second than the code continues to calculate which will add the profit to the entry price. This is over my head a total newbie when it comes to coding stuff been trading for 5 years and looking to send myself alerts when my strategy enters and exits a positions as i have not yet coded a trading not to do this for
Me automatically.

I am trading futures so when i do Entryreturn takes the tick and converts that into dollars how can i have the code keep it on ticks than i can find the last exit price
 
Last edited:
It doesn't work
It's been a while since i looked at this code & was posting from my phone. Try this:

Code:
def isBreakEven = if isExitBar and entryReturn == 0 then 1 else 0;
def isWin = if isExitBar and entryReturn > 0 then 1 else 0;
...//other variables

def entryFPLAll = if isLoss or isWin or isBreakEven then entryReturn else 0;

#Counts
..//other variables
def breakEvenCount = incrementValue(isBreakEven);

#Label
AddLabel(yes,
                text = "BreakEven Count: " + breakEvenCount
                color = Color.WHITE
);


Edit: I did some brief testing on this and there's a divide by zero bug that will occur when using the above code in the case when there are 0 wins. I don't have time at the moment to push the appropriate fixes, but the workaround would be to update all the calculations that use the winCount variable (roughly line 264 -268) to handle if the winCount equals zero
 
Last edited:
Got ya but question is when i take this code

Addlabel (yes, text =“last exit: “ + ( entryreturn +entry price) hit apply and it shows the exit price for a second than the code continues to calculate which will add the profit to the entry price.
That's because entryReturn calculates the net dollar return of a trade. So you're basically adding the P&L (in dollars) of a trade to the entry price of that same trade. If you want the actual price of the underlying instrument that you exited at, like i already mentioned the easiest way is probably to just get the price specific to the exit condition of your strategy. Then fire off an alert using that.
Alternatively, you probably could calculate it as a recursive variable using the built-in TickValue() function and a bit of math. Maybe something along the lines of this (untested):

Code:
def exitPrice = if isExitBar then entryPrice + entryReturn/TickValue() else exitPrice[1] ;

#Labels
...//other labels
AddLabel(yes,
         text = "LastExit: " + AsPrice(exitPrice)
         );
 
I did some brief testing on this and there's a divide by zero bug that will occur when using the above code in the case when there are 0 wins.
It doesn't work. I don't find the bug but I'm not sure if it's only about '0' breakevens, cuz total number of trades never matches actual number. If you don't get the total number that corresponds, all other statistics end up being wrong and unreliable.
 
It doesn't work. I don't find the bug but I'm not sure if it's only about '0' breakevens, cuz total number of trades never matches actual number. If you don't get the total number that corresponds, all other statistics end up being wrong and unreliable.
Share a link to your strategy, along with the ticker and aggregation period you were using so I can try and reproduce the issue on my end.
 
Edit: I did some brief testing on this and there's a divide by zero bug that will occur when using the above code in the case when there are 0 wins.
Hi, it's not about breakeven trades. I think I found the reason why the trades do not count well. Apparently the script ignores the trades that start on the next bar to the bar that marked the closing of a previous trade. How can I fix that?

zR4mtqx.png
 
Last edited:
@eddielee39 Where's the script error that doesn't count trades when 'entry bar' is the next bar to the last 'exit bar'?
Also I don't know, if that script bug doesn't count the trade of the last 'exit bar' or the trade of new 'entry bar'.

def nan = Double.NaN;
def bn = if !IsNaN(close) and !IsNaN(close[1]) and !IsNaN(close[-1]) then BarNumber() else bn[1];
def entry = EntryPrice();
def entryPrice = if !IsNaN(entry) then entry else entryPrice[1];
def hasEntry = !IsNaN(entry);
def isNewEntry = entryPrice != entryPrice[1];
def highFPL = HighestAll(FPL);
def lowFPL = LowestAll(FPL);
def entryBarsTemp = if hasEntry then bn else nan;
def entryBarNum = if hasEntry and isNewEntry then bn else entryBarNum[1];
def isEntryBar = entryBarNum != entryBarNum[1];
def entryBarPL = if isEntryBar then FPL else entryBarPL[1];
def exitBarsTemp = if !hasEntry and bn > entryBarsTemp[1] then bn else nan;
def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp[1]) then bn else exitBarNum[1];
def isExitBar = exitBarNum != exitBarNum[1];
def exitBarPL = if isExitBar then FPL else exitBarPL[1];
def entryReturn = if isExitBar then exitBarPL - exitBarPL[1] else entryReturn[1];
def isWin = if isExitBar and entryReturn >= 0 then 1 else 0;
def isLoss = if isExitBar and entryReturn < 0 then 1 else 0;
def entryReturnWin = if isWin then entryReturn else entryReturnWin[1];
def entryReturnLoss = if isLoss then entryReturn else entryReturnLoss[1];
def entryFPLWins = if isWin then entryReturn else 0;
def entryFPLLosses = if isLoss then entryReturn else 0;
def entryFPLAll = if isLoss or isWin then entryReturn else 0;
 
Be careful with the original script, since it doesn't count the entries when they occur in the bar following the last exit.
Anyone have any idea where the error might be? I can't find it.

def nan = Double.NaN;
def bn = if !IsNaN(close) and !IsNaN(close[1]) and !IsNaN(close[-1]) then BarNumber() else bn[1];
def entry = EntryPrice();

def entryPrice = if !IsNaN(entry) then entry else entryPrice[1];
def hasEntry = !IsNaN(entry);
def isNewEntry = entryPrice != entryPrice[1];

def entryBarsTemp = if hasEntry then bn else nan;
def entryBarNum = if hasEntry and isNewEntry then bn else entryBarNum[1];
def isEntryBar = entryBarNum != entryBarNum[1];

def exitBarsTemp = if !hasEntry and bn > entryBarsTemp[1] then bn else nan;
def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp[1]) then bn else exitBarNum[1];
def isExitBar = exitBarNum != exitBarNum[1];
 
Status
Not open for further replies.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
494 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