# Label with information related to strategy

#### crazyturtle

##### New member
Hello, I have been trying to do some P&L backtesting with this TOS' FloatingPL study but noticed it does not take into consideration Short profits at all. It considers Shorts as losses to Long profits. Is there a way around this, to have both, the Longs and Shorts trades calculated as profit? Txs in advance.

Solution
Here is my strategy code and added yours to the end.
It show the labels but wrong results

Code:
``````input length = 20;
input nk = 1.5;
input nBB = 2.0;

def squeeze = TTM_Squeeze(close, length, nk, nBB).SqueezeAlert;

def EMA13 = ExpAverage(close, 13);
def EMA21 = ExpAverage(close, 21);
def EMA34 = ExpAverage(close, 34);

def bullish = EMA13 > EMA21 and EMA21 > EMA34;
def bearish = EMA13 < EMA21 and EMA21 < EMA34;

def fiveDotSqueeze = Sum(squeeze, 5) == 0;

def bullishSignal = if fiveDotSqueeze and bullish then 1 else 0;
def bearishSignal = if fiveDotSqueeze and bearish then 1 else 0;

def exitBullishTrade = if EMA13 < EMA21 and EMA13[1] >= EMA21[1] then 1 else 0;
def exitBearishTrade = if EMA13 > EMA21 and EMA13[1] <= EMA21[1] then 1...``````
I wonder how I can add a label with information related to a basic TTM Squeeze strategy?
I would appreciate guidance or a relevant code (would be dreamy)

The question concerns every strategy and not necessarily this one.
How to add statistics such as total trades, average profitability, etc?
All in the form of a label, to reduce the need to open the report every time ...

Last edited:
EDIT - FIXED , added bn = ....

here is a study that you can add to the end of your strategy/study.

read the comments at the top of it.
you will have to change 2 formulas, so they are equal to your buy/sell conditions/variables, (probably in your addorder() functions).

it will display the gains for longs and shorts, quantity of trades, and the average gain/trade, and total \$ gain.
green are the longs , red are the shorts.

you can enter a desired average trade price. if the average is above that number, then the label is magenta, if not then it is gray.
you can click through stocks in a watchlist and quickly tell if the average is what you want it to be.

it also finds how long ago was the last buy/sell, in bars and minutes, and colors the label appropriately.

Code:
``````# ==========================================

# gain/loss formulas
#  buy/sell trigger variables/conditions
#  replace the 0's with the variables used in your buy and sell addorders()

def buyz = 0;
def sellz = 0;

def bn = barnumber();

def buy1price = if bn == 1 then 0 else if buyz then close else buy1price[1];
def sell1price = if bn == 1 then 0 else if sellz then close else sell1price[1];

def buycnt = if sellz then buycnt[1] + 1 else buycnt[1];
def shortcnt = if buyz then shortcnt[1] + 1 else shortcnt[1];

else if sellz then (buygain[1] + round(sell1price - buy1price, 2))

def shortgain = if sell1price == 0 then shortgain[1]
else if buyz then (shortgain[1] + round(sell1price - buy1price, 2))
else shortgain[1];

def shortavg = round(shortgain/shortcnt, 2);

# enter a min avg \$
input min_avg_dollar_amount = 0.25;

addlabel(1, "Total gain: " + buygain, color.green);
addlabel(1, "avg \$ " + buyavg, (if buyavg >= min_avg_dollar_amount then color.magenta else color.gray));

addlabel(1, "    ", color.black);
addlabel(1, "Total gain: " + shortgain, color.red);
addlabel(1, "avg \$ " + shortavg, (if shortavg >= min_avg_dollar_amount then color.magenta else color.gray));

addlabel(1, "    ", color.black);
addlabel(1, "total gain: \$" + (buygain + shortgain), color.yellow);
addlabel(1, "    ", color.black);

# ---------------

# calc how long ago was the last buy/sell

