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

Message ListMessage List     Post MessagePost Message

  Volume Moving Average
Posted by Madhu Khemani on Dec-02-2011 03:31
Attachments:
Hi Peter,

Trying to add a 50-period moving average to the volume bars in a finance chart, using PHP
Chartdirector v4.x. Tried to copy an example from the forum but can't get this to work:


#Add a 70 pixels volume bars sub-chart to the bottom of the main chart, using
#green/red/grey for up/down/flat days
$c->addVolBars($vChartHeight, 0x66cd00, 0xff0000, 0x808080);

#Add volume moving average
$myVolBarChart = $c->addVolIndicator($vChartHeight, 0x99ff99, 0xff9999, 0x808080);
$myMovingAvg = ArrayMath($volData).movAvg(50).result();
$c->addLineIndicator2($myVolBarChart, $myMovingAvg, 0xcc6600, "SMA(50)").moveFront();

Your help will be appreciated.

Thanks in advance,

Madhu
wcg.png

  Re: Volume Moving Average
Posted by Peter Kwan on Dec-03-2011 01:43
Hi Madhu,

The code you use seems to be translated from another programming language (it seems to contain code that is not correct in PHP syntax). Anyway, the method you use only works with ChartDirector Ver 5, so they would not work in your code even if translated to PHP correctly.

There is an example in ChartDirector Ver 5 in C# in:

http://www.chartdir.com/forum/download_thread.php?bn=chartdir_support&thread=1277859360#N1277918132

If you would consider to upgrade to ChartDirector Ver 5, I can translate the above code to PHP for you.

If you would prefer to use ChartDirector Ver 4.x, you may try the followings:

# This is your existing main price chart line. Save the returned XYChart object to $mc
$mc = $c->addMainChart(240);

... add moving averages, candlesticks, OHLC bars, ....

#
#add 50 day volume average line
#
$am = new ArrayMath($volData);
$maxVol = $am->max();
if ($maxVol >= 1000000000)
   $am->div(1000000000);
else if ($maxVol >= 1000000)
   $am->div(1000000);
else if (maxVol >= 1000)
   $am->div(1000);

$tmp = $am->movAvg(50);
$myMovAvg = $tmp->result();
$layer = $c->addLineIndicator2($mc, $myMovAvg, 0xcc6600, "Vol SMA (50)");
$layer->setUseYAxis2();

# Add the volume bars
$c->addVolBars(75, 0x99ff99, 0xff9999, 0x808080);


Hope this can help.

Regards
Peter Kwan

  Re: Volume Moving Average
Posted by Madhu Khemani on Dec-03-2011 02:22
Attachments:
Hi Peter,

Thank you. I got it to work with my version 4.x. (I had tried upgrading some time back, but
ran into some issues - can't exactly remember what they were).

In any case, the volume moving average shows up correctly if the volume scale is in
millions. However, if the volume is in the thousands, the volume bars disappear for me.

Any suggestions?

Thanks for your excellent support (and product).

Best regards,

Madhu
sxl.png
orly.png

  Re: Volume Moving Average
Posted by Madhu Khemani on Dec-03-2011 03:49
Peter - Everything works fine now. There was a minor syntax error. Thanks, Again.

Madhu