New Strategy and I am not good at Plotting Lines as well as Defining One Bubble Only until Another Condition Met

a1cturner

Well-known member
Here is what I have so far (below)... Just need some tweaks. As much as I've coded and tried to plot a STRAIGHT line I cannot figure it out. I also can't figure out how to only see one Buy Bubble until the Close Bubble or Stop Bubble. I can only seem to accomplish this if they are directly next to each other.

Code:
#JT Trend V1
#08/10/2022

declare upper;

input ShowEMA = no;
input ShowVWAP = no;
input ShowHMA = no;
input ShowSMA = no;

#VWAP
input VwapNumDevDn = -2.0;
input VwapNumDevUp = 2.0;

def isPeriodRolled = compoundValue(1, getYyyyMmDd()!= getYyyyMmDd()[1], yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

if (isPeriodRolled) {
    volumeSum = volume;
    volumeVwapSum = volume * vwap;
    volumeVwap2Sum = volume * Sqr(vwap);
} else {
    volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
    volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
    volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

plot VWAP = price;
plot UpperBand = price + VwapNumDevUp * deviation;
plot LowerBand = price + VwapNumDevDn * deviation;

VWAP.setDefaultColor(color.orange);
VWAP.sethiding(!ShowVWAP);
UpperBand.setDefaultColor(color.red);
UpperBand.sethiding(!ShowVWAP);
LowerBand.setDefaultColor(color.green);
LowerBand.sethiding(!ShowVWAP);

#HMA
input HMAPrice = close;
input HMALength = 10;
input HMADisplace = 0;

plot HMA = MovingAverage(AverageType.HULL, HMAprice, HMAlength)[-HMAdisplace];

HMA.DefineColor("Up", color.green);
HMA.DefineColor("Down", color.red);
HMA.SetLineWeight(3);
HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Up") else HMA.color("Down"));
HMA.sethiding(!ShowHMA);

def HMA_VWAP_Close_Short = low < LowerBand within 3 bars and HMA > HMA[1];
def HMA_VWAP_Close_Long = high > UpperBand within 3 bars and HMA < HMA[1];

#SMA
input SMAPrice = close;
input SMALength = 20;
input SMADisplace = 0;

plot SMA = Average(SMAprice[-SMAdisplace], SMAlength);

SMA.SetDefaultColor(color.blue);
SMA.SetStyle(curve.short_dash);
SMA.SetLineWeight(3);
SMA.sethiding(!ShowSMA);

#EMA
input EMALength = 5;

plot EMA = ExpAverage(close, EMALength);
EMA.SetStyle(Curve.Short_Dash);
EMA.SetDefaultColor(color.green);
EMA.HideBubble();
EMA.sethiding(!ShowEMA);

#TSI
input TSILongLength = 25;
input TSIShortLength = 13;
input TSISignalLength = 8;

def TSIDiff = close - close[1];
def DoubleSmoothedAbsDiff = ExpAverage(ExpAverage(AbsValue(TSIDiff), TSILongLength), TSIShortLength);
def TSIRound = Round((100 * (ExpAverage(ExpAverage(TSIDiff, TSILongLength), TSIShortLength)) / DoubleSmoothedAbsDiff), 2);

#Volume (By RIPSTER)

input Show30DayAvg = yes;
input ShowTodayVolume =  yes;
input ShowPercentOf30DayAvg = yes;
input UnusualVolumePercent = 110;
input Show30BarAvg = yes;
input ShowCurrentBar = yes;
input ShowPercentOf30BarAvg = yes;
input ShowSellVolumePercent = yes;

def O = open;
def H = high;
def C = close;
def L = low; 
def V = volume;
def buying = V*(C-L)/(H-L);
def selling = V*(H-C)/(H-L);

# Selling Volume

def SellVol = selling; 

# Total Volume

def BuyVol =  volume; 

#Volume Data

def volLast30DayAvg = (volume(period = "DAY")[1] + volume(period = "DAY")[2] + volume(period = "DAY")[3] + volume(period = "DAY")[4] + volume(period = "DAY")[5] + volume(period = "DAY")[6] + volume(period = "DAY")[7] + volume(period = "DAY")[8] + volume(period = "DAY")[9] + volume(period = "DAY")[10] + volume(period = "DAY")[11] + volume(period = "DAY")[12] + volume(period = "DAY")[13] + volume(period = "DAY")[14] + volume(period = "DAY")[15] + volume(period = "DAY")[16] + volume(period = "DAY")[17] + volume(period = "DAY")[18] + volume(period = "DAY")[19] + volume(period = "DAY")[20] + volume(period = "DAY")[21] + volume(period = "DAY")[22] + volume(period = "DAY")[23] + volume(period = "DAY")[24] + volume(period = "DAY")[25] + volume(period = "DAY")[26] + volume(period = "DAY")[27] + volume(period = "DAY")[28] + volume(period = "DAY")[29] + volume(period = "DAY")[30]) / 30;
def today = volume(period = "DAY");
def percentOf30Day = Round((today / volLast30DayAvg) * 100, 0);
def avg30Bars = (volume[1] + volume[2] + volume[3] + volume[4] + volume[5] + volume[6] + volume[7] + volume[8] + volume[9] + volume[10] + volume[11] + volume[12] + volume[13] + volume[14] + volume[15] + volume[16] + volume[17] + volume[18] + volume[19] + volume[20] + volume[21] + volume[22] + volume[23] + volume[24] + volume[25] + volume[26] + volume[27] + volume[28] + volume[29] + volume[30]) / 30;
def curVolume = volume;
def percentOf30Bar = Round((curVolume / avg30Bars) * 100, 0);
def SellVolPercent = Round((Selling / Volume) * 100, 0);

# Labels

AddLabel(Show30DayAvg, "Avg 30 Days: " + Round(volLast30DayAvg, 0), Color.LIGHT_GRAY);

AddLabel(ShowTodayVolume, "Today: " + today, (if percentOf30Day >= UnusualVolumePercent then Color.GREEN else if percentOf30Day >= 100 then Color.ORANGE else Color.LIGHT_GRAY));

AddLabel(ShowPercentOf30DayAvg, percentOf30Day + "%", (if percentOf30Day >= UnusualVolumePercent then Color.GREEN else if percentOf30Day >= 100 then Color.ORANGE else Color.WHITE) );

AddLabel(Show30BarAvg, "Avg 30 Bars: " + Round(avg30Bars, 0), Color.LIGHT_GRAY);

AddLabel(ShowCurrentBar, "Cur Bar: " + curVolume, (if percentOf30Bar >= UnusualVolumePercent then Color.GREEN else if PercentOf30Bar >= 100 then Color.ORANGE else Color.LIGHT_GRAY));

AddLabel(ShowPercentOf30BarAvg, PercentOf30Bar + "%", (if PercentOf30Bar >= UnusualVolumePercent then Color.GREEN else if PercentOf30Bar >= 100 then Color.ORANGE else Color.WHITE) );

AddLabel(ShowSellVolumePercent, "Cur Bar Sell %: " + SellVolPercent, (if SellVolPercent > 51 then Color.pink else if SellVolPercent < 49 then Color.GREEN else Color.ORANGE));

input length = 50;

def Vol = volume;
def VolAvg = Average(volume, length);

###############################################

#Buy/Sell Signals
input ShowBuySellBubbles = yes;

def PerfectLong = (EMA > SMA) and (close > SMA) and (HMA > HMA[1]) and (TSIRound >= 3) and (Buying > Selling) and (high < UpperBand);
addchartbubble(ShowBuySellBubbles and PerfectLong and !PerfectLong[1], Low * 0.995, "Long", color.green, no);
def PerfectShort = (EMA < SMA) and (close < SMA) and HMA < HMA[1] and (TSIRound <= -3) and (Selling > Buying) and (low > LowerBand);
addchartbubble(ShowBuySellBubbles and PerfectShort and !PerfectShort[1], High * 1.005, "Short", color.green, yes);

def ClosePerfectLong = (close < SMA) or ((high > VWAP) within 3 bars and (HMA < HMA[1]) and (HMA[1] < HMA[2])) or ((high > UpperBand) within 3 bars and (HMA < HMA[1]) and (HMA[1] < HMA[2]));
addchartbubble(ShowBuySellBubbles and ClosePerfectLong and !ClosePerfectLong[1], Low * 0.995, "CloseLong", color.red, no);
def ClosePerfectShort = (close > SMA)  or ((high < VWAP) within 3 bars and (HMA > HMA[1]) and (HMA[1] > HMA[2])) or ((high < LowerBand) within 3 bars and (HMA > HMA[1]) and (HMA[1] > HMA[2]));
addchartbubble(ShowBuySellBubbles and ClosePerfectShort and !ClosePerfectShort[1], High * 1.005, "CloseShort", color.red, yes);

plot StopPerfectLong = if PerfectLong then low else double.nan;
addchartbubble(ShowBuySellBubbles and low < StopPerfectLong, Low * 0.995, "StopLong", color.cyan, no);
plot StopPerfectShort = if PerfectShort then high else double.nan;
addchartbubble(ShowBuySellBubbles and high > StopPerfectShort, High * 1.005, "StopShort", color.cyan, yes);

#BUY ORDERS
input ShowOrders = no;
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and PerfectLong, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN);
AddOrder(OrderType.SELL_TO_OPEN, ShowOrders and PerfectShort, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN);

#SELL ORDERS
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and ClosePerfectLong, close, 100, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and low < StopPerfectLong, close, 100, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and ClosePerfectShort, close, 100, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and high > StopPerfectShort, close, 100, Color.RED, Color.LIGHT_RED);

