英文:
Convert pinescript v2 to pinescript v5
问题
//@version=5
strategy(title='v5', shorttitle='v5', overlay=true, pyramiding=0, initial_capital=10, currency=currency.USD)
trade_size = input.int(1)
tf = input.timeframe('60')
ha_ticker = ticker.heikinashi(syminfo.tickerid)
ha_open = request.security(ha_ticker, tf, open)
ha_close = request.security(ha_ticker, tf, close)
sel_entry = ta.crossunder(ha_close, ha_open)
buy_entry = ta.crossover(ha_close, ha_open)
strategy.entry('sell', strategy.short, qty=trade_size, comment='sell', when=sel_entry)
strategy.entry('buy', strategy.long, qty=trade_size, comment='buy', when=buy_entry)
英文:
I have a simple strategy based on Pinescript v2. I would like to convert it to pinescript v5. Here is the v2 code:
//@version=2
strategy(title='Strategy 1', shorttitle='Strategy 1', overlay=true, pyramiding=0, initial_capital=10, currency=currency.USD)
trade_size = input(1)
tf = input('60')
r = heikenashi(tickerid)
ro = security(r, tf, open)
rc = security(r, tf, close)
sel_entry = crossunder(rc, ro)
buy_entry = crossover(rc, ro)
strategy.entry('sell', long=strategy.short, qty=trade_size, comment='sell', when=sel_entry)
strategy.entry('buy', long=strategy.long, qty=trade_size, comment='buy', when=buy_entry)
What I have managed to do so far:
//@version=5
strategy(title='v5', shorttitle='v5', overlay=true, pyramiding=0, initial_capital=10, currency=currency.USD)
trade_size = input.int(1)
tf = input.timeframe('60')
ha_ticker = ticker.heikinashi(syminfo.tickerid)
ha_open = request.security(ha_ticker, tf, open)
ha_close = request.security(ha_ticker, tf, close)
sel_entry = ta.crossunder(ha_close, ha_open)
buy_entry = ta.crossover(ha_close, ha_open)
strategy.entry('sell', strategy.short, qty=trade_size, comment='sell', when=sel_entry)
strategy.entry('buy', strategy.long, qty=trade_size, comment='buy', when=buy_entry)
The problem is that it doesn't work properly.
Results in Pinescript v2:
Results in Pinescript v5:
I tried many solutions, but always the results are completely different than in pinescript v2.
答案1
得分: 1
从v2
到v5
有一些重大变化。在你的情况下,你需要了解的最重要变化是安全函数的默认行为已更改。
这导致了结果的差异。如果你希望获得相同的结果,你应该使用barmerge.lookahead_on
。然而,这样做将使用未来的数据,这是不推荐的。更多信息在这里。
英文:
There are some big changes from v2
to v5
. The most important change you need to know in your case is default behaviour of security function has changed.
This causes the difference in reults. If you wish to have the same results, you should be using barmerge.lookahead_on
. However, by doing so you will be using future data which is not recommended. More info here.
答案2
得分: 0
haClose = (open + high + low + close) / 4
haOpen = float(na)
haOpen := na(haOpen1) ? (open + close) / 2 : (nz(haOpen1) + nz(haClose1)) / 2
haHigh = max(high, max(haOpen, haClose))
haLow = min(low, min(haOpen, haClose))
使用上述公式计算 HA OHLC,然后在 barclosure 上使用它们。
英文:
haClose = (open + high + low + close) / 4
haOpen = float(na)
haOpen := na(haOpen[1]) ? (open + close) / 2 : (nz(haOpen[1]) + nz(haClose[1])) / 2
haHigh = max(high, max(haOpen, haClose))
haLow = min(low, min(haOpen, haClose))
use the above formula to calculate HA OHLC, and then use them upon barclosure.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论