|
79 | 79 |
|
80 | 80 | include(DOCS_RESOURCES."/history/tradebars.php"); |
81 | 81 | ?> |
82 | | - |
83 | | -<p class='csharp'> |
84 | | - To get historical <a href='<?=$dataTypeLink?>'>trade data</a>, call the <code>History<<?=$dataType?>></code> method with an asset's <code>Symbol</code>. |
85 | | -</p> |
86 | | - |
87 | | -<p class='python'> |
88 | | - To get historical <a href='<?=$dataTypeLink?>'>trade data</a>, call the <code>history</code> method with the <code><?=$dataType?></code> type and an asset's <code>Symbol</code>. |
89 | | - This method returns a DataFrame with columns for the open, high, low, close, and volume. |
90 | | -</p> |
91 | | - |
92 | | -<div class="section-example-container"> |
93 | | - <pre class="csharp">public class USEquityTradeBarHistoryAlgorithm : QCAlgorithm |
94 | | -{ |
95 | | - public override void Initialize() |
96 | | - { |
97 | | - SetStartDate(2024, 12, 19); |
98 | | - // Get the Symbol of an asset. |
99 | | - var symbol = AddEquity("SPY").Symbol; |
100 | | - // Get the 5 trailing daily <?=$dataType?> objects of the asset. |
101 | | - var history = History<<?=$dataType?>>(symbol, 5, Resolution.Daily); |
102 | | - // Iterate through each TradeBar and calculate its dollar volume. |
103 | | - foreach (var bar in history) |
104 | | - { |
105 | | - var t = bar.EndTime; |
106 | | - var dollarVolume = bar.Close * bar.Volume; |
107 | | - } |
108 | | - } |
109 | | -}</pre> |
110 | | - <pre class="python">class USEquityTradeBarHistoryAlgorithm(QCAlgorithm): |
111 | | - |
112 | | - def initialize(self) -> None: |
113 | | - self.set_start_date(2024, 12, 19) |
114 | | - # Get the Symbol of an asset. |
115 | | - symbol = self.add_equity('SPY').symbol |
116 | | - # Get the 5 trailing daily <?=$dataType?> objects of the asset in DataFrame format. |
117 | | - history = self.history(<?=$dataType?>, symbol, 5, Resolution.DAILY)</pre> |
118 | | -</div> |
119 | | - |
120 | | -<table border="1" class="dataframe python"> |
121 | | - <thead> |
122 | | - <tr style="text-align: right;"> |
123 | | - <th></th> |
124 | | - <th></th> |
125 | | - <th>close</th> |
126 | | - <th>high</th> |
127 | | - <th>low</th> |
128 | | - <th>open</th> |
129 | | - <th>volume</th> |
130 | | - </tr> |
131 | | - <tr> |
132 | | - <th>symbol</th> |
133 | | - <th>time</th> |
134 | | - <th></th> |
135 | | - <th></th> |
136 | | - <th></th> |
137 | | - <th></th> |
138 | | - <th></th> |
139 | | - </tr> |
140 | | - </thead> |
141 | | - <tbody> |
142 | | - <tr> |
143 | | - <th rowspan="5" valign="top">SPY</th> |
144 | | - <th>2024-12-12 16:00:00</th> |
145 | | - <td>604.33</td> |
146 | | - <td>607.160</td> |
147 | | - <td>604.33</td> |
148 | | - <td>606.64</td> |
149 | | - <td>24962360.0</td> |
150 | | - </tr> |
151 | | - <tr> |
152 | | - <th>2024-12-13 16:00:00</th> |
153 | | - <td>604.21</td> |
154 | | - <td>607.100</td> |
155 | | - <td>602.82</td> |
156 | | - <td>606.38</td> |
157 | | - <td>29250856.0</td> |
158 | | - </tr> |
159 | | - <tr> |
160 | | - <th>2024-12-16 16:00:00</th> |
161 | | - <td>606.79</td> |
162 | | - <td>607.775</td> |
163 | | - <td>605.22</td> |
164 | | - <td>606.00</td> |
165 | | - <td>33686372.0</td> |
166 | | - </tr> |
167 | | - <tr> |
168 | | - <th>2024-12-17 16:00:00</th> |
169 | | - <td>604.29</td> |
170 | | - <td>605.160</td> |
171 | | - <td>602.89</td> |
172 | | - <td>604.22</td> |
173 | | - <td>38527534.0</td> |
174 | | - </tr> |
175 | | - <tr> |
176 | | - <th>2024-12-18 16:00:00</th> |
177 | | - <td>586.28</td> |
178 | | - <td>606.400</td> |
179 | | - <td>585.89</td> |
180 | | - <td>604.00</td> |
181 | | - <td>80642184.0</td> |
182 | | - </tr> |
183 | | - </tbody> |
184 | | -</table> |
185 | | - |
186 | | -<div class="python section-example-container"> |
187 | | - <pre class="python"># Calculate the daily returns. |
188 | | -daily_returns = history.close.pct_change().iloc[1:]</pre> |
189 | | -</div> |
190 | | - |
191 | | -<div class="python section-example-container"> |
192 | | - <pre>symbol time |
193 | | -SPY 2024-12-13 16:00:00 -0.000199 |
194 | | - 2024-12-16 16:00:00 0.004270 |
195 | | - 2024-12-17 16:00:00 -0.004120 |
196 | | - 2024-12-18 16:00:00 -0.029804 |
197 | | -Name: close, dtype: float64</pre> |
198 | | -</div> |
199 | | - |
200 | | - |
201 | | -<p class='python'> |
202 | | - If you intend to use the data in the DataFrame to create <code><?=$dataType?></code> objects, request that the history request returns the data type you need. |
203 | | - Otherwise, LEAN consumes unnecessary computational resources populating the DataFrame. |
204 | | - To get a list of <code><?=$dataType?></code> objects instead of a DataFrame, call the <code>history[<?=$dataType?>]</code> method. |
205 | | -</p> |
206 | | - |
207 | | -<div class="python section-example-container"> |
208 | | - <pre class="python"># Get the 5 trailing daily <?=$dataType?> objects of an asset in <?=$dataType?> format. |
209 | | -history = self.history[<?=$dataType?>](symbol, 5, Resolution.DAILY) |
210 | | -# Iterate through the TradeBar objects and access their volumes. |
211 | | -for trade_bar in history: |
212 | | - volume = trade_bar.volume</pre> |
213 | | -</div> |
0 commit comments