ParabolicSAR - PSAR Moving Average Strategy For ThinkOrSwim

If you change the strategy order code to this below it will allow the trades to be named with the logic options that were used to take that trade.

CSS:
AddOrder(OrderType.BUY_TO_OPEN, BuySignal, open[-1], TradeSize, Color.GREEN, Color.GREEN, name = "Long L" + Logic + " T" + Trend);

AddOrder(OrderType.SELL_TO_CLOSE, BuyExit, open[-1], TradeSize, Color.RED, Color.RED, name = "Close L" + Logic + " C" + Closer);

AddOrder(OrderType.SELL_TO_OPEN, SellSignal, open[-1], TradeSize, Color.ORANGE, Color.ORANGE, name = "Short L" + Logic + " T" + Trend);

AddOrder(OrderType.BUY_TO_CLOSE, SellExit, open[-1], TradeSize, Color.WHITE, Color.WHITE, name = "Close L" + Logic + " C" + Closer);

L1 would indicate Logic 1 condition from the signal portion of code
C1 would indicate Closer 1
T2 would indicate Trend 2

Here are some successful examples:
- Adx Logic 1 (adx is above filter) Here you can see price is trending
LgQHd9c.png



- ADX Logic 2 (adx is below filter) Here you can see sideways price action and the strat is trading perfectly.
xdJFeCU.png



By making that simple amendment you can better set up your strategy for the specific stock or future you are trading.
@SilverWolf , thank you for sharing your updates. Unfortunately, I am not knowledgeable about ToS scripts yet. Would it be possible to release one updated code with these changes? Big big thanks for doing that.
 
declare lower;

input PoopieMALength = 20;
input PoopieAverageType = AverageType.SIMPLE;
input ShowLabel = yes;


def SMA_34 = MovAvgExponential(length=34);
def SMA_89 = MovAvgExponential(length = 89);

Plot momentum = (SMA_34 - SMA_89);

# plot the RSI Moving Average
def PoopiersiMA = MovingAverage(PoopieAverageType, momentum, PoopieMALength);
plot pooprsiMA = poopiersima;
pooprsima.AssignValueColor(if poopiersima > poopiersima[2] then Color.GREEN else if poopiersima < poopiersima[2] then Color.RED else Color.GRAY);
momentum.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
momentum.DefineColor("Positive and Up", Color.GREEN);
momentum.DefineColor("Positive and Down", Color.DARK_GREEN);
momentum.DefineColor("Negative and Down", Color.RED);
momentum.DefineColor("Negative and Up", Color.DARK_RED);
momentum.AssignValueColor(if momentum >= 0 then if momentum > momentum[1] then momentum.color("Positive and Up") else momentum.color("Positive and Down") else if momentum < momentum[1] then momentum.color("Negative and Down") else momentum.color("Negative and Up"));
plot line = 0;
line.SetDefaultColor(Color.WHITE);
#line.SetStyle (Curve.FIRM);

AddLabel(yes, "Momentum:" + Round(momentum,2) + " ",(if momentum > 0 then Color.GREEN else Color.RED));
I found results outstanding when I have used this indicator to trade. Backed tested NQ 1 contract $52780 for 1month on the one min
chart. It eliminates lot of the sideway trades and does not trade as often
 
@SilverWolf , thank you for sharing your updates. Unfortunately, I am not knowledgeable about ToS scripts yet. Would it be possible to release one updated code with these changes? Big big thanks for doing that.

Here you go.. look in the code down to where it says "Strategy Orders". There youll see I commented out the original orders and pasted in the change. When you put a "#" at the beginning TOS doesnt process that code. Pretty cool right? Hopefully this change will help you understand how and why the code takes a trade.

CSS:
# 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/

# ParabolicSAR_withAlerts_JQ
# 2018-04-15 Mods by Johnny Quotron
#    with a very helpful kickstart from DMonkey
# Mods include
#    1. splitting the PSAR into two visible plots so that they can be colored seperately
#    2. adding alert arrows at the PSAR to enhance visibility
#        a. original alert arrows remain available but are hidden by default
#    3. add ability to color color alert arrows
#

# Combined/Modified/Altered by SilverWolf


declare upper;

#======== Inputs ==============================================================================



input accelerationFactor = 0.012;
input accelerationLimit = 0.2;
input extremeoffset = 0.0;

input MovAvgType = AverageType.EXPONENTIAL;
input MovAvgTrendMethod = {default "SINGLE", "CROSSING"};
input CrossingAvgLength = 9;
input TrendTriggerAvgLength = 21;

input TradeClosingMethod = {default "SAR", "MOVAVG"};
input TradeSize = 1;

def Trend = if MovAvgTrendMethod == MovAvgTrendMethod."SINGLE" then 1 else
            if MovAvgTrendMethod == MovAvgTrendMethod."CROSSING" then 2 else 0;

def PlotCross = if Trend == 2 then yes else no;

def Closer = if TradeClosingMethod == TradeClosingMethod."SAR" then 1 else
             if TradeClosingMethod == TradeClosingMethod."MOVAVG" then 2 else 0;




#======== Moving Averages ======================================================================

plot TriggerAVG = MovingAverage(MovAvgType, close, TrendTriggerAvgLength);
TriggerAVG.SetLineWeight(3);
TriggerAVG.SetDefaultColor(Color.WHITE);

plot CrossingAVG = MovingAverage(MovAvgType, close, CrossingAvgLength);
CrossingAVG.SetHiding(!PlotCross);
CrossingAVG.SetLineWeight(3);
CrossingAVG.SetDefaultColor(Color.PINK);

#======== ParabolicSAR =========================================================================

Assert(accelerationFactor > 0, "'acceleration factor' must be positive: " + accelerationFactor);
Assert(accelerationLimit >= accelerationFactor, "'acceleration limit' (" + accelerationLimit + ") must be greater than or equal to 'acceleration factor' (" + accelerationFactor + ")");

def state = {default init, long, short};
def extreme;
def SAR;
def acc;

switch (state[1]) {
case init:
    state = state.long;
    acc = accelerationFactor;
    extreme = high;
    SAR = low;
case short:
    if (SAR[1] < high)
    then {
        state = state.long;
        acc = accelerationFactor;
        extreme = high + extremeoffset;
        SAR = extreme[1];
    } else {
        state = state.short;
        if (low < extreme[1])
        then {
            acc = Min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = low - extremeoffset;
        } else {
            acc = acc[1];
            extreme = extreme[1];
        }
        SAR = Max(Max(high, high[1]), SAR[1] + acc * (extreme - SAR[1]));
    }
case long:
    if (SAR[1] > low)
    then {
        state = state.short;
        acc = accelerationFactor;
        extreme = low - extremeoffset;
        SAR = extreme[1];
    } else {
        state = state.long;
        if (high > extreme[1])
        then {
            acc = Min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = high + extremeoffset;
        } else {
            acc = acc[1];
            extreme = extreme[1];
        }
        SAR = Min(Min(low, low[1]), SAR[1] + acc * (extreme - SAR[1]));
    }
}


#======== SIGNALS =========================================================================




def BuySignal = if Trend == 1 and (close > TriggerAVG ) and (SAR crosses below close)

then 1

else if Trend == 2 and (close > TriggerAVG) and (CrossingAVG crosses above TriggerAVG) and (SAR < close)

then 1
else Double.NaN;


def SellSignal = if Trend == 1 and (close < TriggerAVG ) and (SAR crosses above close)

then 1

else if Trend == 2 and (close < TriggerAVG) and (CrossingAVG crosses below TriggerAVG) and (SAR > close)

then 1

else Double.NaN;


def BuyExit = if Closer == 1 and (close crosses below SAR[-1])

then 1

else if Closer == 2 and (TriggerAVG > CrossingAVG)

then 1

else Double.NaN;


def SellExit = if Closer == 1 and (close crosses above SAR[-1])

then 1

else if Closer == 2 and (TriggerAVG < CrossingAVG)

then 1

else Double.NaN;


#======== STRATEGY ORDERS ===================================================================
#AddOrder(OrderType.BUY_TO_OPEN, BuySignal, open[-1], TradeSize, Color.GREEN, Color.GREEN, name = "Long");
#AddOrder(OrderType.SELL_TO_CLOSE, BuyExit, open[-1], TradeSize, Color.RED, Color.RED, name = "Close");
#AddOrder(OrderType.SELL_TO_OPEN, SellSignal, open[-1], TradeSize, Color.ORANGE, Color.ORANGE, name = "Short");
#AddOrder(OrderType.BUY_TO_CLOSE, SellExit, open[-1], TradeSize, Color.WHITE, Color.WHITE, name = "Close");

