Heiken Ashi Open formula

Discussion in 'Strategy Building' started by luxor, Jul 31, 2006.

  1. luxor

    luxor

    I'm trying to write my own strategy code in Java but an having trouble with the Heiken Ashi "Open" value. The formula is this:

    (HAOpen[1]+HAClose[1])/2

    But this appears to be recursive. How do you find HAOpen when you need it in your formula?

    Thanks!
     
  2. schan

    schan

    haOpen is the open of the previous bar. If you are on the first bar of the data series, just use the open.

    Same goes for the haClose.
     
  3. Here it is in C++

    Code:
    /***********************************************************************/
    extern "C" __declspec( dllexport ) void jPrice(struct s_sg &sg)
    {
    	sg.GraphName="JPrices";
    	sg.Subgraph[0].Name="O";
    	sg.Subgraph[1].Name="H";
    	sg.Subgraph[2].Name="L";
    	sg.Subgraph[3].Name="C";
    
    	sg.FreeDLL=0; if(sg.SetDefaults) {return;}
    	if(sg.ArraySize<100) return;
    	sg.UsePriceGraphStyle=1;
    
    	sg.Input[2].Name="Print Up & Down Bars (0), Up (1), Dn (-1)";
    
    
    	int    pos, updn=0;
    	float  jO=0, jH, jL, jC=0;
    	sg.DataStartIndex=15;
     
    	for (pos=15; pos < sg.ArraySize; pos++)
    	{
    
    		jO=(jO+jC)/2;
    		jC=(sg.BaseDataIn[0][pos]+sg.BaseDataIn[1][pos]+sg.BaseDataIn[2][pos]+sg.BaseDataIn[3][pos])/4;
    
    		if(jO>sg.BaseDataIn[1][pos] && jO>jC) jH=jO; else if(jC>sg.BaseDataIn[1][pos]) jH=jC; else jH=sg.BaseDataIn[1][pos];
    		if(jO<sg.BaseDataIn[2][pos] && jO<jC) jL=jO; else if(jC<sg.BaseDataIn[2][pos]) jL=jC; else jL=sg.BaseDataIn[2][pos];
    
    		sg.Subgraph[5].Data[pos]=jH;
    		sg.Subgraph[6].Data[pos]=jL;
    
    		if(jH>sg.Subgraph[5].Data[pos-1] && jL>=sg.Subgraph[6].Data[pos-1]) {updn=1;} else
    		if(jL < sg.Subgraph[6].Data[pos-1] && jH<=sg.Subgraph[5].Data[pos-1]) {updn=-1;};
    
    		if((sg.Input[2].FloatValue!=1 && sg.Input[2].FloatValue!=-1) || (sg.Input[2].FloatValue==1 && updn==1) || (sg.Input[2].FloatValue==-1 && updn==-1))
    		{
    			sg.Subgraph[0].Data[pos]=jO;
    			sg.Subgraph[1].Data[pos]=jH;
    			sg.Subgraph[2].Data[pos]=jL;
    			sg.Subgraph[3].Data[pos]=jC;
    		};
    	}
    };
     
  4. One way of getting around your problem is to set an initial condition for the first bar of the sequence.

    For example, in tradestation you could write:

    If CurrentBar = 1 Then Begin
    HA_O = O;
    HA_H = H;
    HA_L = L;
    HA_C = C;
    End;

    If CurrentBar > 1 Then Begin
    HA_C = (O+H+L+C)/4;
    HA_O = (HA_O[1] + HA_C[1]) / 2;
    HA_H = MaxList(H, HA_O, HA_C);
    HA_L = MinList(L, HA_O, HA_C);
    End;

    Hope this helps,
    RoughTrader
     
  5. luxor

    luxor

    Thanks for the info guys! It worked.
     
  6. Great question, simple and clean answers.

    BTW, nice logic there Rough Trader, it corrects the thinking as well as showing how to execute the coding.

    Thanks,

    Jimmy