thePetester
New member
Hello,
I have been reading this forum for a long time and while I have found what appears to be a solution to my issue, it is not working in my strategy. As the title indicates, I am attempting to set a stop loss at the previous candle's high if going short or at the previous candle's low if going long. This is a P-Sar strategy, not that it makes any difference. I have read several variations of the following:
This is the entire script. I am trying to modify several things in it. This is my first sticking point and I cannot seem to get it right.
Thank you for any help.
I have been reading this forum for a long time and while I have found what appears to be a solution to my issue, it is not working in my strategy. As the title indicates, I am attempting to set a stop loss at the previous candle's high if going short or at the previous candle's low if going long. This is a P-Sar strategy, not that it makes any difference. I have read several variations of the following:
Code:
# set stop losses -
# if the buy signal was set, then set the stop loss to yesterday's low.
def buyStopLoss = if BuySignal == 1 then low[1] else buyStopLoss[0];
# if the sell signal was set, then set the stop loss to yesterday's high.
def sellStopLoss = if SellSignal == 1 then high[1] else sellStopLoss[0];
...
#if sar crossed above price and close is less than the initial buyStopLoss
def BuyExit = if (ParabolicSAR().SAR crosses above price) or (CLOSE <= buyStopLoss)
then 1
else Double.NaN;
def SellExit = if ParabolicSAR().SAR crosses below price or (CLOSE >= sellStopLoss)
then 1
else Double.NaN;
...
AddOrder(OrderType.SELL_TO_CLOSE, LongExt, open[1], TradeSize, Color.RED, Color.RED);
AddOrder(OrderType.BUY_TO_CLOSE, ShortExt, open[1], TradeSize, Color.WHITE, Color.WHITE);
This is the entire script. I am trying to modify several things in it. This is my first sticking point and I cannot seem to get it right.
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 = 10;
input price = close;
input fastLength = 50;
input slowLength = 200;
input displace = 0;
# Using the code from the default Simple Moving Average
# I was able to set the slow/fast moving averages below
plot FastMA = Average(price[-displace], fastLength);
plot SlowMA = Average(price[-displace], slowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));
# A buy signal is generated if and only if the 2 factors are true, the fastMA is above the slowMA
# and the ParabolicSAR plots below the closing price
# The inverse is also true. A sell signal is generated when the slowMA is above
# the fastMA 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 (LOW > FastMA) and (FastMA > SlowMA) and (ParabolicSAR().SAR crosses below close)
then 1
else Double.NaN;
def SellSignal = if (HIGH < SlowMA) and (FastMA < SlowMA) and (ParabolicSAR().SAR crosses above close)
then 1
else Double.NaN;
# set stop losses -
# if the buy signal was set, then set the stop loss to yesterday's low.
def buyStopLoss = if BuySignal == 1 then low[1] else buyStopLoss[0];
# if the sell signal was set, then set the stop loss to yesterday's high.
def sellStopLoss = if SellSignal == 1 then high[1] else sellStopLoss[0];
#if sar crossed above price and close is less than the initial buyStopLoss
def BuyExit = if (ParabolicSAR().SAR crosses above price) or (CLOSE <= buyStopLoss)
then 1
else Double.NaN;
def SellExit = if ParabolicSAR().SAR crosses below price or (CLOSE >= sellStopLoss)
then 1
else Double.NaN;
plot ArrowUp = if (LOW > FastMA) and FastMA > SlowMA 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 (HIGH > FastMA) and FastMA < SlowMA 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(FastMA, SlowMA, 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);
Thank you for any help.