-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter_ten.py
More file actions
59 lines (43 loc) · 1.78 KB
/
chapter_ten.py
File metadata and controls
59 lines (43 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'''
- In this chapter we will learn how to implement take profit & stop loss
in the existing strategy.
- Important -> Sometimes, when the stop loss hits, then in the next candle a buy position is opened.
This can be very dangerous and we should avoid these kind of scenarios in the trading strategy.
- Important -> Also avoid immediate buy after selling condition, like selling now & buying in next candle.
This type of situation also needs to be taken care of in real life scenarios.
'''
import os
import pandas_ta as ta
import pandas as pd
from backtesting import Backtest
from backtesting import Strategy
from backtesting.lib import crossover, resample_apply
from backtesting.test import GOOG
class RsiOscillator(Strategy):
upperBound = 70
lowerBound = 30
rsiWindow = 14
sl = 5/100;
tp = 15/100
def init(self):
self.dailyRsi = self.I(ta.rsi, pd.Series(self.data.Close), self.rsiWindow)
def next(self):
slPrice = ((1 - self.sl) * self.data.Close[-1])
tpPrice = ((1 + self.tp) * self.data.Close[-1])
if (crossover(self.dailyRsi, self.upperBound)):
self.position.close()
# So when price drops to 0.95 times of current closing price.
# Or we can say when the price drops by 5%. That price value would be our stop loss.
# self.data.Close[-1] is used to take the latest value of closing price.
elif (crossover(self.lowerBound, self.dailyRsi)):
self.buy(tp=tpPrice, sl=slPrice)
bt = Backtest(GOOG, RsiOscillator, cash=10000)
stats = bt.run()
print(stats)
lowerBound = stats['_strategy'].lowerBound
upperBound = stats['_strategy'].upperBound
rsiWindow = stats['_strategy'].rsiWindow
if not os.path.exists('plots'):
os.makedirs('plots')
fileName = f"plot-{lowerBound}-{upperBound}-{rsiWindow}.html"
bt.plot(filename=f"plots/{fileName}")