# add counter - bars since signal
def chartagg = getAggregationPeriod();
def chartmin = (chartagg/1000)/60;
#addlabel(1, "chartmin " + chartmin, color.magenta);

def buy1cnt = if (bn == 1 or buyz)then 0 else buy1cnt[1]+1;
def sell1cnt = if (bn == 1 or sellz)then 0 else sell1cnt[1]+1;
def minbars = min( buy1cnt, sell1cnt);

addlabel(1, minbars + " bars ago", (if buy1cnt < sell1cnt then color.green else color.red));

def barcntmin = (minbars * chartmin);

addlabel(1, barcntmin + " min ago", (if buy1cnt < sell1cnt then color.green else color.red));

# ==========================================
#``````

Last edited:
here is a study that you can add to the end of your strategy/study.

read the comments at the top of it.
you will have to change 2 formulas, so they are equal to your buy/sell conditions/variables, (probably in your addorder() functions).

it will display the gains for longs and shorts, quantity of trades, and the average gain/trade, and total \$ gain.
green are the longs , red are the shorts.

you can enter a desired average trade price. if the average is above that number, then the label is magenta, if not then it is gray.
you can click through stocks in a watchlist and quickly tell if the average is what you want it to be.

it also finds how long ago was the last buy/sell, in bars and minutes, and colors the label appropriately.

Code:
``````# ==========================================

# gain/loss formulas
#  buy/sell trigger variables/conditions
#  replace the 0's with the variables used in your buy and sell addorders()

#def buyz = 0;
#def sellz = 0;

def buy1price = if bn == 1 then 0 else if buyz then close else buy1price[1];
def sell1price = if bn == 1 then 0 else if sellz then close else sell1price[1];

def buycnt = if sellz then buycnt[1] + 1 else buycnt[1];
def shortcnt = if buyz then shortcnt[1] + 1 else shortcnt[1];

else if sellz then (buygain[1] + round(sell1price - buy1price, 2))

def shortgain = if sell1price == 0 then shortgain[1]
else if buyz then (shortgain[1] + round(sell1price - buy1price, 2))
else shortgain[1];

def shortavg = round(shortgain/shortcnt, 2);

# enter a min avg \$
input min_avg_dollar_amount = 0.25;

addlabel(1, "Total gain: " + buygain, color.green);
addlabel(1, "avg \$ " + buyavg, (if buyavg >= min_avg_dollar_amount then color.magenta else color.gray));

addlabel(1, "    ", color.black);
addlabel(1, "Total gain: " + shortgain, color.red);
addlabel(1, "avg \$ " + shortavg, (if shortavg >= min_avg_dollar_amount then color.magenta else color.gray));

addlabel(1, "    ", color.black);
addlabel(1, "total gain: \$" + (buygain + shortgain), color.yellow);
addlabel(1, "    ", color.black);

# ---------------

# calc how long ago was the last buy/sell

# add counter - bars since signal
def chartagg = getAggregationPeriod();
def chartmin = (chartagg/1000)/60;
#addlabel(1, "chartmin " + chartmin, color.magenta);

def buy1cnt = if (bn == 1 or buyz)then 0 else buy1cnt[1]+1;
def sell1cnt = if (bn == 1 or sellz)then 0 else sell1cnt[1]+1;
def minbars = min( buy1cnt, sell1cnt);

addlabel(1, minbars + " bars ago", (if buy1cnt < sell1cnt then color.green else color.red));

def barcntmin = (minbars * chartmin);

addlabel(1, barcntmin + " min ago", (if buy1cnt < sell1cnt then color.green else color.red));

# ==========================================
#``````

Thanks for sharing.
The "bn" is not define and return error, didn't figure it up yet...
BTW - refering to BarNumber() return wrong results

Last edited:
Thanks for sharing.
The "bn" is not define and return error, didn't figure it up yet...
oops, sorry. fixed it.
this was at the end of a long study of mine and bn was defined at the top.

