Page 1 of 1

Multiple crossing MA:s as indicator

Posted: Sun Sep 19, 2021 9:10 pm
by NameOfBand
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

Re: Multiple crossing MA:s as indicator

Posted: Fri Sep 24, 2021 6:11 am
by Matthew
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)