-
Notifications
You must be signed in to change notification settings - Fork 1
/
algo1_trade_live_v3.py
executable file
·211 lines (140 loc) · 4.15 KB
/
algo1_trade_live_v3.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
197
198
199
200
201
202
203
204
205
206
207
208
209
import os
import sys
from datetime import datetime, timedelta
from datetime import date
import time
import pandas as pd
import numpy as np
import mysql.connector
import subprocess
import pathlib
import alpaca_trade_api as tradeapi
# print(pd.__version__)
# exit()
#connect to database
con = mydb = mysql.connector.connect(user='root', password='waWWii21156!', host='127.0.0.1', database='pts', allow_local_infile=True)
DBconnection = mysql.connector.connect(user='root', password='waWWii21156!', host='127.0.0.1', database='pts')
today = date.today()
yesterday = str(today - timedelta(days = 1))
today = str(today)
mdate = today.replace("-","")
# for testing set today to past date
#today = '2020-11-20'
table = "algo1_orders"
table1 = "algo1_trades"
table2 = "closings"
timestamp = int(time.time())
localtime = time.ctime(timestamp)
ux = timestamp
starttime = time.time()
PATH = "/Users/danielsavage/flagler/algos/algo1"
PATH = pathlib.Path().absolute()
sql1 = "update " +table2+ " set pct_change = ((close / open) - 1) where open > 0"
cursor = con.cursor()
cursor.execute(sql1)
con.commit()
cursor.close()
ux = timestamp
uxs = str(ux)
# Dan's Long Account
os.environ["APCA_API_BASE_URL"] = "https://paper-api.alpaca.markets"
os.environ["APCA_API_KEY_ID"] = "PK8ESAYXZ04ETCBV2G7I"
os.environ["APCA_API_SECRET_KEY"] = "v7m3iGUQNVGUrtx7iQ8QDqNvXKYCS1XmsOkDOHeV"
# prepare to make long buys
sql1 = "SELECT trim(symbol), premarket from " +table2+ " where date = '" +yesterday+ "' order by pct_change desc limit 100"
print(sql1)
cursor = con.cursor()
cursor.execute(sql1)
result = cursor.fetchall()
for x in result:
ticker = (x[0])
price = (x[1])
sym = str(ticker)
p1 = str(price)
print("Ticker: ",ticker)
try:
api = tradeapi.REST()
clientid = "LONG_" + sym + uxs
print("Client order id: ",clientid)
qty = '100'
side = 'buy'
type ='limit'
type ='market'
tif = 'day'
tif = 'ioc'
date = today
sql1 = "replace into " + table1 + " (unixtime, date, trade_status, side, qty, symbol, order_price, clientid, order_type, tradeid) values ('" +uxs+ "', from_unixtime(" +uxs+ "), '1', '" +side+ "', '" +qty+ "', '" +sym+ "', '" +p1+ "', '" +clientid+ "', '" +type+ "', '" +clientid+ "')"
print(sql1)
cursor = con.cursor()
cursor.execute(sql1)
con.commit()
cursor.close()
try:
api = tradeapi.REST()
api.submit_order(
symbol=sym,
qty=qty,
side=side,
type=type,
#limit_price=price,
time_in_force=tif,
#extended_hours=True,
client_order_id=clientid)
except Exception as e:
print("Alpaca long trade failed:"+str(e))
pass
except alpaca_trade_api.rest.APIError as e:
print("Alpaca api rest failed:"+e)
pass
except OSError as e:
print("Alpaca long trade failed")
pass
# prepare to make short buys
sql1 = "SELECT trim(symbol), premarket from " +table2+ " where date = '" +yesterday+ "' and shortable = 'True' order by pct_change limit 100"
print(sql1)
cursor = con.cursor()
cursor.execute(sql1)
result = cursor.fetchall()
for x in result:
ticker = (x[0])
price = (x[1])
sym = str(ticker)
p1 = str(price)
print("Ticker: ",ticker)
try:
api = tradeapi.REST()
clientid = "SHORT_" + sym + uxs
print("Client order id: ",clientid)
qty = '100'
side = 'sell'
type ='limit'
type ='market'
tif = 'day'
tif = 'ioc'
date = today
sql1 = "replace into " + table1 + " (unixtime, date, trade_status, side, qty, symbol, order_price, clientid, order_type, tradeid) values ('" +uxs+ "', from_unixtime(" +uxs+ "), '1', '" +side+ "', '" +qty+ "', '" +sym+ "', '" +p1+ "', '" +clientid+ "', '" +type+ "', '" +clientid+ "')"
print(sql1)
cursor = con.cursor()
cursor.execute(sql1)
con.commit()
cursor.close()
try:
api = tradeapi.REST()
api.submit_order(
symbol=sym,
qty=qty,
side=side,
type=type,
#limit_price=price,
time_in_force=tif,
#extended_hours=True,
client_order_id=clientid)
except Exception as e:
print("Alpaca short trade failed:"+str(e))
pass
except alpaca_trade_api.rest.APIError as e:
print("Alpaca api rest failed:"+e)
pass
except OSError as e:
print("Alpaca short trade failed")
pass