What I am trying to achieve, is when price crosses ema and then pulls back to within a certain range and then continues in the direction of the original cross. I would like an alarm and an arrow at this point.
Here is what I have started. It isn't working though.
also would like a stop low or high of last bar.
Here is what I have started. It isn't working though.
also would like a stop low or high of last bar.
Code:
# Define inputs for the EMA period, pullback range, and candle criteria
input emaLength = 20;
input pullbackRange = 0.1; # Adjust this value as needed for your criteria
input candleCriteria = 2; # Number of candles to check after the pullback
# Calculate the EMA
def ema = ExpAverage(close, emaLength);
# Calculate the pullback level
def pullbackLevel = ema * (1 - pullbackRange);
# Define a condition for the crossover and pullback
def crossoverAndPullback = close crosses above ema and low <= pullbackLevel;
# Initialize a counter to track candles after pullback
def candlesAfterPullback = if crossoverAndPullback then 1 else candlesAfterPullback[1] + 1;
# Create a signal when the pullback condition is met for the specified number of candles
def signal = candlesAfterPullback >= candleCriteria;
# Plot arrows on the chart when the signal occurs
plot bullishSignal = signal;
bullishSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
bullishSignal.SetDefaultColor(Color.GREEN);
bullishSignal.SetLineWeight(2);
# Add an alert when the signal occurs
alert(signal, "Bullish Pullback Signal", Alert.BAR, Sound.Bell);