oops, sorry. fixed it.
this was at the end of a long study of mine and bn was defined at the top.
Thanks again
Yet, return wrong data. Cant figure it up

Thanks again
Yet, return wrong data. Cant figure it up

did you change these 2 formulas to be equal to your trigger conditions

def buyz = 0;
def sellz = 0;

did you change these 2 formulas to be equal to your trigger conditions

def buyz = 0;
def sellz = 0;
Yes, Sure

Here is my strategy code and added yours to the end.
It show the labels but wrong results

Code:
``````input length = 20;
input nk = 1.5;
input nBB = 2.0;

def squeeze = TTM_Squeeze(close, length, nk, nBB).SqueezeAlert;

def EMA13 = ExpAverage(close, 13);
def EMA21 = ExpAverage(close, 21);
def EMA34 = ExpAverage(close, 34);

def bullish = EMA13 > EMA21 and EMA21 > EMA34;
def bearish = EMA13 < EMA21 and EMA21 < EMA34;

def fiveDotSqueeze = Sum(squeeze, 5) == 0;

def bullishSignal = if fiveDotSqueeze and bullish then 1 else 0;
def bearishSignal = if fiveDotSqueeze and bearish then 1 else 0;

def exitBullishTrade = if EMA13 < EMA21 and EMA13[1] >= EMA21[1] then 1 else 0;
def exitBearishTrade = if EMA13 > EMA21 and EMA13[1] <= EMA21[1] then 1 else 0;

AddOrder(OrderType.Sell_To_Close, high>= entryPrice() + entryPrice()*0.03, entryPrice() + entryPrice()*0.03, 100);

AddOrder(OrderType.Sell_To_Open, bearishSignal, close, 100);
AddOrder(OrderType.Buy_To_Close, low <= entryPrice() - (entryPrice()*0.03),  entryPrice() - (entryPrice()*0.03), 100);

# gain/loss formulas
#  buy/sell trigger variables/conditions
#  replace the 0's with the variables used in your buy and sell addorders()

def buyz = bullishSignal;
def sellz = bearishSignal;

def bn = barnumber();

def buy1price = if bn == 1 then 0 else if buyz then close else buy1price[1];
def sell1price = if bn == 1 then 0 else if sellz then close else sell1price[1];

def buycnt = if sellz then buycnt[1] + 1 else buycnt[1];
def shortcnt = if buyz then shortcnt[1] + 1 else shortcnt[1];

else if sellz then (buygain[1] + round(sell1price - buy1price, 2))

def shortgain = if sell1price == 0 then shortgain[1]
else if buyz then (shortgain[1] + round(sell1price - buy1price, 2))
else shortgain[1];

def shortavg = round(shortgain/shortcnt, 2);

# enter a min avg \$
input min_avg_dollar_amount = 0.25;

addlabel(1, "Total gain: " + buygain, color.green);
addlabel(1, "avg \$ " + buyavg, (if buyavg >= min_avg_dollar_amount then color.magenta else color.gray));

addlabel(1, "    ", color.black);
addlabel(1, "Total gain: " + shortgain, color.red);
addlabel(1, "avg \$ " + shortavg, (if shortavg >= min_avg_dollar_amount then color.magenta else color.gray));

addlabel(1, "    ", color.black);
addlabel(1, "total gain: \$" + (buygain + shortgain), color.yellow);
addlabel(1, "    ", color.black);

# ---------------

# calc how long ago was the last buy/sell

# add counter - bars since signal
def chartagg = getAggregationPeriod();
def chartmin = (chartagg/1000)/60;
#addlabel(1, "chartmin " + chartmin, color.magenta);

def buy1cnt = if (bn == 1 or buyz)then 0 else buy1cnt[1]+1;
def sell1cnt = if (bn == 1 or sellz)then 0 else sell1cnt[1]+1;
def minbars = min( buy1cnt, sell1cnt);

addlabel(1, minbars + " bars ago", (if buy1cnt < sell1cnt then color.green else color.red));

