Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

pine-script

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")
like image 974
Steve Yarnall Avatar asked Oct 05 '18 15:10

Steve Yarnall


People also ask

Is Heikin-Ashi better than Candlestick?

Heikin-Ashi has a smoother look because it is essentially taking an average of the movement. There is a tendency with Heikin-Ashi for the candles to stay red during a downtrend and green during an uptrend, whereas normal candlesticks alternate color even if the price is moving dominantly in one direction.

How does Tradingview calculate Heikin-Ashi?

The formula for their calculation is given below: Open = (Previous Open + Previous Close) / 2. In regular candles, the open level is at the close of the previous candle (if there is no gap in the market). In Heikin Ashi, a new candlestick opens at the middle level, between the opening and closing of the previous one.

How are Heikin-Ashi candles calculated?

Heikin Ashi candles are calculated this way: Open: (Open (previous candle) + Close (previous candle))/2. Close: (Open + Low + Close + High)/4. High: the same of the actual candle.


1 Answers

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)
like image 184
Mikeyy Avatar answered Nov 10 '22 23:11

Mikeyy