Multi-timeframe (MTF) Moving Average Indicator for ThinkorSwim

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

Ironically, I find myself moving away from moving averages and onto ATR, regression, vwap and pivots seems to have opened up and whole new world for me..
 
Ironically, I find myself moving away from moving averages and onto ATR, regression, vwap and pivots seems to have opened up and whole new world for me..
i use those too, i need the MTF for bigger time frame, some of the EMA in bigger time frame are very important
 
someone can tell me what's wrong with this code
2011-06-13_1458.png


Code:
# SwingTrading_R1V3
# R1V1 2011.06.11.23:00 KumoBob
# A very simple method of SwingTrading with Moving average
# R1V2 2011.06.13.09:00 KumoBob
# Added more periods to AverageMode
# R1V3 2011.06.13.15:00 KumoBob
# Added hints and other bells an whistles

declare upper;
#Hint: Three methods are available and provide a very simple method of SwingTrading with Moving Averages. \nThis orignated when I wanted to see what would happen if two moving averages were used on a daily basis with the buy and sell signal always being the highest and lowest of the two averages.\nIt seems to be a fairly good method, but other indicators such as PPS will most likely out perform this method.
#Hint FastMovingAverage: Length of FAST average\nDefault = 5
#Hint SlowMovingAverage: Length of SLOW average\nDefault = 13
#Hint Method: Choose to signal when Price crosses the Fast MA\nor\nChoose to signal when the Fast MA crosses the Slow MA\nor\n "Buy at the Lowest MA and Sell at the Highest MA when Price crosses these curves
#Hint AverageMode: Choose to average by bar or a selectable time period\nThe choices are DAY, 2 DAYS, 3 DAYS, 4 DAYS, WEEK, and MONTH \nDefault = Day
#Hint BubblesOn: Bubbles identify the curve's average length\nDefault = Yes
#Hint ShowPoints: Shows the exact point the signal occured\nDefault = Yes
#Hint ShowArrows: Shows the signal with an arrow\nDefault = Yes
#Hint ShowPlot: Choose to show the curves or hide them
#Hint VerticalDayBreakLinesOn: Choose to place Vertical Lines at Day, Week or Month intervals\nDefault = No
#Hint VerticalLinePeriod: Specifies the interval to plot vertical lines\nDefault = Day
#Hint PriceColorOn: Allows the signal to be seen as Red and Green price bars\nDefault = No
input FastMovingAverage = 5;
input SlowMovingAverage = 13;
Input Method = {"At Fast MA", "MA Cross",Default "Buy at Min MA Sell at Max MA"};
input AverageMode = {"Bar",default DAY, "2 DAYS", "3 DAYS", "4 DAYS", WEEK, MONTH};

input BubblesOn = Yes;
input ShowPlot = Yes;
input ShowPoints = Yes;
input ShowArrows = Yes;
Input PriceColorOn = No;

def Mode;
switch (Method){
case "At Fast MA":
Mode = 1;
case "MA Cross":
Mode = 2;
case "Buy at Min MA Sell at Max MA":
Mode = 3;
}

Input VerticalDayBreakLinesOn = No;
input VerticalLinePeriod = {default DAY, WEEK, MONTH};



def PeriodChoice;
switch (AverageMode){
case "Day":
PeriodChoice = 1;
case "2 DAYS":
PeriodChoice = 2;
case "3 DAYS":
PeriodChoice = 3;
case "4 DAYS":
PeriodChoice = 4;
case WEEK:
PeriodChoice = 7;
case MONTH:
PeriodChoice = 30;
case "Bar":
PeriodChoice = 0;
}


def BarMA1 = Average(close () , FastMovingAverage );
Def Day1MA1 = Average(close (period = "DAY" ), FastMovingAverage );
Def Day2MA1 = Average(close (period = "2 DAYS"), FastMovingAverage );
Def Day3MA1 = Average(close (period = "3 DAYS"), FastMovingAverage );
Def Day4MA1 = Average(close (period = "4 DAYS"), FastMovingAverage );
Def Day7MA1 = Average(close (period = "WEEK" ), FastMovingAverage);
Def Day30MA1 = Average(close (period = "MONTH") , FastMovingAverage);

def BarMA2 = Average(close () , SlowMovingAverage);
Def Day1MA2 = Average(close (period = "DAY" ), SlowMovingAverage);
Def Day2MA2 = Average(close (period = "2 DAYS"), SlowMovingAverage);
Def Day3MA2 = Average(close (period = "3 DAYS"), SlowMovingAverage);
Def Day4MA2 = Average(close (period = "4 DAYS"), SlowMovingAverage);
Def Day7MA2 = Average(close (period = "WEEK" ), SlowMovingAverage);
Def Day30MA2 = Average(close (period = "MONTH") , SlowMovingAverage);


def FMA =
If PeriodChoice == 1 then Day1MA1 else
If PeriodChoice == 2 then Day2MA1 else
If PeriodChoice == 3 then Day3MA1 else
If PeriodChoice == 4 then Day4MA1 else
If PeriodChoice == 7 then Day7MA1 else
If PeriodChoice == 30 then Day30MA1 else BarMA1;
plot FastMA = if !ShowPlot then Double.NaN else FMA;

