-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrsi_intraday_test.py
129 lines (94 loc) · 3.77 KB
/
rsi_intraday_test.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""Demo of intraday RSI multi symbol strategy.
The author is Zmicier Gotowka
Distributed under Fcore License 1.1 (see license.md)
"""
from backtest.rsi import RSI
from backtest.base import BackTestError
from backtest.stock import StockData
from backtest.reporting import Report
from data.fdata import FdataError
from data.fvalues import Timespans
from data.futils import trim_time
from data.fmp import FmpStock
import settings
import plotly.graph_objects as go
from plotly import subplots
from itertools import repeat
import sys
symbols = ['MSFT', 'AAPL']
period = 14
support = 30
resistance = 70
min_width = 2500 # Minimum width for charting
height = 250 # Height of each subchart in reporting
if __name__ == "__main__":
if settings.FMP.api_key is None:
sys.exit("This test requires FMP api key. Get the free key at financialmodelingprep.com and put it in settings.py")
# Array for the fetched data for all symbols
allrows = []
for symbol in symbols:
try:
rows = FmpStock(symbol=symbol, first_date='2024-01-10', last_date='2024-01-10', timespan=Timespans.Minute, verbosity=True).get()
except FdataError as e:
sys.exit(e)
print(f"The total number of quotes used for {symbol} is {len(rows)}.\n")
allrows.append(rows)
# RSI strategy calculation
data_a = StockData(rows=allrows[0],
title=symbols[0],
spread=0.1,
margin_rec=0.4,
margin_req=0.7,
)
data_b = StockData(rows=allrows[1],
title=symbols[1],
spread=0.1,
margin_rec=0.4,
margin_req=0.7,
)
quotes = [data_a, data_b]
rsi = RSI(data=quotes,
commission=2.5,
initial_deposit=10000,
margin_rec=0.9,
margin_req=1,
period=period,
support=support,
resistance=resistance,
to_short=True
)
try:
rsi.calculate()
except BackTestError as e:
sys.exit(f"Can't perform backtesting calculation: {e}")
results = rsi.get_results()
# Support and resistance for RSI
length = len(allrows[0])
support_arr = []
resistance_arr = []
support_arr.extend(repeat(support, length))
resistance_arr.extend(repeat(resistance, length))
#################
# Create a report
#################
report = Report(data=results, width=max(length, min_width), margin=True)
# Add charts for used symbols
report.add_quotes_chart(title=f"RSI Multi Example Testing for {symbols[0]} and {symbols[1]}", height=250)
report.add_quotes_chart(index=1, height=height)
# Add a custom chart with RSI values
rsi_fig = subplots.make_subplots()
rsi_fig.add_trace(go.Scatter(x=results.DateTime, y=rsi.exec(0).get_vals()['rsi'], mode='lines', name=f"RSI {symbols[0]}"))
rsi_fig.add_trace(go.Scatter(x=results.DateTime, y=rsi.exec(1).get_vals()['rsi'], mode='lines', name=f"RSI {symbols[1]}"))
# Add support and resistance lines to the second chart
rsi_fig.add_trace(go.Scatter(x=results.DateTime, y=support_arr, mode='lines', name="Support"))
rsi_fig.add_trace(go.Scatter(x=results.DateTime, y=resistance_arr, mode='lines', name="Resistance"))
report.add_custom_chart(rsi_fig, height=height)
# Add a chart to represent portfolio performance
fig_portf = report.add_portfolio_chart(height=height)
# Add a chart with expenses
report.add_expenses_chart(height=height)
# Add annotations with strategy results
report.add_annotations()
# Show image
new_file = report.show_image()
print(f"{new_file} is written.")