forked from areed1192/python-trading-robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrading_robot_indicators.py
131 lines (100 loc) · 3.07 KB
/
trading_robot_indicators.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
130
131
import pprint
from datetime import datetime
from datetime import timedelta
from configparser import ConfigParser
from pyrobot.robot import PyRobot
from pyrobot.indicators import Indicators
# Grab configuration values.
config = ConfigParser()
config.read('configs/config.ini')
CLIENT_ID = config.get('main', 'CLIENT_ID')
REDIRECT_URI = config.get('main', 'REDIRECT_URI')
CREDENTIALS_PATH = config.get('main', 'JSON_PATH')
ACCOUNT_NUMBER = config.get('main', 'ACCOUNT_NUMBER')
# Initalize the robot.
trading_robot = PyRobot(
client_id=CLIENT_ID,
redirect_uri=REDIRECT_URI,
credentials_path=CREDENTIALS_PATH,
paper_trading=True
)
# Create a Portfolio
trading_robot_portfolio = trading_robot.create_portfolio()
# Add a single position
trading_robot_portfolio.add_position(
symbol='MSFT',
quantity=10,
purchase_price=10,
asset_type='equity',
purchase_date='2020-04-01'
)
# Grab historical prices, first define the start date and end date.
start_date = datetime.today()
end_date = start_date - timedelta(days=30)
# Grab the historical prices.
historical_prices = trading_robot.grab_historical_prices(
start=end_date,
end=start_date,
bar_size=1,
bar_type='minute'
)
# Convert data to a Data Frame.
stock_frame = trading_robot.create_stock_frame(
data=historical_prices['aggregated']
)
# We can also add the stock frame to the Portfolio object.
trading_robot.portfolio.stock_frame = stock_frame
# Additionally the historical prices can be set as well.
trading_robot.portfolio.historical_prices = historical_prices
# Create an indicator Object.
indicator_client = Indicators(price_data_frame=stock_frame)
# Add the RSI Indicator.
indicator_client.rsi(period=14)
# Add the 200 day simple moving average.
indicator_client.sma(period=200)
# Add the 50 day exponentials moving average.
indicator_client.ema(period=50)
# Add the Bollinger Bands.
indicator_client.bollinger_bands(period=20)
# Add the Rate of Change.
indicator_client.rate_of_change(period=1)
# Add the Average True Range.
indicator_client.average_true_range(period=14)
# Add the Stochastic Oscillator.
indicator_client.stochastic_oscillator()
# Add the MACD.
indicator_client.macd(fast_period=12, slow_period=26)
# Add the Mass Index.
indicator_client.mass_index(period=9)
# # Add the K-Oscillator
# indicator_client.kst_oscillator(
# r1=1,
# r2=2,
# r3=3,
# r4=4,
# n1=1,
# n2=2,
# n3=3,
# n4=4
# )
while True:
# Grab the latest bar.
latest_bars = trading_robot.get_latest_bar()
# Add to the Stock Frame.
stock_frame.add_rows(data=latest_bars)
# Refresh the Indicators.
indicator_client.refresh()
print("="*50)
print("Current StockFrame")
print("-"*50)
print(stock_frame.symbol_groups.tail())
print("-"*50)
print("")
# Check for signals.
signals = indicator_client.check_signals()
# Grab the last bar.
last_bar_timestamp = trading_robot.stock_frame.frame.tail(
n=1
).index.get_level_values(1)
# Wait till the next bar.
trading_robot.wait_till_next_bar(last_bar_timestamp=last_bar_timestamp)