FastMA.AssignValueColor(if Close > FMA then Color.Green else if Close < FMA then Color.Red else Color.Yellow); AddChartBubble(FastMA && BubblesOn && !IsNaN(close) and IsNaN(close[-1]), FastMA, concat("", FastMovingAverage ), if Close > FMA then Color.Green else if Close < FMA then Color.Red else Color.Yellow); def SMA = If PeriodChoice == 1 then Day1MA2 else If PeriodChoice == 2 then Day2MA2 else If PeriodChoice == 3 then Day3MA2 else If PeriodChoice == 4 then Day4MA2 else If PeriodChoice == 7 then Day7MA2 else If PeriodChoice == 30 then Day30MA2 else BarMA2; plot SlowMA = if !ShowPlot then Double.NaN else SMA; SlowMA.AssignValueColor(if Close > SMA then Color.Green else if Close < SMA then Color.Red else Color.Yellow); AddChartBubble(SlowMA && BubblesOn && !IsNaN(close) and IsNaN(close[-1]), SlowMA, concat("", SlowMovingAverage), if Close > SMA then Color.Green else if Close < SMA then Color.Red else Color.Yellow); #################### def Max = Max(FMA, SMA); def Min = Min(FMA, SMA); def Position = if Close > Max(FMA, SMA) then 2 else if Close < Min(FMA, SMA) then -2 else 0; rec Trend = If Mode == 3 then if Position[1] == 2 && Position[0] <= 0 then -2 else if Position[1] == -2 && Position[0] >= 0 then 2 else
if Position[1] == 0 && Position[0] == 2 then 2 else
if Position[1] == 0 && Position[0] == -2 then -2 else Trend[1]
else If Mode == 1 then if Close > FMA then 2 else -2
else if FMA > SMA then 2 else -2;

AssignPriceColor(if !PriceColorOn then Color.Current else If Trend > 0 then Color.Green else Color.Red);


rec Point = If Mode == 3 then
if Position[1] == 2 && Position[0] <= 0 then Max else if Position[1] == -2 && Position[0] >= 0 then Min else
if Position[1] == 0 && Position[0] == 2 then Max else
if Position[1] == 0 && Position[0] == -2 then Min else Point[1]
else If Mode == 1 then if Close > FMA then Close else Point[1]
else if FMA > SMA then Close else Point[1];

plot SignalPoint = If !ShowPoints then Double.NaN else Point;
SignalPoint.SetLineWeight(1);
SignalPoint.SetPaintingStrategy(PaintingStrategy.Points);
SignalPoint.AssignValueColor(If Trend > 0 then Color.Green else Color.Red);
# ARROWS ROUTINE
# ___
def x_over = Crosses(Trend, 0);
plot up = if ShowArrows && x_over && Trend > 0 then Low else Double.NaN;
up.SetPaintingStrategy(PaintingStrategy.Arrow_up);
up.SetDefaultColor(Color.white);
plot dn = if ShowArrows && x_over && Trend < 0 then High else Double.NaN; dn.SetPaintingStrategy(PaintingStrategy.Arrow_down); dn.SetDefaultColor(Color.white); # Vertical Time Lines # ___ def vert = VerticalLinePeriod == VerticalLinePeriod.Day and getDay() <> getDay()[1] or
VerticalLinePeriod == VerticalLinePeriod.WEEK and getWeek() <> getWeek()[1] or VerticalLinePeriod == VerticalLinePeriod.MONTH and getMonth() <> getMonth()[1];
AddVerticalLine(VerticalDayBreakLinesOn && Vert,
"",
if VerticalLinePeriod == VerticalLinePeriod.day then color.cyan else if VerticalLinePeriod == VerticalLinePeriod.week then Color.Magenta else color.Yellow);# SwingTrading_R1V3
# R1V1 2011.06.11.23:00 KumoBob
# A very simple method of SwingTrading with Moving average
# R1V2 2011.06.13.09:00 KumoBob
# Added more periods to AverageMode
# R1V3 2011.06.13.15:00 KumoBob
# Added hints and other bells an whistles

declare upper;
#Hint: Three methods are available and provide a very simple method of SwingTrading with Moving Averages. \nThis orignated when I wanted to see what would happen if two moving averages were used on a daily basis with the buy and sell signal always being the highest and lowest of the two averages.\nIt seems to be a fairly good method, but other indicators such as PPS will most likely out perform this method.
#Hint FastMovingAverage: Length of FAST average\nDefault = 5
#Hint SlowMovingAverage: Length of SLOW average\nDefault = 13
#Hint Method: Choose to signal when Price crosses the Fast MA\nor\nChoose to signal when the Fast MA crosses the Slow MA\nor\n "Buy at the Lowest MA and Sell at the Highest MA when Price crosses these curves
#Hint AverageMode: Choose to average by bar or a selectable time period\nThe choices are DAY, 2 DAYS, 3 DAYS, 4 DAYS, WEEK, and MONTH \nDefault = Day
#Hint BubblesOn: Bubbles identify the curve's average length\nDefault = Yes
#Hint ShowPoints: Shows the exact point the signal occured\nDefault = Yes
#Hint ShowArrows: Shows the signal with an arrow\nDefault = Yes
#Hint ShowPlot: Choose to show the curves or hide them
#Hint VerticalDayBreakLinesOn: Choose to place Vertical Lines at Day, Week or Month intervals\nDefault = No
#Hint VerticalLinePeriod: Specifies the interval to plot vertical lines\nDefault = Day
#Hint PriceColorOn: Allows the signal to be seen as Red and Green price bars\nDefault = No
input FastMovingAverage = 5;
input SlowMovingAverage = 13;
Input Method = {"At Fast MA", "MA Cross",Default "Buy at Min MA Sell at Max MA"};
input AverageMode = {"Bar",default DAY, "2 DAYS", "3 DAYS", "4 DAYS", WEEK, MONTH};

input BubblesOn = Yes;
input ShowPlot = Yes;
input ShowPoints = Yes;
input ShowArrows = Yes;
Input PriceColorOn = No;

def Mode;
switch (Method){
case "At Fast MA":
Mode = 1;
case "MA Cross":
Mode = 2;
case "Buy at Min MA Sell at Max MA":
Mode = 3;
}