def barcntmin = (minbars * chartmin);

addlabel(1, barcntmin + " min ago", (if buy1cnt < sell1cnt then color.green else color.red));

# ==========================================
#``````

Here is my strategy code and added yours to the end.
It show the labels but wrong results

Code:
``````input length = 20;
input nk = 1.5;
input nBB = 2.0;

def squeeze = TTM_Squeeze(close, length, nk, nBB).SqueezeAlert;

def EMA13 = ExpAverage(close, 13);
def EMA21 = ExpAverage(close, 21);
def EMA34 = ExpAverage(close, 34);

def bullish = EMA13 > EMA21 and EMA21 > EMA34;
def bearish = EMA13 < EMA21 and EMA21 < EMA34;

def fiveDotSqueeze = Sum(squeeze, 5) == 0;

def bullishSignal = if fiveDotSqueeze and bullish then 1 else 0;
def bearishSignal = if fiveDotSqueeze and bearish then 1 else 0;

def exitBullishTrade = if EMA13 < EMA21 and EMA13[1] >= EMA21[1] then 1 else 0;
def exitBearishTrade = if EMA13 > EMA21 and EMA13[1] <= EMA21[1] then 1 else 0;

AddOrder(OrderType.Sell_To_Close, high>= entryPrice() + entryPrice()*0.03, entryPrice() + entryPrice()*0.03, 100);

AddOrder(OrderType.Sell_To_Open, bearishSignal, close, 100);
AddOrder(OrderType.Buy_To_Close, low <= entryPrice() - (entryPrice()*0.03),  entryPrice() - (entryPrice()*0.03), 100);

# gain/loss formulas
#  buy/sell trigger variables/conditions
#  replace the 0's with the variables used in your buy and sell addorders()

def buyz = bullishSignal;
def sellz = bearishSignal;

def bn = barnumber();

def buy1price = if bn == 1 then 0 else if buyz then close else buy1price[1];
def sell1price = if bn == 1 then 0 else if sellz then close else sell1price[1];

def buycnt = if sellz then buycnt[1] + 1 else buycnt[1];
def shortcnt = if buyz then shortcnt[1] + 1 else shortcnt[1];

else if sellz then (buygain[1] + round(sell1price - buy1price, 2))

def shortgain = if sell1price == 0 then shortgain[1]
else if buyz then (shortgain[1] + round(sell1price - buy1price, 2))
else shortgain[1];

def shortavg = round(shortgain/shortcnt, 2);

# enter a min avg \$
input min_avg_dollar_amount = 0.25;

addlabel(1, "Total gain: " + buygain, color.green);
addlabel(1, "avg \$ " + buyavg, (if buyavg >= min_avg_dollar_amount then color.magenta else color.gray));

addlabel(1, "    ", color.black);
addlabel(1, "Total gain: " + shortgain, color.red);
addlabel(1, "avg \$ " + shortavg, (if shortavg >= min_avg_dollar_amount then color.magenta else color.gray));

addlabel(1, "    ", color.black);
addlabel(1, "total gain: \$" + (buygain + shortgain), color.yellow);
addlabel(1, "    ", color.black);

# ---------------

# calc how long ago was the last buy/sell

# add counter - bars since signal
def chartagg = getAggregationPeriod();
def chartmin = (chartagg/1000)/60;
#addlabel(1, "chartmin " + chartmin, color.magenta);

def buy1cnt = if (bn == 1 or buyz)then 0 else buy1cnt[1]+1;
def sell1cnt = if (bn == 1 or sellz)then 0 else sell1cnt[1]+1;
def minbars = min( buy1cnt, sell1cnt);

addlabel(1, minbars + " bars ago", (if buy1cnt < sell1cnt then color.green else color.red));

def barcntmin = (minbars * chartmin);

addlabel(1, barcntmin + " min ago", (if buy1cnt < sell1cnt then color.green else color.red));

# ==========================================
#``````

