简体   繁体   English

将策略 Pine 脚本从 v2 转换为 v4,做到了,但得到了不同且错误的结果

[英]Convert strategy Pine script from v2 to v4 , did it, but got different and wrong results

My original script is based on HeikinAshi strategy with certain conditions.我的原始脚本是基于具有某些条件的 HeikinAshi 策略。

//@version=2
//Heikin Ashi Strategy  V2 by breizh29

strategy("Heikin Ashi Strategy  V2",shorttitle="HAS V2",overlay=true,default_qty_type=strategy.percent_of_equity, default_qty_value=100,initial_capital=100,currency=currency.GBP)
res = input(title="Heikin Ashi Candle Time Frame", type=resolution, defval="60")
hshift = input(1,title="Heikin Ashi Candle Time Frame Shift")
res1 = input(title="Heikin Ashi EMA Time Frame", type=resolution, defval="180")
mhshift = input(0,title="Heikin Ashi EMA Time Frame Shift")
fama = input(1,"Heikin Ashi EMA Period")
test = input(1,"Heikin Ashi EMA Shift")
sloma = input(30,"Slow EMA Period")
slomas = input(1,"Slow EMA Shift")
macdf = input(false,title="With MACD filter")
res2 = input(title="MACD Time Frame", type=resolution, defval="15")
macds = input(1,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
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)
if (goshort)
    strategy.close("Buy")



and my compiled script to v4 is我编译到 v4 的脚本是

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Stopperbg

//@version=4

strategy("HAS V3",shorttitle="HAS V3",overlay=true,default_qty_type=strategy.percent_of_equity, default_qty_value=100,initial_capital=1000,currency=currency.USD)
res = input(title="Heikin Ashi Candle Time Frame", type=input.resolution, defval="60")
hshift = input(1,title="Heikin Ashi Candle Time Frame Shift")
res1 = input(title="Heikin Ashi EMA Time Frame", type=input.resolution, defval="180")
mhshift = input(0,title="Heikin Ashi EMA Time Frame Shift")
fama = input(1,"Heikin Ashi EMA Period")
test = input(1,"Heikin Ashi EMA Shift")
sloma = input(30,"Slow EMA Period")
slomas = input(1,"Slow EMA Shift")
macdf = input(false,title="With MACD filter")
res2 = input(title="MACD Time Frame", type=input.resolution, defval="15")
macds = input(1,title="MACD Shift")




//Heikin Ashi Open/Close Price
t = tickerid(syminfo.prefix, syminfo.ticker, session.regular)
ha_t = heikinashi(t)
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
fma = ema(mha_close[test],fama)
sma = ema(ha_close[slomas],sloma)
plot(fma,title="MA",color=color.lime,linewidth=2,style=plot.style_line)
plot(sma,title="SMA",color=color.red,linewidth=2,style=plot.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)
if (goshort)
    strategy.close("Buy")



I followed tutorials and my script is without errors.我按照教程进行操作,我的脚本没有错误。 I do this conversion, because I want to add alertcondition to get alarm when condition is met.我进行此转换,因为我想添加alertcondition以在满足条件时发出警报。

On v2 there is no alert with added buy or sell signal, Please note that in Pine v4 an alertcondition call generates an additional plot.在 v2 上没有添加买入或卖出信号的警报,请注意,在 Pine v4 中, alertcondition调用会生成额外的 plot。 All such calls are taken into account when we calculate the number of the output series per script.当我们计算每个脚本的 output 系列的数量时,所有这些调用都被考虑在内。 But I got different results.但我得到了不同的结果。 whats wrong with it?它出什么问题了?

That script is misleading and producing unrealistic results.该脚本具有误导性并产生不切实际的结果。

Using the default settings, the code is cheating by using future data to calculate the fma variable used to trigger Buy/Sell orders.使用默认设置,代码通过使用未来数据来计算用于触发买/卖订单的fma变量来作弊。 If hshift == 0 or mhshift == 0 (the default), then your script will be using future data, and so misleading traders or you.如果hshift == 0mhshift == 0 (默认值),那么您的脚本将使用未来数据,从而误导交易者或您。

To avoid this, you should change these lines:为避免这种情况,您应该更改这些行:

hshift = input(1,title="Heikin Ashi Candle Time Frame Shift")
mhshift = input(0,title="Heikin Ashi EMA Time Frame Shift")

to:至:

hshift = input(1,title="Heikin Ashi Candle Time Frame Shift", minval = 1)
mhshift = input(1,title="Heikin Ashi EMA Time Frame Shift", minval = 1)

See How to avoid repainting when using security() - PineCoders FAQ for more details.有关更多详细信息,请参阅使用 security() - PineCoders 常见问题时如何避免重绘

Note that the scripts using the source code you reused have been moderated a while back for the exact reason that they were misleading.请注意,使用您重复使用的源代码的脚本已经被审核了一段时间,因为它们具有误导性的确切原因。

so i manage to fix it.所以我设法修复它。 added lookahead=barmerge.lookahead_on添加了lookahead=barmerge.lookahead_on

ha_open = security(ha_t, res, open[hshift], lookahead=barmerge.lookahead_on)
ha_close = security(ha_t, res, close[hshift], lookahead=barmerge.lookahead_on)
mha_close = security(ha_t, res1, close[mhshift], lookahead=barmerge.lookahead_on)

they changed lookahead to off by default他们默认将前瞻更改为关闭

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM