SilverWolf
Member
Sharing a script I modified to suit my trading. Works well and has options to adjust with market conditions. Little here little there Im no pro coder.. more like a script kid. If you have amendments changes improvements go for it. I currently trade ES. Tick charts I use intuition but usually between 150-600t I then adjust the moving average.. "what are the candles responding to" then minor tweak on the acceleration factor. I saved it here with general ES settings on a 500t chart.
I did my best to make it work on mobile app. You may have to change "signalsAtPSAR" from "Line" to "Arrow"... Should show up on mobile after that. I put in the option of using a single moving average like the 100 or use a crossing moving average to determine trend direction.
https://usethinkscript.com/threads/parabolic-sar-moving-average-trading-strategy.9533/
https://usethinkscript.com/threads/...-labels-charts-for-thinkorswim.515/post-29279
I did my best to make it work on mobile app. You may have to change "signalsAtPSAR" from "Line" to "Arrow"... Should show up on mobile after that. I put in the option of using a single moving average like the 100 or use a crossing moving average to determine trend direction.
https://usethinkscript.com/threads/parabolic-sar-moving-average-trading-strategy.9533/
https://usethinkscript.com/threads/...-labels-charts-for-thinkorswim.515/post-29279
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 TradeSize = 1;
input accelerationFactor = 0.012;
input accelerationLimit = 0.2;
input extremeoffset = 0.0;
input price = close;
input AvgType = AverageType.EXPONENTIAL;
input MovAvgTrendMethod = {default "SINGLE", "CROSSING"}; #('Crossing Avgs or Single Avg')
input CrossingLength = 19;
input TrendTriggerAvgLength = 40;
def Trend = if MovAvgTrendMethod == MovAvgTrendMethod."SINGLE" then 1 else
if MovAvgTrendMethod == MovAvgTrendMethod."CROSSING" then 2 else 0;
#======== Moving Averages ======================================================================
plot TriggerAVG = MovingAverage(AvgType, close, TrendTriggerAvgLength);
TriggerAVG.SetDefaultColor(Color.white);
plot CrossingAVG = MovingAverage(AvgType, close, CrossingLength);
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]));
}
}
#def PSAR = ParabolicSAR(accelerationFactor = accelerationFactor, accelerationLimit = accelerationLimit);
#======== SIGNALS =========================================================================
def BuySignal = if Trend == 1 and (close > TriggerAVG ) and (SAR crosses below close)
then 1
else if Trend == 2 and (close > TriggerAVG) and (TriggerAVG < CrossingAVG) and (SAR crosses below 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 (TriggerAVG > CrossingAVG) and (SAR crosses above close)
then 1
else Double.NaN;
#======== STRATEGY ORDERS ===================================================================
# if the buy signal was set, then set the stop loss to previous candle's low.
def buyStopLoss = if IsNaN(close) then buyStopLoss[1] else if !IsNaN(BuySignal) == 1 then low[1] else buyStopLoss[1];
#plot xbuystoploss = buyStopLoss; #Remember to hide on mobile app
#xbuystoploss.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
# if the sell signal was set, then set the stop loss to previous candle's high.
def sellStopLoss = if IsNaN(close) then sellStopLoss[1] else if !IsNaN(SellSignal) then high[1] else sellStopLoss[1];
#plot xsellloss = sellStopLoss; #Remember to hide on mobile app
#xsellloss.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
def BuyExit = if (SAR crosses above price)# or (close <= buyStopLoss)
then 1
else Double.NaN;
def SellExit = if (SAR crosses below price)# or (close crosses above sellStopLoss)
then 1
else Double.NaN;
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, name = "Buy Long");
AddOrder(OrderType.SELL_TO_CLOSE, LongExt, open[1], TradeSize, Color.RED, Color.RED, name = "Close Long");
AddOrder(OrderType.SELL_TO_OPEN, ShortEnt, open[-1], TradeSize, Color.ORANGE, Color.ORANGE, name = "Open Short");
AddOrder(OrderType.BUY_TO_CLOSE, ShortExt, open[-1], TradeSize, Color.WHITE, Color.WHITE, name = "Close Short");
#======== 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 LongEnt then LongEnt else Double.NaN;
LongEntrySignal.SetDefaultColor(Color.UPTICK);
LongEntrySignal.SetLineWeight(5);
LongEntrySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
plot ShortEntrySignal = if ShortEnt then ShortEnt else Double.NaN;
ShortEntrySignal.SetDefaultColor(Color.DOWNTICK);
ShortEntrySignal.SetLineWeight(5);
ShortEntrySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
#======== ALERTS ===========================================================================
Alert(BullSignalAtCandle, "Bullish PSAR", Alert.BAR, Sound.Ring);
Alert(BearSignalAtCandle, "Bearish PSAR", Alert.BAR, Sound.Ring);
Alert(BuySignal, "Bullish PSAR above AVG", Alert.BAR, Sound.Ring);
Alert(SellSignal, "Bullish PSAR below AVG", Alert.BAR, Sound.Ring);
#======== EOF =============================================================================
Last edited by a moderator: