financial chart with volumes

• To: mathgroup at smc.vnet.net
• Subject: [mg97139] financial chart with volumes
• From: mike.honeychurch at gmail.com
• Date: Fri, 6 Mar 2009 04:25:14 -0500 (EST)

```There was an interesting thread on Nov 24 about including volumes with
price to create a "typical" financial plot. The code below makes some
changes to the various codes posted back then in order to "trick" the
output to display volume ticks and scale.

Mike

------------

Clear[getFinancialData];

(* getFinancialData was written by David Park *)

getFinancialData::usage =
"GetFinancialData[name, {property1,property2...},iterator] will \
generate a data set where each record will contain {datelist, \
propertyvalues..}. The iterator is the standard {start,end, period} \
used in FinancialData. This is basically a method of retrieving a \
number of property values at once.";

SyntaxInformation[
getFinancialData] = {"ArgumentsPattern" -> {_, {_, __}, _}};

getFinancialData[name_String, {first_String, rest__String},
iterator_List] := Module[{work, other},
work = Transpose@FinancialData[name, first, iterator];
other = FinancialData[name, #, iterator, "Value"] & /@ {rest};
Transpose[Join[work, other]]
]
(* volume label *)

label /: MakeBoxes[label[x_, y_], StandardForm] :=
RowBox[{SuperscriptBox[MakeBoxes[x, StandardForm],
MakeBoxes[y, StandardForm]]}];

ClearAll[financialChart1];
(* this function is a hack of something I saw on MathGroup *)
(* \
candelstick type chart with volumes *)

financialChart1[company_String, startDate_List] :=
financialChart1[company, startDate, Take[DateList[], 3]];

financialChart1[company_String, startDate_List, endDate_List] :=
Module[{dateForm, data, open, high, highest, close, low, lowest,
openFall, closeFall, openRise, closeRise, volume, maxVolume, ex,

dateForm = {"Day", " ", "MonthNameShort", " ", "YearShort"};
data =
getFinancialData[
company, {"Open", "High", "Low", "Close", "Volume"}, {startDate,
endDate}];
open = data[[All, {1, 2}]];
high = data[[All, {1, 3}]];
highest = Max[data[[All, 3]]];
close = data[[All, {1, 4}]];
low = data[[All, {1, 5}]];
lowest = Min[data[[All, 5]]];

(* create lists: Open on falling day, Close on Falling day,
Open in rising day, Close on rising day *)

{openFall, closeFall, openRise, closeRise} =
Map[If[#[[2]] >= #[[
5]], {{#[[1]], #[[2]]}, {#[[1]], #[[5]]}, {#[[1]],
Null}, {#[[1]], Null}}, {{#[[1]], Null}, {#[[1]],
Null}, {#[[1]], #[[2]]}, {#[[1]], #[[5]]}}] &, data] //
Transpose;

(*volumes*)
volume = data[[All, {1, 6}]];
maxVolume = Max[data[[All, 6]]];
ex = IntegerLength[maxVolume] - 1;
maxVolumeScale = Ceiling[maxVolume*10^-ex];
highest*0.25*(#1[[2]]/maxVolume - 1.)} &) /@ volume;

(* ticks *)
left[xmin_, xmax_] :=
Join[Table[{x,
ToString[
maxVolumeScale*(1 - x/Floor[xmin]), {2, 1}]], {0.01, 0}}, {x,
Floor[xmin], -0.01, -Floor[xmin]/4.}],
Table[{x, ToString[x], {0.01, 0}}, {x, 0, 5*Ceiling[xmax/5],
Ceiling[xmax/5]}]];

right[xmin_, xmax_] :=
Table[{x, "", {0.01, 0}}, {x, 0, 5*Ceiling[xmax/5],
Ceiling[xmax/5]}];

(* plot *)

openRise, closeRise},
Filling -> {1 -> {{2}, Directive[Black, AbsoluteThickness[1]]},
3 -> {-highest*0.25, {{Thick, Darker[Blue, .6]}}},
4 -> {{5}, Directive[Red, AbsoluteThickness[2.5]]},
6 -> {{7}, Directive[Green, AbsoluteThickness[2.5]]}},
GridLines -> {Automatic,
Function[{min, max},
Join[Range[Floor[min], 0, -Floor[min]/4.],
Range[0, Ceiling[max],
Ceiling[max/5]], {{0, {Black, AbsoluteThickness[1]}}}]]},
PlotMarkers -> {"", "", "", "", "", "", ""},
FrameLabel -> {None,
"Vol. (\[Times] " <> ToString[label[10, ex], StandardForm] <>
")\t\t\tPrice (US\$)\t\t"},
PlotLabel ->
FinancialData[company, "Name"] <> ", " <>
DateString[startDate, dateForm] <> " - " <>
DateString[endDate, dateForm],
ImageSize -> 500,
PlotRange -> {Automatic, {-highest*0.25, Automatic}},
FrameTicks -> {Automatic, left, Automatic, right}]];

```

• Prev by Date: L-shaped ListContourPlot
• Next by Date: RE: Epilog doesn't work with loglinearplot
• Previous by thread: Re: L-shaped ListContourPlot