|
1 | 1 | <? |
| 2 | +$symbolC = "AddCrypto(\"BTCUSD\").Symbol"; |
| 3 | +$symbolPy = "self.add_crypto('BTCUSD').symbol"; |
| 4 | +$assetClass = "Crypto"; |
2 | 5 | $dataTypeLink = "/docs/v2/writing-algorithms/securities/asset-classes/crypto/handling-data#03-Trades"; |
3 | 6 | $dataType = "TradeBar"; |
4 | | -?> |
5 | | - |
6 | | -<p class='csharp'> |
7 | | - To get historical <a href='<?=$dataTypeLink?>'>trade data</a>, call the <code>History<<?=$dataType?>></code> method with an asset's <code>Symbol</code>. |
8 | | -</p> |
9 | | - |
10 | | -<p class='python'> |
11 | | - 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>. |
12 | | - This method returns a DataFrame with columns for the open, high, low, close, and volume. |
13 | | -</p> |
14 | | - |
15 | | -<div class="section-example-container"> |
16 | | - <pre class="csharp">public class CryptoTradeBarHistoryAlgorithm : QCAlgorithm |
17 | | -{ |
18 | | - public override void Initialize() |
19 | | - { |
20 | | - SetStartDate(2024, 12, 19); |
21 | | - // Get the Symbol of an asset. |
22 | | - var symbol = AddCrypto("BTCUSD").Symbol; |
23 | | - // Get the 5 trailing daily <?=$dataType?> objects of the asset. |
24 | | - var history = History<<?=$dataType?>>(symbol, 5, Resolution.Daily); |
25 | | - // Iterate through each TradeBar and calculate its dollar volume. |
26 | | - foreach (var bar in history) |
27 | | - { |
28 | | - var t = bar.EndTime; |
29 | | - var dollarVolume = bar.Close * bar.Volume; |
30 | | - } |
31 | | - } |
32 | | -}</pre> |
33 | | - <pre class="python">class CryptoTradeBarHistoryAlgorithm(QCAlgorithm): |
34 | | - |
35 | | - def initialize(self) -> None: |
36 | | - self.set_start_date(2024, 12, 19) |
37 | | - # Get the Symbol of an asset. |
38 | | - symbol = self.add_crypto('BTCUSD').symbol |
39 | | - # Get the 5 trailing daily <?=$dataType?> objects of the asset in DataFrame format. |
40 | | - history = self.history(<?=$dataType?>, symbol, 5, Resolution.DAILY)</pre> |
41 | | -</div> |
42 | | - |
43 | | -<table border="1" class="dataframe python"> |
| 7 | +$dataFrame = "<table border='1' class='dataframe python'> |
44 | 8 | <thead> |
45 | | - <tr style="text-align: right;"> |
| 9 | + <tr style='text-align: right;'> |
46 | 10 | <th></th> |
47 | 11 | <th></th> |
48 | 12 | <th>close</th> |
|
63 | 27 | </thead> |
64 | 28 | <tbody> |
65 | 29 | <tr> |
66 | | - <th rowspan="5" valign="top">BTCUSD</th> |
| 30 | + <th rowspan='5' valign='top'>BTCUSD</th> |
67 | 31 | <th>2024-12-15</th> |
68 | 32 | <td>101399.99</td> |
69 | 33 | <td>102650.00</td> |
|
104 | 68 | <td>21659.470502</td> |
105 | 69 | </tr> |
106 | 70 | </tbody> |
107 | | -</table> |
108 | | - |
109 | | -<div class="python section-example-container"> |
110 | | - <pre class="python"># Calculate the daily returns. |
111 | | -daily_returns = history.close.pct_change().iloc[1:]</pre> |
112 | | -</div> |
| 71 | +</table>"; |
113 | 72 |
|
114 | | -<div class="python section-example-container"> |
115 | | - <pre>symbol time |
| 73 | +$series = "symbol time |
116 | 74 | BTCUSD 2024-12-16 0.029979 |
117 | 75 | 2024-12-17 0.015894 |
118 | 76 | 2024-12-18 0.000473 |
119 | 77 | 2024-12-19 -0.056517 |
120 | | -Name: close, dtype: float64</pre> |
121 | | -</div> |
122 | | - |
| 78 | +Name: close, dtype: float64"; |
123 | 79 |
|
124 | | -<p class='python'> |
125 | | - 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. |
126 | | - Otherwise, LEAN consumes unnecessary computational resources populating the DataFrame. |
127 | | - To get a list of <code><?=$dataType?></code> objects instead of a DataFrame, call the <code>history[<?=$dataType?>]</code> method. |
128 | | -</p> |
129 | | - |
130 | | -<div class="python section-example-container"> |
131 | | - <pre class="python"># Get the 5 trailing daily <?=$dataType?> objects of an asset in <?=$dataType?> format. |
132 | | -history = self.history[<?=$dataType?>](symbol, 5, Resolution.DAILY) |
133 | | -# Iterate through the TradeBar objects and access their volumes. |
134 | | -for trade_bar in history: |
135 | | - volume = trade_bar.volume</pre> |
136 | | -</div> |
| 80 | +include(DOCS_RESOURCES."/history/tradebars.php"); |
| 81 | +?> |
0 commit comments