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
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);