# 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);