Floating P/L (FPL) Labels For ThinkOrSwim

tonight

New member
I have this strategy Flag showing total profits or losses of a strategy you test and it seems to work fine.
But what I would like to have is a similar strategy flag that shows the percentage of profits or losses for the test.
For a simple mind like mine, percentage made or lost would be better than the profit or loss number.
That way no matter what amount you start with, you will know the percentage it makes or loses over that time period?

The code for showing the amount made or loss with a strategy I have is below:
And what I would like is a flag that shows percentage made or loss.

Thanks,

#Script provided courtesy of Mbox56

#Hint: This script will display on the Chart the Profit or Loss of the Strategy being run. Select Dark or Light Theme by selecting Yes or No.

input show_label = yes;
input theme_color_dark = yes;

def FPL_val = Round(FPL(),2);

## DARK THEME
AddLabel(show_label and theme_color_dark and !IsNaN(FPL_val), "FPL: " + AsDollars(FPL_val), if FPL_val > 0.0 then Color.GREEN else
if FPL_val < 0.0 then color.RED else color.ORANGE);

## LIGHT THEME
AddLabel(show_label and !theme_color_dark and !IsNaN(FPL_val), "FPL: " + AsDollars(FPL_val), if FPL_val > 0.0 then Color.DARK_GREEN else if FPL_val < 0.0 then color.DARK_RED else color.DARK_ORANGE);
 

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

Neither Win / Loss Counts Nor Percentages tell the whole story.
In the below example Wins == 3 Losses == 7 the Win percentage is only 30%.
BUT if your Wins were large and your Losses were small. You can have a profitable strategy.
wNVc3aZ.png

The above chart uses the ToS built-in Golden Cross Strategy. This chart is JUST an example. It is NOT recommended.
No indicator can be used in isolation. Research shows you need 3 non-collinear non-repainting studies to build a successful strategy
Shared Chart Link: http://tos.mx/QvIedPw Click here for --> Easiest way to load shared links

Below is the code for the labels displayed on the above chart.
To use these labels, your code must be saved in the STRATEGY tab and must have valid ADDORDER() statements.
Cut & paste the code below to the bottom of your script.

The below code is a snippet of a larger set of labels found here:
https://usethinkscript.com/threads/...acktesting-data-utility-for-thinkorswim.1624/
FPL Labels for Order Management:
Ruby:
# ####################################
# FPL Labels
# https://usethinkscript.com/threads/extended-floating-profit-loss-backtesting-data-utility-for-thinkorswim.1624/
DefineGlobalColor("Pre_Cyan", CreateColor(50, 200, 255)) ;
DefineGlobalColor("LabelGreen",  CreateColor(0, 165, 0)) ;
DefineGlobalColor("LabelRed",  color.magenta) ;
DefineGlobalColor("Neutral",  color.violet) ;
#Inputs
input fplTargetWinLoss = .50;
#hint fplTargetWinLoss: sets the target winlossRatio (in percent) which determines display colors of the W/L label.

input fplTargetWinRate = 1;
#hint fplTargetWinRate: sets the target winRate (float) which determines display colors of the WinRate label;

input fplHidePrice = no;
#hint fplHidePrice: hide's the underlying price graph. \nDefault is no.

input fplHideFPL = yes;
#hint fplHideFPL: hide's the underlying P&L graph.\nDefault is yes.

#temp input var references
def targetWinLoss = fplTargetWinLoss;
def targetWinRate = fplTargetWinRate;
def hidePrice = fplHidePrice;
def hideFPL = fplHideFPL;
#Globals
def nan = Double.NaN;
def bn = if !IsNaN(close) and !IsNaN(close[1]) and !IsNaN(close[-1]) then BarNumber() else bn[1];

#hide chart candles
HidePricePlot(hidePrice);

#Plot default Floating P&L
plot FPL = FPL();
FPL.SetHiding(hideFPL);

#Global Scripts
script incrementValue {
    input condition = yes;
    input increment =  1;
    input startingValue = 0;

    def _value = CompoundValue(1,
                if condition
                then _value[1] + increment
                else _value[1], startingValue);

    plot incrementValue = _value;
}
;

# Entry Calculations.  Note: Only parses on a Strategy Chart
def entry = EntryPrice();

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

def hasEntry = !IsNaN(entry);

def isNewEntry = entryPrice != entryPrice[1];

#is active trade
def highFPL = HighestAll(FPL);
def lowFPL = LowestAll(FPL);

def fplreturn = (FPL - FPL[1]) / FPL[1];
def cumsum = Sum(fplreturn);

def highBarNumber = CompoundValue(1, if FPL == highFPL
                                    then bn
                                    else highBarNumber[1], 0);

def lowBarNumber = CompoundValue(1, if FPL == lowFPL
                                then bn
                                else lowBarNumber[1], 0);

#Win/Loss ratios
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;

#Counts
def entryCount = incrementValue(entryFPLAll);
def winCount = incrementValue(isWin);
def lossCount = incrementValue(isLoss);

def highestReturn = if entryReturnWin[1] > highestReturn[1]
                then entryReturnWin[1]
                else highestReturn[1];

def lowestReturn = if entryReturnLoss[1] < lowestReturn[1]
                then entryReturnLoss[1]
                else lowestReturn[1];


def winRate = if winCount == 0 and lossCount == 0 then 0 else
              if lossCount == 0 then 1 else winCount / lossCount;
def winLossRatio = winCount / entryCount;
def avgReturn = TotalSum(entryFPLAll) / entryCount;
def avgWin = TotalSum(entryFPLWins) / winCount;
def avgLoss = TotalSum(entryFPLLosses) / lossCount;

#Labels

AddLabel(yes,
        text = "Total Trades: " + entryCount + "  ",
        color = GlobalColor("Pre_Cyan")
        );

AddLabel(yes,
        text = "WinCount: " + winCount +
            " | LossCount: " + lossCount ,
        color = if winRate >= targetWinRate
                then GlobalColor("LabelGreen")
                else GlobalColor("LabelRed")
        );

AddLabel(yes,
        text = "W/L: " + AsPercent(winLossRatio) + " ",
        color = if winLossRatio > targetWinLoss
                then GlobalColor("LabelGreen")
                else GlobalColor("LabelRed")
        );

AddLabel(yes,
            " | AvgWin: " + AsDollars(avgWin) +
            " | AvgLoss: " + AsDollars(avgLoss) + "   ",
        color = if avgReturn >= 0
                then GlobalColor("Neutral")
                else GlobalColor("LabelRed")
        );

AddLabel(yes,
        text = "Total Profit: " + AsDollars(TotalSum(entryFPLAll)),
        color = if FPL > 0
                then GlobalColor("LabelGreen")
                else GlobalColor("LabelRed")
    );

Customize the label colors to your preference:
1. click on the gear to the right of the indicator in chart settings
2. scroll down and click on Global Colors.
3. click on the color you want to change
4. choose one of the standard colors or click on RGB and create a custom color
5. click on Save as default at the top of the customizing box so you won't have to make the changes every time you add it to a chart.
VgVBmPG.png


@tonight
 
Last edited:
Thanks for the quick reply. These flags are very helpful.
But I still need another flag. I may have not explained what I need correctly
in my first post.
What I want is a flag that shows the percentage profits or losses for the strategy test.
Like one of the flags shows total profits. I want one that shows percentage of profits made.
Using simple round numbers to explain:
If you start a test with say $1,000 and at the end of the test you have made or lost $1,000.
That would mean you made or lost 100%.
So I would want a flag that shows that the strategy made 100% or what ever percentage it made over the test period.

Thanks again for trying to help,
 
Thanks for the quick reply. These flags are very helpful.
But I still need another flag. I may have not explained what I need correctly
in my first post.
What I want is a flag that shows the percentage profits or losses for the strategy test.
Like one of the flags shows total profits. I want one that shows percentage of profits made.
Using simple round numbers to explain:
If you start a test with say $1,000 and at the end of the test you have made or lost $1,000.
That would mean you made or lost 100%.
So I would want a flag that shows that the strategy made 100% or what ever percentage it made over the test period.

Thanks again for trying to help,
I haven't seen what you are asking for.
But here are all the complete FPL labels. Maybe you can make one of them work for you:
https://usethinkscript.com/threads/...acktesting-data-utility-for-thinkorswim.1624/
 
Upon further analysis the code is working correctly with one exception. If an open trade is closed on the close of the penultimate bar of the current (last day) day of the timeframe, then that trade is not included in the calculations.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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