-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalpaca_v4.py
196 lines (171 loc) · 10 KB
/
alpaca_v4.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import alpaca_trade_api as tradeapi
import mysql.connector
from mysql.connector import Error
import time
from datetime import datetime
from pytz import timezone
import threading
# insert your API keys here
# a
API_KEY_a = 'a'
SECRET_KEY_a = 'a'
# b
API_KEY_b = 'b'
SECRET_KEY_b = 'b'
# c
API_KEY_c = 'c'
SECRET_KEY_c = 'c'
# d
API_KEY_d = 'd'
SECRET_KEY_d = 'd'
base_url = 'https://paper-api.alpaca.markets'
api_1 = tradeapi.REST(API_KEY_a, SECRET_KEY_a, base_url, api_version='v2')
api_2 = tradeapi.REST(API_KEY_b, SECRET_KEY_b, base_url, api_version='v2')
api_3 = tradeapi.REST(API_KEY_c, SECRET_KEY_c, base_url, api_version='v2')
api_4 = tradeapi.REST(API_KEY_d, SECRET_KEY_d, base_url, api_version='v2')
APIs = [api_1, api_2, api_3, api_4]
# specify Time zone
tz = timezone('US/Eastern')
account_tables = ['yyyy_mm_dd_a', 'yyyy_mm_dd_b', 'yyyy_mm_dd_c', 'yyyy_mm_dd_d']
position_tables = ['Stocks_account_a', 'Stocks_account_b', 'Stocks_account_c', 'Stocks_account_d']
account_names = {
'yyyy_mm_dd_a' : 'Account_a',
'yyyy_mm_dd_b' : 'Account_b',
'yyyy_mm_dd_c' : 'Account_c',
'yyyy_mm_dd_d' : 'Account_d'
}
def insertAccountsData():
#Mysql Connection with database
connection = mysql.connector.connect(host='localhost',
database='dashboard',
user='root',
password='')
accounts = []
positions = []
for index, api in enumerate(APIs):
try:
accounts.append(api.get_account())
positions.append(api.list_positions())
except:
positions.append(None)
accounts.append(None)
print('Something went wrong with Account {0} API'.format(index+1))
cursor = connection.cursor()
timeNow = datetime.now(tz)
for (account, account_table, position) in zip(accounts, account_tables, positions):
try:
if connection.is_connected() and account != None and position!= None:
sql = """INSERT INTO {} (account_blocked, account_number, accrued_fees, buying_power, cash, created_at, crypto_status,
currency, daytrade_count, daytrading_buying_power, equity, id, initial_margin, last_equity, last_maintenance_margin
, long_market_value, maintenance_margin, multiplier, non_marginable_buying_power, pattern_day_trader,
pending_transfer_in, portfolio_value, regt_buying_power, short_market_value, shorting_enabled,
sma, status, trade_suspended_by_user, trading_blocked, transfers_blocked, pl_last_equity, ts, cash_last_equity,
daytrading_buying_power_last_equity, initial_margin_last_equity, last_maintenance_margin_last_equity,
long_market_value_last_equity, maintenance_margin_last_equity, portfolio_value_last_equity,
regt_buying_power_last_equity, short_market_value_last_equity, long_market_value_stock,
short_market_value_stock) VALUES (%s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""".format(account_table)
# calculating all additional column values
current_pl_value = (float(account.equity)/float(account.last_equity) - 1)
cursor.execute('SELECT entry_id, pl_last_equity FROM {} ORDER BY entry_id DESC LIMIT 1;'.format(account_table))
prev_pl_value_row = cursor.fetchall()
if not prev_pl_value_row:
pl_value = current_pl_value
else:
prev_pl_value = float(prev_pl_value_row[0][1])
pl_value = current_pl_value
diff = current_pl_value - prev_pl_value
if diff > 0.1 or diff < -0.1:
pl_value = prev_pl_value
cash_last_equity = (float(account.cash)/float(account.last_equity))
daytrading_buying_power_last_equity = (float(account.daytrading_buying_power)/float(account.last_equity))
initial_margin_last_equity = (float(account.initial_margin)/float(account.last_equity))
last_maintenance_margin_last_equity = (float(account.last_maintenance_margin)/float(account.last_equity))
long_market_value_last_equity = (float(account.long_market_value)/float(account.last_equity))
maintenance_margin_last_equity = (float(account.maintenance_margin)/float(account.last_equity))
portfolio_value_last_equity = (float(account.portfolio_value)/float(account.last_equity))
regt_buying_power_last_equity = (float(account.regt_buying_power)/float(account.last_equity))
short_market_value_last_equity = (float(account.short_market_value)/float(account.last_equity))
positve_market_stocks = []
negative_market_stocks = []
stocks_list = ['SPY', 'QQQ', 'DIA', 'IWM']
for stock in position:
if str(stock.symbol) in stocks_list:
if float(stock.market_value) >= 0:
positve_market_stocks.append(float(stock.market_value))
elif float(stock.market_value) < 0:
negative_market_stocks.append(float(stock.market_value))
negative_market_stocks = [abs(item) for item in negative_market_stocks]
long_market_value_stock = (float(account.long_market_value) - sum(positve_market_stocks))/float(account.last_equity)
short_market_value_stock = (abs(float(account.short_market_value)) - sum(negative_market_stocks))/float(account.last_equity)
# inserting all values to database
val = (account.account_blocked, account.account_number, account.accrued_fees, account.buying_power, account.cash,
account.created_at, account.crypto_status, account.currency, account.daytrade_count, account.daytrading_buying_power,
account.equity, account.id, account.initial_margin, account.last_equity, account.last_maintenance_margin,
account.long_market_value, account.maintenance_margin, account.multiplier, account.non_marginable_buying_power, account.pattern_day_trader,
account.pending_transfer_in, account.portfolio_value, account.regt_buying_power, account.short_market_value, account.shorting_enabled,
account.sma, account.status, account.trade_suspended_by_user, account.trading_blocked, account.transfers_blocked,
pl_value, timeNow.strftime('%Y-%m-%d %H:%M:%S'), cash_last_equity, daytrading_buying_power_last_equity,
initial_margin_last_equity, last_maintenance_margin_last_equity, long_market_value_last_equity, maintenance_margin_last_equity,
portfolio_value_last_equity, regt_buying_power_last_equity, short_market_value_last_equity, long_market_value_stock, short_market_value_stock)
cursor.execute(sql, val)
connection.commit()
except Error as e:
print("Error while connecting to MySQL", e)
time.sleep(5)
def insertPositionData():
#Mysql Connection with database
connection = mysql.connector.connect(host='localhost',
database='dashboard',
user='root',
password='')
accounts = []
positions = []
for index, api in enumerate(APIs):
try:
accounts.append(api.get_account())
positions.append(api.list_positions())
except:
positions.append(None)
accounts.append(None)
print('Something went wrong with Account {0} API'.format(index+1))
cursor = connection.cursor()
timeNow = datetime.now(tz)
for (account, account_table, position_table, position) in zip(accounts, account_tables, position_tables, positions):
try:
if connection.is_connected() and account != None and position!= None:
# inserting position values
for stock in position:
# formulas for position percentage and open pl percentage
position_percentage = float(stock.market_value)/float(account.last_equity)
open_pl_percentage = float(stock.unrealized_pl)/float(account.last_equity)
parent = account_names[account_table]
position_query = """INSERT INTO {0} (entry_id, symbol, open_pl, position_percentage, open_pl_percentage, parent, ts
) VALUES (%s, %s, %s, %s, %s, %s, %s)""".format(position_table)
# get last element id of account tabl
cursor.execute('SELECT entry_id FROM {} ORDER BY entry_id DESC LIMIT 1;'.format(account_table))
last_entry = cursor.fetchone()
# insert into db
position_val = (last_entry[0], stock.symbol, float(stock.unrealized_pl), position_percentage, open_pl_percentage
, parent, timeNow.strftime('%Y-%m-%d %H:%M:%S'))
cursor.execute(position_query, position_val)
connection.commit()
except Error as e:
print("Error while connecting to MySQL", e)
time.sleep(60)
def insert_1():
while True:
insertAccountsData()
def insert_2():
while True:
insertPositionData()
def main():
t1 = threading.Thread(target=insert_1)
t2 = threading.Thread(target=insert_2)
t1.start()
t2.start()
if __name__ == "__main__":
main()