This is my request by priority:

Priority 1. Once I get a long signal bubble I don't want to SEE additional long signal bubbles again until after a sell or stop signal (stop signal still needs work, that is priority 2). I still want the long criteria to be there in a "def =" I just don't want to see any additional bubbles past the first (I still want the signal just not the bubble because that affects priority 2). Once I get a SellLong or StopLong signal, then the next trend begins and I want that Long Signal Bubble again. This applies to Short as well.

Ktwrpq0.png


Priority 2. I am trying to plot a stop loss line from the high (for now) of the candle that gives me the signal. i.e.
Code:
plot StopPerfectLong = if PerfectLong then low else double.nan;
plot StopPerfectShort = if PerfectShort then high else double.nan;

I want this line to continue until I either get stopped out OR until I get a sell signal. In addition I would like to have it automatically adjust, if possible to the new low for a long position or high for a short position if I get another long or short signal (not bubble)

yPuH2Uh.png


I hope this makes since. I believe it will be easy for some of the experienced guys. It's frustrating to me that I can't wrap my head around it.
 
My42Ha7.png


O0WTycf.png

Ruby:
#JT Trend V1
#08/10/2022

declare upper;

input ShowEMA = no;
input ShowVWAP = no;
input ShowHMA = no;
input ShowSMA = no;

