Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Built in Heikinashi indicator
 Post subject: Built in Heikinashi indicator Post rating: 0   New post Posted: Mon 07 Mar, 2011, 12:49 

User rating: 0
Joined: Thu 24 Feb, 2011, 23:25
Posts: 2
Does someboday understand, whats the meaning of the graphical representation of the built in Heikinashi indicator is? How to interpret it?


 
 Post subject: Re: Built in Heikinashi indicator Post rating: 0   New post Posted: Tue 08 Mar, 2011, 09:24 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hi,
Heikin Ashi indicator provides an alternative to the default bar chart. Heikin Ashi bars are formed as follows:
Close = (Open+High+Low+Close)/4
Open = [Open (previous bar) + Close (previous bar)]/2
High = Max (High,Open,Close)
Low = Min (Low,Open, Close)


 
 Post subject: Re: Built in Heikinashi indicator Post rating: 0   New post Posted: Wed 06 Jul, 2011, 23:15 
User avatar

User rating: 1
Joined: Wed 06 Jul, 2011, 23:12
Posts: 42
Location: Romania, Bucharest
Dear Sir,

Can you please post the java code for the build-in HeikinAshi indicator?

I intend to slightly modify it to fit some of my ideas, but I am not capable to write it from zero (at least not now, maybe latter after I learn more about Java).

Thank you very much in advance!


 
 Post subject: Re: Built in Heikinashi indicator Post rating: 0   New post Posted: Thu 07 Jul, 2011, 15:18 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Consider a strategy which prints out the Heikinashi indicator values in two approaches (last 10 Heikinashi candles and just 1 Heikinashi candle):
package jforex.strategies.indicators;

import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.HashMap;
import java.util.TimeZone;

import com.dukascopy.api.*;
import static com.dukascopy.indicators.HeikinAshiIndicator.*;

@SuppressWarnings("serial")
public class HeikinAshiStrategy2 implements IStrategy {
    private IIndicators indicators = null;
    private IConsole console;
    private IChart chart;


    // strategy specific params
    @Configurable("Insturment")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("Period")
    public Period period = Period.TEN_MINS;
   
    private static final Map<Integer,String> nameMap = new HashMap<Integer, String>(){{
        put(OPEN, "Open");
        put(CLOSE, "Close");
        put(LOW, "Low");
        put(HIGH, "High");
    }};

    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") {{setTimeZone(TimeZone.getTimeZone("GMT"));}};

    public void onStart(IContext context) throws JFException {
        indicators = context.getIndicators();
        this.console = context.getConsole();
        this.chart = context.getChart(instrument);
        console.getOut().println("Started");

        chart.addIndicator(indicators.getIndicator("heikinAshi".toUpperCase()));
    }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {

        if (period != this.period || instrument != this.instrument)
            return;

        double[] heikin = indicators.heikinAshi(instrument, period, OfferSide.BID, 1);
        double[][] heikin10 = indicators.heikinAshi(instrument, period, OfferSide.BID, Filter.WEEKENDS, 10, bidBar.getTime(), 0);

        print("last heikin ashi: " + arrayToString(heikin));
        print("last 10 heikin ashi: " + arrayToString(heikin10));
    }

    private void print(Object o) {
        console.getOut().println(o);
    }

    private static String arrayToString(double[] arr) {
        String str = "";
        for (int r = 0; r < arr.length; r++) {
            str += String.format("%s=%.5f,", nameMap.get(r), arr[r]);
        }
        return str;
    }
   
    private static String arrayToString(double [][] arr){
        String str = "";
        for (int r=0; r<arr.length; r++) {
            for (int c=0; c<arr[r].length; c++) {
                str += String.format("[%s,%s]=%.5f,", nameMap.get(c),r, arr[r][c]);
            }
            str += "; ";
        }
        return str;
    }

    public void onStop() throws JFException {}

    public void onTick(Instrument instrument, ITick tick) throws JFException {}

