0

I am trying to implement one of user "layzybear" indicator into a strategy. The indicator is Elder Impuls System "EIS_lb", which I have coded to v4. The goal is to set (green backgound = OK to Buy) and (red backgound = OK to Sell) and (blue backgound = NOK to trade). But I just cant understand how I can de-construct the multi line-function to set these rules. Usually I just set the plot-conditions as buy and sell rules, but that wont work here.

I would be very thankful for any help or thoughts.

useCustomResolution=input(true, type=input.bool)
customResolution=input("D", type=input.resolution)
source = security(syminfo.ticker, useCustomResolution ? customResolution : timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_on)
lengthEMA = input(13)
fastLength = input(12, minval=1), 
slowLength=input(26,minval=1)
signalLength=input(9,minval=1)

calc_hist(source, fastLength, slowLength) =>
    fastMA = ema(source, fastLength)
    slowMA = ema(source, slowLength)
    macd = fastMA - slowMA
    signal = sma(macd, signalLength)
    macd - signal

get_color(emaSeries, macdHist) =>
    g_f = (emaSeries > emaSeries[1]) and (macdHist > macdHist[1])
    r_f = (emaSeries < emaSeries[1]) and (macdHist < macdHist[1])
    g_f ? color.green : r_f ? color.red : color.blue
    
b_color = get_color(ema(source, lengthEMA), calc_hist(source, fastLength, slowLength))
bgcolor(b_color, transp=50)
Filip Vj
  • 3
  • 2

1 Answers1

0

First thing to note - using security function with lookahead_on argument without a history reference operator [] will lead your script to 'repaint'. I added the history reference 1 to the close source and added the second commented line with the repainted value if you still want to use this.

//@version=4
strategy("My Strategy", overlay=true)

useCustomResolution=input(true, type=input.bool)
customResolution=input("D", type=input.resolution)
source = security(syminfo.ticker, useCustomResolution ? customResolution : timeframe.period, close[1], barmerge.gaps_off, barmerge.lookahead_on)
// source = security(syminfo.ticker, useCustomResolution ? customResolution : timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_on) // Repaint
lengthEMA = input(13)
fastLength = input(12, minval=1), 
slowLength=input(26,minval=1)
signalLength=input(9,minval=1)

calc_hist(source, fastLength, slowLength) =>
    fastMA = ema(source, fastLength)
    slowMA = ema(source, slowLength)
    macd = fastMA - slowMA
    signal = sma(macd, signalLength)
    macd - signal

// get_color(emaSeries, macdHist) =>
//     g_f = (emaSeries > emaSeries[1]) and (macdHist > macdHist[1])
//     r_f = (emaSeries < emaSeries[1]) and (macdHist < macdHist[1])
//     g_f ? color.green : r_f ? color.red : color.blue

get_signal(emaSeries, macdHist) =>
    g_f = (emaSeries > emaSeries[1]) and (macdHist > macdHist[1])
    r_f = (emaSeries < emaSeries[1]) and (macdHist < macdHist[1])
    g_f ? true : r_f ? false : na

signal = get_signal(ema(source, lengthEMA), calc_hist(source, fastLength, slowLength))

bgcolor(signal == true ? color.green : signal == false ? color.red : color.blue, transp=50)


if signal == true
    strategy.entry("My Long Entry Id", strategy.long)

if signal == false
    strategy.entry("My Short Entry Id", strategy.short)
e2e4
  • 3,428
  • 6
  • 18
  • 30