AddOrder(OrderType.BUY_TO_OPEN, BuySignal, open[-1], TradeSize, Color.GREEN, Color.GREEN, name = "Long L" + Logic + " T" + Trend);

AddOrder(OrderType.SELL_TO_CLOSE, BuyExit, open[-1], TradeSize, Color.RED, Color.RED, name = "Close L" + Logic + " C" + Closer);

AddOrder(OrderType.SELL_TO_OPEN, SellSignal, open[-1], TradeSize, Color.ORANGE, Color.ORANGE, name = "Short L" + Logic + " T" + Trend);

AddOrder(OrderType.BUY_TO_CLOSE, SellExit, open[-1], TradeSize, Color.WHITE, Color.WHITE, name = "Close L" + Logic + " C" + Closer);
#======== PLOTS ============================================================================

plot BullPSAR = if SAR < close then SAR else Double.NaN;
BullPSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
BullPSAR.SetDefaultColor(Color.LIME);

plot BearPSAR = if SAR > close then SAR else Double.NaN;
BearPSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
BearPSAR.SetDefaultColor(Color.PINK);

#---

def BullSignalAtCandle = Crosses(SAR, close, CrossingDirection.BELOW);
plot BullSignalAtPSAR = if close crosses above SAR
                     then SAR
                     else Double.NaN;
BullSignalAtPSAR.SetLineWeight(1);
BullSignalAtPSAR.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BullSignalAtPSAR.SetDefaultColor(Color.LIME);

def BearSignalAtCandle = Crosses(SAR, close, CrossingDirection.ABOVE);
plot BearSignalAtPSAR = if close crosses below SAR
                     then SAR
                     else Double.NaN;
BearSignalAtPSAR.SetLineWeight(1);
BearSignalAtPSAR.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
BearSignalAtPSAR.SetDefaultColor(Color.PINK);

#---

plot LongEntrySignal = if BuySignal then BuySignal else Double.NaN;
LongEntrySignal.SetDefaultColor(Color.UPTICK);
LongEntrySignal.SetLineWeight(5);
LongEntrySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);


plot ShortEntrySignal = if SellSignal then SellSignal else Double.NaN;
ShortEntrySignal.SetDefaultColor(Color.DOWNTICK);
ShortEntrySignal.SetLineWeight(5);
ShortEntrySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

plot LongExitSignal = if BuyExit then BuyExit else Double.NaN;
LongExitSignal.SetDefaultColor(Color.White);
LongExitSignal.SetLineWeight(1);
LongExitSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);


plot ShortExitSignal = if SellExit then SellExit else Double.NaN;
ShortExitSignal.SetDefaultColor(Color.White);
ShortExitSignal.SetLineWeight(1);
ShortExitSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);


#======== ALERTS ===========================================================================

input AlertsOn = No;
Alert(AlertsOn and BullSignalAtCandle, "Bullish PSAR", Alert.BAR, Sound.Ring);
Alert(AlertsOn and BearSignalAtCandle, "Bearish PSAR", Alert.BAR, Sound.Ring);
Alert(AlertsOn and BuySignal, "Bullish PSAR above AVG", Alert.BAR, Sound.Ring);
Alert(AlertsOn and SellSignal, "Bullish PSAR below AVG", Alert.BAR, Sound.Ring);

#======== EOF ===========================================================================
 
Here you go.. look in the code down to where it says "Strategy Orders". There youll see I commented out the original orders and pasted in the change. When you put a "#" at the beginning TOS doesnt process that code. Pretty cool right? Hopefully this change will help you understand how and why the code takes a trade.

CSS:
# 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/

# ParabolicSAR_withAlerts_JQ
# 2018-04-15 Mods by Johnny Quotron
#    with a very helpful kickstart from DMonkey
# Mods include
#    1. splitting the PSAR into two visible plots so that they can be colored seperately
#    2. adding alert arrows at the PSAR to enhance visibility
#        a. original alert arrows remain available but are hidden by default
#    3. add ability to color color alert arrows
#

# Combined/Modified/Altered by SilverWolf


declare upper;

#======== Inputs ==============================================================================



input accelerationFactor = 0.012;
input accelerationLimit = 0.2;
input extremeoffset = 0.0;

input MovAvgType = AverageType.EXPONENTIAL;
input MovAvgTrendMethod = {default "SINGLE", "CROSSING"};
input CrossingAvgLength = 9;
input TrendTriggerAvgLength = 21;

input TradeClosingMethod = {default "SAR", "MOVAVG"};
input TradeSize = 1;

def Trend = if MovAvgTrendMethod == MovAvgTrendMethod."SINGLE" then 1 else
            if MovAvgTrendMethod == MovAvgTrendMethod."CROSSING" then 2 else 0;

def PlotCross = if Trend == 2 then yes else no;

def Closer = if TradeClosingMethod == TradeClosingMethod."SAR" then 1 else
             if TradeClosingMethod == TradeClosingMethod."MOVAVG" then 2 else 0;




#======== Moving Averages ======================================================================

plot TriggerAVG = MovingAverage(MovAvgType, close, TrendTriggerAvgLength);
TriggerAVG.SetLineWeight(3);
TriggerAVG.SetDefaultColor(Color.WHITE);

plot CrossingAVG = MovingAverage(MovAvgType, close, CrossingAvgLength);
CrossingAVG.SetHiding(!PlotCross);
CrossingAVG.SetLineWeight(3);
CrossingAVG.SetDefaultColor(Color.PINK);

#======== ParabolicSAR =========================================================================

Assert(accelerationFactor > 0, "'acceleration factor' must be positive: " + accelerationFactor);
Assert(accelerationLimit >= accelerationFactor, "'acceleration limit' (" + accelerationLimit + ") must be greater than or equal to 'acceleration factor' (" + accelerationFactor + ")");

def state = {default init, long, short};
def extreme;
def SAR;
def acc;

switch (state[1]) {
case init:
    state = state.long;
    acc = accelerationFactor;
    extreme = high;
    SAR = low;
case short:
    if (SAR[1] < high)
    then {
        state = state.long;
        acc = accelerationFactor;
        extreme = high + extremeoffset;
        SAR = extreme[1];
    } else {
        state = state.short;
        if (low < extreme[1])
        then {
            acc = Min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = low - extremeoffset;
        } else {
            acc = acc[1];
            extreme = extreme[1];
        }
        SAR = Max(Max(high, high[1]), SAR[1] + acc * (extreme - SAR[1]));
    }
case long:
    if (SAR[1] > low)
    then {
        state = state.short;
        acc = accelerationFactor;
        extreme = low - extremeoffset;
        SAR = extreme[1];
    } else {
        state = state.long;
        if (high > extreme[1])
        then {
            acc = Min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = high + extremeoffset;
        } else {
            acc = acc[1];
            extreme = extreme[1];
        }
        SAR = Min(Min(low, low[1]), SAR[1] + acc * (extreme - SAR[1]));
    }
}


#======== SIGNALS =========================================================================




def BuySignal = if Trend == 1 and (close > TriggerAVG ) and (SAR crosses below close)

then 1

else if Trend == 2 and (close > TriggerAVG) and (CrossingAVG crosses above TriggerAVG) and (SAR < close)

then 1
else Double.NaN;


def SellSignal = if Trend == 1 and (close < TriggerAVG ) and (SAR crosses above close)

then 1

else if Trend == 2 and (close < TriggerAVG) and (CrossingAVG crosses below TriggerAVG) and (SAR > close)

then 1

else Double.NaN;


def BuyExit = if Closer == 1 and (close crosses below SAR[-1])

then 1

else if Closer == 2 and (TriggerAVG > CrossingAVG)

then 1

else Double.NaN;


def SellExit = if Closer == 1 and (close crosses above SAR[-1])

then 1

else if Closer == 2 and (TriggerAVG < CrossingAVG)

then 1

else Double.NaN;


#======== STRATEGY ORDERS ===================================================================
#AddOrder(OrderType.BUY_TO_OPEN, BuySignal, open[-1], TradeSize, Color.GREEN, Color.GREEN, name = "Long");
#AddOrder(OrderType.SELL_TO_CLOSE, BuyExit, open[-1], TradeSize, Color.RED, Color.RED, name = "Close");
#AddOrder(OrderType.SELL_TO_OPEN, SellSignal, open[-1], TradeSize, Color.ORANGE, Color.ORANGE, name = "Short");
#AddOrder(OrderType.BUY_TO_CLOSE, SellExit, open[-1], TradeSize, Color.WHITE, Color.WHITE, name = "Close");

