forked from areed1192/python-trading-robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrading_robot_indicators_compare.py
122 lines (93 loc) · 2.95 KB
/
trading_robot_indicators_compare.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
import pprint
import operator
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 simple moving average.
indicator_client.sma(period=50, column_name='sma_50')
# 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 a signal to check for.
indicator_client.set_indicator_signal_compare(
indicator_1='sma',
indicator_2='ema',
condition_buy=operator.ge,
condition_sell=None
)
# Add a signal to check for.
indicator_client.set_indicator_signal_compare(
indicator_1='sma',
indicator_2='sma_50',
condition_buy=operator.ge,
condition_sell=None
)
# Print the Head.
print(trading_robot.stock_frame.frame.tail())
# Check for signals.
signals = indicator_client.check_signals()
# Print the Head.
print(trading_robot.stock_frame.frame.head())
# Print the Signals.
pprint.pprint(signals)