#VWAP
input VwapNumDevDn = -2.0;
input VwapNumDevUp = 2.0;

def isPeriodRolled = compoundValue(1, getYyyyMmDd()!= getYyyyMmDd()[1], yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

if (isPeriodRolled) {
    volumeSum = volume;
    volumeVwapSum = volume * vwap;
    volumeVwap2Sum = volume * Sqr(vwap);
} else {
    volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
    volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
    volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

plot VWAP = price;
plot UpperBand = price + VwapNumDevUp * deviation;
plot LowerBand = price + VwapNumDevDn * deviation;

VWAP.setDefaultColor(color.orange);
VWAP.sethiding(!ShowVWAP);
UpperBand.setDefaultColor(color.red);
UpperBand.sethiding(!ShowVWAP);
LowerBand.setDefaultColor(color.green);
LowerBand.sethiding(!ShowVWAP);

#HMA
input HMAPrice = close;
input HMALength = 10;
input HMADisplace = 0;

plot HMA = MovingAverage(AverageType.HULL, HMAprice, HMAlength)[-HMAdisplace];

HMA.DefineColor("Up", color.green);
HMA.DefineColor("Down", color.red);
HMA.SetLineWeight(3);
HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Up") else HMA.color("Down"));
HMA.sethiding(!ShowHMA);

def HMA_VWAP_Close_Short = low < LowerBand within 3 bars and HMA > HMA[1];
def HMA_VWAP_Close_Long = high > UpperBand within 3 bars and HMA < HMA[1];

#SMA
input SMAPrice = close;
input SMALength = 20;
input SMADisplace = 0;

plot SMA = Average(SMAprice[-SMAdisplace], SMAlength);

SMA.SetDefaultColor(color.blue);
SMA.SetStyle(curve.short_dash);
SMA.SetLineWeight(3);
SMA.sethiding(!ShowSMA);

#EMA
input EMALength = 5;

plot EMA = ExpAverage(close, EMALength);
EMA.SetStyle(Curve.Short_Dash);
EMA.SetDefaultColor(color.green);
EMA.HideBubble();
EMA.sethiding(!ShowEMA);

#TSI
input TSILongLength = 25;
input TSIShortLength = 13;
input TSISignalLength = 8;

def TSIDiff = close - close[1];
def DoubleSmoothedAbsDiff = ExpAverage(ExpAverage(AbsValue(TSIDiff), TSILongLength), TSIShortLength);
def TSIRound = Round((100 * (ExpAverage(ExpAverage(TSIDiff, TSILongLength), TSIShortLength)) / DoubleSmoothedAbsDiff), 2);

#Volume (By RIPSTER)

input Show30DayAvg = yes;
input ShowTodayVolume =  yes;
input ShowPercentOf30DayAvg = yes;
input UnusualVolumePercent = 110;
input Show30BarAvg = yes;
input ShowCurrentBar = yes;
input ShowPercentOf30BarAvg = yes;
input ShowSellVolumePercent = yes;

def O = open;
def H = high;
def C = close;
def L = low; 
def V = volume;
def buying = V*(C-L)/(H-L);
def selling = V*(H-C)/(H-L);

# Selling Volume

def SellVol = selling; 

# Total Volume

def BuyVol =  volume; 

#Volume Data

def volLast30DayAvg = (volume(period = "DAY")[1] + volume(period = "DAY")[2] + volume(period = "DAY")[3] + volume(period = "DAY")[4] + volume(period = "DAY")[5] + volume(period = "DAY")[6] + volume(period = "DAY")[7] + volume(period = "DAY")[8] + volume(period = "DAY")[9] + volume(period = "DAY")[10] + volume(period = "DAY")[11] + volume(period = "DAY")[12] + volume(period = "DAY")[13] + volume(period = "DAY")[14] + volume(period = "DAY")[15] + volume(period = "DAY")[16] + volume(period = "DAY")[17] + volume(period = "DAY")[18] + volume(period = "DAY")[19] + volume(period = "DAY")[20] + volume(period = "DAY")[21] + volume(period = "DAY")[22] + volume(period = "DAY")[23] + volume(period = "DAY")[24] + volume(period = "DAY")[25] + volume(period = "DAY")[26] + volume(period = "DAY")[27] + volume(period = "DAY")[28] + volume(period = "DAY")[29] + volume(period = "DAY")[30]) / 30;
def today = volume(period = "DAY");
def percentOf30Day = Round((today / volLast30DayAvg) * 100, 0);
def avg30Bars = (volume[1] + volume[2] + volume[3] + volume[4] + volume[5] + volume[6] + volume[7] + volume[8] + volume[9] + volume[10] + volume[11] + volume[12] + volume[13] + volume[14] + volume[15] + volume[16] + volume[17] + volume[18] + volume[19] + volume[20] + volume[21] + volume[22] + volume[23] + volume[24] + volume[25] + volume[26] + volume[27] + volume[28] + volume[29] + volume[30]) / 30;
def curVolume = volume;
def percentOf30Bar = Round((curVolume / avg30Bars) * 100, 0);
def SellVolPercent = Round((Selling / Volume) * 100, 0);

# Labels

AddLabel(Show30DayAvg, "Avg 30 Days: " + Round(volLast30DayAvg, 0), Color.LIGHT_GRAY);

AddLabel(ShowTodayVolume, "Today: " + today, (if percentOf30Day >= UnusualVolumePercent then Color.GREEN else if percentOf30Day >= 100 then Color.ORANGE else Color.LIGHT_GRAY));

AddLabel(ShowPercentOf30DayAvg, percentOf30Day + "%", (if percentOf30Day >= UnusualVolumePercent then Color.GREEN else if percentOf30Day >= 100 then Color.ORANGE else Color.WHITE) );

AddLabel(Show30BarAvg, "Avg 30 Bars: " + Round(avg30Bars, 0), Color.LIGHT_GRAY);

AddLabel(ShowCurrentBar, "Cur Bar: " + curVolume, (if percentOf30Bar >= UnusualVolumePercent then Color.GREEN else if PercentOf30Bar >= 100 then Color.ORANGE else Color.LIGHT_GRAY));

AddLabel(ShowPercentOf30BarAvg, PercentOf30Bar + "%", (if PercentOf30Bar >= UnusualVolumePercent then Color.GREEN else if PercentOf30Bar >= 100 then Color.ORANGE else Color.WHITE) );

AddLabel(ShowSellVolumePercent, "Cur Bar Sell %: " + SellVolPercent, (if SellVolPercent > 51 then Color.pink else if SellVolPercent < 49 then Color.GREEN else Color.ORANGE));

input length = 50;

def Vol = volume;
def VolAvg = Average(volume, length);

###############################################

#Buy/Sell Signals
input ShowBuySellBubbles = yes;

#Long Condition#
def Long = (EMA > SMA) and (close > SMA) and (HMA > HMA[1]) and (TSIRound >= 3) and (Buying > Selling) and (high < UpperBand);
#Close Long Condition#
def LongClose = ((close < SMA) or ((high > VWAP) within 3 bars and (HMA < HMA[1]) and (HMA[1] < HMA[2])) or ((high > UpperBand) within 3 bars and (HMA < HMA[1]) and (HMA[1] < HMA[2])));
#Stop Long Condition#
def LongStop = (EMA > SMA) and (close > SMA) and (HMA > HMA[1]) and (TSIRound >= 3) and (Buying > Selling) and (high < UpperBand);

#Short Condtion#
def Short = (EMA < SMA) and (close < SMA) and HMA < HMA[1] and (TSIRound <= -3) and (Selling > Buying) and (low > LowerBand);
#Close Short Condition#
def ShortClose = ((close > SMA)  or ((high < VWAP) within 3 bars and (HMA > HMA[1]) and (HMA[1] > HMA[2])) or ((high < LowerBand) within 3 bars and (HMA > HMA[1]) and (HMA[1] > HMA[2])));
#Stop Short Condition#
def ShortStop = (EMA < SMA) and (close < SMA) and HMA < HMA[1] and (TSIRound <= -3) and (Selling > Buying) and (low > LowerBand);

###############################################

def PerfectLong; 
def PerfectShort;
def ClosePerfectLong;
def ClosePerfectShort;
def StopPerfectLong;
def StopPerfectShort;

if high >= StopPerfectShort[1] and PerfectShort[1] {
    PerfectLong = PerfectLong[1];
    PerfectShort = 0;
    ClosePerfectLong = ClosePerfectLong[1];
    ClosePerfectShort = 1;
    StopPerfectLong = StopPerfectLong[1];
    StopPerfectShort = Double.NaN;
} else if low <= StopPerfectLong[1] and PerfectLong[1] {
    PerfectLong = 0;
    PerfectShort = PerfectShort[1];
    ClosePerfectLong = 1;
    ClosePerfectShort = ClosePerfectShort[1];
    StopPerfectLong = Double.NaN;
    StopPerfectShort = StopPerfectShort[1];
} else if LongStop and PerfectLong[1] {
    PerfectLong = PerfectLong[1];
    PerfectShort = PerfectShort[1];
    ClosePerfectLong = ClosePerfectLong[1];
    ClosePerfectShort = ClosePerfectShort[1];
    StopPerfectLong = if !StopPerfectLong[1] or IsNaN(StopPerfectLong[1]) then low else if low > StopPerfectLong[1] then low else StopPerfectLong[1];
    StopPerfectShort = StopPerfectShort[1];
} else if ShortStop and PerfectShort[1] {
    PerfectLong = PerfectLong[1];
    PerfectShort = PerfectShort[1];
    ClosePerfectLong = ClosePerfectLong[1];
    ClosePerfectShort = ClosePerfectShort[1];
    StopPerfectLong = StopPerfectLong[1];    
    StopPerfectShort = if !StopPerfectShort[1] or IsNaN(StopPerfectShort[1]) then high else if high < StopPerfectShort[1] then high else StopPerfectShort[1];
} else if LongClose and PerfectLong[1] {
    PerfectLong = 0;
    PerfectShort = PerfectShort[1];
    ClosePerfectLong = 1;
    ClosePerfectShort = ClosePerfectShort[1];
    StopPerfectLong = Double.NaN;
    StopPerfectShort = StopPerfectShort[1];
} else if ShortClose and PerfectShort[1] {
    PerfectLong = PerfectLong[1];
    PerfectShort = 0;
    ClosePerfectLong = ClosePerfectLong[1];
    ClosePerfectShort = 1;
    StopPerfectLong = StopPerfectLong[1];
    StopPerfectShort = Double.NaN;
} else if Long and !PerfectShort[1] {
    PerfectLong = 1;
    PerfectShort = PerfectShort[1];
    ClosePerfectLong = 0;
    ClosePerfectShort = ClosePerfectShort[1];
    StopPerfectLong = low;
    StopPerfectShort = StopPerfectShort[1];
} else if Short and !PerfectLong[1] {
    PerfectLong = PerfectLong[1];
    PerfectShort = 1;
    ClosePerfectLong = ClosePerfectLong[1];
    ClosePerfectShort = 0;
    StopPerfectLong = StopPerfectLong[1];
    StopPerfectShort = high;
} else {
    PerfectLong = PerfectLong[1];
    PerfectShort = PerfectShort[1];
    ClosePerfectLong = ClosePerfectLong[1];
    ClosePerfectShort = ClosePerfectShort[1];
    StopPerfectLong = StopPerfectLong[1];
    StopPerfectShort = StopPerfectShort[1];}

addchartbubble(ShowBuySellBubbles and PerfectLong and !PerfectLong[1], Low * 0.995, "Long", color.green, no);
addchartbubble(ShowBuySellBubbles and PerfectShort and !PerfectShort[1], High * 1.005, "Short", color.green, yes);
addchartbubble(ShowBuySellBubbles and ClosePerfectLong and !ClosePerfectLong[1] and low > StopPerfectLong[1], Low * 0.995, "CloseLong", color.red, no);
addchartbubble(ShowBuySellBubbles and ClosePerfectShort and !ClosePerfectShort[1] and High < StopPerfectShort[1], High * 1.005, "CloseShort", color.red, yes);


plot StopPerfectLongPlot = if PerfectLong then StopPerfectLong else if low <= StopPerfectLong[1] or ClosePerfectLong then StopPerfectLong[1] else StopPerfectLong;
StopPerfectLongPlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
addchartbubble(ShowBuySellBubbles and low <= StopPerfectLong[1], Low * 0.995, "StopLong", color.cyan, no);

plot StopPerfectShortPlot = if PerfectShort then StopPerfectShort else if high >= StopPerfectShort[1] or ClosePerfectShort then StopPerfectShort[1] else StopPerfectShort;
StopPerfectShortPlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
addchartbubble(ShowBuySellBubbles and high >= StopPerfectShort[1], High * 1.005, "StopShort", color.cyan, yes);

#BUY ORDERS
input ShowOrders = yes;
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and PerfectLong, open[-1], 1, Color.GREEN, Color.LIGHT_GREEN);
AddOrder(OrderType.SELL_TO_OPEN, ShowOrders and PerfectShort, open[-1], 1, Color.GREEN, Color.LIGHT_GREEN);

#SELL ORDERS
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and ClosePerfectLong and IsNan(StopPerfectLong), close, 1, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and low[-1] <= StopPerfectLongPlot, StopPerfectLongPlot, 1, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and ClosePerfectShort and IsNaN(StopPerfectShort), close, 1, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and high[-1] >= StopPerfectShortPlot, StopPerfectShortPlot, 1, Color.RED, Color.LIGHT_RED);
 
