0Predictions
New member
I'm working on combining the ThinkorSwim MomentumSMA and Momentum Studies into a combined Strategy but need some help as i'm not able to get the strategy to work correctly. Here is the two generic Studies along with my attempt to combine them into a Strategy. There is something I have incorrect here that I can't seem to fix. Thanks in advance for your help!
#Momentum Study
# TD Ameritrade IP Company, Inc. (c) 2007-2023
declare lower;
input length = 12;
input price = close;
input showBreakoutSignals = no;
assert(length > 0, "'length' must be positive: " + length);
plot Momentum = price - price[length];
plot ZeroLine = 0;
plot UpSignal = if Momentum crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Momentum crosses below ZeroLine then ZeroLine else Double.NaN;
UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);
Momentum.SetDefaultColor(GetColor(1));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#MomemtumSMA
# TD Ameritrade IP Company, Inc. (c) 2007-2023
declare lower;
input price = close;
input momentumLength = 100;
input smaLength = 300;
assert(momentumLength > 0, "'momentum length' must be positive: " + momentumLength);
plot Momentum = price - price[momentumLength];
plot Avg = Average(data = Momentum, length = smaLength);
plot ZeroLine = 0;
Momentum.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(5));
ZeroLine.SetDefaultColor(GetColor(0));
plot upSignal = if Momentum crosses above Avg then Avg else Double.NaN;
plot DownSignal = if Momentum crosses below Avg then Avg else Double.NaN;
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
Here is my attempt to combine the two studies above into a Strategy together:
# Momentum Strategy
input price = close;
input momentumLength = 100;
input smaLength = 300;
assert(momentumLength > 0, "'momentum length' must be positive: " + momentumLength);
plot ZeroLine = 0;
def Momentum = price - price[momentumLength];
def Avg = Average(data = Momentum, length = smaLength);
# Signal conditions
def BuySignal = Momentum > Avg and Momentum[1] <= Avg[1] and Momentum crosses above ZeroLine;
def SellSignal = Momentum < Avg and Momentum[1] >= Avg[1] and Momentum crosses below ZeroLine;
# Plot signals on the same day
plot BuySignalPlot = BuySignal;
plot SellSignalPlot = SellSignal;
BuySignalPlot.SetDefaultColor(Color.UPTICK);
SellSignalPlot.SetDefaultColor(Color.DOWNTICK);
# Strategy orders
AddOrder(OrderType.BUY_TO_OPEN, condition = BuySignal, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "BuySignal");
AddOrder(OrderType.SELL_TO_CLOSE, condition = SellSignal, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "SellSignal");
#Momentum Study
# TD Ameritrade IP Company, Inc. (c) 2007-2023
declare lower;
input length = 12;
input price = close;
input showBreakoutSignals = no;
assert(length > 0, "'length' must be positive: " + length);
plot Momentum = price - price[length];
plot ZeroLine = 0;
plot UpSignal = if Momentum crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Momentum crosses below ZeroLine then ZeroLine else Double.NaN;
UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);
Momentum.SetDefaultColor(GetColor(1));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#MomemtumSMA
# TD Ameritrade IP Company, Inc. (c) 2007-2023
declare lower;
input price = close;
input momentumLength = 100;
input smaLength = 300;
assert(momentumLength > 0, "'momentum length' must be positive: " + momentumLength);
plot Momentum = price - price[momentumLength];
plot Avg = Average(data = Momentum, length = smaLength);
plot ZeroLine = 0;
Momentum.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(5));
ZeroLine.SetDefaultColor(GetColor(0));
plot upSignal = if Momentum crosses above Avg then Avg else Double.NaN;
plot DownSignal = if Momentum crosses below Avg then Avg else Double.NaN;
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
Here is my attempt to combine the two studies above into a Strategy together:
# Momentum Strategy
input price = close;
input momentumLength = 100;
input smaLength = 300;
assert(momentumLength > 0, "'momentum length' must be positive: " + momentumLength);
plot ZeroLine = 0;
def Momentum = price - price[momentumLength];
def Avg = Average(data = Momentum, length = smaLength);
# Signal conditions
def BuySignal = Momentum > Avg and Momentum[1] <= Avg[1] and Momentum crosses above ZeroLine;
def SellSignal = Momentum < Avg and Momentum[1] >= Avg[1] and Momentum crosses below ZeroLine;
# Plot signals on the same day
plot BuySignalPlot = BuySignal;
plot SellSignalPlot = SellSignal;
BuySignalPlot.SetDefaultColor(Color.UPTICK);
SellSignalPlot.SetDefaultColor(Color.DOWNTICK);
# Strategy orders
AddOrder(OrderType.BUY_TO_OPEN, condition = BuySignal, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "BuySignal");
AddOrder(OrderType.SELL_TO_CLOSE, condition = SellSignal, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "SellSignal");