pine-scriptmoving-averagecrossover

How to crossover moving average with a specific candle height? Pinescript


I have my strategy set however I would like to test entering trades when a candlestick crosses over the hma49 by a specific amount of ticks. Such as 4. Can I make this an input so I can adjust the amount of ticks above the hma49 ? This would be great for testing. I'm sure there must be a way to code this but I have googled and tried many times and can't figure it out. Any help on this one would be so awesome. I'm without a way to get there on my own.

//@version=4
strategy ("15MinChart400", overlay=true)

hma23 = hma(close, 23)
hma49 = hma(close, 49)
hma16 = hma(close, 16)
hma200 = hma(close, 200)
hma400 = hma(close, 400)
CO =  crossover(hma49, 4 * syminfo.mintick)  

long = CO and close >= hma49
exitLong = close <= hma49 

//Second Attempt and now in need of Candlestick lookback

hma49 = hma(close, 49)
ticks = input(4)
tickPrice = hma49 + ticks * syminfo.mintick
lookback = input(4, "Candlestick Lookback")
CL = close[lookback]

long = crossover(close,tickPrice) and crossover(close,hma49) and CL
exitLong = close <= hma49

This was my attempt but it doesn't work.

Any help would be greatly appreciated!

Big thanks,

Paul


Solution

  • With the crossover, you'll want to evaluate when the close price has crossed over the hma49 value + ticks.

    ticks = input(4)
    
    hma49 = hma(close, 49)
    
    tickPrice = hma49 + ticks * syminfo.mintick
    
    long = crossover(close, tickPrice)  
    exitLong = close <= hma49
    
    strategy.entry(id = "enter long", long = true, when = long)
    strategy.close(id = "enter long", when = exitLong)
    

    Or if you were looking at evaluating whether the price crossed over the hma + ticks during the bar, but closed anywhere above the hma you would use

    long = open < tickPrice and high > tickPrice and close >= hma49