MACD PSAR indicator?

METAL

Well-known member
Plus
I am attempting to write a strategy/indicator that will only signal when:
1) MACD value line crosses over average line when below zero line. 2) Parabolic SAR becomes uptrend. Plot a sell signal arrow only when Average crosses over Value and only when this occurs when above the zero line and Parabolic Sar is in downtrend or starts to plot a downtrend. Make sure the signals do not occur unless all conditions are met. For example: If the MACD Value crosses over the average line and it is below zero but Parabolic Sar has not plotted an uptrend then no signal should be plotted however, if the parabolic sar plots an uptrend after the macd lines cross then a signal shall be plotted within a certain number of bars. This applies to all conditions

Code:
# Define the input parameters
input macdFastLength = 12;
input macdSlowLength = 26;
input macdSignalSmoothing = 9;
input sarAccelerationFactor = 0.02;
input sarMaxAccelerationFactor = 0.2;
input sarStep = 0.02;
input parabolicSARBarsToWait = 3;

# Calculate MACD and Parabolic SAR
def macd = MACD(macdFastLength, macdSlowLength, macdSignalSmoothing);
def macdValue = macd - macd[1];
def macdAvg = ExpAverage(macdValue, macdSignalSmoothing);
def sar = ParabolicSAR(sarAccelerationFactor, sarMaxAccelerationFactor, sarStep);

# Define conditions for buy and sell signals
def buySignalCondition = macdValue > 0 && sar[1] < close && sar > close;
def sellSignalCondition = macdValue < 0 && sar[1] > close && sar < close;

# Plot buy and sell arrows when conditions are met
plot buySignal = if buySignalCondition and HighestAll(buySignalCondition[-parabolicSARBarsToWait]) then low else double.nan;
buySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buySignal.SetDefaultColor(Color.GREEN);

plot sellSignal = if sellSignalCondition and HighestAll(sellSignalCondition[-parabolicSARBarsToWait]) then high else double.nan;
sellSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sellSignal.SetDefaultColor(Color.RED);

1699718115464.png
 
Solution
That did not work. The error I get is: Invalid parameter at position 2 at 14:11
which is this line:
def sar = ParabolicSAR(sarAccelerationFactor, sarMaxAccelerationFactor, sarStep);
and ParabolicSAR is what is highlighted.

These are causing problems. They are fixed below.

With the SAR def, you cannot add a parameter, sarstep, to a referenced indicator that is not part of the original.
def sar = ParabolicSAR(sarAccelerationFactor, sarMaxAccelerationFactor, sarStep);

With the macd def, it's default plot is value, so I deleted def macd and made def macdvalue
def macd = MACD(macdFastLength, macdSlowLength, macdSignalSmoothing);

Here is the revised code:

Code:
# Define the input...
I will give that a try.
That did not work. The error I get is: Invalid parameter at position 2 at 14:11
which is this line:
def sar = ParabolicSAR(sarAccelerationFactor, sarMaxAccelerationFactor, sarStep);
and ParabolicSAR is what is highlighted.
 
That did not work. The error I get is: Invalid parameter at position 2 at 14:11
which is this line:
def sar = ParabolicSAR(sarAccelerationFactor, sarMaxAccelerationFactor, sarStep);
and ParabolicSAR is what is highlighted.

These are causing problems. They are fixed below.

With the SAR def, you cannot add a parameter, sarstep, to a referenced indicator that is not part of the original.
def sar = ParabolicSAR(sarAccelerationFactor, sarMaxAccelerationFactor, sarStep);

With the macd def, it's default plot is value, so I deleted def macd and made def macdvalue
def macd = MACD(macdFastLength, macdSlowLength, macdSignalSmoothing);

Here is the revised code:

Code:
# Define the input parameters
input macdFastLength = 12;
input macdSlowLength = 26;
input macdSignalSmoothing = 9;
input sarAccelerationFactor = 0.02;
input sarMaxAccelerationFactor = 0.2;
input sarStep = 0.02;
input parabolicSARBarsToWait = 3;

# Calculate MACD and Parabolic SAR
def macdValue = reference MACD(macdFastLength, macdSlowLength, macdSignalSmoothing).value;
def macdAvg = ExpAverage(macdvalue, macdSignalSmoothing);
def sar = reference ParabolicSAR(sarAccelerationFactor, sarMaxAccelerationFactor);

# Define conditions for buy and sell signals
def buySignalCondition = macdValue > 0 && sar[1] < close && sar > close;
def sellSignalCondition = macdValue < 0 && sar[1] > close && sar < close;

# Plot buy and sell arrows when conditions are met
plot buySignal = if buySignalCondition and HighestAll(buySignalCondition[-parabolicSARBarsToWait]) then low else Double.NaN;
buySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buySignal.SetDefaultColor(Color.GREEN);

plot sellSignal = if sellSignalCondition and HighestAll(sellSignalCondition[-parabolicSARBarsToWait]) then high else Double.NaN;
sellSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sellSignal.SetDefaultColor(Color.RED);
 
Last edited:
Solution
These are causing problems. They are fixed below.

With the SAR def, you cannot add a parameter, sarstep, to a referenced indicator that is not part of the original.
def sar = ParabolicSAR(sarAccelerationFactor, sarMaxAccelerationFactor, sarStep);

With the macd def, it's default plot is value, so I deleted def macd and made def macdvalue
def macd = MACD(macdFastLength, macdSlowLength, macdSignalSmoothing);

Here is the revised code:
Thank you SleepyZ. You are awesome!
 
Thank you SleepyZ. You are awesome!
@SleepyZ, I believe my coding is wrong anyway. What I am wanting to achieve is to get a buy signal when macd crosses below zero line and parabolic SAR is already showing uptrend or when it shows uptrend. and the opposite for sell signal. For example: If MACD Value line crosses above Average line when below zero line and Parabolic SAR has not potted an uptrend "dot" then once or if a dot is plotted, then a signal will occur. Signal should occur as soon as the cross happens if the SAR is already showing dots. Can you help me to fix it?
1699921580174.png
 
@SleepyZ , Never mind. I believe I got it. Take a look in any case you may have more to add. Let me know what you think. It seems pretty good. Does miss some moves due to the MACD cross location but is pretty decent. I added the option to use zero line parameter or not.
Actually, It isn't signaling if the SAR has not already started. Still working on it.
Code:
#Fixed by @SleepyZ
# Define the input parameters
input macdFastLength = 12;
input macdSlowLength = 26;
input macdSignalSmoothing = 9;
input sarAccelerationFactor = 0.02;
input sarMaxAccelerationFactor = 0.2;
input sarStep = 0.02;
input useZeroLine = yes;

# Calculate MACD and Parabolic SAR
def macdValue = MACD(macdFastLength, macdSlowLength, macdSignalSmoothing).Value;
def macdAvg = ExpAverage(macdValue, macdSignalSmoothing);
def sar = reference ParabolicSAR(sarAccelerationFactor, sarMaxAccelerationFactor);

# Define conditions for buy and sell signals
def sarUptrend = sar > sar[1];
def sarDowntrend = sar < sar[1];

# Buy signal: MACD Value line crosses above MACD Average line, and SAR is uptrending or starting to uptrend
def buySignalCondition;
if (useZeroLine) {
    buySignalCondition = macdValue > macdAvg && macdValue[1] < macdAvg[1] && macdValue < 0 && (sarUptrend);
} else {
    buySignalCondition = macdValue > macdAvg && macdValue[1] < macdAvg[1] && sarUptrend;
}

# Sell signal: MACD Average line crosses above MACD Value line, and SAR is downtrending or starting to downtrend
def sellSignalCondition;
if (useZeroLine) {
    sellSignalCondition = macdAvg > macdValue && macdAvg[1] < macdValue[1] && macdAvg > 0 && (sarDowntrend);
} else {
    sellSignalCondition = macdAvg > macdValue && macdAvg[1] < macdValue[1] && sarDowntrend;
}

# Plot buy and sell signals
plot buySignal = if buySignalCondition then low else Double.NaN;
buySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buySignal.SetDefaultColor(Color.GREEN);

plot sellSignal = if sellSignalCondition then high else Double.NaN;
sellSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sellSignal.SetDefaultColor(Color.RED);
 
@SleepyZ or anyone that knows how to code, Can you please help with correcting a part of this code that isn't working correctly. At the moment it isn't plotting a signal if the SAR comes after the MACD cross. I would like for it to do that but within a certain number of bars. I may add in something else to help cut out some of the chop areas. Please feel free to add something in order to accomplish weeding out chop. BTW, This works pretty darn well. Have you looked at it? It may be worth a back test to see how profitable it may be. I am testing on a 2min chart and I have changed the MACD to 13,48, 7. I am in the process of testing the SAR options.
 
I am attempting to write a strategy/indicator that will only signal when:
1) MACD value line crosses over average line when below zero line. 2) Parabolic SAR becomes uptrend. Plot a sell signal arrow only when Average crosses over Value and only when this occurs when above the zero line and Parabolic Sar is in downtrend or starts to plot a downtrend. Make sure the signals do not occur unless all conditions are met. For example: If the MACD Value crosses over the average line and it is below zero but Parabolic Sar has not plotted an uptrend then no signal should be plotted however, if the parabolic sar plots an uptrend after the macd lines cross then a signal shall be plotted within a certain number of bars. This applies to all conditions

Code:
# Define the input parameters
input macdFastLength = 12;
input macdSlowLength = 26;
input macdSignalSmoothing = 9;
input sarAccelerationFactor = 0.02;
input sarMaxAccelerationFactor = 0.2;
input sarStep = 0.02;
input parabolicSARBarsToWait = 3;

# Calculate MACD and Parabolic SAR
def macd = MACD(macdFastLength, macdSlowLength, macdSignalSmoothing);
def macdValue = macd - macd[1];
def macdAvg = ExpAverage(macdValue, macdSignalSmoothing);
def sar = ParabolicSAR(sarAccelerationFactor, sarMaxAccelerationFactor, sarStep);

# Define conditions for buy and sell signals
def buySignalCondition = macdValue > 0 && sar[1] < close && sar > close;
def sellSignalCondition = macdValue < 0 && sar[1] > close && sar < close;

# Plot buy and sell arrows when conditions are met
plot buySignal = if buySignalCondition and HighestAll(buySignalCondition[-parabolicSARBarsToWait]) then low else double.nan;
buySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buySignal.SetDefaultColor(Color.GREEN);

plot sellSignal = if sellSignalCondition and HighestAll(sellSignalCondition[-parabolicSARBarsToWait]) then high else double.nan;
sellSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sellSignal.SetDefaultColor(Color.RED);

thoughts on post #1

things are easier if rules are complete, consistent, and use the proper words.
cross over is not clear. is it a cross above or below?
buy and sell rules have different wordings
after the rules, in an example, you list another rule criteria, which is unclear, and not sure if it is a buy rule or sell rule.
do you wish to allow all 3 rules to trigger within x bars of each other? no matter which come first? or just SAR?

plots have a formula that specifies a price, but are PaintingStrategy( boolean )
if plotting boolean shapes, then the plot formula is a true/false.

--------------------

i redid the study and added 2 variables,
input extend_signals = 4;
input enable_macd_above_below_0 = yes;

the first one extends a signal 3 bars, so if a signal is 3 bars after another, the master signal should trigger.

the 2nd one allows the middle rule, macd lines above/below zero, to be enables/disabled. to me it seems you miss out on trades by using this rule.

it uses sum() to 'extend' each signal by x bars.
the first time all 3 signals are true , an arrow is drawn.

Code:
#macd_psar_seq_00

#https://usethinkscript.com/threads/macd-psar-indicator.17142/
#MACD PSAR indicator?
#METAL  11/11
##1
#I am attempting to write a strategy/indicator that will only signal when:

# extend each signal by x bars
# buy
#  MACD value line crosses above average line, 
#  and macd lines are below zero line,
#  and Parabolic SAR is in uptrend.

# sell
#  MACD value line crosses below average line, 
#  and macd lines are above zero line,
#  and Parabolic Sar is in downtrend.

# For example: If the MACD Value crosses over the average line and it is below zero but Parabolic Sar has not plotted an uptrend then no signal should be plotted 
# however, if the parabolic sar plots an uptrend after the macd lines cross then a signal shall be plotted within a certain number of bars. This applies to all conditions


# Define the input parameters
input macdFastLength = 12;
input macdSlowLength = 26;
input macdSignalSmoothing = 9;

input sarAccelerationFactor = 0.02;
input sarMaxAccelerationFactor = 0.2;
input sarStep = 0.02;
input useZeroLine = yes;

# extend signals by x bars
input extend_signals = 4;
input enable_macd_above_below_0 = yes;

# Calculate MACD and Parabolic SAR
def macdValue = MACD(macdFastLength, macdSlowLength, macdSignalSmoothing).Value;
def macdAvg = ExpAverage(macdValue, macdSignalSmoothing);
def sar = reference ParabolicSAR(sarAccelerationFactor, sarMaxAccelerationFactor);

# Define conditions for buy and sell signals
# buy
#  MACD value line crosses above average line, 
#  and macd lines are below zero line,
#  and Parabolic SAR is in uptrend.

# sell
#  MACD value line crosses below average line, 
#  and macd lines are above zero line,
#  and Parabolic Sar is in downtrend.

# buy rules
def crossa = (macdValue crosses above macdAvg);
def mb = if enable_macd_above_below_0 then (macdValue < 0) else 1;
def sarup = sar > sar[1];
# sell rules
def crossb = (macdValue crosses below macdAvg);
def ma = if enable_macd_above_below_0 then (macdValue > 0) else 1;
def sardwn = sar < sar[1];


# extend rules
# buy
def xcrossa = sum(crossa, extend_signals);
def xmb = sum(mb, extend_signals);
def xsarup = sum(sarup, extend_signals);
# sell
def xcrossb = sum(crossb, extend_signals);
def xma = sum(ma, extend_signals);
def xsardwn = sum(sardwn, extend_signals);

# true every bar when all 3 are true
def buy1 = (xcrossa > 0 and xmb > 0 and xsarup > 0);
def sell1 = (xcrossb > 0 and xma > 0 and xsardwn > 0);

# true just on the first bar that all 3 are true
def buy2 = (!buy1[1] and buy1);
def sell2 = (!sell1[1] and sell1);


# Plot buy and sell signals
plot zbuy = if buy2 then low*0.992 else Double.NaN;
zbuy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zbuy.SetDefaultColor(Color.GREEN);
zbuy.setlineweight(4);
zbuy.hidebubble();

plot zsell = if sell2 then high*1.008 else Double.NaN;
zsell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zsell.SetDefaultColor(Color.RED);
zsell.setlineweight(4);
zsell.hidebubble();

#-------------------------

input test1_buy = no;
addchartbubble(test1_buy, low*0.97,
xcrossa + "\n" +
xmb + "\n" +
xsarup + "\n"
, color.yellow, no);

#
 
weeding out chop.

could add a formula to see if the absvalue change in sar is > some % of sar.
if it is too small , then choppy.

add this to the end of study to see what the % change numbers are

Code:
def sar_chg_per = round(100*((sar - sar[1])/sar),3);
input sar_per_limit = 0.25;

input sar_per = no;
addchartbubble(sar_per, low*0.97,
sar_chg_per
, (if absvalue(sar_chg_per) >= sar_per_limit then color.yellow else color.gray), no);
 
thoughts on post #1

things are easier if rules are complete, consistent, and use the proper words.
cross over is not clear. is it a cross above or below?
buy and sell rules have different wordings
after the rules, in an example, you list another rule criteria, which is unclear, and not sure if it is a buy rule or sell rule.
do you wish to allow all 3 rules to trigger within x bars of each other? no matter which come first? or just SAR?

plots have a formula that specifies a price, but are PaintingStrategy( boolean )
if plotting boolean shapes, then the plot formula is a true/false.

--------------------