i see, i was using it with just 2 triggers. always in a trade, either long or short.
you are using 4 triggers.
i will update it and post in a bit.

Would you post your script so we can see what's happening?
You could always have 2 scripts - one short, one long - and combine the results in a spreadsheet.

Here is my strategy code and added yours to the end.
It show the labels but wrong results

Code:
``````input length = 20;
input nk = 1.5;
input nBB = 2.0;

def squeeze = TTM_Squeeze(close, length, nk, nBB).SqueezeAlert;

def EMA13 = ExpAverage(close, 13);
def EMA21 = ExpAverage(close, 21);
def EMA34 = ExpAverage(close, 34);

def bullish = EMA13 > EMA21 and EMA21 > EMA34;
def bearish = EMA13 < EMA21 and EMA21 < EMA34;

def fiveDotSqueeze = Sum(squeeze, 5) == 0;

def bullishSignal = if fiveDotSqueeze and bullish then 1 else 0;
def bearishSignal = if fiveDotSqueeze and bearish then 1 else 0;

def exitBullishTrade = if EMA13 < EMA21 and EMA13[1] >= EMA21[1] then 1 else 0;
def exitBearishTrade = if EMA13 > EMA21 and EMA13[1] <= EMA21[1] then 1 else 0;

AddOrder(OrderType.Sell_To_Close, high>= entryPrice() + entryPrice()*0.03, entryPrice() + entryPrice()*0.03, 100);

AddOrder(OrderType.Sell_To_Open, bearishSignal, close, 100);
AddOrder(OrderType.Buy_To_Close, low <= entryPrice() - (entryPrice()*0.03),  entryPrice() - (entryPrice()*0.03), 100);

# gain/loss formulas
#  buy/sell trigger variables/conditions
#  replace the 0's with the variables used in your buy and sell addorders()

def buyz = bullishSignal;
def sellz = bearishSignal;

def bn = barnumber();

def buy1price = if bn == 1 then 0 else if buyz then close else buy1price[1];
def sell1price = if bn == 1 then 0 else if sellz then close else sell1price[1];

def buycnt = if sellz then buycnt[1] + 1 else buycnt[1];
def shortcnt = if buyz then shortcnt[1] + 1 else shortcnt[1];

else if sellz then (buygain[1] + round(sell1price - buy1price, 2))

def shortgain = if sell1price == 0 then shortgain[1]
else if buyz then (shortgain[1] + round(sell1price - buy1price, 2))
else shortgain[1];

def shortavg = round(shortgain/shortcnt, 2);

# enter a min avg \$
input min_avg_dollar_amount = 0.25;

addlabel(1, "Total gain: " + buygain, color.green);
addlabel(1, "avg \$ " + buyavg, (if buyavg >= min_avg_dollar_amount then color.magenta else color.gray));

addlabel(1, "    ", color.black);
addlabel(1, "Total gain: " + shortgain, color.red);
addlabel(1, "avg \$ " + shortavg, (if shortavg >= min_avg_dollar_amount then color.magenta else color.gray));

addlabel(1, "    ", color.black);
addlabel(1, "total gain: \$" + (buygain + shortgain), color.yellow);
addlabel(1, "    ", color.black);

# ---------------

# calc how long ago was the last buy/sell

# add counter - bars since signal
def chartagg = getAggregationPeriod();
def chartmin = (chartagg/1000)/60;
#addlabel(1, "chartmin " + chartmin, color.magenta);

def buy1cnt = if (bn == 1 or buyz)then 0 else buy1cnt[1]+1;
def sell1cnt = if (bn == 1 or sellz)then 0 else sell1cnt[1]+1;
def minbars = min( buy1cnt, sell1cnt);

addlabel(1, minbars + " bars ago", (if buy1cnt < sell1cnt then color.green else color.red));

def barcntmin = (minbars * chartmin);

addlabel(1, barcntmin + " min ago", (if buy1cnt < sell1cnt then color.green else color.red));

