NameOfBand
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: September 19th, 2021

Multiple crossing MA:s as indicator

Hi!

I wonder if anyone could be so kind and try to help me with how I can code a strategy in Pinescript that uses one MA crossing two other MA:s as an indicator? Thanks!

/NaOfBa

User avatar
Matthew
Site Admin
Site Admin
Posts: 92
Joined: July 1st, 2020
Location: Australia
Contact: Website Facebook Twitter TradingView Profile

Re: Multiple crossing MA:s as indicator

That's an interesting challenge! Unfortunately I don't have time to take it on myself, but here is some code that might help you get started with the idea if you can't find someone to help - this is one way to detect if an MA has crossed over two others:

Code: Select all

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ZenAndTheArtOfTrading / www.PineScriptMastery.com
// @version=4
study("MA Double-Cross", overlay=true)

// Get indicator values
ema1 = ema(close, 20)
ema2 = ema(close, 50)
ema3 = ema(close, 100)

// Declare cross variables
var crossedMA2 = false
cross = false

// Check if we've crossed MA2 then set flag
if cross(ema1, ema2) and not crossedMA2
    crossedMA2 := true
else
    crossedMA2 := false
    
// Check if we've crossed MA3 after first crossing MA2
if cross(ema1, ema3)
    cross := true
    crossedMA2 := false

// Change color if double-cross is confirmed
bgcolor(cross ? color.new(color.red,50) : na)

// Draw MAs
plot(ema1, color=color.green)
plot(ema2, color=color.orange)
plot(ema3, color=color.red)

Return to “Pine Script Q&A”