Long Beach MS, USA
Experience: Advanced
Platform: TOS, NinjaTrader
Trading: Emini
Posts: 3 since May 2017
Thanks Given: 0
Thanks Received: 5
|
Hi there... I was hoping someone could help with this code.... it plots a moving average as a line on the current timeframe. I started to add code to make it MTF indicator. Plotting the Daily moving averages on a 5 min for example.
# Plot longer moving average support lines
def lastBar = HighestAll(if !IsNaN(close) then BarNumber() else 0);
def mostRecentClose = HighestAll(if BarNumber() == lastBar then close else 0);
def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(close), 0, barNumber));
input period = {default "AggregationPeriod.Hour", "AggregationPeriod.Day"};
input avgType = {default "simple", "exponential"};
input shtAvg = 50;
input medAvg = 100;
input lngAvg = 200;
def avg1;
def avg2;
def avg3;
def data = close(period = period);
switch (avgType) {
case "simple":
avg1 = Average(data, shtAvg);
avg2 = Average(data, medAvg);
avg3 = Average(data, lngAvg);
case "exponential":
avg1 = ExpAverage(close, shtAvg);
avg2 = ExpAverage(close, medAvg);
avg3 = ExpAverage(close, lngAvg);
}
input proximity = 10; #ref 0.5 = 1/2%
def value1 = GetValue(avg1, BarNumber() - HighestAll(lastBar));
def value2 = GetValue(avg2, BarNumber() - HighestAll(lastBar));
def value3 = GetValue(avg3, BarNumber() - HighestAll(lastBar));
def inRange1 = mostRecentClose > (value1 * (1 - proximity / 100)) and mostRecentClose < (value1 * (1 + proximity / 100));
def inRange2 = mostRecentClose > (value2 * (1 - proximity / 100)) and mostRecentClose < (value2 * (1 + proximity / 100));
def inRange3 = mostRecentClose > (value3 * (1 - proximity / 100)) and mostRecentClose < (value3 * (1 + proximity / 100));
def avg1Line = if barNumber == 1 then Double.NaN else if barNumber == barCount then avg1 else if barNumber == barCount then Double.NaN else avg1Line[1];
def avg2Line = if barNumber == 1 then Double.NaN else if barNumber == barCount then avg2 else if barNumber == barCount then Double.NaN else avg2Line[1];
def avg3Line = if barNumber == 1 then Double.NaN else if barNumber == barCount then avg3 else if barNumber == barCount then Double.NaN else avg3Line[1];
plot sandline1 = avg1Line;
sandline1.AssignValueColor(globalColor("sand" ));
sandline1.SetLineWeight(1);
sandline1.SetHiding(!inRange1);
AddChartBubble(yes, if inRange1 and BarNumber() == HighestAll(Lastbar+3) then sandline1 else double.nan, shtAvg + (if avgType == avgType.simple then "sma" else "ema" ), globalColor("sand" ), 0);
plot sandline2 = avg2Line;
sandline2.AssignValueColor(globalColor("sand" ));
sandline2.SetLineWeight(1);
sandline2.SetHiding(!inRange2);
AddChartBubble(yes, if inRange2 and BarNumber() == HighestAll(Lastbar+3) then sandline2 else double.nan, medAvg + (if avgType == avgType.simple then "sma" else "ema" ), globalColor("sand" ), 0);
plot sandline3 = avg3Line;
sandline3.AssignValueColor(globalColor("sand" ));
sandline3.SetLineWeight(1);
sandline3.SetHiding(!inRange3);
AddChartBubble(yes, if inRange3 and BarNumber() == HighestAll(Lastbar+3) then sandline3 else double.nan, lngAvg + (if avgType == avgType.simple then "sma" else "ema" ), globalColor("sand" ), 0);
AddLabel(yes, "proximity: " + proximity + "%", color.white);
AddLabel(yes, shtAvg + (if avgType == avgType.simple then "sma: " else "ema: " ) + round(avg1,2), globalColor("sand" )); #for reference only
AddLabel(yes, medAvg + (if avgType == avgType.simple then "sma: " else "ema: " ) + round(avg2,2), globalColor("sand" )); #for reference only
AddLabel(yes, lngAvg + (if avgType == avgType.simple then "sma: " else "ema: " ) + round(avg3,2), globalColor("sand" )); #for reference only
DefineGlobalColor("sand", CreateColor(255,204,102)); #sand
|