Solution for v2 to v4, heikin ashi ema
is Given Below:
I’m trying to convert this v2 script to v4 so I can add more stuff, but I can’t seem to replicate the moving heikin ashi line the way v2 script does. I’m doing everything the same so I’m not sure what the problem is.
I tried changing the shift and looked at the reference but I can’t figure out what I’m doing wrong. Please help
v2 code:
//@version=2
//Heikin Ashi F1
strategy("Heikin Ashi F1",shorttitle="HAS F1",overlay=true,default_qty_value=50,initial_capital=100,currency=currency.USD)
res = input(title="Heikin Ashi Candle Time Frame", type=resolution, defval="5")
hshift = input(2,title="Heikin Ashi Candle Time Frame Shift")
res1 = input(title="Heikin Ashi EMA Time Frame", type=resolution, defval="45")
mhshift = input(0,title="Heikin Ashi EMA Time Frame Shift")
fama = input(1,"Heikin Ashi EMA Period")
test = input(0,"Heikin Ashi EMA Shift")
sloma = input(40,"Slow EMA Period")
slomas = input(0,"Slow EMA Shift")
macdf = input(false,title="With MACD filter")
res2 = input(title="MACD Time Frame", type=resolution, defval="15")
macds = input(0,title="MACD Shift")
//Heikin Ashi Open/Close Price
ha_t = heikinashi(tickerid)
ha_open = security(ha_t, res, open[hshift])
ha_close = security(ha_t, res, close[hshift])
mha_close = security(ha_t, res1, close[mhshift])
//macd
[macdLine, signalLine, histLine] = macd(close, 12, 26, 9)
macdl = security(ha_t,res2,macdLine[macds])
macdsl= security(ha_t,res2,signalLine[macds])
//Moving Average
//THE PROBLEM IS FMA
fma = ema(mha_close[test],fama)
sma = ema(ha_close[slomas],sloma)
plot(fma,title="MA",color=lime,linewidth=2,style=line)
plot(sma,title="SMA",color=red,linewidth=2,style=line)
//Strategy
golong = crossover(fma,sma) and (macdl > macdsl or macdf == false )
goshort = crossunder(fma,sma) and (macdl < macdsl or macdf == false )
strategy.entry("Buy",strategy.long,when = golong)
strategy.entry("Sell",strategy.short,when = goshort)
// strategy.exit("ExitLong","Buy", limit = close+atr)
// strategy.exit("ExitShort","Sell",limit = close - at)
v4 code:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zainali282000
//@version=4
strategy("HAF1v4", overlay=true, initial_capital=100,currency=currency.USD, default_qty_type = strategy.percent_of_equity, default_qty_value = 25, commission_type = strategy.commission.percent, commission_value = 0.04, margin_long = 4, margin_short = 4)
//Get Heikin Ashi Data
res = input(title="Heikin Ashi Candle Time Frame", type=input.resolution, defval="")
hshift = input(2,title="Heikin Ashi Candle Time Frame Shift")
res1 = input(title="Heikin Ashi EMA Time Frame", type=input.resolution, defval="45")
mhshift = input(0,title="Heikin Ashi EMA Time Frame Shift")
fama = input(1,"Heikin Ashi EMA Period")
test = input(0,"Heikin Ashi EMA Shift")
sloma = input(40,"Slow EMA Period")
slomas = input(0,"Slow EMA Shift")
macdf = input(false,title="With MACD filter")
res2 = input(title="MACD Time Frame", type=input.resolution, defval="15")
macds = input(0,title="MACD Shift")
//Heikin Ashi Open/Close Price
ha_t = heikinashi(syminfo.tickerid)
ha_open = security(ha_t, res, open[hshift])
ha_close = security(ha_t, res, close[hshift])
mha_close = security(ha_t, res1, close[mhshift])
//Moving Average
//THE PROBLEM IS FMA
fma = ema(mha_close[test],fama)
sma = ema(ha_close[slomas],sloma)
plot(fma,title="MA",color=color.blue,linewidth=2)
plot(sma,title="SMA",color=color.black,linewidth=2)
//strategy
golong = crossover(fma,sma)
goshort = crossunder(fma,sma)
strategy.entry("Buy",strategy.long,when = golong)
strategy.entry("Sell",strategy.short,when = goshort)
The problem in security
calls. For v2 security(ha_t, res, open[hshift])
the equivalent for v4 is security(ha_t, res, open[hshift], lookahead=barmerge.lookahead_on)