    public void onMessage(IMessage message) throws JFException {    }

    public void onAccount(IAccount account) throws JFException {    }
}

See here more about the use of indicators in startegies:
https://www.dukascopy.com/wiki/index.php ... Indicators
And a simple strategy which makes orders in accordance to indicator's values:
https://www.dukascopy.com/wiki/index.php ... SMA_Simple


 
 Post subject: Re: Built in Heikinashi indicator Post rating: 0   New post Posted: Mon 26 Mar, 2012, 11:49 
User avatar

User rating: 0
Joined: Thu 25 Aug, 2011, 14:04
Posts: 45
Location: Russian Federation,
Hello Support Team,

Could you provide how to get value of "heikin" (and also "open", "close", "high" and "low" prices)?
When I print value of "heikin":

print("last heikin ashi: " + (heikin) + " Time current " + sdf.format(bidBar.getTime()));

I no get correct value, only follow:

07:14:10 last heikin ashi: [D@4cc9997 Time current 2012-03-26 04:10:00

Best regards,
vs_trade


 
 Post subject: Re: Built in Heikinashi indicator Post rating: 0   New post Posted: Mon 26 Mar, 2012, 15:49 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The example strategy does just that. Try it with 10 sec timeframe.


 
 Post subject: Re: Built in Heikinashi indicator Post rating: 0   New post Posted: Tue 27 Mar, 2012, 08:21 
User avatar

User rating: 0
Joined: Thu 25 Aug, 2011, 14:04
Posts: 45
Location: Russian Federation,
Hello Support Team,

May be I not clear explain. I no need example strategy.

My question was. How I can get "open, close, high and low" from heikin bar?

bidBar.getOpen(heikin) no work so as heikin is double.

Best regards,
vs_trade


 
 Post subject: Re: Built in Heikinashi indicator Post rating: 0   New post Posted: Tue 27 Mar, 2012, 08:32 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
vs_trade wrote:
My question was. How I can get "open, close, high and low" from heikin bar?
The example already shows that:
   
   console.getOut().println(arrayToString(heikin));

   public static String arrayToString(double[] arr) {
      String str = "";
      for (int r = 0; r < arr.length; r++) {
         str +=  getPriceName(r) + "=" + (new DecimalFormat("0.00000")).format(arr[r]) + ", ";
      }
      return str;
   }

   private static String getPriceName(int i){
      return  i == 0 ? "Open" : i == 1 ? "Close" : i == 2 ? "High" : i == 3 ? "Low" : "";
   }
Or without price names:
   console.getOut().println(heikin[0] + " " + heikin[1] + " "+ heikin[2] + " "+ heikin[3] + " ");


 
 Post subject: Re: Built in Heikinashi indicator Post rating: 0   New post Posted: Tue 27 Mar, 2012, 16:57 
User avatar

User rating: 0
Joined: Thu 25 Aug, 2011, 14:04
Posts: 45
Location: Russian Federation,
Hello Support Team,

Thanks for soon reply.

You wrote i == 0 ? "Open" : i == 1 ? "Close" : i == 2 ? "Low" : i == 3 ? "High" : "";

in HeikinAshiIndicator I find
.....
public static final int OPEN = 0;
public static final int CLOSE = 1;
public static final int HIGH = 2;
public static final int LOW = 3;
.....

where correct High = 3 or 2, Low = 2 or 3 ?

Thanks.

Best regards,
vs_trade


 
 Post subject: Re: Built in Heikinashi indicator Post rating: 0   New post Posted: Wed 28 Mar, 2012, 07:48 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
We have corrected the example


 
 Post subject: Re: Built in Heikinashi indicator Post rating: 0   New post Posted: Thu 28 Jun, 2012, 19:56 
User avatar

User rating: 1
Joined: Tue 26 Jun, 2012, 19:18
Posts: 29
Location: France, Saint Martin d Heres
Hello,

it seems to be an error in the provided example.

I think that the line 77:
             str += getPriceName(r) + (new DecimalFormat("0.00000")).format(arr[r][c]) + ", ";

must be
             str += getPriceName(c) + (new DecimalFormat("0.00000")).format(arr[r][c]) + ", ";


I also have a question regarding this specific example. Why every time that OnBar get calls, the last 10 heikin ashi candles that we obtain with the call:

      double[][] heikin10 = indicators.heikinAshi(instrument, period, OfferSide.BID, Filter.WEEKENDS, 10, bidBar.getTime(), 0);


are completely different from the 10 heikin ashi candles we obtained in the previous OnBar call?

I expected to see that some of them are actually the same. I expected to see this:

OnBar gets called at instant N
heinki10 = h1, h2, .... h10

OnBar gets called at instant N+1
heinki10 = h6, h7, .... h10, ... , h15

Or something similar, i.e., some of the heinki candles are the same, and I can keep track of the new heinki ashi candles between OnBar calls. But in the provided strategy all the heinki10 candles in the second call are completely different from those of the first call.

How can I keep track of all of the heinki candles?

Thank you very much,
Regards


 
 Post subject: Re: Built in Heikinashi indicator Post rating: 0   New post Posted: Mon 02 Jul, 2012, 08:51 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
SmithJr wrote:
Hello,

it seems to be an error in the provided example.

I think that the line 77:
             str += getPriceName(r) + (new DecimalFormat("0.00000")).format(arr[r][c]) + ", ";


must be
             str += getPriceName(c) + (new DecimalFormat("0.00000")).format(arr[r][c]) + ", ";

The example has been fixed.
SmithJr wrote:
I also have a question regarding this specific example. Why every time that OnBar get calls, the last 10 heikin ashi candles that we obtain with the call:
      double[][] heikin10 = indicators.heikinAshi(instrument, period, OfferSide.BID, Filter.WEEKENDS, 10, bidBar.getTime(), 0);

are completely different from the 10 heikin ashi candles we obtained in the previous OnBar call?
We could not replicate this. Please provide a more precise case, preferably in Historical Tester and please provide precise settings.
SmithJr wrote:
How can I keep track of all of the heinki candles?
Just increase the numberOfCandlesBefore parameter from 10 to your preferred one.


 
 Post subject: Re: Built in Heikinashi indicator Post rating: 0   New post Posted: Tue 03 Jul, 2012, 09:58 
User avatar

User rating: 1
Joined: Tue 26 Jun, 2012, 19:18
Posts: 29
Location: France, Saint Martin d Heres
Dear support,

please consider the attached strategy. It outputs the value of the last Heikin Ashi candle, and the three last Heikin Ashi candles simultaneously. It is based on your own example.

package jforex.strategies.algar; 

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
 
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
 
public class HeikinAshiTest implements IStrategy {
   private IEngine engine = null;
   private IIndicators indicators = null;
   private int tagCounter = 0;
   private IConsole console;
   private IHistory history;
   private IChart chart;
   private IOrder order;

   // strategy specific params
   @Configurable("Insturment")
   public Instrument instrument = Instrument.EURUSD;
   @Configurable("Period")
   public Period period = Period.DAILY;
 
   @SuppressWarnings("serial")
   private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") {{setTimeZone(TimeZone.getTimeZone("GMT"));}};
 
   public void onStart(IContext context) throws JFException {
      engine = context.getEngine();
      history = context.getHistory();
      indicators = context.getIndicators();
      this.console = context.getConsole();
      this.chart = context.getChart(instrument);
      console.getOut().println("Started");
 
      // Initialising the order
      this.order = null;
   
      // Adding the Heikin Ashi indicator to the chart
      // chart.addIndicator(indicators.getIndicator("HEIKINASHI"));

   }
 
   public void onStop() throws JFException {
      console.getOut().println("Stopped");
   }

   public void onTick(Instrument instrument, ITick tick) throws JFException {
      // double[][] heikin3 = indicators.heikinAshi(instrument, period, OfferSide.BID, Filter.WEEKENDS, 3, bidBar.getTime(), 0);
      // double[] heikin = indicators.heikinAshi(instrument, period, OfferSide.BID, 1);   
      // print("last 3 heikin ashi: " + arrayToString(heikin3));
      // print("last heikin ashi: " + arrayToString(heikin));
     
      // console.getOut().println(tick);
   }

   public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {

      // Filtering right period and instrument
      if (period != this.period || instrument != this.instrument)
         return;

      // Getting the Heikin Ashi candles
      double[][] heikin3 = indicators.heikinAshi(instrument, period, OfferSide.BID, Filter.WEEKENDS, 3, bidBar.getTime(), 0);
      double[] heikin = indicators.heikinAshi(instrument, period, OfferSide.BID, 1);
       
      // Printing candles
      print("last 3 heikin ashi: " + arrayToString(heikin3));
      print("last heikin ashi: " + arrayToString(heikin));
     
   }

   private void print(Object o) {
      console.getOut().println(o);
   }

   public static String arrayToString(double[] arr) {
      String str = "";
      for (int r = 0; r < arr.length; r++) {
         str +=  getPriceName(r) + "=" + (new DecimalFormat("0.00000")).format(arr[r]) + ", ";
      }
      return str;
   }

   public static String arrayToString(double [][] arr){
      String str = "";
      for (int r=0; r<arr.length; r++) {
         str += "[" + r + "]";
          for (int c=0; c<arr[r].length; c++) {
             str += getPriceName(c) + (new DecimalFormat("0.00000")).format(arr[r][c]) + ", ";
          }
          str += "; ";
      }
      return str;
   }

   private static String getPriceName(int i){
      return  i == 0 ? "Open" : i == 1 ? "Close" : i == 2 ? "High" : i == 3 ? "Low" : "";
   }

   protected String getLabel(Instrument instrument) {
      return (instrument.getPrimaryCurrency().toString() + instrument.getSecondaryCurrency().toString() + (tagCounter++)).toLowerCase();
   }
 
   public void onMessage(IMessage message) throws JFException {
       console.getOut().println("MESSAGE=" + message);
   }
 
   public void onAccount(IAccount account) throws JFException {
   }
}



The output for the "last week" with a Daily period is the following:

Quote:
08:39:01 Stopped
08:39:01 last 3 heikin ashi: [0]Open1.24808, Close1.24603, High1.25239, Low1.24071, ; [1]Open1.24706, Close1.25628, High1.26924, Low1.24323, ; [2]Open1.25167, Close1.26674, High1.26787, Low1.25167, ;
08:39:01 last heikin ashi: Open=1.26859, Close=1.26674, High=1.26859, Low=1.26502,
08:39:01 -----------------------------------------------------------------------------------------
08:39:01 last 3 heikin ashi: [0]Open1.24959, Close1.24768, High1.25079, Low1.24450, ; [1]Open1.24863, Close1.24603, High1.25239, Low1.24071, ; [2]Open1.24733, Close1.25628, High1.26924, Low1.24323, ;
08:39:01 last heikin ashi: Open=1.25017, Close=1.26859, High=1.26859, Low=1.25017,
08:39:01 -----------------------------------------------------------------------------------------
08:39:01 last 3 heikin ashi: [0]Open1.24959, Close1.24768, High1.25079, Low1.24450, ; [1]Open1.24863, Close1.24603, High1.25239, Low1.24071, ; [2]Open1.24733, Close1.25628, High1.26924, Low1.24323, ;
08:39:01 last heikin ashi: Open=1.24650, Close=1.25628, High=1.26924, Low=1.24323,
08:39:01 -----------------------------------------------------------------------------------------
08:39:00 last 3 heikin ashi: [0]Open1.25255, Close1.24895, High1.25300, Low1.24410, ; [1]Open1.25075, Close1.24768, High1.25079, Low1.24450, ; [2]Open1.24922, Close1.24603, High1.25239, Low1.24071, ;
08:39:00 last heikin ashi: Open=1.24808, Close=1.24603, High=1.25239, Low=1.24071,
08:39:00 -----------------------------------------------------------------------------------------
08:38:59 last 3 heikin ashi: [0]Open1.25450, Close1.25137, High1.25450, Low1.24709, ; [1]Open1.25293, Close1.24895, High1.25300, Low1.24410, ; [2]Open1.25094, Close1.24768, High1.25094, Low1.24450, ;
08:38:59 last heikin ashi: Open=1.24959, Close=1.24768, High=1.25079, Low=1.24450,
08:38:59 -----------------------------------------------------------------------------------------
08:38:58 last 3 heikin ashi: [0]Open1.25521, Close1.25439, High1.25580, Low1.25344, ; [1]Open1.25480, Close1.25137, High1.25480, Low1.24709, ; [2]Open1.25308, Close1.24895, High1.25308, Low1.24410, ;
08:38:58 last heikin ashi: Open=1.25255, Close=1.24895, High=1.25300, Low=1.24410,
08:38:58 -----------------------------------------------------------------------------------------
08:38:56 last 3 heikin ashi: [0]Open1.26477, Close1.25545, High1.26477, Low1.25188, ; [1]Open1.26011, Close1.25439, High1.26011, Low1.25344, ; [2]Open1.25725, Close1.25137, High1.25725, Low1.24709, ;
08:38:56 last heikin ashi: Open=1.25450, Close=1.25137, High=1.25450, Low=1.24709,
08:38:56 -----------------------------------------------------------------------------------------
08:38:55 Started


As you can see, computing at the same time the last three heikin ashi candles and the last heinsi candle provides different results. I expect to see that the last candle is one of the three returned by the last three candles, but this is not the case.

In other words, if calling the last three candles returns:
[0]candle A,
[1]candle B,
[2]candle C,

and the last candle call returns:
candle D,

I am surprised to see that D is different to A,B and C. I also see that all the time, candles D and C have the same CLOSE and LOW values, except for the very last candle just before the stop. So I guess that C must be D and there is a problem in the way one of them is computed.


You can check this looking for instance at the first calls:

Quote:
2012-07-03 08:38:58 -----------------------------------------------------------------------------------------
2012-07-03 08:38:56 last 3 heikin ashi: [0]Open1.26477, Close1.25545, High1.26477, Low1.25188, ; [1]Open1.26011, Close1.25439, High1.26011, Low1.25344, ; [2]Open1.25725, Close1.25137, High1.25725, Low1.24709, ;
2012-07-03 08:38:56 last heikin ashi: Open=1.25450, Close=1.25137, High=1.25450, Low=1.24709,
2012-07-03 08:38:56 -----------------------------------------------------------------------------------------
2012-07-03 08:38:55 Started


Note how the last of the three candles (what I called C in the example) has the values:
[2]Open1.25725, Close1.25137, High1.25725, Low1.24709

and the candle for the last heikin ashi call has the values
Open=1.25450, Close=1.25137, High=1.25450, Low=1.24709

and this pattern is the same for all the candles except for the last one before the stop.

I am using Linux with the very last version of JForex:
Dukascopy Europe ver 2.17.16
JForex API ver 2.6.71

Best regards,


 
 Post subject: Re: Built in Heikinashi indicator Post rating: 0   New post Posted: Wed 04 Jul, 2012, 14:43 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please use as the base case the HeikinAshiStrategy2 from our original post. The difference you get is because of the filters - the one of candle interval uses WEEKEND filter, but the one by shift - NO_FILTER, see the last sentences of:
https://www.dukascopy.com/wiki/#Indicator_Calculation/Candlestick_price_feed
See more on filters here:
https://www.dukascopy.com/wiki/#History_bars/Filter_usage


 

Jump to:  

  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com