# ==========================================
#``````

tracking gain/loss
here is an updated version , that uses 4 inputs, instead of 2
i added a couple of test bubbles to display values

Ruby:
``````# gain-loss ver2

# gain/loss formulas
#  buy/sell trigger variables/conditions
#  replace the following 4 formulas with the variables used in your buy and sell addorders()

def long_open = 0;
def long_close = 0;
def short_open = long_close;
def short_close = long_open;

def bn = barnumber();

# true when an open trade
def longtrade = if bn == 1 then 0 else if long_open then 1 else if long_close then 0 else longtrade[1];
def shorttrade = if bn == 1 then 0 else if short_open then 1 else if short_close then 0 else shorttrade[1];

input test_trade_bubbles = no;

def long1price = if bn == 1 then 0 else if long_open then close else long1price[1];
def short1price = if bn == 1 then 0 else if short_open then close else short1price[1];

def longcnt = if long_close then longcnt[1] + 1 else longcnt[1];
def shortcnt = if short_close then shortcnt[1] + 1 else shortcnt[1];

def longgain = if long1price == 0 then longgain[1]
else if long_close then (longgain[1] + round(short1price - long1price, 2))
else longgain[1];

# ??  bw ?  round(short1price - long1price, 2))
def shortgain = if short1price == 0 then shortgain[1]
else if short_close then (shortgain[1] + round(short1price - long1price, 2))
else shortgain[1];

def longavg = round(longgain/longcnt, 2);
def shortavg = round(shortgain/shortcnt, 2);

def ttlgain = (longgain + shortgain);

def tradeclose = (long_close or short_close);

input test_buysell_amounts = yes;
# long price , long ttl,  short price , short ttl , total gain
addchartbubble(test_buysell_amounts and !isnan(close), 10 , "Longs " + "\nP" + long1price + "\nttl " + longgain  + "\n " + "Shorts" + "\nP " + short1price + "\nttl " + shortgain + "\n " + "Total" + "\n " + ttlgain , (if tradeclose then color.cyan else color.gray), yes);

# enter a min avg \$
input min_avg_dollar_amount = 0.5;

addlabel(1, "Total gain: " + longgain, color.green);
addlabel(1, "avg \$ " + longavg, (if longavg >= min_avg_dollar_amount then color.magenta else color.gray));

addlabel(1, "    ", color.black);
addlabel(1, "Total gain: " + shortgain, color.red);
addlabel(1, "avg \$ " + shortavg, (if shortavg >= min_avg_dollar_amount then color.magenta else color.gray));

addlabel(1, "    ", color.black);
addlabel(1, "total gain: \$" + (longgain + shortgain), color.yellow);
addlabel(1, "    ", color.black);

# ---------------

# calc how long ago was the last buy/sell

# add counter - bars since signal
def chartagg = getAggregationPeriod();
def chartmin = (chartagg/1000)/60;
#addlabel(1, "chartmin " + chartmin, color.magenta);

def long2cnt = if (bn == 1 or long_open)then 0 else long2cnt[1]+1;
def short2cnt = if (bn == 1 or short_open)then 0 else short2cnt[1]+1;
def minbars = min(long2cnt, short2cnt);

addlabel(1, minbars + " bars ago", (if long2cnt < short2cnt then color.green else color.red));

def barcntmin = (minbars * chartmin);

addlabel(1, barcntmin + " min ago", (if long2cnt < short2cnt then color.green else color.red));
#``````
hal_pl

Last edited:
# gain-loss ver2 # gain/loss formulas # buy/sell trigger variables/conditions # replace the foll
is it possible to post the version as i tried this code but didn't work. How can we remove the bubble with the right trigger and result?

Thread starter Similar threads Forum Replies Date
Help With Label Scripts Questions 1
Need RVOL Label Questions 0
Help with this label Questions 2
average stock price including reinvested dividends label? Questions 0
AGAIG Stochastics Label Questions 2

### Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
373 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• 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?