Skip to content

Commit a830b2f

Browse files
committed
progression
1 parent cb2f1bd commit a830b2f

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

README.md

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ You can then get the historical candlesticks you want by defining the cryptocurr
4646
candlesticks = client.get_historical_klines("BTCUSDT", Client.KLINE_INTERVAL_4HOUR, "1 Jan, 2017", "25 Dec, 2020")
4747
```
4848

49-
The datas we will get are as follow :
49+
The datas we obtain are as follow :
5050

5151
```
5252
[
@@ -67,7 +67,9 @@ The datas we will get are as follow :
6767
]
6868
```
6969

70-
Before saving these datas to a csv we need to divide the timestamp we get ('Open time') by 1000 to ignore the miliseconds.
70+
Before saving these datas to a csv we need to divide the timestamp we obtain ('Open time') by 1000 to ignore the miliseconds.
71+
72+
We fetched the historical candlesticks for the **BTCUSDT** and **ETHUSDT** pairs for each **1w-3d-1d-12h-8h-6h-4h-2h-1h-30m-15m** timeframes.
7173

7274

7375
## Get results <a name="getresults"></a>
@@ -122,6 +124,8 @@ BTCUSDT,1h,2017-01-01,2020-12-31,SMA,30,11023.452,10.123,265,1199,0.88
122124

123125
For example for this strategy ([SMA-BTCUSDT-20170101-20201231-1h.csv](result/SMA-BTCUSDT-20170101-20201231-1h.csv)) using SMA on the 1 hour timeframe BTCUSDT pair from 2017 to 2020, we can notice that the lower the SMA period is the lower our sqn and profit will be (in that case even negative), and conversely when the SMA period is higher our profit is better.
124126

127+
*The code was run again between 2018-03-01 and 2020-11-15 to exclude the bullrun period and test our strategies during upward trend and range period.*
128+
125129

126130
## Backtest <a name="backtest"></a>
127131

@@ -201,7 +205,59 @@ else :
201205

202206
&nbsp;
203207

204-
*compression and timeframe*
208+
* Get compression and timeframe
209+
210+
When we create the data feed we need to define the compression and timeframe.
211+
212+
```python
213+
data = bt.feeds.GenericCSVData(
214+
dataname = datapath,
215+
dtformat = 2,
216+
compression = compression,
217+
timeframe = timeframe,
218+
fromdate = datetime.datetime.strptime(start, '%Y-%m-%d'),
219+
todate = datetime.datetime.strptime(end, '%Y-%m-%d'),
220+
reverse = False)
221+
222+
cerebro.adddata(data)
223+
```
224+
225+
For a 3 days timeframe (one candlestick represent 3 days) we would have ```compression = 3``` and ```timeframe = bt.TimeFrame.Days```. However, for a 2h timeframe ```compression = 120``` but ```timeframe = bt.TimeFrame.Minutes```.
226+
227+
To automatically do this conversion the ```runbacktest()``` function will analyse the given datapath name and pass it to another function that will read the file name and retrieve its timeframe (e.g. data/ETHUSDT-2017-2020-**4h**.csv).
228+
229+
```python
230+
def timeFrame(datapath):
231+
"""
232+
Select the write compression and timeframe.
233+
"""
234+
sepdatapath = datapath[5:-4].split(sep='-') # ignore name file 'data/' and '.csv'
235+
tf = sepdatapath[3]
236+
237+
if tf == '1mth':
238+
compression = 1
239+
timeframe = bt.TimeFrame.Months
240+
elif tf == '12h':
241+
compression = 720
242+
timeframe = bt.TimeFrame.Minutes
243+
244+
...
245+
246+
elif tf == '8h':
247+
compression = 480
248+
timeframe = bt.TimeFrame.Minutes
249+
else:
250+
print('dataframe not recognized')
251+
exit()
252+
253+
return compression, timeframe
254+
```
255+
256+
*Later this function can be improved to save more lines by autocomputing the compression if we have a hour timeframe (e.g. if 'h' take what's in the front, say 12 and multiply it by 60 => 720 and timeframe = bt.TimeFrame.Minutes).*
257+
258+
&nbsp;
259+
260+
205261

206262

207263
# Analysis <a name="analysis"></a>

0 commit comments

Comments
 (0)