Hello,
I am trying to learn how to write/manipulate code to combine a signal from existing code to generate if another criteria is met such as stacked ema's. I have an indicator that gives a buy and sell signal that I use with confluence of stacked ema's (8 and 21). The code I wrote shows an up arrow when 8 crosses above 21 and a down arrow when 5 crosses below the 21.
I dabbled in writing some code for the ema crossover that I was able to get to work so I was happy about that with it being my first time but I know I have a ton to learn to get this to work how I would like and eventually build the strategy.
# EMA Crossover Buy Signal and Exit signal
# This script generates buy signals when the 8-period EMA crosses above the 21-period EMA and generates sell signals when then 5 crosses below the 21-period EMA
input fastLength = 5;
input medianLength = 8;
input slowLength = 21;
def EMAfast = ExpAverage(close, fastLength);
def EMAmedian = EXpAverage (close, medianLength);
def EMAslow = ExpAverage(close, slowLength);
plot BuySignal = if EMAmedian > EMAslow and EMAmedian[1] <= EMAslow[1] then 1 else 0;
BuySignal.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_UP);
BuySignal.SetDefaultColor(Color.GREEN);
plot SellSignal = if EMAfast < EMAslow and EMAfast[1] >= EMAslow[1] then 1 else 0;
SellSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SellSignal.SetDefaultColor(Color.RED);
Now I am trying to figure out how to use this existing code to plot only when there are stacked ema's but honestly I have no idea where to start. Any guidance is appreciated.
input Key_Value = 1.0;
def a = key_value;# input(1, title = "Key Vaule. 'This changes the sensitivity'")
input atr_period = 10;
def c = atr_period; # input(10, title = "ATR Period")
input Signals_from_Heikin_Ashi_Candles = no;
def h = signals_from_Heikin_Ashi_Candles; # input(false, title = "Signals from Heikin Ashi Candles")
input show_labels = yes;
def xATR = atr(c);
def nLoss = a * xATR;
def src = if h then (open + high + low + close) / 4 else close;
script nz{
input x = close;
input y = 0;
plot result = if isnan(x) then y else x;
}
#xATRTrailingStop = 0.0
def xATRTrailingStop =compoundValue(1, if(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
if(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss),
if(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss))),0);
#pos = 0
def pos = compoundValue(1, if(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
if(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) , 0);
def ema = expaverage(src,1);
def above = crosses(ema, xATRTrailingStop, crossingDirection.ABOVE);
def below = crosses(xATRTrailingStop, ema, crossingDirection.ABOVE);
def buy = src > xATRTrailingStop and above ;
def sell = src < xATRTrailingStop and below;
def barbuy = src > xATRTrailingStop ;
def barsell = src < xATRTrailingStop ;
defineglobalColor("Green", color.Green);
defineglobalcolor("Red", color.Red);
addchartbubble(show_labels and buy, low, "Buy", globalColor("Green"), no);
addchartbubble(show_labels and sell, high, "Sell", globalColor("Red"), yes);
input price_color_on = yes;
assignPriceColor(if !price_color_on then color.current else if barbuy then globalColor("Green") else color.CURRENT);
assignpriceColor(if !price_color_on then color.current else if barsell then globalColor("Red") else color.currENT);
input alert_sound_on = no;
alert(buy, "UT Long", alert.bar,if alert_sound_on then sound.ring else sound.NoSound);
alert(sell, "UT Short", alert.bar,if alert_sound_on then sound.bell else sound.NoSound);
input mobile_arrows_on = yes;
plot mobile_arrows_buy = if buy and mobile_arrows_on then 1 else 0;
mobile_arrows_buy.SetLineWeight(5);
mobile_arrows_buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
mobile_arrows_buy.SetDefaultColor(Color.UPTICK);
plot mobile_arrows_sell = if sell and mobile_arrows_on then 1 else 0;
mobile_arrows_sell.SetLineWeight(5);
mobile_arrows_sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
mobile_arrows_sell.SetDefaultColor(Color.DOWNTICK);
Thanks in advance
Jason
I am trying to learn how to write/manipulate code to combine a signal from existing code to generate if another criteria is met such as stacked ema's. I have an indicator that gives a buy and sell signal that I use with confluence of stacked ema's (8 and 21). The code I wrote shows an up arrow when 8 crosses above 21 and a down arrow when 5 crosses below the 21.
I dabbled in writing some code for the ema crossover that I was able to get to work so I was happy about that with it being my first time but I know I have a ton to learn to get this to work how I would like and eventually build the strategy.
# EMA Crossover Buy Signal and Exit signal
# This script generates buy signals when the 8-period EMA crosses above the 21-period EMA and generates sell signals when then 5 crosses below the 21-period EMA
input fastLength = 5;
input medianLength = 8;
input slowLength = 21;
def EMAfast = ExpAverage(close, fastLength);
def EMAmedian = EXpAverage (close, medianLength);
def EMAslow = ExpAverage(close, slowLength);
plot BuySignal = if EMAmedian > EMAslow and EMAmedian[1] <= EMAslow[1] then 1 else 0;
BuySignal.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_UP);
BuySignal.SetDefaultColor(Color.GREEN);
plot SellSignal = if EMAfast < EMAslow and EMAfast[1] >= EMAslow[1] then 1 else 0;
SellSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SellSignal.SetDefaultColor(Color.RED);
Now I am trying to figure out how to use this existing code to plot only when there are stacked ema's but honestly I have no idea where to start. Any guidance is appreciated.
input Key_Value = 1.0;
def a = key_value;# input(1, title = "Key Vaule. 'This changes the sensitivity'")
input atr_period = 10;
def c = atr_period; # input(10, title = "ATR Period")
input Signals_from_Heikin_Ashi_Candles = no;
def h = signals_from_Heikin_Ashi_Candles; # input(false, title = "Signals from Heikin Ashi Candles")
input show_labels = yes;
def xATR = atr(c);
def nLoss = a * xATR;
def src = if h then (open + high + low + close) / 4 else close;
script nz{
input x = close;
input y = 0;
plot result = if isnan(x) then y else x;
}
#xATRTrailingStop = 0.0
def xATRTrailingStop =compoundValue(1, if(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
if(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss),
if(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss))),0);
#pos = 0
def pos = compoundValue(1, if(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
if(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) , 0);
def ema = expaverage(src,1);
def above = crosses(ema, xATRTrailingStop, crossingDirection.ABOVE);
def below = crosses(xATRTrailingStop, ema, crossingDirection.ABOVE);
def buy = src > xATRTrailingStop and above ;
def sell = src < xATRTrailingStop and below;
def barbuy = src > xATRTrailingStop ;
def barsell = src < xATRTrailingStop ;
defineglobalColor("Green", color.Green);
defineglobalcolor("Red", color.Red);
addchartbubble(show_labels and buy, low, "Buy", globalColor("Green"), no);
addchartbubble(show_labels and sell, high, "Sell", globalColor("Red"), yes);
input price_color_on = yes;
assignPriceColor(if !price_color_on then color.current else if barbuy then globalColor("Green") else color.CURRENT);
assignpriceColor(if !price_color_on then color.current else if barsell then globalColor("Red") else color.currENT);
input alert_sound_on = no;
alert(buy, "UT Long", alert.bar,if alert_sound_on then sound.ring else sound.NoSound);
alert(sell, "UT Short", alert.bar,if alert_sound_on then sound.bell else sound.NoSound);
input mobile_arrows_on = yes;
plot mobile_arrows_buy = if buy and mobile_arrows_on then 1 else 0;
mobile_arrows_buy.SetLineWeight(5);
mobile_arrows_buy.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
mobile_arrows_buy.SetDefaultColor(Color.UPTICK);
plot mobile_arrows_sell = if sell and mobile_arrows_on then 1 else 0;
mobile_arrows_sell.SetLineWeight(5);
mobile_arrows_sell.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
mobile_arrows_sell.SetDefaultColor(Color.DOWNTICK);
Thanks in advance
Jason