Last edited:
My42Ha7.png


O0WTycf.png

Ruby:
#JT Trend V1
#08/10/2022

declare upper;

input ShowEMA = no;
input ShowVWAP = no;
input ShowHMA = no;
input ShowSMA = no;

#VWAP
input VwapNumDevDn = -2.0;
input VwapNumDevUp = 2.0;

def isPeriodRolled = compoundValue(1, getYyyyMmDd()!= getYyyyMmDd()[1], yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

if (isPeriodRolled) {
    volumeSum = volume;
    volumeVwapSum = volume * vwap;
    volumeVwap2Sum = volume * Sqr(vwap);
} else {
    volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
    volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
    volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

plot VWAP = price;
plot UpperBand = price + VwapNumDevUp * deviation;
plot LowerBand = price + VwapNumDevDn * deviation;

VWAP.setDefaultColor(color.orange);
VWAP.sethiding(!ShowVWAP);
UpperBand.setDefaultColor(color.red);
UpperBand.sethiding(!ShowVWAP);
LowerBand.setDefaultColor(color.green);
LowerBand.sethiding(!ShowVWAP);

#HMA
input HMAPrice = close;
input HMALength = 10;
input HMADisplace = 0;

plot HMA = MovingAverage(AverageType.HULL, HMAprice, HMAlength)[-HMAdisplace];

HMA.DefineColor("Up", color.green);
HMA.DefineColor("Down", color.red);
HMA.SetLineWeight(3);
HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Up") else HMA.color("Down"));
HMA.sethiding(!ShowHMA);

def HMA_VWAP_Close_Short = low < LowerBand within 3 bars and HMA > HMA[1];
def HMA_VWAP_Close_Long = high > UpperBand within 3 bars and HMA < HMA[1];

#SMA
input SMAPrice = close;
input SMALength = 20;
input SMADisplace = 0;

plot SMA = Average(SMAprice[-SMAdisplace], SMAlength);

SMA.SetDefaultColor(color.blue);
SMA.SetStyle(curve.short_dash);
SMA.SetLineWeight(3);
SMA.sethiding(!ShowSMA);

#EMA
input EMALength = 5;

plot EMA = ExpAverage(close, EMALength);
EMA.SetStyle(Curve.Short_Dash);
EMA.SetDefaultColor(color.green);
EMA.HideBubble();
EMA.sethiding(!ShowEMA);

#TSI
input TSILongLength = 25;
input TSIShortLength = 13;
input TSISignalLength = 8;

def TSIDiff = close - close[1];
def DoubleSmoothedAbsDiff = ExpAverage(ExpAverage(AbsValue(TSIDiff), TSILongLength), TSIShortLength);
def TSIRound = Round((100 * (ExpAverage(ExpAverage(TSIDiff, TSILongLength), TSIShortLength)) / DoubleSmoothedAbsDiff), 2);

#Volume (By RIPSTER)

input Show30DayAvg = yes;
input ShowTodayVolume =  yes;
input ShowPercentOf30DayAvg = yes;
input UnusualVolumePercent = 110;
input Show30BarAvg = yes;
input ShowCurrentBar = yes;
input ShowPercentOf30BarAvg = yes;
input ShowSellVolumePercent = yes;

def O = open;
def H = high;
def C = close;
def L = low;
def V = volume;
def buying = V*(C-L)/(H-L);
def selling = V*(H-C)/(H-L);

# Selling Volume

def SellVol = selling;

# Total Volume

def BuyVol =  volume;

#Volume Data

def volLast30DayAvg = (volume(period = "DAY")[1] + volume(period = "DAY")[2] + volume(period = "DAY")[3] + volume(period = "DAY")[4] + volume(period = "DAY")[5] + volume(period = "DAY")[6] + volume(period = "DAY")[7] + volume(period = "DAY")[8] + volume(period = "DAY")[9] + volume(period = "DAY")[10] + volume(period = "DAY")[11] + volume(period = "DAY")[12] + volume(period = "DAY")[13] + volume(period = "DAY")[14] + volume(period = "DAY")[15] + volume(period = "DAY")[16] + volume(period = "DAY")[17] + volume(period = "DAY")[18] + volume(period = "DAY")[19] + volume(period = "DAY")[20] + volume(period = "DAY")[21] + volume(period = "DAY")[22] + volume(period = "DAY")[23] + volume(period = "DAY")[24] + volume(period = "DAY")[25] + volume(period = "DAY")[26] + volume(period = "DAY")[27] + volume(period = "DAY")[28] + volume(period = "DAY")[29] + volume(period = "DAY")[30]) / 30;
def today = volume(period = "DAY");
def percentOf30Day = Round((today / volLast30DayAvg) * 100, 0);
def avg30Bars = (volume[1] + volume[2] + volume[3] + volume[4] + volume[5] + volume[6] + volume[7] + volume[8] + volume[9] + volume[10] + volume[11] + volume[12] + volume[13] + volume[14] + volume[15] + volume[16] + volume[17] + volume[18] + volume[19] + volume[20] + volume[21] + volume[22] + volume[23] + volume[24] + volume[25] + volume[26] + volume[27] + volume[28] + volume[29] + volume[30]) / 30;
def curVolume = volume;
def percentOf30Bar = Round((curVolume / avg30Bars) * 100, 0);
def SellVolPercent = Round((Selling / Volume) * 100, 0);

# Labels

AddLabel(Show30DayAvg, "Avg 30 Days: " + Round(volLast30DayAvg, 0), Color.LIGHT_GRAY);

AddLabel(ShowTodayVolume, "Today: " + today, (if percentOf30Day >= UnusualVolumePercent then Color.GREEN else if percentOf30Day >= 100 then Color.ORANGE else Color.LIGHT_GRAY));

AddLabel(ShowPercentOf30DayAvg, percentOf30Day + "%", (if percentOf30Day >= UnusualVolumePercent then Color.GREEN else if percentOf30Day >= 100 then Color.ORANGE else Color.WHITE) );

AddLabel(Show30BarAvg, "Avg 30 Bars: " + Round(avg30Bars, 0), Color.LIGHT_GRAY);

AddLabel(ShowCurrentBar, "Cur Bar: " + curVolume, (if percentOf30Bar >= UnusualVolumePercent then Color.GREEN else if PercentOf30Bar >= 100 then Color.ORANGE else Color.LIGHT_GRAY));

AddLabel(ShowPercentOf30BarAvg, PercentOf30Bar + "%", (if PercentOf30Bar >= UnusualVolumePercent then Color.GREEN else if PercentOf30Bar >= 100 then Color.ORANGE else Color.WHITE) );

AddLabel(ShowSellVolumePercent, "Cur Bar Sell %: " + SellVolPercent, (if SellVolPercent > 51 then Color.pink else if SellVolPercent < 49 then Color.GREEN else Color.ORANGE));

input length = 50;

def Vol = volume;
def VolAvg = Average(volume, length);

###############################################

#Buy/Sell Signals
input ShowBuySellBubbles = yes;

def PerfectLong;
def PerfectShort;
def ClosePerfectLong;
def ClosePerfectShort;
def StopPerfectLong;
def StopPerfectShort;

if high >= StopPerfectShort[1] and PerfectShort[1] {
    PerfectLong = PerfectLong[1];
    PerfectShort = 0;
    ClosePerfectLong = ClosePerfectLong[1];
    ClosePerfectShort = 1;
    StopPerfectLong = StopPerfectLong[1];
    StopPerfectShort = Double.NaN;
} else if low <= StopPerfectLong[1] and PerfectLong[1] {
    PerfectLong = 0;
    PerfectShort = PerfectShort[1];
    ClosePerfectLong = 1;
    ClosePerfectShort = closePerfectShort[1];
    StopPerfectLong = Double.NaN;
    StopPerfectShort = StopPerfectShort[1];
} else if (EMA > SMA) and (close > SMA) and (HMA > HMA[1]) and (TSIRound >= 3) and (Buying > Selling) and (high < UpperBand) and PerfectLong[1] {
    PerfectLong = PerfectLong[1];
    PerfectShort = PerfectShort[1];
    ClosePerfectLong = ClosePerfectLong[1];
    ClosePerfectShort = closePerfectShort[1];
    StopPerfectLong = if !StopPerfectLong[1] or IsNaN(StopPerfectLong[1]) then low else if low > StopPerfectLong[1] then low else StopPerfectLong[1];
    StopPerfectShort = StopPerfectShort[1];
} else if (EMA < SMA) and (close < SMA) and HMA < HMA[1] and (TSIRound <= -3) and (Selling > Buying) and (low > LowerBand) and PerfectShort[1] {
    PerfectLong = PerfectLong[1];
    PerfectShort = PerfectShort[1];
    ClosePerfectLong = ClosePerfectLong[1];
    ClosePerfectShort = ClosePerfectShort[1];
    StopPerfectLong = StopPerfectLong[1];  
    StopPerfectShort = if !StopPerfectShort[1] or IsNaN(StopPerfectShort[1]) then high else if high < StopPerfectShort[1] then high else StopPerfectShort[1];
} else if ((close < SMA) or ((high > VWAP) within 3 bars and (HMA < HMA[1]) and (HMA[1] < HMA[2])) or ((high > UpperBand) within 3 bars and (HMA < HMA[1]) and (HMA[1] < HMA[2]))) and PerfectLong[1] {
    PerfectLong = 0;
    PerfectShort = PerfectShort[1];
    ClosePerfectLong = 1;
    ClosePerfectShort = ClosePerfectShort[1];
    StopPerfectLong = Double.NaN;
    StopPerfectShort = StopPerfectShort[1];
} else if ((close > SMA)  or ((high < VWAP) within 3 bars and (HMA > HMA[1]) and (HMA[1] > HMA[2])) or ((high < LowerBand) within 3 bars and (HMA > HMA[1]) and (HMA[1] > HMA[2]))) and PerfectShort[1] {
    PerfectLong = PerfectLong[1];
    PerfectShort = 0;
    ClosePerfectLong = ClosePerfectLong[1];
    ClosePerfectShort = 1;
    StopPerfectLong = StopPerfectLong[1];
    StopPerfectShort = Double.NaN;
} else if (EMA > SMA) and (close > SMA) and (HMA > HMA[1]) and (TSIRound >= 3) and (Buying > Selling) and (high < UpperBand) and !PerfectShort[1]{
    PerfectLong = 1;
    PerfectShort = PerfectShort[1];
    ClosePerfectLong = 0;
    ClosePerfectShort = ClosePerfectShort[1];
    StopPerfectLong = low;
    StopPerfectShort = StopPerfectShort[1];
} else if (EMA < SMA) and (close < SMA) and HMA < HMA[1] and (TSIRound <= -3) and (Selling > Buying) and (low > LowerBand) and !PerfectLong[1] {
    PerfectLong = PerfectLong[1];
    PerfectShort = 1;
    ClosePerfectLong = ClosePerfectLong[1];
    ClosePerfectShort = 0;
    StopPerfectLong = StopPerfectLong[1];
    StopPerfectShort = high;
} else {
    PerfectLong = PerfectLong[1];
    PerfectShort = PerfectShort[1];
    ClosePerfectLong = ClosePerfectLong[1];
    ClosePerfectShort = closePerfectShort[1];
    StopPerfectLong = StopPerfectLong[1];
    StopPerfectShort = StopPerfectShort[1];}

addchartbubble(ShowBuySellBubbles and PerfectLong and !PerfectLong[1], Low * 0.995, "Long", color.green, no);
addchartbubble(ShowBuySellBubbles and PerfectShort and !PerfectShort[1], High * 1.005, "Short", color.green, yes);
addchartbubble(ShowBuySellBubbles and ClosePerfectLong and !ClosePerfectLong[1] and low > StopPerfectLong[1], Low * 0.995, "CloseLong", color.red, no);
addchartbubble(ShowBuySellBubbles and ClosePerfectShort and !ClosePerfectShort[1] and High < StopPerfectShort[1], High * 1.005, "CloseShort", color.red, yes);


plot StopPerfectLongPlot = StopPerfectLong;
StopPerfectLongPlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
addchartbubble(ShowBuySellBubbles and low < StopPerfectLong[1], Low * 0.995, "StopLong", color.cyan, no);
plot StopPerfectShortPlot = StopPerfectShort;
StopPerfectShortPlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
addchartbubble(ShowBuySellBubbles and high > StopPerfectShort[1], High * 1.005, "StopShort", color.cyan, yes);

#BUY ORDERS
input ShowOrders = no;
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and PerfectLong, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN);
AddOrder(OrderType.SELL_TO_OPEN, ShowOrders and PerfectShort, open[-1], 100, Color.GREEN, Color.LIGHT_GREEN);

#SELL ORDERS
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and ClosePerfectLong and IsNan(StopPerfectLong), close, 100, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and low[-1] <= StopPerfectLong, StopPerfectLong, 100, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and ClosePerfectShort and IsNaN(StopPerfectShort), close, 100, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and high[-1] >= StopPerfectShort, StopPerfectShort, 100, Color.RED, Color.LIGHT_RED);
I knew y'all could do it! Still tweaking the code a bit. Tons of losers but good gains on the winners. Backtest showed a $16,000 profit in 20 days on TSLA with a draw down of about $3,000. Very minimal gains on AAPL, NVDA, GOOG. SPY made about $700 with very minimal losers and QQQ made about $1,100 with most losses only around $50 or less on average.

I will probably give the stop a little more room to breath and see how that works.

@Svanoy one last request. Do you know how I could extend those stop lines one extra bar to the right so that it goes through the candle that stopped it out?
 
Update code above to extend stop loss line through bar triggering stop loss. Also separated out Long, Short, Close, and Stop conditions for ease of modification.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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