Reputation: 3
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.
Upvotes: 0
Views: 247
Reputation: 11
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.
Upvotes: 0
Reputation: 21342
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.
Upvotes: 1