AddLabel box to display MA cross on different TF?

hashy

Member
I'd love to have a text box in the upper left of my chart that shows what direction the last cross was for two EMAs on a particular time frame. If anyone gets bored and would be kind enough to code it I would be grateful. I've tried, but I've come to the conclusion I suck at scripting.

Something like... 10EMA and 20EMA on 1H time frame. If 10 is above 20 it says "bullish" in the text box. If 10 is below 20 it says "bearish". TIA!
 

tomsk

Well-known member
VIP
@hashy Here is your moving average direction label, works on any timeframe. I have color coded it up for you. Cross Up = GREEN, Cross down = RED

Code:
# Moving Average Cross Direction Label
# tomsk
# 1.5.2020

input price = close;
input length1 = 10;
input length2 = 20;

def MA1 = Average(price, length1);
def MA2 = Average(price, length2);

AddLabel(1, "Moving Average " + length1 + "/" + length2 + " Cross Direction = " + if MA1 > MA2 then "UP" else "DOWN", if MA1 > MA2 then Color.GREEN else Color.RED);
# End Moving Average Cross Direction Label
 
Last edited:

Billions

Active member
2019 Donor
VIP
@hashy Here is your moving average direction label, works on any timeframe. I have color coded it up for you. Cross Up = GREEN, Cross down = RED

Code:
# Moving Average Cross Direction Label
# tomsk
# 1.5.2020

input price = close;
input length1 = 10;
input length2 = 20;

def MA1 = Average(price, length1);
def MA2 = Average(price, length2);

AddLabel(1, "Moving Average " + length1 + "/" + length2 + " Cross Direction = " + if MA1 > MA2 then "UP" else "DOWN", if MA1 > MA2 then Color.GREEN else Color.RED);
# End Moving Average Cross Direction Label
Can you add an alert to this
 

stormy77

New member
Can you add an alert to this
@hashy Here is your moving average direction label, works on any timeframe. I have color coded it up for you. Cross Up = GREEN, Cross down = RED

Code:
# Moving Average Cross Direction Label
# tomsk
# 1.5.2020

input price = close;
input length1 = 10;
input length2 = 20;

def MA1 = Average(price, length1);
def MA2 = Average(price, length2);

AddLabel(1, "Moving Average " + length1 + "/" + length2 + " Cross Direction = " + if MA1 > MA2 then "UP" else "DOWN", if MA1 > MA2 then Color.GREEN else Color.RED);
# End Moving Average Cross Direction Label
Beautiful!
 
Last edited:

cabe1332

Active member
Beautiful
A bubble on chart and alert has worked for me well. It provides an uptrend or bullish trend ahead to help me go/stay long on the trade. I have attached a screenshot circled MX and GX for MA and Golden crossover scripts below. Good luck! @cabe1332

######## EMA Crossover bullish bubble on chart with price
# cabe1332 20210304

#def price = close(period = aggregationPeriod.two_MIN);
def price = close;
def fastLength = 15;
def slowLength = 50;
#def averageType = AverageType.SIMPLE;
def averageType = AverageType.expONENTIAL;
def FastMA = MovingAverage(averageType, price, fastLength);
def SlowMA = MovingAverage(averageType, price, slowLength);

#def e = if stackedUp and y then 1 else 0;
def emax = if FastMA crosses above SlowMA then 1 else 0;
def z = if emax == 1 then 1 else 0;

# Bubble on candle + close
#AddChartBubble(goldx, price, "GoldX " + Round(price, 2), Color.cyan);
AddChartBubble(z, price, "MX " + Round(price, 2), Color.cyan);

# Alert
#Alert(goldx, Round(price, 2) + " on 1 min chart", Alert.BAR, Sound.Ding);
Alert(z, Round(price, 2) + " MX", Alert.BAR);


######## Golden Crossover bullish bubble on chart with price
# cabe1332 20210304

def price = close;
def fastLength = 50;
def slowLength = 200;
#def averageType = AverageType.SIMPLE;
def averageType = AverageType.expONENTIAL;
def FastMA = MovingAverage(averageType, price, fastLength);
def SlowMA = MovingAverage(averageType, price, slowLength);

# EMAs Stacked
def stackedUp = MovAvgExponential("length" = 8)."AvgExp" is greater than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is greater than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is greater than MovAvgExponential("length" = 55)."AvgExp"
and MovAvgExponential("length" = 55)."AvgExp" is greater than MovAvgExponential("length" = 89)."AvgExp";

# HMA
def hprice = close;
def hlength = 20;
def displace = 0;
def HMA = MovingAverage(AverageType.HULL, hprice, hlength)[-displace];
#def y = if HMA > HMA[1] then 1 else 0;
def y = HMA > HMA[1];

def e = if stackedUp and y then 1 else 0;
def goldx = FastMA crosses above SlowMA;
def goldy = if goldx and e==1 then 1 else 0;

# Bubble on chart
#AddChartBubble(goldx, price, "GoldX " + Round(price, 2), Color.cyan);
AddChartBubble(goldy, price, "GX " + Round(price, 2), Color.cyan);

# Alert
Alert(Goldx, Round(price, 2) + " Golden Xover", Alert.BAR);


XEDc4Nk.png
 

Similar threads

Top