Skip to content

Commit cb2f1bd

Browse files
committed
update
1 parent 0b43d95 commit cb2f1bd

File tree

2 files changed

+61
-23
lines changed

2 files changed

+61
-23
lines changed

README.ipynb

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"1. [Best strategies](#strategies)\n",
66
" 1. [Binance historical data (08/17/2017 - 12/25/2020)](#alltime)\n",
77
" 2. [No bullrun phase (03/01/2018 - 11/15/2020)](#nobullrun)\n",
8-
" 3. [Save strategies](#save)\n",
9-
"2. [Global analysis](#analysis)"
8+
" 3. [Save strategies](#save)"
109
],
1110
"cell_type": "markdown",
1211
"metadata": {}
@@ -562,20 +561,6 @@
562561
"source": [
563562
"topSQN.to_json(r'top10sqn.json', orient='records')"
564563
]
565-
},
566-
{
567-
"source": [
568-
"# Global analysis <a name=\"analysis\"></a>"
569-
],
570-
"cell_type": "markdown",
571-
"metadata": {}
572-
},
573-
{
574-
"source": [
575-
"Do some more global analysis here"
576-
],
577-
"cell_type": "markdown",
578-
"metadata": {}
579564
}
580565
],
581566
"metadata": {

README.md

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ This is the result of a simple strategy using RSI with a period 14 on the BTCUSD
101101

102102
We then loop through each strategy in each file and in each period, and we get in return additional details for each strategy such as the **portofolio final amount**, the **total number of wins and losses**, the **net profit and loss** and finally the **SQN value** which is an indicator designed to assist traders in determining the global quality of a trading system.
103103

104+
For that we call the ```runbacktest``` function imported from **backtest.py**.
105+
106+
```python
107+
end_val, totalwin, totalloss, pnl_net, sqn = backtest.runbacktest(datapath, start, end, period, strategy, commission_val, portofolio, stake_val, quantity, plot)
108+
```
109+
104110
At last, we save in differents files the result of each strategy as follow :
105111

106112
```
@@ -114,41 +120,88 @@ BTCUSDT,1h,2017-01-01,2020-12-31,SMA,29,11033.763,10.226,269,1226,0.88
114120
BTCUSDT,1h,2017-01-01,2020-12-31,SMA,30,11023.452,10.123,265,1199,0.88
115121
```
116122

117-
For example for this strategy ([SMA-BTCUSDT-20170101-20201231-1h.csv](result/SMA-BTCUSDT-20170101-20201231-1h.csv)) using SMA on the 1 hour dataframe 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.
123+
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.
118124

119125

120126
## Backtest <a name="backtest"></a>
121127

122-
The [backtest.py](backtest.py) code was mostly based on the [Backtrader Quickstart Guide](https://www.backtrader.com/docu/quickstart/quickstart/). However, some modifications were applied and functions added to respond our needs so until this section is filled with more details know that :
128+
The [backtest.py](backtest.py) code was mostly based on the [Backtrader Quickstart Guide](https://www.backtrader.com/docu/quickstart/quickstart/). However, some modifications were applied and functionalities added to respond our needs.
129+
130+
**Two strategies were implemented :**
123131

124-
**Two strategies are implemented :**
132+
* Using SMA :
125133

126134
```python
127135
class SMAStrategy(bt.Strategy):
136+
params = (
137+
('maperiod', None),
138+
('quantity', None)
139+
)
128140
```
129141

130-
* SMA strategy based on the SMA indicator. If we are not already in a position and the closure price of the last candlestick is higher than the indicator (that mean we cross the sma from bellow to top), then we buy a size equivalent to 10% of the current portofolio amount.
142+
This strategy is based on the SMA indicator. If we are not already in a position and the closure price of the last candlestick is higher than the indicator (i.e. we cross the sma from bellow to top), then we buy a size equivalent to 10% of the current portofolio amount. We sell when the opposite happen.
143+
144+
![SMA crossed](./README_files/crossSMA.png "SMA crossed")
145+
131146
```python
132147
# Check if we are in the market
133148
if not self.position:
134-
135149
# Not yet ... we MIGHT BUY if ...
136150
if self.dataclose[0] > self.sma[0]:
137-
138151
# Keep track of the created order to avoid a 2nd order
139152
self.amount = (self.broker.getvalue() * self.params.quantity) / self.dataclose[0]
140153
self.order = self.buy(size=self.amount)
141154
else:
142155
# Already in the market ... we might sell
143156
if self.dataclose[0] < self.sma[0]:
157+
# Keep track of the created order to avoid a 2nd order
158+
self.order = self.sell(size=self.amount)
159+
```
160+
161+
* Using RSI :
162+
163+
```python
164+
class RSIStrategy(bt.Strategy):
165+
params = (
166+
('maperiod', None),
167+
('quantity', None)
168+
)
169+
```
144170

171+
Based on the RSI indicator, if we are not already in a position and the rsi go below 30 then we buy a size equivalent to 10% of the current portofolio amount that we will sell when rsi > 70.
172+
173+
```python
174+
# Check if we are in the market
175+
if not self.position:
176+
# Not yet ... we MIGHT BUY if ...
177+
if self.rsi < 30:
178+
# Keep track of the created order to avoid a 2nd order
179+
self.amount = (self.broker.getvalue() * self.params.quantity) / self.dataclose[0]
180+
self.order = self.buy(size=self.amount)
181+
else:
182+
# Already in the market ... we might sell
183+
if self.rsi > 70:
145184
# Keep track of the created order to avoid a 2nd order
146185
self.order = self.sell(size=self.amount)
147186
```
148187

149-
![SMA crossed](./README_files/crossSMA.png "SMA crossed")
188+
* Strategy selection
189+
190+
Depending on the parameter given in the ```runbacktest``` function we will add one of the two strategy we have written and give it the period we want to use and quantity in % of our portofolio that we want to use.
150191

192+
```python
193+
if strategy == 'SMA':
194+
cerebro.addstrategy(SMAStrategy, maperiod=period, quantity=quantity)
195+
elif strategy == 'RSI':
196+
cerebro.addstrategy(RSIStrategy, maperiod=period, quantity=quantity)
197+
else :
198+
print('no strategy')
199+
exit()
200+
```
201+
202+
&nbsp;
151203

204+
*compression and timeframe*
152205

153206

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

0 commit comments

Comments
 (0)