Input VerticalDayBreakLinesOn = No;
input VerticalLinePeriod = {default DAY, WEEK, MONTH};



def PeriodChoice;
switch (AverageMode){
case "Day":
PeriodChoice = 1;
case "2 DAYS":
PeriodChoice = 2;
case "3 DAYS":
PeriodChoice = 3;
case "4 DAYS":
PeriodChoice = 4;
case WEEK:
PeriodChoice = 7;
case MONTH:
PeriodChoice = 30;
case "Bar":
PeriodChoice = 0;
}


def BarMA1 = Average(close () , FastMovingAverage );
Def Day1MA1 = Average(close (period = "DAY" ), FastMovingAverage );
Def Day2MA1 = Average(close (period = "2 DAYS"), FastMovingAverage );
Def Day3MA1 = Average(close (period = "3 DAYS"), FastMovingAverage );
Def Day4MA1 = Average(close (period = "4 DAYS"), FastMovingAverage );
Def Day7MA1 = Average(close (period = "WEEK" ), FastMovingAverage);
Def Day30MA1 = Average(close (period = "MONTH") , FastMovingAverage);

def BarMA2 = Average(close () , SlowMovingAverage);
Def Day1MA2 = Average(close (period = "DAY" ), SlowMovingAverage);
Def Day2MA2 = Average(close (period = "2 DAYS"), SlowMovingAverage);
Def Day3MA2 = Average(close (period = "3 DAYS"), SlowMovingAverage);
Def Day4MA2 = Average(close (period = "4 DAYS"), SlowMovingAverage);
Def Day7MA2 = Average(close (period = "WEEK" ), SlowMovingAverage);
Def Day30MA2 = Average(close (period = "MONTH") , SlowMovingAverage);


def FMA =
If PeriodChoice == 1 then Day1MA1 else
If PeriodChoice == 2 then Day2MA1 else
If PeriodChoice == 3 then Day3MA1 else
If PeriodChoice == 4 then Day4MA1 else
If PeriodChoice == 7 then Day7MA1 else
If PeriodChoice == 30 then Day30MA1 else BarMA1;
plot FastMA = if !ShowPlot then Double.NaN else FMA;

FastMA.AssignValueColor(if Close > FMA then Color.Green else if Close < FMA then Color.Red else Color.Yellow); AddChartBubble(FastMA && BubblesOn && !IsNaN(close) and IsNaN(close[-1]), FastMA, concat("", FastMovingAverage ), if Close > FMA then Color.Green else if Close < FMA then Color.Red else Color.Yellow); def SMA = If PeriodChoice == 1 then Day1MA2 else If PeriodChoice == 2 then Day2MA2 else If PeriodChoice == 3 then Day3MA2 else If PeriodChoice == 4 then Day4MA2 else If PeriodChoice == 7 then Day7MA2 else If PeriodChoice == 30 then Day30MA2 else BarMA2; plot SlowMA = if !ShowPlot then Double.NaN else SMA; SlowMA.AssignValueColor(if Close > SMA then Color.Green else if Close < SMA then Color.Red else Color.Yellow); AddChartBubble(SlowMA && BubblesOn && !IsNaN(close) and IsNaN(close[-1]), SlowMA, concat("", SlowMovingAverage), if Close > SMA then Color.Green else if Close < SMA then Color.Red else Color.Yellow); #################### def Max = Max(FMA, SMA); def Min = Min(FMA, SMA); def Position = if Close > Max(FMA, SMA) then 2 else if Close < Min(FMA, SMA) then -2 else 0; rec Trend = If Mode == 3 then if Position[1] == 2 && Position[0] <= 0 then -2 else if Position[1] == -2 && Position[0] >= 0 then 2 else if Position[1] == 0 && Position[0] == 2 then 2 else if Position[1] == 0 && Position[0] == -2 then -2 else Trend[1] else If Mode == 1 then if Close > FMA then 2 else -2 else if FMA > SMA then 2 else -2;

AssignPriceColor(if !PriceColorOn then Color.Current else If Trend > 0 then Color.Green else Color.Red);


rec Point = If Mode == 3 then
if Position[1] == 2 && Position[0] <= 0 then Max else if Position[1] == -2 && Position[0] >= 0 then Min else
if Position[1] == 0 && Position[0] == 2 then Max else
if Position[1] == 0 && Position[0] == -2 then Min else Point[1]
else If Mode == 1 then if Close > FMA then Close else Point[1]
else if FMA > SMA then Close else Point[1];

plot SignalPoint = If !ShowPoints then Double.NaN else Point;
SignalPoint.SetLineWeight(1);
SignalPoint.SetPaintingStrategy(PaintingStrategy.Points);
SignalPoint.AssignValueColor(If Trend > 0 then Color.Green else Color.Red);
# ARROWS ROUTINE
# ___
def x_over = Crosses(Trend, 0);
plot up = if ShowArrows && x_over && Trend > 0 then Low else Double.NaN;
up.SetPaintingStrategy(PaintingStrategy.Arrow_up);
up.SetDefaultColor(Color.white);
plot dn = if ShowArrows && x_over && Trend < 0 then High else Double.NaN; dn.SetPaintingStrategy(PaintingStrategy.Arrow_down); dn.SetDefaultColor(Color.white); # Vertical Time Lines # ___ def vert = VerticalLinePeriod == VerticalLinePeriod.Day and getDay() <> getDay()[1] or VerticalLinePeriod == VerticalLinePeriod.WEEK and getWeek() <> getWeek()[1] or VerticalLinePeriod == VerticalLinePeriod.MONTH and getMonth() <> getMonth()[1];
AddVerticalLine(VerticalDayBreakLinesOn && Vert, "", if VerticalLinePeriod == VerticalLinePeriod.day then color.cyan else if VerticalLinePeriod == VerticalLinePeriod.week then Color.Magenta else color.Yellow);
 
