NexusFi: Find Your Edge


Home Menu

 





MTF Moving average as a line


Discussion in ThinkOrSwim

Updated
    1. trending_up 1,152 views
    2. thumb_up 0 thanks given
    3. group 1 followers
    1. forum 0 posts
    2. attach_file 0 attachments




 
Search this Thread

MTF Moving average as a line

  #1 (permalink)
 Tunatrader 
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

Started this thread Reply With Quote




Last Updated on February 24, 2020


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts