ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Custom Indicator
Posted by John on Jul-09-2010 11:03
Hello,

I'm trying to add ichimoku indicator in chartdir, which is missing, by adding my own custom indicator code. The formula is:

(Highest High of 9 days - Lowest low of 9 days)/2

So, I've written code as

    function addIchimoku($color) {
         $label = "Ichimoku (9)";

  $tmpArrayMath1 = new ArrayMath($this->m_highData);
          $tmpArrayMath1->movMax(9);

  $tmpArrayMath2 = new ArrayMath($this->m_lowData);
          $tmpArrayMath2->movMin(9);

  $tmpArrayMath3 =( $tmpArrayMath1->result -  $tmpArrayMath2->result )/2;


        return $this->addLineIndicator2($this->m_mainChart, $tmpArrayMath3->result(), $color, "Ichi"  );
    }

Something is wrong above. I think I need the code to subtract two arrays.

Please look in.
Thanks

  Re: Custom Indicator
Posted by John on Jul-09-2010 11:23
Ok. I've modified the code as follows now:

    function addIchimoku($color) {
        $label = "Ichimoku (9)";

  $tmpArrayMath1 = new ArrayMath($this->m_highData);
        $tmpArrayMath1->movMax(9);

  $tmpArrayMath2 = new ArrayMath($this->m_lowData);
        $tmpArrayMath2->movMin(9);


for ($i=0;$i<=count($tmpArrayMath1);$i++)
{
for ($k=0;$k<=count($tmpArrayMath2);$k++)
{

$tmpArrayMath3[$i] = ($tmpArrayMath1[$i] - $tmpArrayMath2[$k])/2;
}

}




        return $this->addLineIndicator2($this->m_mainChart, $tmpArrayMath3->result(), $color, "Ichi"  );
    }


But this also gave error as "Fatal error: Cannot use object of type ArrayMath as array in ......"

Please help.
Thanks

  Re: Custom Indicator
Posted by Peter Kwan on Jul-09-2010 16:47
Hi John,

The PHP function like "count" and indexing like $tmpArrayMath1[$i] only works for arrays. From your code, $tmpArrayMath1 is not an array. It is an ArrayMath object.

If you want to use PHP array functions, please use arrays instead.

Also, you latest code is even attempting to substract two arrays. It is subtracting either element of the first array with all the elements of the second array.

The correct code is:

$tmpArrayMath1 = new ArrayMath($this->m_highData);
$tmpArrayMath1->movMax(9);

$tmpArrayMath2 = new ArrayMath($this->m_lowData);
$tmpArrayMath2->movMin(9);

#If you want to use PHP array functions - use arrays, not ArrayMath objects
$myArray1 = $tmpArrayMath1->result();
$myArray2 = $tmpArrayMath2->result();

for ($i=0;$i<=count($myArray1);$i++)
    $myArray3[$i] = ($myArray1[$i] - $myArray2[$i])/2;

return $this->addLineIndicator2($this->m_mainChart, $myArray3, $color, "Ichi");

Hope this can help.

Regards
Peter Kwan

  Re: Custom Indicator
Posted by John on Jul-09-2010 20:56
Thanks Peter. Got your point. It's working

Thanks a lot.

  Re: Custom Indicator
Posted by John on Sep-19-2010 02:21
Attachments:
Hello Peter,

Just implemented the code on my chart. It's just going down to zero in the last.

Secondly, can we just increase the height of candles to make the chart more clear. It's little bit confusing.

Thanks
chart.PNG

  Re: Custom Indicator
Posted by Peter Kwan on Sep-19-2010 14:15
Hi John,

I think there is an error in the computation of the "Ichi" indicator. Your indicator seems to closely fllow the price, but the last value of the indicator is 0 in your chart, which seems abnormal.

If your code is copied from the code I suggest earlier, note that there is an error. The code "$i<=count($myArray1)" should be "$i<count($myArray1)".

(The above error does not produce unusual scale in the original code, because in the original code, it is computing the differences between two moving averages, which ocillates around 0, so 0 is a natural value in the scale. In your current case, you seem to be computing something else and plots it in the main price chart, and 0 is not a natural value there.)

Hope this can help.

Regards
Peter Kwan

  Re: Custom Indicator
Posted by Ajmal on Apr-04-2012 17:18
I tried adding a band to it with this code.. getting a error.. after defining line color fill color
etc..?

return $this->addBand($this->m_mainChart, $myArray1->result(), $myArray2->result(),
$myArray3->result(), $lineColor,
            $fillColor, $label);

  Re: Custom Indicator
Posted by Peter Kwan on Apr-05-2012 02:33
Hi Ajmal,

Would you mind to clarify what is $this in your code, and what is "addBand" in your code? Are you trying to use the addBand method in the FinanceChart object?

