Re: financial chart with volumes
- To: mathgroup at smc.vnet.net
- Subject: [mg93866] Re: financial chart with volumes
- From: Andreas <aagas at ix.netcom.com>
- Date: Thu, 27 Nov 2008 05:32:28 -0500 (EST)
I took a further if not quite elegant shot at a candle stick chart. dateOffset = 100; datesStartEnd = {DatePlus[DateList[], -dateOffset], DateList[]}; instrument = "IBM"; (* The following uses David Parks' GetFinancialData[] function (reproduced at the end of this post):*) financialData = GetFinancialData[ instrument, {"Open", "High", "Low", "Close"}, {DatePlus[DateList[], -dateOffset], DateList[], "Day"}]; (* The next 6 lines take the financial data and through a series of steps form 2 data lists: openAboveClose and closeAboveOpen. They each have a list of opening prices and a list of closing prices, but only for the relevant dates. When we plot these below they will make up the different colored candle bodies. I know, the code looks ridiculously complicated to me. Mathematica must have a more elegant way to generate these data sets. I tried to use Select[] but to no avail... Any suggestions welcome.*) t1 = financialData[[All, 2]] - financialData[[All, 5]]; t2 = Partition[ Flatten[Table[{financialData[[i]], t1[[i]]}, {i, 1, Length[financialData]}][[All]], 2], 6]; t3a = DeleteCases[ Table[If[t2[[i, 6]] > 0, t2[[i]]], {i, 1, Length[t2]}], Null]; t3b = DeleteCases[ Table[If[t2[[i, 6]] <= 0, t2[[i]]], {i, 1, Length[t2]}], Null]; openAboveClose = {t3a[[All, ;; 2]], t3a[[All, {1, 5}]]}; closeAboveOpen = {t3b[[All, ;; 2]], t3b[[All, {1, 5}]]}; (* The following produces a 2 data set list to generate the high to low line of the candlestick chart *) highLow = {FinancialData[instrument, "Low", datesStartEnd], FinancialData[instrument, "High", datesStartEnd]}; (* Ploting proceeds quite directly, 3 overlaid DateListPlots: 1 for the high to low line, 1 for days where the market opens higher than its close, and 1 for the reverse. The candle bodies scale relative to both the number of intervals plotted and the ImageSize of the overall chart. *) Show[ {DateListPlot[highLow , Filling -> {1 -> {2}}, FillingStyle -> Directive[Black, Thin], PlotMarkers -> {"", ""}], DateListPlot[openAboveClose , Filling -> {1 -> {2}}, FillingStyle -> Directive[Red, Thickness[.0075*100/dateOffset], Opacity[1]], PlotMarkers -> {"", ""}], DateListPlot[closeAboveOpen , Filling -> {1 -> {2}}, FillingStyle -> Directive[Blue, Thickness[.0075*100/dateOffset], Opacity[1]], PlotMarkers -> {"", ""}] }, ImageSize -> 400, PlotLabel -> instrument ] _________________ One can easily change the color of the candles, and they stay readable with quite a few bars. To summarize the strategy, I broke up the data into pieces which would each have different graphic characteristics in the final chart, then assembled the pieces using Show[]. One could combine this with some of the other responses in this thread to display volume or other indicators. Again, if anyone has any ideas for producing the data sets a bit more elegantly, I'd much appreciate it. (I've wanted to do this for a long time ;-) A. _________________ 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]] ]