i redid the study and added 2 variables,
input extend_signals = 4;
input enable_macd_above_below_0 = yes;

the first one extends a signal 3 bars, so if a signal is 3 bars after another, the master signal should trigger.

the 2nd one allows the middle rule, macd lines above/below zero, to be enables/disabled. to me it seems you miss out on trades by using this rule.

it uses sum() to 'extend' each signal by x bars.
the first time all 3 signals are true , an arrow is drawn.

Code:
#macd_psar_seq_00

#https://usethinkscript.com/threads/macd-psar-indicator.17142/
#MACD PSAR indicator?
#METAL  11/11
##1
#I am attempting to write a strategy/indicator that will only signal when:

# extend each signal by x bars
# buy
#  MACD value line crosses above average line,
#  and macd lines are below zero line,
#  and Parabolic SAR is in uptrend.

# sell
#  MACD value line crosses below average line,
#  and macd lines are above zero line,
#  and Parabolic Sar is in downtrend.

# For example: If the MACD Value crosses over the average line and it is below zero but Parabolic Sar has not plotted an uptrend then no signal should be plotted
# however, if the parabolic sar plots an uptrend after the macd lines cross then a signal shall be plotted within a certain number of bars. This applies to all conditions


# Define the input parameters
input macdFastLength = 12;
input macdSlowLength = 26;
input macdSignalSmoothing = 9;

input sarAccelerationFactor = 0.02;
input sarMaxAccelerationFactor = 0.2;
input sarStep = 0.02;
input useZeroLine = yes;

# extend signals by x bars
input extend_signals = 4;
input enable_macd_above_below_0 = yes;

# Calculate MACD and Parabolic SAR
def macdValue = MACD(macdFastLength, macdSlowLength, macdSignalSmoothing).Value;
def macdAvg = ExpAverage(macdValue, macdSignalSmoothing);
def sar = reference ParabolicSAR(sarAccelerationFactor, sarMaxAccelerationFactor);

# Define conditions for buy and sell signals
# buy
#  MACD value line crosses above average line,
#  and macd lines are below zero line,
#  and Parabolic SAR is in uptrend.

# sell
#  MACD value line crosses below average line,
#  and macd lines are above zero line,
#  and Parabolic Sar is in downtrend.

# buy rules
def crossa = (macdValue crosses above macdAvg);
def mb = if enable_macd_above_below_0 then (macdValue < 0) else 1;
def sarup = sar > sar[1];
# sell rules
def crossb = (macdValue crosses below macdAvg);
def ma = if enable_macd_above_below_0 then (macdValue > 0) else 1;
def sardwn = sar < sar[1];


# extend rules
# buy
def xcrossa = sum(crossa, extend_signals);
def xmb = sum(mb, extend_signals);
def xsarup = sum(sarup, extend_signals);
# sell
def xcrossb = sum(crossb, extend_signals);
def xma = sum(ma, extend_signals);
def xsardwn = sum(sardwn, extend_signals);

# true every bar when all 3 are true
def buy1 = (xcrossa > 0 and xmb > 0 and xsarup > 0);
def sell1 = (xcrossb > 0 and xma > 0 and xsardwn > 0);

# true just on the first bar that all 3 are true
def buy2 = (!buy1[1] and buy1);
def sell2 = (!sell1[1] and sell1);


# Plot buy and sell signals
plot zbuy = if buy2 then low*0.992 else Double.NaN;
zbuy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zbuy.SetDefaultColor(Color.GREEN);
zbuy.setlineweight(4);
zbuy.hidebubble();

plot zsell = if sell2 then high*1.008 else Double.NaN;
zsell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zsell.SetDefaultColor(Color.RED);
zsell.setlineweight(4);
zsell.hidebubble();

#-------------------------

input test1_buy = no;
addchartbubble(test1_buy, low*0.97,
xcrossa + "\n" +
xmb + "\n" +
xsarup + "\n"
, color.yellow, no);

#
Thank you @halcyonguy . I had explained some of what you asked for in my first post or I thought I did. I agree with the missing out on some trades. I will give this a go.
 

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
448 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