AddOrder(OrderType.BUY_TO_OPEN, BuySignal, open[-1], TradeSize, Color.GREEN, Color.GREEN, name = "Long L" + Logic + " T" + Trend);

AddOrder(OrderType.SELL_TO_CLOSE, BuyExit, open[-1], TradeSize, Color.RED, Color.RED, name = "Close L" + Logic + " C" + Closer);

AddOrder(OrderType.SELL_TO_OPEN, SellSignal, open[-1], TradeSize, Color.ORANGE, Color.ORANGE, name = "Short L" + Logic + " T" + Trend);

AddOrder(OrderType.BUY_TO_CLOSE, SellExit, open[-1], TradeSize, Color.WHITE, Color.WHITE, name = "Close L" + Logic + " C" + Closer);
#======== PLOTS ============================================================================

plot BullPSAR = if SAR < close then SAR else Double.NaN;
BullPSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
BullPSAR.SetDefaultColor(Color.LIME);

plot BearPSAR = if SAR > close then SAR else Double.NaN;
BearPSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
BearPSAR.SetDefaultColor(Color.PINK);

#---

def BullSignalAtCandle = Crosses(SAR, close, CrossingDirection.BELOW);
plot BullSignalAtPSAR = if close crosses above SAR
                     then SAR
                     else Double.NaN;
BullSignalAtPSAR.SetLineWeight(1);
BullSignalAtPSAR.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BullSignalAtPSAR.SetDefaultColor(Color.LIME);

def BearSignalAtCandle = Crosses(SAR, close, CrossingDirection.ABOVE);
plot BearSignalAtPSAR = if close crosses below SAR
                     then SAR
                     else Double.NaN;
BearSignalAtPSAR.SetLineWeight(1);
BearSignalAtPSAR.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
BearSignalAtPSAR.SetDefaultColor(Color.PINK);

#---

plot LongEntrySignal = if BuySignal then BuySignal else Double.NaN;
LongEntrySignal.SetDefaultColor(Color.UPTICK);
LongEntrySignal.SetLineWeight(5);
LongEntrySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);


plot ShortEntrySignal = if SellSignal then SellSignal else Double.NaN;
ShortEntrySignal.SetDefaultColor(Color.DOWNTICK);
ShortEntrySignal.SetLineWeight(5);
ShortEntrySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

plot LongExitSignal = if BuyExit then BuyExit else Double.NaN;
LongExitSignal.SetDefaultColor(Color.White);
LongExitSignal.SetLineWeight(1);
LongExitSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);


plot ShortExitSignal = if SellExit then SellExit else Double.NaN;
ShortExitSignal.SetDefaultColor(Color.White);
ShortExitSignal.SetLineWeight(1);
ShortExitSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);


#======== ALERTS ===========================================================================

input AlertsOn = No;
Alert(AlertsOn and BullSignalAtCandle, "Bullish PSAR", Alert.BAR, Sound.Ring);
Alert(AlertsOn and BearSignalAtCandle, "Bearish PSAR", Alert.BAR, Sound.Ring);
Alert(AlertsOn and BuySignal, "Bullish PSAR above AVG", Alert.BAR, Sound.Ring);
Alert(AlertsOn and SellSignal, "Bullish PSAR below AVG", Alert.BAR, Sound.Ring);

#======== EOF ===========================================================================
@SilverWolf, thank you for sharing. I am getting script compatability error, so your changes are automatically being changed by TOS. Will research and find out the cause.

On the other question, related to comparing close > SAR or close < SAR instead of crosses, do you think that is the right method?
 
@SilverWolf, thank you for sharing. I am getting script compatability error, so your changes are automatically being changed by TOS. Will research and find out the cause.

On the other question, related to comparing close > SAR or close < SAR instead of crosses, do you think that is the right method?
Anybody looked at PL from back testing is amazing
 
I found results outstanding when I have used this indicator to trade. Backed tested NQ 1 contract $52780 for 1month on the one min
chart. It eliminates lot of the sideway trades and does not trade as often
Hi
do you combine this indicator with the strategy (version code 2) . if yes what is the entry/exist criteria ,stop loss
 
@SilverWolf, thank you for sharing. I am getting script compatability error, so your changes are automatically being changed by TOS. Will research and find out the cause.

On the other question, related to comparing close > SAR or close < SAR instead of crosses, do you think that is the right method?
I used the older script sorry about that. Oh an yes you can make that change and it will work. I used the cross to have the action happen on one bar. But feel free to change it how you like. play around test. Good Luck


CSS:
# Parabolic_SAR_Moving_Average_Trading_Strategy
# ParabolicSAR_Trendy_Strategy_1_18

# by BabyTrader using the following article: Parabolic SAR Moving Average Trading Strategy

# https://tradingstrategyguides.com/parabolic-sar-moving-average-trade-strategy/

# ParabolicSAR_withAlerts_JQ
# 2018-04-15 Mods by Johnny Quotron
#    with a very helpful kickstart from DMonkey
# Mods include
#    1. splitting the PSAR into two visible plots so that they can be colored seperately
#    2. adding alert arrows at the PSAR to enhance visibility
#        a. original alert arrows remain available but are hidden by default
#    3. add ability to color color alert arrows
#

# Combined/Modified/Altered by SilverWolf


declare upper;
 
#======== Inputs ==============================================================================

input TradeSize = 1;
input SarBars = 1;
input accelerationFactor = 0.012;
input accelerationLimit = 0.2;
input extremeoffset = 0.0;

input MovAvgType = AverageType.EXPONENTIAL;
input MovAvgTrendMethod = {default "SINGLE", "CROSSING"};
input CrossingAvgLength = 9;
input TrendTriggerAvgLength = 21;

input TradeClosingMethod = {default "SAR", "MOVAVG"};


def Trend = if MovAvgTrendMethod == MovAvgTrendMethod."SINGLE" then 1 else
            if MovAvgTrendMethod == MovAvgTrendMethod."CROSSING" then 2 else 0;

def PlotCross = if Trend == 2 then yes else no;

def Closer = if TradeClosingMethod == TradeClosingMethod."SAR" then 1 else
             if TradeClosingMethod == TradeClosingMethod."MOVAVG" then 2 else 0;

#======== Moving Averages ======================================================================

plot TriggerAVG = MovingAverage(MovAvgType, close, TrendTriggerAvgLength);
TriggerAVG.SetLineWeight(3);
TriggerAVG.SetDefaultColor(Color.WHITE);

plot CrossingAVG = MovingAverage(MovAvgType, close, CrossingAvgLength);
CrossingAVG.SetHiding(!PlotCross);
CrossingAVG.SetLineWeight(3);
CrossingAVG.SetDefaultColor(Color.PINK);

#======== ParabolicSAR =========================================================================

Assert(accelerationFactor > 0, "'acceleration factor' must be positive: " + accelerationFactor);
Assert(accelerationLimit >= accelerationFactor, "'acceleration limit' (" + accelerationLimit + ") must be greater than or equal to 'acceleration factor' (" + accelerationFactor + ")");

def state = {default init, long, short};
def extreme;
def SAR;
def acc;

switch (state[1]) {
case init:
    state = state.long;
    acc = accelerationFactor;
    extreme = high;
    SAR = low;
case short:
    if (SAR[1] < high)
    then {
        state = state.long;
        acc = accelerationFactor;
        extreme = high + extremeoffset;
        SAR = extreme[1];
    } else {
        state = state.short;
        if (low < extreme[1])
        then {
            acc = Min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = low - extremeoffset;
        } else {
            acc = acc[1];
            extreme = extreme[1];
        }
        SAR = Max(Max(high, high[1]), SAR[1] + acc * (extreme - SAR[1]));
    }
case long:
    if (SAR[1] > low)
    then {
        state = state.short;
        acc = accelerationFactor;
        extreme = low - extremeoffset;
        SAR = extreme[1];
    } else {
        state = state.long;
        if (high > extreme[1])
        then {
            acc = Min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = high + extremeoffset;
        } else {
            acc = acc[1];
            extreme = extreme[1];
        }
        SAR = Min(Min(low, low[1]), SAR[1] + acc * (extreme - SAR[1]));
    }
}

#======== ADX ===========================================================================

input ADXlength = 14;
input averageType = AverageType.WILDERS;
input mom = 20;

def hiDiff = high - high[1];
def loDiff = low[1] - low;

def plusDM = if hiDiff > loDiff and hiDiff > 0 then hiDiff else 0;
def minusDM =  if loDiff > hiDiff and loDiff > 0 then loDiff else 0;