A quick visual inspection shows that the code is duplicated - look around line 124. You will see the header there again. On top of that some of the really long commented lines seems to be broken across multiple statements and so the code editor seems to think those are code when they are in fact comments.
 
someone can tell me what's wrong with this code
2011-06-13_1458.png


Code:
# SwingTrading_R1V3
# R1V1 2011.06.11.23:00 KumoBob
# A very simple method of SwingTrading with Moving average
# R1V2 2011.06.13.09:00 KumoBob
# Added more periods to AverageMode
# R1V3 2011.06.13.15:00 KumoBob
# Added hints and other bells an whistles

declare upper;
#Hint: Three methods are available and provide a very simple method of SwingTrading with Moving Averages. \nThis orignated when I wanted to see what would happen if two moving averages were used on a daily basis with the buy and sell signal always being the highest and lowest of the two averages.\nIt seems to be a fairly good method, but other indicators such as PPS will most likely out perform this method.
#Hint FastMovingAverage: Length of FAST average\nDefault = 5
#Hint SlowMovingAverage: Length of SLOW average\nDefault = 13
#Hint Method: Choose to signal when Price crosses the Fast MA\nor\nChoose to signal when the Fast MA crosses the Slow MA\nor\n "Buy at the Lowest MA and Sell at the Highest MA when Price crosses these curves
#Hint AverageMode: Choose to average by bar or a selectable time period\nThe choices are DAY, 2 DAYS, 3 DAYS, 4 DAYS, WEEK, and MONTH \nDefault = Day
#Hint BubblesOn: Bubbles identify the curve's average length\nDefault = Yes
#Hint ShowPoints: Shows the exact point the signal occured\nDefault = Yes
#Hint ShowArrows: Shows the signal with an arrow\nDefault = Yes
#Hint ShowPlot: Choose to show the curves or hide them
#Hint VerticalDayBreakLinesOn: Choose to place Vertical Lines at Day, Week or Month intervals\nDefault = No
#Hint VerticalLinePeriod: Specifies the interval to plot vertical lines\nDefault = Day
#Hint PriceColorOn: Allows the signal to be seen as Red and Green price bars\nDefault = No
input FastMovingAverage = 5;
input SlowMovingAverage = 13;
Input Method = {"At Fast MA", "MA Cross",Default "Buy at Min MA Sell at Max MA"};
input AverageMode = {"Bar",default DAY, "2 DAYS", "3 DAYS", "4 DAYS", WEEK, MONTH};

input BubblesOn = Yes;
input ShowPlot = Yes;
input ShowPoints = Yes;
input ShowArrows = Yes;
Input PriceColorOn = No;

def Mode;
switch (Method){
case "At Fast MA":
Mode = 1;
case "MA Cross":
Mode = 2;
case "Buy at Min MA Sell at Max MA":
Mode = 3;
}

Input VerticalDayBreakLinesOn = No;
input VerticalLinePeriod = {default DAY, WEEK, MONTH};



def PeriodChoice;
switch (AverageMode){
case "Day":
PeriodChoice = 1;
case "2 DAYS":
PeriodChoice = 2;
case "3 DAYS":
PeriodChoice = 3;
case "4 DAYS":
PeriodChoice = 4;
case WEEK:
PeriodChoice = 7;
case MONTH:
PeriodChoice = 30;
case "Bar":
PeriodChoice = 0;
}


def BarMA1 = Average(close () , FastMovingAverage );
Def Day1MA1 = Average(close (period = "DAY" ), FastMovingAverage );
Def Day2MA1 = Average(close (period = "2 DAYS"), FastMovingAverage );
Def Day3MA1 = Average(close (period = "3 DAYS"), FastMovingAverage );
Def Day4MA1 = Average(close (period = "4 DAYS"), FastMovingAverage );
Def Day7MA1 = Average(close (period = "WEEK" ), FastMovingAverage);
Def Day30MA1 = Average(close (period = "MONTH") , FastMovingAverage);

def BarMA2 = Average(close () , SlowMovingAverage);
Def Day1MA2 = Average(close (period = "DAY" ), SlowMovingAverage);
Def Day2MA2 = Average(close (period = "2 DAYS"), SlowMovingAverage);
Def Day3MA2 = Average(close (period = "3 DAYS"), SlowMovingAverage);
Def Day4MA2 = Average(close (period = "4 DAYS"), SlowMovingAverage);
Def Day7MA2 = Average(close (period = "WEEK" ), SlowMovingAverage);
Def Day30MA2 = Average(close (period = "MONTH") , SlowMovingAverage);


def FMA =
If PeriodChoice == 1 then Day1MA1 else
If PeriodChoice == 2 then Day2MA1 else
If PeriodChoice == 3 then Day3MA1 else
If PeriodChoice == 4 then Day4MA1 else
If PeriodChoice == 7 then Day7MA1 else
If PeriodChoice == 30 then Day30MA1 else BarMA1;
plot FastMA = if !ShowPlot then Double.NaN else FMA;

