Help Moving Average Study

Toesz

New member
VIP
Hello ThinkScript community,

Can someone help me write the following code?

In the chart below, there are multiple studies:

  • MovAvgExponential (CLOSE, 10, 0, no)
  • 1729544633372.png
  • MovAvgExponential (CLOSE, 25, 0, no)
  • 1729544671055.png
  • ATR (14, WILDERS)
  • 1729544700893.png
  • RIS (14, 70, 30, CLOSE, WILDERS, no)
  • 1729544726631.png
  • What I’d like to do:
    • 10 period EMA crosses fully above the 25 EMA.
    • Full green candle (including wick) sits above the lined 10 period EMA at the close of the one minute time frame.
    • 1729544747517.png
    • Alert potential long set up with a ding and a green arrow below the signal candle.
    • 10 period EMA crosses fully below the 25 EMA.
    • Full red candle (including wick) sits below the lined 10 period EMA at the close of the one minute time frame.
    • 1729544764647.png
    • Alert potential short set up with a ding and a red arrow above the the signal candle.
    • The arrows can go above/below the dashed EMA line as well.
    • 1729544783728.png
      1729544797464.png
  • Larger chart example:
    1729544829457.png
    • Once there is a signal, I use the ATR to determine risk and target. I use RSI just to give me a gauge on current activity.
    • I rely heavily on Price Action to provide the real edge on the probability of where price MAY go next.
    • If I could get some help, I'd appreciate it. Thank you all. Toesz
 
Last edited by a moderator:
Hello ThinkScript community,

Can someone help me write the following code?

In the chart below, there are multiple studies:

  • MovAvgExponential (CLOSE, 10, 0, no)

  • MovAvgExponential (CLOSE, 25, 0, no)

  • ATR (14, WILDERS)

  • RIS (14, 70, 30, CLOSE, WILDERS, no)

  • What I’d like to do:
    • 10 period EMA crosses fully above the 25 EMA.
    • Full green candle (including wick) sits above the lined 10 period EMA at the close of the one minute time frame.

    • Alert potential long set up with a ding and a green arrow below the signal candle.
    • 10 period EMA crosses fully below the 25 EMA.
    • Full red candle (including wick) sits below the lined 10 period EMA at the close of the one minute time frame.

    • Alert potential short set up with a ding and a red arrow above the the signal candle.
    • The arrows can go above/below the dashed EMA line as well.
  • Larger chart example:
    • Once there is a signal, I use the ATR to determine risk and target. I use RSI just to give me a gauge on current activity.
    • I rely heavily on Price Action to provide the real edge on the probability of where price MAY go next.
    • If I could get some help, I'd appreciate it. Thank you all. Toesz hal_risk

here is something that draws arrows on your signals.
for long, it checks if low has crossed above the average

long rule1
..ema10 > ema25
..low > ema10 and green candle
long = avg1 > avg2 and low > avg1 and grn and low[1] < avg1[1];

short rule1
..ema10 < ema25
..high < ema10 and red candle
short = avg1 < avg2 and high < avg1 and red and high[1] > avg1[1];

this draws a line from close , when a signal happens. i call this a buy line, for longs and shorts.
it uses ATR x some number, to calc a risk amount.
the reward factor (2) is multiplied by the risk.
draws clouds for the risk and reward areas
when close crosses the buy line, the lines and clouds stop.


Code:
#avg_atr_rsi1
#https://usethinkscript.com/threads/help-moving-average-study.19870/
#Help Moving Average Study

# MovAvgExponential (CLOSE, 10, 0, no)
# MovAvgExponential (CLOSE, 25, 0, no)
# ATR (14, WILDERS)
# RIS (14, 70, 30, CLOSE, WILDERS, no)

def na = double.nan;
def bn = barnumber();
def o = open;
def c = close;
def grn = c > o;
def red = c < o;

#--------------------------------
# averages

input avg1_type = AverageType.exponential;
#input avg1_type = AverageType.Simple;
input avg1_length = 10;
def avg1 = MovingAverage(avg1_type, c, avg1_length );

input avg2_type = AverageType.exponential;
#input avg2_type = AverageType.Simple;
input avg2_length = 25;
def avg2 = MovingAverage(avg2_type, c, avg2_length );

input show_avg1_line = yes;
input show_avg2_line = yes;
plot zavg1 = if show_avg1_line then avg1 else na;
plot zavg2 = if show_avg2_line then avg2 else na;
zavg1.SetDefaultColor(Color.cyan);
zavg1.setlineweight(1);
zavg1.hidebubble();
zavg2.SetDefaultColor(Color.yellow);
zavg2.setlineweight(1);
zavg2.hidebubble();

def xup = avg1 crosses above avg2;
def xdwn = avg1 crosses below avg2;


# long rule1
#  ema10 > ema25
#  low > ema10 and green candle
def long1 = avg1 > avg2 and low > avg1 and grn and low[1] < avg1[1];