def ATR = MovingAverage(averageType, TrueRange(high, close, low), ADXlength);
def "DI+" = 100 * MovingAverage(averageType, plusDM, ADXlength) / ATR;
def "DI-" = 100 * MovingAverage(averageType, minusDM, ADXlength) / ATR;

def DX = if ("DI+" + "DI-" > 0) then 100 * AbsValue("DI+" - "DI-") / ("DI+" + "DI-") else 0;
def ADX = MovingAverage(averageType, DX, ADXlength);


#======== Logic =========================================================================
input adxLogic = yes;

def Logic = if adxLogic and ADX > mom

then 1

else if adxLogic and ADX < mom

then 2

else if !adxLogic

then 1

else Double.NaN;



#======== SIGNALS =========================================================================

def BuySignal = if Logic == 1 and Trend == 1 and (close > TriggerAVG ) and (SAR crosses below close)

then 1

else if Logic == 1 and Trend == 2 and (close > TriggerAVG) and (CrossingAVG > TriggerAVG) and (SAR crosses below close)

then 1

else if Logic == 2 and Trend == 1 and (close < TriggerAVG ) and (SAR crosses above close)

then 1

else if Logic == 2 and Trend == 2 and (close < TriggerAVG) and (CrossingAVG < TriggerAVG) and (SAR crosses above close)

then 1

else Double.NaN;


def SellSignal = if Logic == 1 and Trend == 1 and (close < TriggerAVG ) and (SAR crosses above close)

then 1

else if Logic == 1 and Trend == 2 and (close < TriggerAVG) and (CrossingAVG < TriggerAVG) and (SAR crosses above close)

then 1

else if Logic == 2 and Trend == 1 and (close > TriggerAVG ) and (SAR crosses below close)

then 1

else if Logic == 2 and Trend == 2 and (close > TriggerAVG) and (CrossingAVG > TriggerAVG) and (SAR crosses below close)

then 1

else Double.NaN;


def BuyExit = if Logic == 1 and Closer == 1 and (close crosses below SAR)

then 1

else if Logic == 1 and Closer == 2 and (close crosses below TriggerAVG)

then 1

else if Logic == 2 and Closer == 1 and (close crosses above SAR)

then 1

else if Logic == 2 and Closer == 2 and (close crosses above TriggerAVG)

then 1

else if Logic == 1 and Closer == 1 and (adx crosses mom)

then 1

else if Logic == 1 and Closer == 2 and (adx crosses mom)

then 1

else if Logic == 2 and Closer == 1 and (adx crosses mom)

then 1

else if Logic == 2 and Closer == 2 and (adx crosses mom)

then 1

else Double.NaN;


def SellExit = if Logic == 1 and Closer == 1 and (close crosses above SAR)

then 1

else if Logic == 1 and Closer == 2 and (close crosses above TriggerAVG)

then 1

else if Logic == 2 and Closer == 1 and (close crosses below SAR)

then 1

else if Logic == 2 and Closer == 2 and (close crosses below TriggerAVG)

then 1

else if Logic == 1 and Closer == 1 and (adx crosses mom)

then 1

else if Logic == 1 and Closer == 2 and (adx crosses mom)

then 1

else if Logic == 2 and Closer == 1 and (adx crosses mom)

then 1

else if Logic == 2 and Closer == 2 and (adx crosses mom)

then 1

else Double.NaN;


#======== STRATEGY ORDERS ===================================================================

def VolStrength = Lg(volume[1]) - Lg(SimpleMovingAvg(volume[1], 2000));
def HighPrice = high;
def LowPrice = low;


AddOrder(OrderType.BUY_TO_OPEN, BuySignal, open[-1], TradeSize, Color.GREEN, Color.GREEN, name = "Long L" + Logic + " T" + Trend);

AddOrder(OrderType.SELL_TO_CLOSE, BuyExit, open[-1], TradeSize, Color.RED, Color.RED, name = "Close L" + Logic + " C" + Closer);

AddOrder(OrderType.SELL_TO_OPEN, SellSignal, open[-1], TradeSize, Color.ORANGE, Color.ORANGE, name = "Short L" + Logic + " T" + Trend);

AddOrder(OrderType.BUY_TO_CLOSE, SellExit, open[-1], TradeSize, Color.WHITE, Color.WHITE, name = "Close L" + Logic + " C" + Closer);


#======== PLOTS ============================================================================

plot BullPSAR = if SAR < close then SAR else Double.NaN;
BullPSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
BullPSAR.SetDefaultColor(Color.LIME);

plot BearPSAR = if SAR > close then SAR else Double.NaN;
BearPSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
BearPSAR.SetDefaultColor(Color.PINK);

#---

def BullSignalAtCandle = Crosses(SAR, close, CrossingDirection.BELOW);
plot BullSignalAtPSAR = if close crosses above SAR
                     then SAR
                     else Double.NaN;
BullSignalAtPSAR.SetLineWeight(1);
BullSignalAtPSAR.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BullSignalAtPSAR.SetDefaultColor(Color.LIME);

def BearSignalAtCandle = Crosses(SAR, close, CrossingDirection.ABOVE);
plot BearSignalAtPSAR = if close crosses below SAR
                     then SAR
                     else Double.NaN;
BearSignalAtPSAR.SetLineWeight(1);
BearSignalAtPSAR.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
BearSignalAtPSAR.SetDefaultColor(Color.PINK);

#---

plot LongEntrySignal = if BuySignal then BuySignal else Double.NaN;
LongEntrySignal.SetDefaultColor(Color.UPTICK);
LongEntrySignal.SetLineWeight(5);
LongEntrySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);


plot ShortEntrySignal = if SellSignal then SellSignal else Double.NaN;
ShortEntrySignal.SetDefaultColor(Color.DOWNTICK);
ShortEntrySignal.SetLineWeight(5);
ShortEntrySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

plot LongExitSignal = if BuyExit then BuyExit else Double.NaN;
LongExitSignal.SetDefaultColor(Color.WHITE);
LongExitSignal.SetLineWeight(1);
LongExitSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);


plot ShortExitSignal = if SellExit then SellExit else Double.NaN;
ShortExitSignal.SetDefaultColor(Color.WHITE);
ShortExitSignal.SetLineWeight(1);
ShortExitSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

#======== ALERTS ===========================================================================

input AlertsOn = No;
Alert(AlertsOn and BullSignalAtCandle, "Bullish PSAR", Alert.BAR, Sound.Ring);
Alert(AlertsOn and BearSignalAtCandle, "Bearish PSAR", Alert.BAR, Sound.Ring);
Alert(AlertsOn and BuySignal, "Bullish PSAR above AVG", Alert.BAR, Sound.Ring);
Alert(AlertsOn and SellSignal, "Bullish PSAR below AVG", Alert.BAR, Sound.Ring);

#======== Paint Bars =======================================================================

input paintBars = No;
AssignPriceColor(if !paintBars
    then Color.CURRENT
    else if SAR < close
        then Color.GREEN
        else if  SAR > close
            then Color.RED
            else Color.CURRENT);

#======== Labels ===========================================================================

#Spacer
input blink = no;
input AdvLabelsOn = no;
input simpleLabels = no;

def NewBar = if close[1] != close[2] or high[1] != high[2] or low[1] != low[2] then yes else Double.NaN;
def Clock = if !IsNaN(NewBar) and Clock[1] == 1 then 0 else if !IsNaN(NewBar) and Clock[1] == 0 then 1 else Clock[1];

AddLabel(blink and AdvLabelsOn or SimpleLabels, "                                                                     ParabolicSAR Strategy Version 01.11                                                                                                     ", if Clock == 0 then Color.WHITE else Color.YELLOW);
AddLabel(!blink and AdvLabelsOn or SimpleLabels, "                                                                     ParabolicSAR Strategy Version 01.11                                                                                                     ",  Color.WHITE);


#Buy
AddLabel(AdvLabelsOn,
if  Logic == 1 and Trend == 1 and (close > TriggerAVG ) and (SAR crosses below close) within SarBars bars
or  Logic == 1 and Trend == 2 and (close > TriggerAVG) and (CrossingAVG > TriggerAVG) and (SAR crosses below close) within SarBars bars
or  Logic == 2 and Trend == 1 and (close < TriggerAVG) and (SAR crosses above close) within SarBars bars
or  Logic == 2 and Trend == 2 and (close < TriggerAVG) and (CrossingAVG < TriggerAVG) and (SAR crosses above close) within SarBars bars then "         " else "         ",

if  Logic == 1 and Trend == 1 and (close > TriggerAVG) and (SAR crosses below close) within SarBars bars
or  Logic == 1 and Trend == 2 and (close > TriggerAVG) and (CrossingAVG > TriggerAVG) and (SAR crosses below close) within SarBars bars
or  Logic == 2 and Trend == 1 and (close < TriggerAVG) and (SAR crosses above close) within SarBars bars
or  Logic == 2 and Trend == 2 and (close < TriggerAVG) and (CrossingAVG < TriggerAVG) and (SAR crosses above close) within SarBars bars then Color.GREEN else Color.WHITE);


