BabyTrader
New member
Hello everyone,
So I am having a hard time making this work. I have tried the trading strategy in real life but this is the first time attempting to put it into code.
Please help.
Code:
# Parabolic_SAR_Moving_Average_Trading_Strategy
# by BabyTrader using the following article: Parabolic SAR Moving Average Trading Strategy
# https://tradingstrategyguides.com/parabolic-sar-moving-average-trade-strategy/
# =====================================================
declare upper;
input TradeSize = 1;
input price = close;
input fastLength = 20;
input slowLength = 40;
input displace = 0;
# Using the code from the default Simple Moving Average
# I was able to set the slow/fast moving averages below
plot FastMA20 = Average(price[-displace], fastLength);
plot SlowMA40 = Average(price[-displace], fastLength);
FastMA20.SetDefaultColor(GetColor(1));
SlowMA40.SetDefaultColor(GetColor(2));
# A buy signal is generated if and only if the 2 factors are true, the 20MA is above the 40MA
# and the ParabolicSAR plots below the closing price
# The inverse is also true. A sell signal is generated when the 40MA crosses above
# the 20MA and the ParabolicSAR plots above the closing price
# The buy exit is generated at the first instance that the ParabolicSAR plots above the close price after the buy signal
# The sell exit is generated at the first instance that the ParabolicSAR plots below the close price after the sell signal
def BuySignal = if FastMA20 crosses above SlowMA40 and ParabolicSAR().SAR crosses below price
then 1
else Double.NaN;
def SellSignal = if FastMA20 crosses below SlowMA40 and ParabolicSAR().SAR crosses above price
then 1
else Double.NaN;
def BuyExit = if ParabolicSAR().SAR crosses above price
then 1
else Double.NaN;
def SellExit = if ParabolicSAR().SAR crosses below price
then 1
else Double.NaN;
plot ArrowUp = if FastMA20 crosses above SlowMA40 and ParabolicSAR().SAR crosses below price
then low
else Double.NaN;
ArrowUp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
ArrowUp.SetLineWeight(3);
ArrowUp.SetDefaultColor(Color.GREEN);
plot ArrowDN = if FastMA20 crosses below SlowMA40 and ParabolicSAR().SAR crosses above price
then high
else Double.NaN;
ArrowDN.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ArrowDN.SetLineWeight(3);
ArrowDN.SetDefaultColor(Color.RED);
Alert(ArrowUp, " ", Alert.BAR, Sound.Chimes);
Alert(ArrowDN, " ", Alert.BAR, Sound.Bell);
AddCloud(FastMA20, SlowMA40, Color.GREEN, Color.RED);
# End Code
def LongEnt = BuySignal is equal to 1;
def ShortEnt = SellSignal is equal to 1 ;
def LongExt = BuyExit is equal to 1;
def ShortExt = SellExit is equal to 1;
AddOrder(OrderType.BUY_TO_OPEN, LongEnt, open[-1], TradeSize, Color.GREEN, Color.GREEN);
AddOrder(OrderType.SELL_TO_CLOSE, LongExt, open[1], TradeSize, Color.RED, Color.RED);
AddOrder(OrderType.SELL_TO_OPEN, ShortEnt, open[-1], TradeSize, Color.ORANGE, Color.ORANGE);
AddOrder(OrderType.BUY_TO_CLOSE, ShortExt, open[1], TradeSize, Color.WHITE, Color.WHITE);
#==============================================
plot LongEntrySignal = if LongEnt then LongEnt else Double.NaN;
LongEntrySignal.SetDefaultColor(Color.UPTICK);
LongEntrySignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
plot LongExitSignal = if LongExt then LongExt else Double.NaN;
LongExitSignal.SetDefaultColor(Color.UPTICK);
LongExitSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
plot ShortEntrySignal = if ShortEnt then ShortEnt else Double.NaN;
ShortEntrySignal.SetDefaultColor(Color.UPTICK);
ShortEntrySignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
plot ShortExitSignal = if ShortExt then ShortExt else Double.NaN;
ShortExitSignal.SetDefaultColor(Color.UPTICK);
ShortExitSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
So I am having a hard time making this work. I have tried the trading strategy in real life but this is the first time attempting to put it into code.
Please help.
Code:
# Parabolic_SAR_Moving_Average_Trading_Strategy
# by BabyTrader using the following article: Parabolic SAR Moving Average Trading Strategy
# https://tradingstrategyguides.com/parabolic-sar-moving-average-trade-strategy/
# =====================================================
declare upper;
input TradeSize = 1;
input price = close;
input fastLength = 20;
input slowLength = 40;
input displace = 0;
# Using the code from the default Simple Moving Average
# I was able to set the slow/fast moving averages below
plot FastMA20 = Average(price[-displace], fastLength);
plot SlowMA40 = Average(price[-displace], fastLength);
FastMA20.SetDefaultColor(GetColor(1));
SlowMA40.SetDefaultColor(GetColor(2));
# A buy signal is generated if and only if the 2 factors are true, the 20MA is above the 40MA
# and the ParabolicSAR plots below the closing price
# The inverse is also true. A sell signal is generated when the 40MA crosses above
# the 20MA and the ParabolicSAR plots above the closing price
# The buy exit is generated at the first instance that the ParabolicSAR plots above the close price after the buy signal
# The sell exit is generated at the first instance that the ParabolicSAR plots below the close price after the sell signal
def BuySignal = if FastMA20 crosses above SlowMA40 and ParabolicSAR().SAR crosses below price
then 1
else Double.NaN;
def SellSignal = if FastMA20 crosses below SlowMA40 and ParabolicSAR().SAR crosses above price
then 1
else Double.NaN;
def BuyExit = if ParabolicSAR().SAR crosses above price
then 1
else Double.NaN;
def SellExit = if ParabolicSAR().SAR crosses below price
then 1
else Double.NaN;
plot ArrowUp = if FastMA20 crosses above SlowMA40 and ParabolicSAR().SAR crosses below price
then low
else Double.NaN;
ArrowUp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
ArrowUp.SetLineWeight(3);
ArrowUp.SetDefaultColor(Color.GREEN);
plot ArrowDN = if FastMA20 crosses below SlowMA40 and ParabolicSAR().SAR crosses above price
then high
else Double.NaN;
ArrowDN.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ArrowDN.SetLineWeight(3);
ArrowDN.SetDefaultColor(Color.RED);
Alert(ArrowUp, " ", Alert.BAR, Sound.Chimes);
Alert(ArrowDN, " ", Alert.BAR, Sound.Bell);
AddCloud(FastMA20, SlowMA40, Color.GREEN, Color.RED);
# End Code
def LongEnt = BuySignal is equal to 1;
def ShortEnt = SellSignal is equal to 1 ;
def LongExt = BuyExit is equal to 1;
def ShortExt = SellExit is equal to 1;
AddOrder(OrderType.BUY_TO_OPEN, LongEnt, open[-1], TradeSize, Color.GREEN, Color.GREEN);
AddOrder(OrderType.SELL_TO_CLOSE, LongExt, open[1], TradeSize, Color.RED, Color.RED);
AddOrder(OrderType.SELL_TO_OPEN, ShortEnt, open[-1], TradeSize, Color.ORANGE, Color.ORANGE);
AddOrder(OrderType.BUY_TO_CLOSE, ShortExt, open[1], TradeSize, Color.WHITE, Color.WHITE);
#==============================================
plot LongEntrySignal = if LongEnt then LongEnt else Double.NaN;
LongEntrySignal.SetDefaultColor(Color.UPTICK);
LongEntrySignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
plot LongExitSignal = if LongExt then LongExt else Double.NaN;
LongExitSignal.SetDefaultColor(Color.UPTICK);
LongExitSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
plot ShortEntrySignal = if ShortEnt then ShortEnt else Double.NaN;
ShortEntrySignal.SetDefaultColor(Color.UPTICK);
ShortEntrySignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
plot ShortExitSignal = if ShortExt then ShortExt else Double.NaN;
ShortExitSignal.SetDefaultColor(Color.UPTICK);
ShortExitSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);