# short rule1
#  ema10 < ema25
#  high < ema10 and red candle
def short1 = avg1 < avg2 and high < avg1 and red and high[1] > avg1[1];

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

def y = 0.001;
plot zup = if long1 then (low*(1-y)) else na;
zup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zup.SetDefaultColor(Color.green);
zup.setlineweight(3);
zup.hidebubble();

plot zdwn = if short1 then (high*(1+y)) else na;
zdwn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zdwn.SetDefaultColor(Color.red);
zdwn.setlineweight(3);
zdwn.hidebubble();


addlabel(1, "  ", color.black);
addlabel(1,
     (if avg1_type == AverageType.Simple then "SMA"
 else if avg1_type == AverageType.exponential then "EMA"
 else if avg1_type == AverageType.hull then "HULL"
 else if avg1_type == AverageType.weighted then "WT"
 else if avg1_type == AverageType.wilders then "WILD"
 else "---")
+ avg1_length
, color.cyan);

addlabel(1, "  ", color.black);
addlabel(1,
     (if avg2_type == AverageType.Simple then "SMA"
 else if avg2_type == AverageType.exponential then "EMA"
 else if avg2_type == AverageType.hull then "HULL"
 else if avg2_type == AverageType.weighted then "WT"
 else if avg2_type == AverageType.wilders then "WILD"
 else "---")
+ avg2_length
, color.yellow);
addlabel(1, "  ", color.black);


#--------------------------------
# ATR

input length = 14;
input averageType = AverageType.WILDERS;
def ATR = MovingAverage(averageType, TrueRange(high, c, low), length);

#--------------------------------
# RSI

#declare lower;
input rsi_length = 14;
input rsi_over_Bought = 70;
input rsi_over_Sold = 30;
input rsi_price = close;
input rsi_averageType = AverageType.WILDERS;
#input showBreakoutSignals = no;

def NetChgAvg = MovingAverage(rsi_averageType, rsi_price - rsi_price[1], rsi_length);
def TotChgAvg = MovingAverage(rsi_averageType, AbsValue(rsi_price - rsi_price[1]), rsi_length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

def RSI = 50 * (ChgRatio + 1);
def OverSold = rsi_over_Sold;
def OverBought = rsi_over_Bought;
def UpSignal = if RSI crosses above OverSold then OverSold else Double.NaN;
def DownSignal = if RSI crosses below OverBought then OverBought else Double.NaN;

addlabel(1, "  ", color.black);
addlabel(1, "RSI: " + floor(rsi), color.magenta);


#--------------------------------
# alerts , sounds

# alert(condition, text, alert type, sound);
# Sound.Ding,  higher , up sound
# Sound.Bell,  lower , down sound
alert(long1, "LONG" ,alert.BAR, sound.DING);
alert(short1, "SHORT" ,alert.BAR, sound.bell);

#--------------------------------
# buy lines for long and short

#  cancel = when close crosses buy line

def long_line = if long1 then close
 else if close[1] < long_line[1] then na
 else long_line[1];

plot zlline = long_line;
zlline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zlline.SetDefaultColor(Color.cyan);
zlline.setlineweight(2);
zlline.hidebubble();

def short_line = if short1 then close
 else if close[1] > short_line[1] then na
 else short_line[1];

plot zsline = short_line;
zsline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zsline.SetDefaultColor(Color.yellow);
zsline.setlineweight(2);
zsline.hidebubble();

#--------------------------------
# risk / reward

input risk_atr_factor = 1;
def risk = risk_atr_factor * atr;
input reward_factor = 2.0;
def reward = reward_factor * risk;

# LONG
def long_risk = if !isnan(long_line) then long_line - risk else na;
def long_reward = if !isnan(long_line) then long_line + reward else na;
plot z1 = long_risk;
plot z2 = long_reward;
z1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z1.SetDefaultColor(Color.red);
z1.setlineweight(2);
z2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z2.SetDefaultColor(Color.green);
z2.setlineweight(2);

def c1 = long_reward;
def c2 = zlline;
def c3 = long_risk;
addcloud(c1, c2, color.light_green);
addcloud(c2, c3, color.light_red);

# SHORT
def short_risk = if !isnan(short_line) then short_line + risk else na;
def short_reward = if !isnan(short_line) then short_line - reward else na;
plot z3 = short_risk;
plot z4 = short_reward;
z3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z3.SetDefaultColor(Color.red);
z3.setlineweight(2);
z4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z4.SetDefaultColor(Color.green);
z4.setlineweight(2);

def c4 = short_reward;
def c5 = zsline;
def c6 = short_risk;
addcloud(c5, c4, color.light_green);
addcloud(c6, c5, color.light_red);
#
 

Attachments

  • img22.JPG
    img22.JPG
    72.7 KB · Views: 136

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