#BuyClose
AddLabel(AdvLabelsOn,
if  Logic == 1 and Closer == 1 and (SAR > close) within SarBars bars
or  Logic == 1 and  Closer == 2 and (close < TriggerAVG) within SarBars bars
or  Logic == 2 and Closer == 1 and (SAR < close) within SarBars bars
or  Logic == 2 and Closer == 2 and (close > TriggerAVG)
or  Logic == 1 and Closer == 1 and (adx crosses above mom) within SarBars bars
or  Logic == 1 and Closer == 2 and (adx crosses above mom) within SarBars bars
or  Logic == 2 and Closer == 1 and (adx crosses below mom) within SarBars bars
or  Logic == 2 and Closer == 2 and (adx crosses below mom) within SarBars bars then "         " else "         ",

if  Logic == 1 and Closer == 1 and (SAR > close) within SarBars bars
or  Logic == 1 and  Closer == 2 and (close < TriggerAVG) within SarBars bars
or  Logic == 2 and Closer == 1 and (SAR < close) within SarBars bars
or  Logic == 2 and Closer == 2 and (close > TriggerAVG)
or  Logic == 1 and Closer == 1 and (adx crosses above mom) within SarBars bars
or  Logic == 1 and Closer == 2 and (adx crosses above mom) within SarBars bars
or  Logic == 2 and Closer == 1 and (adx crosses below mom) within SarBars bars
or  Logic == 2 and Closer == 2 and (adx crosses below mom) within SarBars bars then Color.MAGENTA else Color.WHITE);

#Sell
AddLabel(AdvLabelsOn,
if  Logic == 1 and Trend == 1 and (close < TriggerAVG) and (SAR crosses above close) within SarBars bars
or  Logic == 1 and Trend == 2 and (close < TriggerAVG) and (CrossingAVG < TriggerAVG) and (SAR crosses above close) within SarBars bars
or Logic == 2 and Trend == 1 and (close > TriggerAVG) and (SAR crosses below close) within SarBars bars
or  Logic == 2 and Trend == 2 and (close > TriggerAVG) and (CrossingAVG > TriggerAVG) and (SAR crosses below close) within SarBars bars then "         " else "         ",

if  Logic == 1 and Trend == 1 and (close < TriggerAVG) and (SAR crosses above close) within SarBars bars
or  Logic == 1 and Trend == 2 and (close < TriggerAVG) and (CrossingAVG < TriggerAVG) and (SAR crosses above close) within SarBars bars
or  Logic == 2 and Trend == 1 and (close > TriggerAVG) and (SAR crosses below close) within SarBars bars
or  Logic == 2 and Trend == 2 and (close > TriggerAVG) and (CrossingAVG > TriggerAVG) and (SAR crosses below close) within SarBars bars then Color.RED else Color.WHITE);


#SellClose
AddLabel(AdvLabelsOn,
if  Logic == 1 and Closer == 1 and (SAR < close) within SarBars bars
or  Logic == 1 and Closer == 2 and (close > TriggerAVG) within SarBars bars
or  Logic == 2 and Closer == 1 and (SAR > close) within SarBars bars
or  Logic == 2 and Closer == 2 and (close < TriggerAVG)
or  Logic == 1 and Closer == 1 and (adx crosses below mom) within SarBars bars
or  Logic == 1 and Closer == 2 and (adx crosses below mom) within SarBars bars
or  Logic == 2 and Closer == 1 and (adx crosses above mom) within SarBars bars
or  Logic == 2 and Closer == 2 and (adx crosses above mom) within SarBars bars then "         " else "         ",

if  Logic == 1 and Closer == 1 and (SAR < close) within SarBars bars
or  Logic == 1 and Closer == 2 and (close > TriggerAVG) within SarBars bars
or Logic == 2 and Closer == 1 and (SAR> close) within SarBars bars
or  Logic == 2 and Closer == 2 and (close < TriggerAVG)
or  Logic == 1 and Closer == 1 and (adx crosses below mom) within SarBars bars
or  Logic == 1 and Closer == 2 and (adx crosses below mom) within SarBars bars
or  Logic == 2 and Closer == 1 and (adx crosses above mom) within SarBars bars
or  Logic == 2 and Closer == 2 and (adx crosses above mom) within SarBars bars then Color.LIGHT_GREEN else Color.WHITE);




AddLabel (simpleLabels, if BuySignal within SarBars bars then  "         " else   "         ", if BuySignal >= 1 within SarBars bars then Color.GREEN else Color.WHITE);
AddLabel (simpleLabels, if BuyExit within SarBars bars then  "         " else   "         ", if BuyExit >= 1 within SarBars bars then Color.MAGENTA else Color.WHITE);
AddLabel (simpleLabels, if SellSignal within SarBars bars then  "         " else   "         ", if SellSignal >= 1 within SarBars bars then Color.RED else Color.WHITE);
AddLabel (simpleLabels, if SellExit within SarBars bars then  "         " else   "         ", if SellExit >= 1 within SarBars bars then Color.LIGHT_GREEN else Color.WHITE);



AddLabel (AdvLabelsOn or simpleLabels,
if Logic == 2 then "  Modified Logic  " else "   Normal Logic   ", Color.WHITE);


#======== EOF =========================================================================
 
Last edited:
Hi
do you combine this indicator with the strategy (version code 2) . if yes what is the entry/exist criteria ,stop loss
I have not combined this with any other stragety used as is. I will combine with the other and see what I get.
 
I found results outstanding when I have used this indicator to trade. Backed tested NQ 1 contract $52780 for 1month on the one min
chart. It eliminates lot of the sideway trades and does not trade as often
@dsohi11 thanks for sharing. How to you trade this indicator? Do you auto trade or trade manually?
 
I have not combined this with any other stragety used as is. I will combine with the other and see what I get.
How this single indicator works from entry/exit and stop loss perspective

I used the older script sorry about that. Oh an yes you can make that change and it will work. I used the cross to have the action happen on one bar. But feel free to change it how you like. play around test. Good Luck


CSS:
# Parabolic_SAR_Moving_Average_Trading_Strategy
# ParabolicSAR_Trendy_Strategy_1_18

# by BabyTrader using the following article: Parabolic SAR Moving Average Trading Strategy

# https://tradingstrategyguides.com/parabolic-sar-moving-average-trade-strategy/

# ParabolicSAR_withAlerts_JQ
# 2018-04-15 Mods by Johnny Quotron
#    with a very helpful kickstart from DMonkey
# Mods include
#    1. splitting the PSAR into two visible plots so that they can be colored seperately
#    2. adding alert arrows at the PSAR to enhance visibility
#        a. original alert arrows remain available but are hidden by default
#    3. add ability to color color alert arrows
#

# Combined/Modified/Altered by SilverWolf


declare upper;
 
#======== Inputs ==============================================================================

input TradeSize = 1;
input SarBars = 1;
input accelerationFactor = 0.012;
input accelerationLimit = 0.2;
input extremeoffset = 0.0;

input MovAvgType = AverageType.EXPONENTIAL;
input MovAvgTrendMethod = {default "SINGLE", "CROSSING"};
input CrossingAvgLength = 9;
input TrendTriggerAvgLength = 21;

input TradeClosingMethod = {default "SAR", "MOVAVG"};


def Trend = if MovAvgTrendMethod == MovAvgTrendMethod."SINGLE" then 1 else
            if MovAvgTrendMethod == MovAvgTrendMethod."CROSSING" then 2 else 0;

def PlotCross = if Trend == 2 then yes else no;

def Closer = if TradeClosingMethod == TradeClosingMethod."SAR" then 1 else
             if TradeClosingMethod == TradeClosingMethod."MOVAVG" then 2 else 0;

#======== Moving Averages ======================================================================

plot TriggerAVG = MovingAverage(MovAvgType, close, TrendTriggerAvgLength);
TriggerAVG.SetLineWeight(3);
TriggerAVG.SetDefaultColor(Color.WHITE);

