I want to make a stock chart by using TradingChart
with my own data. But, the imported data is in the format
price={{MM/DD/YYYY1,open1, high1, low1, close1}, {MM/DD/YYYY2,open2, high2, low2, close2}, {MM/DD/YYYY3,open3, high3, low3, close3}...}
I want to partition my list into the TradingChart format as
price={date1,{open1,high1,low1,close1,volume1}},…}
But I have no idea how to partition with different size like the format. Could you please help me? Another question is in this case, can I use
DatePattern[{"Month", "Day", "Year"}, "/"]
in order to not change all my date, which is in MM/DD/YYYY
. My understanding is if I use DatePattern[{"Month", "Day", "Year"}, "/"]
, I don't have to change the date from MM/DD/YYYY
to {y,m,d}
, is it correct? Thank you in advance.
2 Answers
There are a lot of ways. Here is just one.
Transpose[{DateList[{#, {"Month", "Day", "Year"}}] & /@
price[[All, 1]], {##, 0} & @@@ price[[All, 2 ;;]]}]
Example:
price = {{"10/10/2015", Sequence @@ RandomReal[{90, 100}, 4]},
{"10/11/2015", Sequence @@ RandomReal[{90, 100}, 4]},
{"10/12/2015", Sequence @@ RandomReal[{90, 100}, 4]}}
{{"10/10/2015", 99.4675, 91.3724, 99.8884, 94.7369}, {"10/11/2015", 94.7509, 91.7196, 91.8822, 91.2132}, {"10/12/2015", 99.3705, 97.0185, 96.3484, 92.8318}}
TradingChart[
Transpose[{DateList[{#, {"Month", "Day", "Year"}}] & /@
price[[All, 1]], {##, 0} & @@@ price[[All, 2 ;;]]}]]
A possible Composition
(extended version of the comment by J.M.) is
Composition[Through, {DateList[{First@#, {"Month", "Day", "Year"}}] &, Append[0]@*Rest}] /@
price
Borrowing the data from @Karsten7
Off[DateList::ambig]
TradingChart[{DateList@#1, {##2, 0}} & @@@ price]
Composition[Through, {First, Rest}] /@ price
. $\endgroup$