Need help coding moving avg bounce signal

O

Options Trader

New member
Anyone able to provide code for price bouncing off of moving average? See attached photo

Signal would be when price has retraced to MA50 (touched it) and then crosses the second MA10 and continues in the trend direction. Willing to pay modest fee!

Thanks for all considerations.

 
M

mashume

Active member
@Options Trader :

Let me know if this fits the bill. You were a bit short on some specifics, so I made a few guesses. There is nothing about continuing in the trend direction in this. I code for fun, but if it's what you wanted, feel free to make a donation to the COVID-19 charity of your choice in your area for whatever amount you'd have given me.

Trade Healthy!
mashume

I looked at /ES for the last year, and it gives a few good signals, but a fair bit of noise when the market is not trending.

Code:
#
# Moving Average Bounce
#
# Coded per request on UseThinkScript
#
# V. 2020-03-23 V1
#
# by mashume
#

input fast_length = 10;
input slow_length = 50;
input price = CLOSE;

input MovingAverage = {default "EMA", "SMA", "WMA", "HMA"};

plot fast_MA;
switch (MovingAverage) {
case SMA:
    fast_MA = simpleMovingAvg(price, length = fast_length);
case WMA:
    fast_MA = wma(price, length = fast_length);
case HMA:
    fast_MA = HullMovingAvg(price = price, length = fast_length);
default:
    fast_MA = MovAvgExponential(price, length = fast_length);
}

plot slow_MA;
switch (MovingAverage) {
case SMA:
    slow_MA = simpleMovingAvg(price, length = slow_length);
case WMA:
    slow_MA = wma(price, length = slow_length);
case HMA:
    slow_MA = HullMovingAvg(price = price, length = slow_length);
default:
    slow_MA = MovAvgExponential(price, length = slow_length);
}


def phase1 = if low < slow_MA and low[1] > slow_MA[1] then -1 else if high > slow_MA and high[1] < slow_MA[1]  then 1 else double.NaN;
def phase2 = if close > fast_MA and close[1] < fast_MA[1] then 1 else if close < fast_MA and close[1] > fast_MA[1] then -1 else double.NaN;

# turn on these plots if you want to see where each of the phase conditions is true.
# Make them boolean dots or some such
# plot p1 = if phase1 == 1 then high else if phase1 == -1 then low else double.nan;
# plot p2 = if phase2 == 1 then high else if phase2 == -1 then low else double.nan;

plot buy = if (! isnan(phase1[1]) or! isnan( phase1[2]) or ! isnan( phase1[3])) and phase2 == 1 then LOW else Double.NaN;
buy.SetDefaultColor(Color.GREEN);
buy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

plot sell = if (! isnan(phase1[1]) or! isnan( phase1[2]) or ! isnan( phase1[3])) and phase2 == -1 then HIGH else Double.NaN;
sell.SetDefaultColor(Color.RED);
sell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
 
O

Options Trader

New member
Thank your for your work. I did not expect such a quick response. I am away from being able to test this in my charts but will let you know soon if this is what i was looking for. Regardless, it was very kind of you to take the time on my behalf.
 

Top