plot CrossingAVG = MovingAverage(MovAvgType, close, CrossingAvgLength);
CrossingAVG.SetHiding(!PlotCross);
CrossingAVG.SetLineWeight(3);
CrossingAVG.SetDefaultColor(Color.PINK);

#======== ParabolicSAR =========================================================================

Assert(accelerationFactor > 0, "'acceleration factor' must be positive: " + accelerationFactor);
Assert(accelerationLimit >= accelerationFactor, "'acceleration limit' (" + accelerationLimit + ") must be greater than or equal to 'acceleration factor' (" + accelerationFactor + ")");

def state = {default init, long, short};
def extreme;
def SAR;
def acc;

switch (state[1]) {
case init:
    state = state.long;
    acc = accelerationFactor;
    extreme = high;
    SAR = low;
case short:
    if (SAR[1] < high)
    then {
        state = state.long;
        acc = accelerationFactor;
        extreme = high + extremeoffset;
        SAR = extreme[1];
    } else {
        state = state.short;
        if (low < extreme[1])
        then {
            acc = Min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = low - extremeoffset;
        } else {
            acc = acc[1];
            extreme = extreme[1];
        }
        SAR = Max(Max(high, high[1]), SAR[1] + acc * (extreme - SAR[1]));
    }
case long:
    if (SAR[1] > low)
    then {
        state = state.short;
        acc = accelerationFactor;
        extreme = low - extremeoffset;
        SAR = extreme[1];
    } else {
        state = state.long;
        if (high > extreme[1])
        then {
            acc = Min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = high + extremeoffset;
        } else {
            acc = acc[1];
            extreme = extreme[1];
        }
        SAR = Min(Min(low, low[1]), SAR[1] + acc * (extreme - SAR[1]));
    }
}

#======== ADX ===========================================================================

input ADXlength = 14;
input averageType = AverageType.WILDERS;
input mom = 20;

def hiDiff = high - high[1];
def loDiff = low[1] - low;

def plusDM = if hiDiff > loDiff and hiDiff > 0 then hiDiff else 0;
def minusDM =  if loDiff > hiDiff and loDiff > 0 then loDiff else 0;

def ATR = MovingAverage(averageType, TrueRange(high, close, low), ADXlength);
def "DI+" = 100 * MovingAverage(averageType, plusDM, ADXlength) / ATR;
def "DI-" = 100 * MovingAverage(averageType, minusDM, ADXlength) / ATR;

def DX = if ("DI+" + "DI-" > 0) then 100 * AbsValue("DI+" - "DI-") / ("DI+" + "DI-") else 0;
def ADX = MovingAverage(averageType, DX, ADXlength);


#======== Logic =========================================================================
input adxLogic = yes;

def Logic = if adxLogic and ADX > mom

then 1

else if adxLogic and ADX < mom

then 2

else if !adxLogic

then 1

else Double.NaN;



#======== SIGNALS =========================================================================

def BuySignal = if Logic == 1 and Trend == 1 and (close > TriggerAVG ) and (SAR crosses below close)

then 1

else if Logic == 1 and Trend == 2 and (close > TriggerAVG) and (CrossingAVG > TriggerAVG) and (SAR crosses below close)

then 1

else if Logic == 2 and Trend == 1 and (close < TriggerAVG ) and (SAR crosses above close)

then 1

else if Logic == 2 and Trend == 2 and (close < TriggerAVG) and (CrossingAVG < TriggerAVG) and (SAR crosses above close)

then 1

else Double.NaN;


def SellSignal = if Logic == 1 and Trend == 1 and (close < TriggerAVG ) and (SAR crosses above close)

then 1

else if Logic == 1 and Trend == 2 and (close < TriggerAVG) and (CrossingAVG < TriggerAVG) and (SAR crosses above close)

then 1

else if Logic == 2 and Trend == 1 and (close > TriggerAVG ) and (SAR crosses below close)

then 1

else if Logic == 2 and Trend == 2 and (close > TriggerAVG) and (CrossingAVG > TriggerAVG) and (SAR crosses below close)

then 1

else Double.NaN;


def BuyExit = if Logic == 1 and Closer == 1 and (close crosses below SAR)

then 1

else if Logic == 1 and Closer == 2 and (close crosses below TriggerAVG)

then 1

else if Logic == 2 and Closer == 1 and (close crosses above SAR)

then 1

else if Logic == 2 and Closer == 2 and (close crosses above TriggerAVG)

then 1

else if Logic == 1 and Closer == 1 and (adx crosses mom)

then 1

else if Logic == 1 and Closer == 2 and (adx crosses mom)

then 1

else if Logic == 2 and Closer == 1 and (adx crosses mom)

then 1

else if Logic == 2 and Closer == 2 and (adx crosses mom)

then 1

else Double.NaN;


def SellExit = if Logic == 1 and Closer == 1 and (close crosses above SAR)

then 1

else if Logic == 1 and Closer == 2 and (close crosses above TriggerAVG)

then 1

else if Logic == 2 and Closer == 1 and (close crosses below SAR)

then 1

else if Logic == 2 and Closer == 2 and (close crosses below TriggerAVG)

then 1

else if Logic == 1 and Closer == 1 and (adx crosses mom)

then 1

else if Logic == 1 and Closer == 2 and (adx crosses mom)

then 1

else if Logic == 2 and Closer == 1 and (adx crosses mom)

then 1

else if Logic == 2 and Closer == 2 and (adx crosses mom)

then 1

else Double.NaN;


#======== STRATEGY ORDERS ===================================================================

def VolStrength = Lg(volume[1]) - Lg(SimpleMovingAvg(volume[1], 2000));
def HighPrice = high;
def LowPrice = low;


AddOrder(OrderType.BUY_TO_OPEN, BuySignal, open[-1], TradeSize, Color.GREEN, Color.GREEN, name = "Long L" + Logic + " T" + Trend);

AddOrder(OrderType.SELL_TO_CLOSE, BuyExit, open[-1], TradeSize, Color.RED, Color.RED, name = "Close L" + Logic + " C" + Closer);

AddOrder(OrderType.SELL_TO_OPEN, SellSignal, open[-1], TradeSize, Color.ORANGE, Color.ORANGE, name = "Short L" + Logic + " T" + Trend);

AddOrder(OrderType.BUY_TO_CLOSE, SellExit, open[-1], TradeSize, Color.WHITE, Color.WHITE, name = "Close L" + Logic + " C" + Closer);


#======== PLOTS ============================================================================

plot BullPSAR = if SAR < close then SAR else Double.NaN;
BullPSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
BullPSAR.SetDefaultColor(Color.LIME);

plot BearPSAR = if SAR > close then SAR else Double.NaN;
BearPSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
BearPSAR.SetDefaultColor(Color.PINK);

#---

def BullSignalAtCandle = Crosses(SAR, close, CrossingDirection.BELOW);
plot BullSignalAtPSAR = if close crosses above SAR
                     then SAR
                     else Double.NaN;
BullSignalAtPSAR.SetLineWeight(1);
BullSignalAtPSAR.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BullSignalAtPSAR.SetDefaultColor(Color.LIME);

def BearSignalAtCandle = Crosses(SAR, close, CrossingDirection.ABOVE);
plot BearSignalAtPSAR = if close crosses below SAR
                     then SAR
                     else Double.NaN;
BearSignalAtPSAR.SetLineWeight(1);
BearSignalAtPSAR.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
BearSignalAtPSAR.SetDefaultColor(Color.PINK);

#---

plot LongEntrySignal = if BuySignal then BuySignal else Double.NaN;
LongEntrySignal.SetDefaultColor(Color.UPTICK);
LongEntrySignal.SetLineWeight(5);
LongEntrySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);


plot ShortEntrySignal = if SellSignal then SellSignal else Double.NaN;
ShortEntrySignal.SetDefaultColor(Color.DOWNTICK);
ShortEntrySignal.SetLineWeight(5);
ShortEntrySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

plot LongExitSignal = if BuyExit then BuyExit else Double.NaN;
LongExitSignal.SetDefaultColor(Color.WHITE);
LongExitSignal.SetLineWeight(1);
LongExitSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);


plot ShortExitSignal = if SellExit then SellExit else Double.NaN;
ShortExitSignal.SetDefaultColor(Color.WHITE);
ShortExitSignal.SetLineWeight(1);
ShortExitSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

#======== ALERTS ===========================================================================

