Steve Yarnall
Steve Yarnall

Reputation: 61

Pine script - strategy entry using heikin ashi but strategy tester uses the real price not ha bar open

So the following code in Pine-script on TradingView uses the Heikin-Ashi candle-bar open price instead of the actual real open in the strategy tester panel.

Is there a way to get the strategy tester to use the real price?

This link explains the issue further.

//@version=2
strategy("haup", overlay=true)

cci20 = cci(close, 20)
sma10 = sma(close, 10)
source = close
sourcea = open

haclose = (open + high + low + close) / 4
haopen = na(haopen[1]) ? (open + close) / 2 : (haopen[1] + haclose[1]) / 2

fromYear = year > 2016
toYear = year < 2019

longCondition = haopen < haclose
if (longCondition and fromYear and toYear)
    strategy.entry("Long 1", strategy.long)

closeCondition = haopen > haclose
if (closeCondition)
    strategy.close("Long 1")

Upvotes: 6

Views: 18202

Answers (2)

user2830432
user2830432

Reputation: 222

The easiest way to to do it from the "Strategy Properties" tab in the strategy, just select "Fill orders using standard OHLC". But calling security via code works as well. I tested with both code and selecting the checkbox, the strategy backtesting came back with the same results.

Strategy Properties tab

Upvotes: 0

Mikeyy
Mikeyy

Reputation: 285

You can do this two ways:

  1. Use regular candles for strategy back-test and pull HA candles value via code for indicator.
  2. Usa HA candles for indicator and pull regular candles values via code, but you need to tell exact price to strategy back-test entries and exits.

So I suggest using option (1).

Use this code to pull open/close/high/low of HA candles for your indicator.

openHA  = security(heikinashi(tickerid), period, open)
closeHA = security(heikinashi(tickerid), period, close)
highHA  = security(heikinashi(tickerid), period, high)
lowHA  = security(heikinashi(tickerid), period, low)

Upvotes: 12

Related Questions