FastMA.AssignValueColor(if Close > FMA then Color.Green else if Close < FMA then Color.Red else Color.Yellow); AddChartBubble(FastMA && BubblesOn && !IsNaN(close) and IsNaN(close[-1]), FastMA, concat("", FastMovingAverage ), if Close > FMA then Color.Green else if Close < FMA then Color.Red else Color.Yellow); def SMA = If PeriodChoice == 1 then Day1MA2 else If PeriodChoice == 2 then Day2MA2 else If PeriodChoice == 3 then Day3MA2 else If PeriodChoice == 4 then Day4MA2 else If PeriodChoice == 7 then Day7MA2 else If PeriodChoice == 30 then Day30MA2 else BarMA2; plot SlowMA = if !ShowPlot then Double.NaN else SMA; SlowMA.AssignValueColor(if Close > SMA then Color.Green else if Close < SMA then Color.Red else Color.Yellow); AddChartBubble(SlowMA && BubblesOn && !IsNaN(close) and IsNaN(close[-1]), SlowMA, concat("", SlowMovingAverage), if Close > SMA then Color.Green else if Close < SMA then Color.Red else Color.Yellow); #################### def Max = Max(FMA, SMA); def Min = Min(FMA, SMA); def Position = if Close > Max(FMA, SMA) then 2 else if Close < Min(FMA, SMA) then -2 else 0; rec Trend = If Mode == 3 then if Position[1] == 2 && Position[0] <= 0 then -2 else if Position[1] == -2 && Position[0] >= 0 then 2 else
if Position[1] == 0 && Position[0] == 2 then 2 else
if Position[1] == 0 && Position[0] == -2 then -2 else Trend[1]
else If Mode == 1 then if Close > FMA then 2 else -2
else if FMA > SMA then 2 else -2;

AssignPriceColor(if !PriceColorOn then Color.Current else If Trend > 0 then Color.Green else Color.Red);


rec Point = If Mode == 3 then
if Position[1] == 2 && Position[0] <= 0 then Max else if Position[1] == -2 && Position[0] >= 0 then Min else
if Position[1] == 0 && Position[0] == 2 then Max else
if Position[1] == 0 && Position[0] == -2 then Min else Point[1]
else If Mode == 1 then if Close > FMA then Close else Point[1]
else if FMA > SMA then Close else Point[1];

plot SignalPoint = If !ShowPoints then Double.NaN else Point;
SignalPoint.SetLineWeight(1);
SignalPoint.SetPaintingStrategy(PaintingStrategy.Points);
SignalPoint.AssignValueColor(If Trend > 0 then Color.Green else Color.Red);
# ARROWS ROUTINE
# ___
def x_over = Crosses(Trend, 0);
plot up = if ShowArrows && x_over && Trend > 0 then Low else Double.NaN;
up.SetPaintingStrategy(PaintingStrategy.Arrow_up);
up.SetDefaultColor(Color.white);
plot dn = if ShowArrows && x_over && Trend < 0 then High else Double.NaN; dn.SetPaintingStrategy(PaintingStrategy.Arrow_down); dn.SetDefaultColor(Color.white); # Vertical Time Lines # ___ def vert = VerticalLinePeriod == VerticalLinePeriod.Day and getDay() <> getDay()[1] or
VerticalLinePeriod == VerticalLinePeriod.WEEK and getWeek() <> getWeek()[1] or VerticalLinePeriod == VerticalLinePeriod.MONTH and getMonth() <> getMonth()[1];
AddVerticalLine(VerticalDayBreakLinesOn && Vert,
"",
if VerticalLinePeriod == VerticalLinePeriod.day then color.cyan else if VerticalLinePeriod == VerticalLinePeriod.week then Color.Magenta else color.Yellow);# SwingTrading_R1V3
# R1V1 2011.06.11.23:00 KumoBob
# A very simple method of SwingTrading with Moving average
# R1V2 2011.06.13.09:00 KumoBob
# Added more periods to AverageMode
# R1V3 2011.06.13.15:00 KumoBob
# Added hints and other bells an whistles

declare upper;
#Hint: Three methods are available and provide a very simple method of SwingTrading with Moving Averages. \nThis orignated when I wanted to see what would happen if two moving averages were used on a daily basis with the buy and sell signal always being the highest and lowest of the two averages.\nIt seems to be a fairly good method, but other indicators such as PPS will most likely out perform this method.
#Hint FastMovingAverage: Length of FAST average\nDefault = 5
#Hint SlowMovingAverage: Length of SLOW average\nDefault = 13
#Hint Method: Choose to signal when Price crosses the Fast MA\nor\nChoose to signal when the Fast MA crosses the Slow MA\nor\n "Buy at the Lowest MA and Sell at the Highest MA when Price crosses these curves
#Hint AverageMode: Choose to average by bar or a selectable time period\nThe choices are DAY, 2 DAYS, 3 DAYS, 4 DAYS, WEEK, and MONTH \nDefault = Day
#Hint BubblesOn: Bubbles identify the curve's average length\nDefault = Yes
#Hint ShowPoints: Shows the exact point the signal occured\nDefault = Yes
#Hint ShowArrows: Shows the signal with an arrow\nDefault = Yes
#Hint ShowPlot: Choose to show the curves or hide them
#Hint VerticalDayBreakLinesOn: Choose to place Vertical Lines at Day, Week or Month intervals\nDefault = No
#Hint VerticalLinePeriod: Specifies the interval to plot vertical lines\nDefault = Day
#Hint PriceColorOn: Allows the signal to be seen as Red and Green price bars\nDefault = No
input FastMovingAverage = 5;
input SlowMovingAverage = 13;
Input Method = {"At Fast MA", "MA Cross",Default "Buy at Min MA Sell at Max MA"};
input AverageMode = {"Bar",default DAY, "2 DAYS", "3 DAYS", "4 DAYS", WEEK, MONTH};

input BubblesOn = Yes;
input ShowPlot = Yes;
input ShowPoints = Yes;
input ShowArrows = Yes;
Input PriceColorOn = No;

def Mode;
switch (Method){
case "At Fast MA":
Mode = 1;
case "MA Cross":
Mode = 2;
case "Buy at Min MA Sell at Max MA":
Mode = 3;
}