input AlertsOn = No;
Alert(AlertsOn and BullSignalAtCandle, "Bullish PSAR", Alert.BAR, Sound.Ring);
Alert(AlertsOn and BearSignalAtCandle, "Bearish PSAR", Alert.BAR, Sound.Ring);
Alert(AlertsOn and BuySignal, "Bullish PSAR above AVG", Alert.BAR, Sound.Ring);
Alert(AlertsOn and SellSignal, "Bullish PSAR below AVG", Alert.BAR, Sound.Ring);

#======== Paint Bars =======================================================================

input paintBars = No;
AssignPriceColor(if !paintBars
    then Color.CURRENT
    else if SAR < close
        then Color.GREEN
        else if  SAR > close
            then Color.RED
            else Color.CURRENT);

#======== Labels ===========================================================================

#Spacer
input blink = no;
input AdvLabelsOn = no;
input simpleLabels = no;

def NewBar = if close[1] != close[2] or high[1] != high[2] or low[1] != low[2] then yes else Double.NaN;
def Clock = if !IsNaN(NewBar) and Clock[1] == 1 then 0 else if !IsNaN(NewBar) and Clock[1] == 0 then 1 else Clock[1];

AddLabel(blink and AdvLabelsOn or SimpleLabels, "                                                                     ParabolicSAR Strategy Version 01.11                                                                                                     ", if Clock == 0 then Color.WHITE else Color.YELLOW);
AddLabel(!blink and AdvLabelsOn or SimpleLabels, "                                                                     ParabolicSAR Strategy Version 01.11                                                                                                     ",  Color.WHITE);


#Buy
AddLabel(AdvLabelsOn,
if  Logic == 1 and Trend == 1 and (close > TriggerAVG ) and (SAR crosses below close) within SarBars bars
or  Logic == 1 and Trend == 2 and (close > TriggerAVG) and (CrossingAVG > TriggerAVG) and (SAR crosses below close) within SarBars bars
or  Logic == 2 and Trend == 1 and (close < TriggerAVG) and (SAR crosses above close) within SarBars bars
or  Logic == 2 and Trend == 2 and (close < TriggerAVG) and (CrossingAVG < TriggerAVG) and (SAR crosses above close) within SarBars bars then "         " else "         ",

if  Logic == 1 and Trend == 1 and (close > TriggerAVG) and (SAR crosses below close) within SarBars bars
or  Logic == 1 and Trend == 2 and (close > TriggerAVG) and (CrossingAVG > TriggerAVG) and (SAR crosses below close) within SarBars bars
or  Logic == 2 and Trend == 1 and (close < TriggerAVG) and (SAR crosses above close) within SarBars bars
or  Logic == 2 and Trend == 2 and (close < TriggerAVG) and (CrossingAVG < TriggerAVG) and (SAR crosses above close) within SarBars bars then Color.GREEN else Color.WHITE);


#BuyClose
AddLabel(AdvLabelsOn,
if  Logic == 1 and Closer == 1 and (SAR > close) within SarBars bars
or  Logic == 1 and  Closer == 2 and (close < TriggerAVG) within SarBars bars
or  Logic == 2 and Closer == 1 and (SAR < close) within SarBars bars
or  Logic == 2 and Closer == 2 and (close > TriggerAVG)
or  Logic == 1 and Closer == 1 and (adx crosses above mom) within SarBars bars
or  Logic == 1 and Closer == 2 and (adx crosses above mom) within SarBars bars
or  Logic == 2 and Closer == 1 and (adx crosses below mom) within SarBars bars
or  Logic == 2 and Closer == 2 and (adx crosses below mom) within SarBars bars then "         " else "         ",

if  Logic == 1 and Closer == 1 and (SAR > close) within SarBars bars
or  Logic == 1 and  Closer == 2 and (close < TriggerAVG) within SarBars bars
or  Logic == 2 and Closer == 1 and (SAR < close) within SarBars bars
or  Logic == 2 and Closer == 2 and (close > TriggerAVG)
or  Logic == 1 and Closer == 1 and (adx crosses above mom) within SarBars bars
or  Logic == 1 and Closer == 2 and (adx crosses above mom) within SarBars bars
or  Logic == 2 and Closer == 1 and (adx crosses below mom) within SarBars bars
or  Logic == 2 and Closer == 2 and (adx crosses below mom) within SarBars bars then Color.MAGENTA else Color.WHITE);

#Sell
AddLabel(AdvLabelsOn,
if  Logic == 1 and Trend == 1 and (close < TriggerAVG) and (SAR crosses above close) within SarBars bars
or  Logic == 1 and Trend == 2 and (close < TriggerAVG) and (CrossingAVG < TriggerAVG) and (SAR crosses above close) within SarBars bars
or Logic == 2 and Trend == 1 and (close > TriggerAVG) and (SAR crosses below close) within SarBars bars
or  Logic == 2 and Trend == 2 and (close > TriggerAVG) and (CrossingAVG > TriggerAVG) and (SAR crosses below close) within SarBars bars then "         " else "         ",

if  Logic == 1 and Trend == 1 and (close < TriggerAVG) and (SAR crosses above close) within SarBars bars
or  Logic == 1 and Trend == 2 and (close < TriggerAVG) and (CrossingAVG < TriggerAVG) and (SAR crosses above close) within SarBars bars
or  Logic == 2 and Trend == 1 and (close > TriggerAVG) and (SAR crosses below close) within SarBars bars
or  Logic == 2 and Trend == 2 and (close > TriggerAVG) and (CrossingAVG > TriggerAVG) and (SAR crosses below close) within SarBars bars then Color.RED else Color.WHITE);


#SellClose
AddLabel(AdvLabelsOn,
if  Logic == 1 and Closer == 1 and (SAR < close) within SarBars bars
or  Logic == 1 and Closer == 2 and (close > TriggerAVG) within SarBars bars
or  Logic == 2 and Closer == 1 and (SAR > close) within SarBars bars
or  Logic == 2 and Closer == 2 and (close < TriggerAVG)
or  Logic == 1 and Closer == 1 and (adx crosses below mom) within SarBars bars
or  Logic == 1 and Closer == 2 and (adx crosses below mom) within SarBars bars
or  Logic == 2 and Closer == 1 and (adx crosses above mom) within SarBars bars
or  Logic == 2 and Closer == 2 and (adx crosses above mom) within SarBars bars then "         " else "         ",

if  Logic == 1 and Closer == 1 and (SAR < close) within SarBars bars
or  Logic == 1 and Closer == 2 and (close > TriggerAVG) within SarBars bars
or Logic == 2 and Closer == 1 and (SAR> close) within SarBars bars
or  Logic == 2 and Closer == 2 and (close < TriggerAVG)
or  Logic == 1 and Closer == 1 and (adx crosses below mom) within SarBars bars
or  Logic == 1 and Closer == 2 and (adx crosses below mom) within SarBars bars
or  Logic == 2 and Closer == 1 and (adx crosses above mom) within SarBars bars
or  Logic == 2 and Closer == 2 and (adx crosses above mom) within SarBars bars then Color.LIGHT_GREEN else Color.WHITE);




AddLabel (simpleLabels, if BuySignal within SarBars bars then  "         " else   "         ", if BuySignal >= 1 within SarBars bars then Color.GREEN else Color.WHITE);
AddLabel (simpleLabels, if BuyExit within SarBars bars then  "         " else   "         ", if BuyExit >= 1 within SarBars bars then Color.MAGENTA else Color.WHITE);
AddLabel (simpleLabels, if SellSignal within SarBars bars then  "         " else   "         ", if SellSignal >= 1 within SarBars bars then Color.RED else Color.WHITE);
AddLabel (simpleLabels, if SellExit within SarBars bars then  "         " else   "         ", if SellExit >= 1 within SarBars bars then Color.LIGHT_GREEN else Color.WHITE);



AddLabel (AdvLabelsOn or simpleLabels,
if Logic == 2 then "  Modified Logic  " else "   Normal Logic   ", Color.WHITE);


#======== EOF =========================================================================
what are the possibilities to add Stop loss option to this strategy ? I had one strategy with stop loss(though it is crap not good results, I can share the code in case it is needed to add stop loss option)
 
I use two indicators, TTM Squeeze and Kang indicator in this forum. My results are 80% winning trades
Thanks. You mind sharing how you enter and exit trades. What are your parameters for entry and exit? Is your 80% win ratio is only with TTM + Kang indicator or in combination with the parabolic SAR strategy in this forum.
 
