6

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")
PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
Steve Yarnall
  • 61
  • 1
  • 1
  • 2
  • Hi Steven and welcome to SO! Please provide code of what you have already tried yourself and remember that SO is not a code writing service. Otherwise you risk getting your posts down-voted or closed. – not2qubit Oct 09 '18 at 05:30
  • What do you mean with "real open in the strategy tester panel"? – not2qubit Oct 09 '18 at 05:31
  • Thanks not2qubit . The code is in original post. Pine script uses the heikin ashi value in the backtest strategy test panel for the list of trades. This value is not the true price one would buy or sell at. – Steve Yarnall Oct 11 '18 at 09:47

2 Answers2

12

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)
not2qubit
  • 14,531
  • 8
  • 95
  • 135
Mikeyy
  • 285
  • 1
  • 7
  • 1
    Thanks mikeyy . I'll think about it. For the strategy i hoped to use heikin ashi to trigger buy sells but wanted the list of trades to calculate based on the actual price. – Steve Yarnall Oct 11 '18 at 09:50
  • 1
    Yes, I understood that. Above code allows you that. Define those variables first and then in your buy/sell conditions use closeHA instead of close for claculations. Since you will be using regular candles, strategy backtest will calculate trades automaticly based on regular / real prices, not HA prices. – Mikeyy Oct 11 '18 at 13:07
  • 1
    Thanks. It works when the chart style is bar, candles or hollow candles. awesome. – Steve Yarnall Oct 12 '18 at 17:22
  • 2
    tickerid: "syminfo.tickerid" and period: "timeframe.period" , to get whatever the chart is showing – Leo Ma Jul 30 '21 at 11:12
  • will this do problem with lookahead i guess? not good for backtesting? – luky Feb 05 '22 at 17:03
0

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

user2830432
  • 222
  • 2
  • 4