Parabolic SAR PSAR Moving Average Trading Strategy

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);
 
Solution
plot FastMA20 = Average(price[-displace], fastLength);
plot SlowMA40 = Average(price[-displace], fastLength);


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;

First things first, your fast and slow averages are defined exactly the same, so...
plot FastMA20 = Average(price[-displace], fastLength);
plot SlowMA40 = Average(price[-displace], fastLength);


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;

First things first, your fast and slow averages are defined exactly the same, so they will never cross. Change the SlowMA40 to use slowLength.

Second, your buy and sell signals are based on both the moving averages crossing and the SAR crossing below price on the same bar. The likelihood of that happening is probably pretty low so you'll have few to no signals.

Try this instead

def BuySignal = if FastMA20 > SlowMA40 and ParabolicSAR().SAR crosses below price
then 1
else Double.NaN;
def SellSignal = if FastMA20 < SlowMA40 and ParabolicSAR().SAR crosses above price
then 1
else Double.NaN;
 
Solution
Hi, I am asking for some assistance with the conversion of the following TOS ParabolicSAR study into a TOS strategy for backtesting.

I have attempted to do this myself but am not very good at coding and I am not sure if this type of study can be converted or should it be broken into two studies one for the Bullish side and another for the Bearish. Any help will be greatly appreciated.



Thank You,

Vince Lenarcic



#

# TD Ameritrade IP Company, Inc. (c) 2009-2022

#



#wizard input: crossingType

#wizard text: Inputs: acceleration factor:

#wizard input: accelerationFactor

#wizard text: acceleration limit:

#wizard input: accelerationLimit



input accelerationFactor = 0.02;

input accelerationLimit = 0.2;

input crossingType = {default Bearish, Bullish};



def sar = ParabolicSAR(accelerationFactor=accelerationFactor, accelerationLimit=accelerationLimit);



plot signal = crosses(sar, close, CrossingType == CrossingType.Bearish);



signal.DefineColor("Bullish", GetColor(5));

signal.DefineColor("Bearish", GetColor(6));

signal.AssignValueColor(if crossingType == CrossingType.Bullish then signal.color("Bullish") else signal.color("Bearish"));



signal.SetPaintingStrategy(if crossingType == CrossingType.bullish

then PaintingStrategy.BOOLEAN_ARROW_UP

else PaintingStrategy.BOOLEAN_ARROW_DOWN);
 
Hi, I am asking for some assistance with the conversion of the following TOS ParabolicSAR study into a TOS strategy for backtesting.

I have attempted to do this myself but am not very good at coding and I am not sure if this type of study can be converted or should it be broken into two studies one for the Bullish side and another for the Bearish. Any help will be greatly appreciated.
https://usethinkscript.com/threads/parabolic-sar-moving-average-trading-strategy.9533/
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
449 Online
Create Post

Similar threads

Similar threads

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