Input VerticalDayBreakLinesOn = No;
input VerticalLinePeriod = {default DAY, WEEK, MONTH};



def PeriodChoice;
switch (AverageMode){
case "Day":
PeriodChoice = 1;
case "2 DAYS":
PeriodChoice = 2;
case "3 DAYS":
PeriodChoice = 3;
case "4 DAYS":
PeriodChoice = 4;
case WEEK:
PeriodChoice = 7;
case MONTH:
PeriodChoice = 30;
case "Bar":
PeriodChoice = 0;
}


def BarMA1 = Average(close () , FastMovingAverage );
Def Day1MA1 = Average(close (period = "DAY" ), FastMovingAverage );
Def Day2MA1 = Average(close (period = "2 DAYS"), FastMovingAverage );
Def Day3MA1 = Average(close (period = "3 DAYS"), FastMovingAverage );
Def Day4MA1 = Average(close (period = "4 DAYS"), FastMovingAverage );
Def Day7MA1 = Average(close (period = "WEEK" ), FastMovingAverage);
Def Day30MA1 = Average(close (period = "MONTH") , FastMovingAverage);

def BarMA2 = Average(close () , SlowMovingAverage);
Def Day1MA2 = Average(close (period = "DAY" ), SlowMovingAverage);
Def Day2MA2 = Average(close (period = "2 DAYS"), SlowMovingAverage);
Def Day3MA2 = Average(close (period = "3 DAYS"), SlowMovingAverage);
Def Day4MA2 = Average(close (period = "4 DAYS"), SlowMovingAverage);
Def Day7MA2 = Average(close (period = "WEEK" ), SlowMovingAverage);
Def Day30MA2 = Average(close (period = "MONTH") , SlowMovingAverage);


def FMA =
If PeriodChoice == 1 then Day1MA1 else
If PeriodChoice == 2 then Day2MA1 else
If PeriodChoice == 3 then Day3MA1 else
If PeriodChoice == 4 then Day4MA1 else
If PeriodChoice == 7 then Day7MA1 else
If PeriodChoice == 30 then Day30MA1 else BarMA1;
plot FastMA = if !ShowPlot then Double.NaN else FMA;

FastMA.AssignValueColor(if Close > FMA then Color.Green else if Close < FMA then Color.Red else Color.Yellow); AddChartBubble(FastMA && BubblesOn && !IsNaN(close) and IsNaN(close[-1]), FastMA, concat("", FastMovingAverage ), if Close > FMA then Color.Green else if Close < FMA then Color.Red else Color.Yellow); def SMA = If PeriodChoice == 1 then Day1MA2 else If PeriodChoice == 2 then Day2MA2 else If PeriodChoice == 3 then Day3MA2 else If PeriodChoice == 4 then Day4MA2 else If PeriodChoice == 7 then Day7MA2 else If PeriodChoice == 30 then Day30MA2 else BarMA2; plot SlowMA = if !ShowPlot then Double.NaN else SMA; SlowMA.AssignValueColor(if Close > SMA then Color.Green else if Close < SMA then Color.Red else Color.Yellow); AddChartBubble(SlowMA && BubblesOn && !IsNaN(close) and IsNaN(close[-1]), SlowMA, concat("", SlowMovingAverage), if Close > SMA then Color.Green else if Close < SMA then Color.Red else Color.Yellow); #################### def Max = Max(FMA, SMA); def Min = Min(FMA, SMA); def Position = if Close > Max(FMA, SMA) then 2 else if Close < Min(FMA, SMA) then -2 else 0; rec Trend = If Mode == 3 then if Position[1] == 2 && Position[0] <= 0 then -2 else if Position[1] == -2 && Position[0] >= 0 then 2 else if Position[1] == 0 && Position[0] == 2 then 2 else if Position[1] == 0 && Position[0] == -2 then -2 else Trend[1] else If Mode == 1 then if Close > FMA then 2 else -2 else if FMA > SMA then 2 else -2;

AssignPriceColor(if !PriceColorOn then Color.Current else If Trend > 0 then Color.Green else Color.Red);


rec Point = If Mode == 3 then
if Position[1] == 2 && Position[0] <= 0 then Max else if Position[1] == -2 && Position[0] >= 0 then Min else
if Position[1] == 0 && Position[0] == 2 then Max else
if Position[1] == 0 && Position[0] == -2 then Min else Point[1]
else If Mode == 1 then if Close > FMA then Close else Point[1]
else if FMA > SMA then Close else Point[1];

plot SignalPoint = If !ShowPoints then Double.NaN else Point;
SignalPoint.SetLineWeight(1);
SignalPoint.SetPaintingStrategy(PaintingStrategy.Points);
SignalPoint.AssignValueColor(If Trend > 0 then Color.Green else Color.Red);
# ARROWS ROUTINE
# ___
def x_over = Crosses(Trend, 0);
plot up = if ShowArrows && x_over && Trend > 0 then Low else Double.NaN;
up.SetPaintingStrategy(PaintingStrategy.Arrow_up);
up.SetDefaultColor(Color.white);
plot dn = if ShowArrows && x_over && Trend < 0 then High else Double.NaN; dn.SetPaintingStrategy(PaintingStrategy.Arrow_down); dn.SetDefaultColor(Color.white); # Vertical Time Lines # ___ def vert = VerticalLinePeriod == VerticalLinePeriod.Day and getDay() <> getDay()[1] or VerticalLinePeriod == VerticalLinePeriod.WEEK and getWeek() <> getWeek()[1] or VerticalLinePeriod == VerticalLinePeriod.MONTH and getMonth() <> getMonth()[1];
AddVerticalLine(VerticalDayBreakLinesOn && Vert, "", if VerticalLinePeriod == VerticalLinePeriod.day then color.cyan else if VerticalLinePeriod == VerticalLinePeriod.week then Color.Magenta else color.Yellow);
Error in this code 94through 97 can this get updated ? thanks
 
Just quick fix I hope. Removed vertical lines as did not want to spend time on it.


Code:
    # SwingTrading_R1V3
    # R1V1 2011.06.11.23:00 KumoBob
    # A very simple method of SwingTrading with Moving average
    # R1V2 2011.06.13.09:00 KumoBob
    # Added more periods to AverageMode
    # R1V3 2011.06.13.15:00 KumoBob
    # Added hints and other bells an whistles

    declare upper;
    #Hint: Three methods are available and provide a very simple method of SwingTrading with Moving Averages. \nThis orignated when I wanted to see what would happen if two moving averages were used on a daily basis with the buy and sell signal always being the highest and lowest of the two averages.\nIt seems to be a fairly good method, but other indicators such as PPS will most likely out perform this method.
    #Hint FastMovingAverage: Length of FAST average\nDefault = 5
    #Hint SlowMovingAverage: Length of SLOW average\nDefault = 13
    #Hint Method: Choose to signal when Price crosses the Fast MA\nor\nChoose to signal when the Fast MA crosses the Slow MA\nor\n "Buy at the Lowest MA and Sell at the Highest MA when Price crosses these curves
    #Hint AverageMode: Choose to average by bar or a selectable time period\nThe choices are DAY, 2 DAYS, 3 DAYS, 4 DAYS, WEEK, and MONTH \nDefault = Day
    #Hint BubblesOn: Bubbles identify the curve's average length\nDefault = Yes
    #Hint ShowPoints: Shows the exact point the signal occured\nDefault = Yes
    #Hint ShowArrows: Shows the signal with an arrow\nDefault = Yes
    #Hint ShowPlot: Choose to show the curves or hide them
    #Hint VerticalDayBreakLinesOn: Choose to place Vertical Lines at Day, Week or Month intervals\nDefault = No
    #Hint VerticalLinePeriod: Specifies the interval to plot vertical lines\nDefault = Day
    #Hint PriceColorOn: Allows the signal to be seen as Red and Green price bars\nDefault = No
    input FastMovingAverage = 5;
    input SlowMovingAverage = 13;
    Input Method = {"At Fast MA", "MA Cross",Default "Buy at Min MA Sell at Max MA"};
    input AverageMode = {"Bar",default DAY, "2 DAYS", "3 DAYS", "4 DAYS", WEEK, MONTH};

    input BubblesOn = Yes;
    input ShowPlot = Yes;
    input ShowPoints = Yes;
    input ShowArrows = Yes;
    Input PriceColorOn = No;

    def Mode;
    switch (Method){
    case "At Fast MA":
    Mode = 1;
    case "MA Cross":
    Mode = 2;
    case "Buy at Min MA Sell at Max MA":
    Mode = 3;
    }

    


    def PeriodChoice;
    switch (AverageMode){
    case "Day":
    PeriodChoice = 1;
    case "2 DAYS":
    PeriodChoice = 2;
    case "3 DAYS":
    PeriodChoice = 3;
    case "4 DAYS":
    PeriodChoice = 4;
    case WEEK:
    PeriodChoice = 7;
    case MONTH:
    PeriodChoice = 30;
    case "Bar":
    PeriodChoice = 0;
    }


    def BarMA1 = Average(close () , FastMovingAverage );
    Def Day1MA1 = Average(close (period = "DAY" ), FastMovingAverage );
    Def Day2MA1 = Average(close (period = "2 DAYS"), FastMovingAverage );
    Def Day3MA1 = Average(close (period = "3 DAYS"), FastMovingAverage );
    Def Day4MA1 = Average(close (period = "4 DAYS"), FastMovingAverage );
    Def Day7MA1 = Average(close (period = "WEEK" ), FastMovingAverage);
    Def Day30MA1 = Average(close (period = "MONTH") , FastMovingAverage);

    def BarMA2 = Average(close () , SlowMovingAverage);
    Def Day1MA2 = Average(close (period = "DAY" ), SlowMovingAverage);
    Def Day2MA2 = Average(close (period = "2 DAYS"), SlowMovingAverage);
    Def Day3MA2 = Average(close (period = "3 DAYS"), SlowMovingAverage);
    Def Day4MA2 = Average(close (period = "4 DAYS"), SlowMovingAverage);
    Def Day7MA2 = Average(close (period = "WEEK" ), SlowMovingAverage);
    Def Day30MA2 = Average(close (period = "MONTH") , SlowMovingAverage);


    def FMA =
    If PeriodChoice == 1 then Day1MA1 else
    If PeriodChoice == 2 then Day2MA1 else
    If PeriodChoice == 3 then Day3MA1 else
    If PeriodChoice == 4 then Day4MA1 else
    If PeriodChoice == 7 then Day7MA1 else
    If PeriodChoice == 30 then Day30MA1 else BarMA1;
    plot FastMA = if !ShowPlot then Double.NaN else FMA;

    FastMA.AssignValueColor(if Close > FMA then Color.Green else if Close < FMA then Color.Red else Color.Yellow); AddChartBubble(FastMA && BubblesOn && !IsNaN(close) and IsNaN(close[-1]), FastMA, concat("", FastMovingAverage ), if Close > FMA then Color.Green else if Close < FMA then Color.Red else Color.Yellow); def SMA = If PeriodChoice == 1 then Day1MA2 else If PeriodChoice == 2 then Day2MA2 else If PeriodChoice == 3 then Day3MA2 else If PeriodChoice == 4 then Day4MA2 else If PeriodChoice == 7 then Day7MA2 else If PeriodChoice == 30 then Day30MA2 else BarMA2; plot SlowMA = if !ShowPlot then Double.NaN else SMA; SlowMA.AssignValueColor(if Close > SMA then Color.Green else if Close < SMA then Color.Red else Color.Yellow); AddChartBubble(SlowMA && BubblesOn && !IsNaN(close) and IsNaN(close[-1]), SlowMA, concat("", SlowMovingAverage), if Close > SMA then Color.Green else if Close < SMA then Color.Red else Color.Yellow);  def Max = Max(FMA, SMA); def Min = Min(FMA, SMA); def Position = if Close > Max(FMA, SMA) then 2 else if Close < Min(FMA, SMA) then -2 else 0; rec Trend = If Mode == 3 then if Position[1] == 2 && Position[0] <= 0 then -2 else if Position[1] == -2 && Position[0] >= 0 then 2 else
    if Position[1] == 0 && Position[0] == 2 then 2 else
    if Position[1] == 0 && Position[0] == -2 then -2 else Trend[1]
    else If Mode == 1 then if Close > FMA then 2 else -2
    else if FMA > SMA then 2 else -2;

    AssignPriceColor(if !PriceColorOn then Color.Current else If Trend > 0 then Color.Green else Color.Red);


    rec Point = If Mode == 3 then
    if Position[1] == 2 && Position[0] <= 0 then Max else if Position[1] == -2 && Position[0] >= 0 then Min else
    if Position[1] == 0 && Position[0] == 2 then Max else
    if Position[1] == 0 && Position[0] == -2 then Min else Point[1]
    else If Mode == 1 then if Close > FMA then Close else Point[1]
    else if FMA > SMA then Close else Point[1];

    plot SignalPoint = If !ShowPoints then Double.NaN else Point;
    SignalPoint.SetLineWeight(1);
    SignalPoint.SetPaintingStrategy(PaintingStrategy.Points);
    SignalPoint.AssignValueColor(If Trend > 0 then Color.Green else Color.Red);
    # ARROWS ROUTINE
    # ___
    def x_over = Crosses(Trend, 0);
    plot up = if ShowArrows && x_over && Trend > 0 then Low else Double.NaN;
    up.SetPaintingStrategy(PaintingStrategy.Arrow_up);
    up.SetDefaultColor(Color.white);
    plot dn = if ShowArrows && x_over && Trend < 0 then High else Double.NaN; dn.SetPaintingStrategy(PaintingStrategy.Arrow_down); dn.SetDefaultColor(Color.white);
 
@Parker427 You can change the color of the candles via your ToS Settings. As for your second request, it's on the first page of this thread.
 
Can any proficient coder in here please add on to this script that @BenTen posted on page 1 the ability to change the MA color when it is going UP and DOWN...kind of like how Hull MA changes color? Would that take long? I would like to be able to change the colors myself from UP and DOWN...so please no allow for that in the code...Thanks in advance guys.

EDIT: I think I got it...but it looks terrible IMO...Is there a way that some sort of smoothing could be implemented so that it looks half way descent on the color change? I know @horserider was able to do something similar with Trend Magic indicator at least the color part...I have some scripts that have smoothing in them for any coder that would like to try...this is above me.

Code:
# MTF Moving Average

input Period = AggregationPeriod.HOUR;
input AvgType = AverageType.EXPONENTIAL;
input Length = 50;
input priceclose = close;

plot AVG = MovingAverage(AvgType, close(period = Period), Length);
AVG.setdefaultcolor(color.yellow);

AVG.AssignValueColor(if AVG > AVG[1] then Color.GREEN else Color.RED);
 
Last edited:
Is this possible on thinkorswim maybe someone has posted the code already on here?

For example... 5min moving avg on a 1min chart... If anyone can help that would be nice ;)
 
Is this possible on thinkorswim maybe someone has posted the code already on here?

For example... 5min moving avg on a 1min chart... If anyone can help that would be nice ;)


This is color coded to show the change in direction of the moving average...

Code:
# MTF Moving Average

input Period = AggregationPeriod.FIFTEEN_MIN;
input AvgType = AverageType.HULL;
input Length = 20;
input priceclose = close;

plot AVG = MovingAverage(AvgType, close(period = Period), Length);
AVG.SetDefaultColor(Color.YELLOW);

def sState2 = if AVG > AVG[1] then 100 else -100;
AVG.AssignValueColor(if sState2 == 100 then Color.MAGENTA else Color.CYAN);
AVG.SetLineWeight(2);
 
Hi,

Is it possible to add lower time frame EMA in higher time frame chart. Eg- 30 min 200 EMA irrespective of the time-frame I am using in chart.

below code works as long as I am in lower time frame than 30 min otherwise it shows NA. I would like to see 30min EMA in Daily chart.
input Period = aggregationPeriod.THIRTY_MIN;
plot eema = MovAvgExponential(close(period=period),200);
 
@sham You can display moving average from a higher timeframe on a lower timeframe, but not the other way around.
 
I want to apply a moving average from a much higher timeframe onto a 10 minute chart however ToS won't allow a moving average greater than 2000. Is there anyway to bypass this restriction?
 
I want to apply a moving average from a much higher timeframe onto a 10 minute chart however ToS won't allow a moving average greater than 2000. Is there anyway to bypass this restriction?
One of the coding savants here might have a different answer but if nothing else this is where you can just set up a flexible grid with the same stock in both charts, set up the first using the 10 minute chart and the other with the time frame of the moving average you want. Then click the synchronize crosshairs in the settings box. It will show you in both time frames where you are sitting in relation to that MA.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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