(The code in this thread is for modifying or extending the FinanceChart class library, which may or may not apply to you. According to the ChartDirector documentation, the FinanceChart.addBand method only takes 5 parameters "addBand(upperLine, lowerLine, lineColor, fillColor, name)", not 7 parameters as in your code. It only needs two data series, and it fills the region between these two series. An example is:

return $myFinanceChartObj->addBand($myArray1->result(), $myArray2->result(), $lineColor, $fillColor, $label);

For your case, would you mind to clarify exactly what you would like to plot (why there are 3 data series)?

Regards
Peter Kwan

  Re: Custom Indicator
Posted by Ajmal on Apr-05-2012 10:29
Thanks Peter..

Basically I am looking at plotting Ichimoku cloud... on the main chart.. I tried using teh
above given formula.. but just a ichimoku data is seen and also there is a line along the
volume bar.. which doesnt make sense to me..

$this is the main chart..

I tried editing the code as advised by u but i see no band..

  Re: Custom Indicator
Posted by Peter Kwan on Apr-06-2012 00:18
Hi Ajmal,

You mentioned "$this is the main chart". I assume you mean "$this" refers to FinanceChart object (so that "$this->m_mainChart" refers to the main price chart within the FinanceChart).  As "$this" must be the object that contains the code, so your code must be part of the FinanceChart. In other words, you are modifying or subclassing the FinanceChart class library by (directly inserting code in FinanceChart.php or to subclass it).

In any case, the code:

$myFinanceChartObj->addBand($myArray1->result(), $myArray2->result(), $lineColor, $fillColor, $label);

should insert the band in the main price chart of the FinanceChart. In the above, I assume $myFinanceChartObj is the FinanceChart object you are using. However, if in your case, you are really modifying the FinanceChart, and $this is the FinanceChart object, you can use $this instead of $myFinanceChartObj.

If the above does not produce any band, please check if your data are correct. I instead of the above code, you use:

$myFinanceChartObj->addBand($highData, $lowData, 0xff0000, 0x8000ff00, "ABCD");

The above should add a band between the $highData and $lowData. To make sure your data are correct, before calling the above code, please add the main price chart (use FinanceChart.addMainChart) and add a candlestick layer to the main price chart. If you can see the candlesticks, this proves that the $highData and $lowData are correct, and there should be the band. If there is no candlesticks but no band, please kindly inform me your exact testing code. I will try to diagnose the problem.

For the Ichimoku cloud, I searched from the Internet, and for the samples I found, it is not really a price band (in the sense of a Bollinger band, Donchian channel, etc). It seems to be two indicator lines, which do not necessarily form a band (they can interest and reverse order). The samples I found have colors between the lines depending on which line is higher. For this type of representation, and InterLineLayer is more appropriate (see the Inter-Line Coloring sample code for an example of using the InterLineLayer).

For the InterLineLayer in a FinanceChart, the code should be like:

#assume this is your existing addMainChart code
$mainChart = $myFinanceChartObj->addMainChart(............);

#add the two lines
$line1 = $myFinanceChartObj->addLineIndicator2($mainChart, $topDataSeries, 0x0000ff, "Top Line");
$line2 = $myFinanceChartObj->addLineIndicator2($mainChart, $bottomDataSeries, 0x888888, "Bottom Line");

#fill the region between the two lines
$mainChart->addInterLinelayer($line1->getLine(), $line2->getLine(), 0x80ff9999, 0x8099ff99);

Hope this can help.

Regards
Peter Kwan

  Re: Custom Indicator
Posted by Ajmal on Apr-07-2012 20:13
Hello Peter..

I worked on the code you had given but couldnt get the Ichimoku cloud indicator to work..

From what i could read Ichimoku has 5 inidcators  lines...
http://www.traderslog.com/ichimoku/

None the less there is example in CD of Ichimoku Cloud http://nseguide.com/charts.php?
symbol=NIFTY even though it is not complete.. I think if u guide we can code it
accordingly..

  Re: Custom Indicator
Posted by Ajmal on Apr-07-2012 21:03
Working on the lines now..

How to plot 26 periods into the future

Tenkan-sen (Conversion Line): (9-period high + 9-period low)/2))
The default setting is 9 periods and can be adjusted. On a daily
chart, this line is the mid point of the 9 day high-low range,
which is almost two weeks.

Kijun-sen (Base Line): (26-period high + 26-period low)/2))
The default setting is 26 periods and can be adjusted. On a daily
chart, this line is the mid point of the 26 day high-low range,
which is almost one month).

Senkou Span A (Leading Span A): (Conversion Line + Base Line)/2))
This is the midpoint between the Conversion Line and the Base Line.
The Leading Span A forms one of the two Cloud boundaries. It is
referred to as "Leading" because it is plotted 26 periods in the future
and forms the faster Cloud boundary.

Senkou Span B (Leading Span B): (52-period high + 52-period low)/2))
On the daily chart, this line is the mid point of the 52 day high-low range,
which is a little less than 3 months. The default calculation setting is
52 periods, but can be adjusted. This value is plotted 26 periods in the future
and forms the slower Cloud boundary.

Chikou Span (Lagging Span): Close plotted 26 days in the past
The default setting is 26 periods, but can be adjusted.

  Re: Custom Indicator
Posted by Peter Kwan on Apr-10-2012 01:28
Hi Amjal,

In the FinanceChart source code, you can see how all the indicators are plotted. They are all plotted using similar ways. The only difference are how they are calculated.

For your case, first, you would need to calcuate the Ichimoku. Please make sure your code calculates it correctly. Before you try to plot the chart, may be you can write a separate program that just prints out the value as text, and verify if your code can correctly calculate the values.

Note that we cannot write the code to compute the Ichimoku indicator for you. You would need to compute it with your own code.

To get started, you may try to plot the line one at a time. For example, you may try to see if you can plot the Tenkan-sen first.

If you have computed an indicator data array, and you would like to plot every point in the array 26 period in the future, you may insert 26 NoValue point at the start of the array. (If you use ArrayMath, you may use ArrayMath.insert2).

Regards
Peter Kwan