2
$\begingroup$

I plot a trading chart by using TradingChart for early December data:

spy = FinancialData["SPY", "OHLCV", {{2020, 12, 1}, {2020, 12, 10}}];
TradingChart[spy, {"Volume"}]

This gives a nice chart:

chartimage

Note that when I put my cursor on each chart-bar, the volume indicator plotted below the chart also displays the corresponding value.

Now, suppose I already have a data set associated with date:

indicatorData = 
  {{{2020, 12, 1}, 1}, {{2020, 12, 2}, 2}, {{2020, 12, 3}, 3}, 
   {{2020, 12, 4}, 4}, {{2020, 12, 7}, 5}, {{2020, 12, 8},6},
   {{2020, 12, 9}, 7}, {{2020, 12, 10}, 8}}

I want to plot my indicatorData just below the trading chart with the same horizontal axis, exactly like the Volume indicator is plotted in the above image, so that the corresponding value of my indicator is displayed when the cursor is positioned on a chart-bar.

DateListPlot of my financial indicator is

mydataimage

I notice that my question is closely related to this question, where an original FinancialIndicator, using a complicated equation, is introduced.

But, my situation is slightly different. I already have numerical data in indicatorData. I do not need to calculate any values using equations. I only need to plot my data below the trading chart with appropriate behavior of the cursor.

I want to use my indicator exactly like Volume, SimpleMovingAverage, BolingerBands, etc. embedded in FinancialIndicator.

Updated question

I update my question by receiving an answer to the original question.

The dates of my actual data set (which is much longer than the example data set of indicatorData) is not equal to the dates of SPY financial data.

For simplicity, say, my actual data is like

actualData = {{{2020, 12, 1}, 1}, {{2020, 12, 3}, 3}, {{2020, 12, 4}, 4}, 
{{2020, 12, 7}, 5}, {{2020, 12, 8},6}, {{2020, 12, 9}, 7}}

As you can see, datas for {2020,12,3} and {2020,12,10} are missing in the actual data compared to the example indicatorData. In this case, an elegant answer by kglr does not work. I want to plot my indicator corresponding to the date appropriately.

$\endgroup$
0

2 Answers 2

2
$\begingroup$
myIndicator = FinancialIndicator[TimeSeries[indicatorData] &, 
   "LineStyle" -> Blue, "Filling" -> Bottom, "IndexLine" -> 5, 
   "RenderingElement" -> "Step"];

TradingChart[spy, {myIndicator}, ImageSize -> Large]

enter image description here

Use "RenderingElement" -> "Line" to get:

enter image description here

You can alternatively use TemporalData[indicatorData] & instead of TimeSeries[indicatorData] &.

$\endgroup$
1
  • $\begingroup$ Thank you for your elegant solution. I updated my question by receiving your reply, because my actual data set is indeed more complicated than the example data. I clarified this point in the update. I hope you will be able to provide the information. $\endgroup$ Commented Jan 13, 2021 at 16:16
2
$\begingroup$

Adding to kgir answer, this is an attempt to get better control on the name appearing on the chart (here myIndic). A bar chart is used for this example.

spy = FinancialData["SPY", "OHLCV", {{2020, 12, 1}, {2020, 12, 10}}];
indicatorData = {{{2020, 12, 1}, 1}, {{2020, 12, 2}, 
    2}, {{2020, 12, 3}, 3}, {{2020, 12, 4}, 4}, {{2020, 12, 7}, 
    5}, {{2020, 12, 8}, 6}, {{2020, 12, 9}, 7}, {{2020, 12, 10}, 8}};
myIndic[_] := TemporalData[indicatorData]
indic = FinancialIndicator[myIndic, "LineStyle" -> Red, 
   "IndexLine" -> 5, "RenderingElement" -> "Bar"];

TradingChart[spy, {"Volume", indic}, ImageSize -> Large]

enter image description here

Part 2 of the question has to do with data with missing dates. This requires resampling over the whole range of BusinessDay. Normally, the ResamplingMethod should be None, which replaces missing values with Missing[] and ignores the data in the graphic. Although the replacement works, for some reason I can't get it to produce the required graphic. So I used the next best thing: set missing values to 0 for that day. This works well with a bar chart, but with a line, the 0 is not ignored.

actualData = {{{2020, 12, 1}, 1}, {{2020, 12, 3}, 3}, {{2020, 12, 4}, 4}, 
{{2020, 12, 7}, 5}, {{2020, 12, 8},6}, {{2020, 12, 9}, 7}};
resampled = 
 TimeSeriesResample[
  TemporalData[
   actualData], {DateRange[{2020, 12, 1}, {2020, 12, 10}, 
    "BusinessDay"]}, ResamplingMethod -> {"Constant", 0}]
spy = FinancialData["SPY", "OHLCV", {{2020, 12, 1}, {2020, 12, 10}}];
myIndic[_] := resampled
indic = FinancialIndicator[myIndic, "LineStyle" -> Red, 
   "IndexLine" -> 5, "RenderingElement" -> "Bar"];

TradingChart[spy, {"Volume", indic}, ImageSize -> Large]

enter image description here

$\endgroup$
2
  • $\begingroup$ Your operation is what I also wanted to do. Thank you. Since I updated my question, I hope you will be able to provide the information. $\endgroup$ Commented Jan 13, 2021 at 16:18
  • $\begingroup$ Inspired by your answer, I came up with ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}, i.e., I interpolated missing data, then, it worked. Interpolation is good enough for my purpose. I appreciate your support. $\endgroup$ Commented Jan 14, 2021 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.