Day 3 of Parabolic MA report :
(didn't record day 1 and 2 )

TOok 4 trades with this indicator

1st trade was amd




Bought AMD 75.19 SL 74.57 PT 75.87/76.54/77.21

sold ten at 75.21 ( had a closing but didn't hit stop loss so adjusted to below that red candle a little. sold twenty at 75.3 eventually



2nd trade was TSLA

Bought 30 139.4 Sold 20 at 138.62 sold 10 at 140.95


When I took the trade , if I were to set the stop loss to the previous candle , I would have to sell straight away ...

So I split it into 2 logical zones , one slightly below the Moving average , and another slightly below the previous low.

20 got stopped out while the last 10
 
3rd trade was NFLX
I was about to call it a day when I heard another signal

Bought 30 @ 354.35 sold 10 at 355.36 , moved SL As i was deciding where the SL is and the price keeps dropping , I was hesitant the moment it turned green , I just hit the buy with a stop loss larger than the amount I have drawn in sold 2 at 2 TP quickly after that and manually adjusted S/L with the active trader ladder





for the 3rd TP , I manually dragged the OCO up a few times.


I dragged it a few times and flattened the trade when I see the close.
 
Improved/Corrected Entry and Exit orders. Works well on mobile too

CSS:
# 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/

# ParabolicSAR_withAlerts_JQ
# 2018-04-15 Mods by Johnny Quotron
#    with a very helpful kickstart from DMonkey
# Mods include
#    1. splitting the PSAR into two visible plots so that they can be colored seperately
#    2. adding alert arrows at the PSAR to enhance visibility
#        a. original alert arrows remain available but are hidden by default
#    3. add ability to color color alert arrows
#

# Combined/Modified/Altered by SilverWolf


declare upper;

#======== Inputs ==============================================================================



input accelerationFactor = 0.012;
input accelerationLimit = 0.2;
input extremeoffset = 0.0;

input MovAvgType = AverageType.EXPONENTIAL;
input MovAvgTrendMethod = {default "SINGLE", "CROSSING"};
input CrossingAvgLength = 9;
input TrendTriggerAvgLength = 21;

input TradeClosingMethod = {default "SAR", "MOVAVG"};
input TradeSize = 1;

def Trend = if MovAvgTrendMethod == MovAvgTrendMethod."SINGLE" then 1 else
            if MovAvgTrendMethod == MovAvgTrendMethod."CROSSING" then 2 else 0;

def PlotCross = if Trend == 2 then yes else no;

def Closer = if TradeClosingMethod == TradeClosingMethod."SAR" then 1 else
             if TradeClosingMethod == TradeClosingMethod."MOVAVG" then 2 else 0;




#======== Moving Averages ======================================================================

plot TriggerAVG = MovingAverage(MovAvgType, close, TrendTriggerAvgLength);
TriggerAVG.SetLineWeight(3);
TriggerAVG.SetDefaultColor(Color.WHITE);

plot CrossingAVG = MovingAverage(MovAvgType, close, CrossingAvgLength);
CrossingAVG.SetHiding(!PlotCross);
CrossingAVG.SetLineWeight(3);
CrossingAVG.SetDefaultColor(Color.PINK);

#======== ParabolicSAR =========================================================================

Assert(accelerationFactor > 0, "'acceleration factor' must be positive: " + accelerationFactor);
Assert(accelerationLimit >= accelerationFactor, "'acceleration limit' (" + accelerationLimit + ") must be greater than or equal to 'acceleration factor' (" + accelerationFactor + ")");

def state = {default init, long, short};
def extreme;
def SAR;
def acc;

switch (state[1]) {
case init:
    state = state.long;
    acc = accelerationFactor;
    extreme = high;
    SAR = low;
case short:
    if (SAR[1] < high)
    then {
        state = state.long;
        acc = accelerationFactor;
        extreme = high + extremeoffset;
        SAR = extreme[1];
    } else {
        state = state.short;
        if (low < extreme[1])
        then {
            acc = Min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = low - extremeoffset;
        } else {
            acc = acc[1];
            extreme = extreme[1];
        }
        SAR = Max(Max(high, high[1]), SAR[1] + acc * (extreme - SAR[1]));
    }
case long:
    if (SAR[1] > low)
    then {
        state = state.short;
        acc = accelerationFactor;
        extreme = low - extremeoffset;
        SAR = extreme[1];
    } else {
        state = state.long;
        if (high > extreme[1])
        then {
            acc = Min(acc[1] + accelerationFactor, accelerationLimit);
            extreme = high + extremeoffset;
        } else {
            acc = acc[1];
            extreme = extreme[1];
        }
        SAR = Min(Min(low, low[1]), SAR[1] + acc * (extreme - SAR[1]));
    }
}


#======== SIGNALS =========================================================================




def BuySignal = if Trend == 1 and (close > TriggerAVG ) and (SAR crosses below close)

then 1

else if Trend == 2 and (close > TriggerAVG) and (CrossingAVG crosses above TriggerAVG) and (SAR < close)

then 1
else Double.NaN;


def SellSignal = if Trend == 1 and (close < TriggerAVG ) and (SAR crosses above close)

then 1

else if Trend == 2 and (close < TriggerAVG) and (CrossingAVG crosses below TriggerAVG) and (SAR > close)

then 1

else Double.NaN;


def BuyExit = if Closer == 1 and (close crosses below SAR[-1])

then 1

else if Closer == 2 and (TriggerAVG > CrossingAVG)

then 1

else Double.NaN;


def SellExit = if Closer == 1 and (close crosses above SAR[-1])

then 1

else if Closer == 2 and (TriggerAVG < CrossingAVG)

then 1

else Double.NaN;


#======== STRATEGY ORDERS ===================================================================
AddOrder(OrderType.BUY_TO_OPEN, BuySignal, open[-1], TradeSize, Color.GREEN, Color.GREEN, name = "Long");
AddOrder(OrderType.SELL_TO_CLOSE, BuyExit, open[-1], TradeSize, Color.RED, Color.RED, name = "Close");
AddOrder(OrderType.SELL_TO_OPEN, SellSignal, open[-1], TradeSize, Color.ORANGE, Color.ORANGE, name = "Short");
AddOrder(OrderType.BUY_TO_CLOSE, SellExit, open[-1], TradeSize, Color.WHITE, Color.WHITE, name = "Close");
#======== PLOTS ============================================================================

plot BullPSAR = if SAR < close then SAR else Double.NaN;
BullPSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
BullPSAR.SetDefaultColor(Color.LIME);

plot BearPSAR = if SAR > close then SAR else Double.NaN;
BearPSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
BearPSAR.SetDefaultColor(Color.PINK);

#---

def BullSignalAtCandle = Crosses(SAR, close, CrossingDirection.BELOW);
plot BullSignalAtPSAR = if close crosses above SAR
                     then SAR
                     else Double.NaN;
BullSignalAtPSAR.SetLineWeight(1);
BullSignalAtPSAR.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BullSignalAtPSAR.SetDefaultColor(Color.LIME);

def BearSignalAtCandle = Crosses(SAR, close, CrossingDirection.ABOVE);
plot BearSignalAtPSAR = if close crosses below SAR
                     then SAR
                     else Double.NaN;
BearSignalAtPSAR.SetLineWeight(1);
BearSignalAtPSAR.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
BearSignalAtPSAR.SetDefaultColor(Color.PINK);

#---

plot LongEntrySignal = if BuySignal then BuySignal else Double.NaN;
LongEntrySignal.SetDefaultColor(Color.UPTICK);
LongEntrySignal.SetLineWeight(5);
LongEntrySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);


plot ShortEntrySignal = if SellSignal then SellSignal else Double.NaN;
ShortEntrySignal.SetDefaultColor(Color.DOWNTICK);
ShortEntrySignal.SetLineWeight(5);
ShortEntrySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

plot LongExitSignal = if BuyExit then BuyExit else Double.NaN;
LongExitSignal.SetDefaultColor(Color.White);
LongExitSignal.SetLineWeight(1);
LongExitSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);


plot ShortExitSignal = if SellExit then SellExit else Double.NaN;
ShortExitSignal.SetDefaultColor(Color.White);
ShortExitSignal.SetLineWeight(1);
ShortExitSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);


#======== ALERTS ===========================================================================

input AlertsOn = No;
Alert(AlertsOn and BullSignalAtCandle, "Bullish PSAR", Alert.BAR, Sound.Ring);
Alert(AlertsOn and BearSignalAtCandle, "Bearish PSAR", Alert.BAR, Sound.Ring);
Alert(AlertsOn and BuySignal, "Bullish PSAR above AVG", Alert.BAR, Sound.Ring);
Alert(AlertsOn and SellSignal, "Bullish PSAR below AVG", Alert.BAR, Sound.Ring);

#======== EOF ===========================================================================
Yes, I'm testing this version
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
359 Online
Create Post

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