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.Buy_To_Open, bullishSignal, close, 100);
AddOrder(OrderType.Sell_To_Close, high>= entryPrice() + entryPrice()*0.03, entryPrice() + entryPrice()*0.03, 100);
AddOrder(OrderType.Sell_To_Close, exitBullishTrade, close, 100);
AddOrder(OrderType.Sell_To_Open, bearishSignal, close, 100);
AddOrder(OrderType.Buy_To_Close, low <= entryPrice() - (entryPrice()*0.03), entryPrice() - (entryPrice()*0.03), 100);
AddOrder(OrderType.Buy_To_Close, exitBearishTrade, close, 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];
def buygain = if buy1price == 0 then buygain[1]
else if sellz then (buygain[1] + round(sell1price - buy1price, 2))
else buygain[1];
def shortgain = if sell1price == 0 then shortgain[1]
else if buyz then (shortgain[1] + round(sell1price - buy1price, 2))
else shortgain[1];
def buyavg = round(buygain/buycnt, 2);
def shortavg = round(shortgain/shortcnt, 2);
# enter a min avg $
input min_avg_dollar_amount = 0.25;
addlabel(1, "Longs:", color.gray);
addlabel(1, "Total gain: " + buygain, color.green);
addlabel(1, "trades: " + buycnt, color.green);
addlabel(1, "avg $ " + buyavg, (if buyavg >= min_avg_dollar_amount then color.magenta else color.gray));
addlabel(1, " ", color.black);
addlabel(1, "Shorts:", color.gray);
addlabel(1, "Total gain: " + shortgain, color.red);
addlabel(1, "trades: " + shortcnt, 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, "---", color.gray);
addlabel(1, minbars + " bars ago", (if buy1cnt < sell1cnt then color.green else color.red));
#addlabel(1, "---", color.gray);
def barcntmin = (minbars * chartmin);
addlabel(1, barcntmin + " min ago", (if buy1cnt < sell1cnt then color.green else color.red));
# ==========================================
#