-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalgo1_get_positions.py
executable file
·167 lines (137 loc) · 3.75 KB
/
algo1_get_positions.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
import datetime
import time
import mysql.connector
import sys, os
import alpaca_trade_api as tradeapi
con = mydb = mysql.connector.connect(user='root', password='waWWii21156!', host='127.0.0.1', database='pts')
timestamp = int(time.time())
localtime = time.ctime(timestamp)
print("Local time:", localtime)
#print(timestamp)
#print(today)
#today = str(today)
#mtoday = today.replace("-","")
#print(mtoday)
uxs = str(timestamp)
# 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"
api = tradeapi.REST()
portfolio = api.list_positions()
print(portfolio)
table1 = "algo1_trades"
#print(table1)
# Print the quantity of shares for each position.
for Position in portfolio:
price = Position.current_price
symbol = Position.symbol
qty = Position.qty
side = Position.side
# print(price,symbol,qty,side)
if (side == 'long'):
clientid = symbol+ "_COVER" + uxs
qty = int(qty)
print("Sell ", side, "order :",symbol," qty: ",qty," at price: ",price, " with clientid: ",clientid)
sym = str(symbol)
p1 = str(price)
quant = str(qty)
sides = str(side)
type = 'limit'
api.submit_order(
symbol=symbol,
qty=qty,
side='sell',
# type='limit',
type='market',
# limit_price=price,
# time_in_force='ioc',
time_in_force='day',
#extended_hours=True,
client_order_id=clientid)
# sql1 = "replace into " + table1 + " (unixtime, date, trade_status, side, qty, symbol, order_price, clientid, order_type, tradeid) values ('" +uxs+ "', from_unixtime(" +uxs+ "), '1', 'sell', '" +quant+ "', '" +sym+ "', '" +p1+ "', '" +clientid+ "', '" +type+ "', '" +clientid+ "')"
# print(sql1)
# cursor = con.cursor()
# cursor.execute(sql1)
# con.commit()
# cursor.close()
#
if (side == 'short'):
clientid = symbol+ "_COVER" + uxs
qty = int(qty)
print("Cover ", side, "order :",symbol," qty: ",qty," at price: ",price, " with clientid: ",clientid)
sym = str(symbol)
p1 = str(price)
qty = (qty * -1)
quant = str(qty)
type = 'limit'
sql1 = "replace into " + table1 + " (unixtime, date, trade_status, side, qty, symbol, order_price, clientid, order_type, tradeid) values ('" +uxs+ "', from_unixtime(" +uxs+ "), '1', 'buy', '" +quant+ "', '" +sym+ "', '" +p1+ "', '" +clientid+ "', '" +type+ "', '" +clientid+ "')"
# print(sql1)
# cursor = con.cursor()
# cursor.execute(sql1)
# con.commit()
# cursor.close()
#
api.submit_order(
symbol=symbol,
qty=qty,
side='buy',
# type='limit',
type='market',
# limit_price=price,
# time_in_force='ioc',
time_in_force='day',
#extended_hours=True,
client_order_id=clientid)
# Get our position in AAPL.
#
##qqq_position = api.get_position('QQQ')
##print(qqq_position)
#
#portfolio = api.list_positions()
#
#
## Print the quantity of shares for each position.
#for position in portfolio:
# price = position.current_price
# symbol = position.symbol
# qty = position.qty
# side = position.side
#
# print(price,symbol,qty,side)
#
# time.sleep(3)
#
#
# portfolio = api.list_positions()
# #print(portfolio)
#
#
# # Print the quantity of shares for each position.
# for position in portfolio:
# price1 = position.current_price
# symbol = position.symbol
# qty = position.qty
# side = position.side
#
# print(price1,symbol,qty,side)
#
# price = float(price)
# price1 = float(price1)
# change = price1 - price
# print("Change: ", change)
# quantity = int(qty)
# buyqty = quantity * -1
# clientid = "SHORTCOVER" + ux
#
# if (change > 0 and quantity < 0):
# print("Making a covering trade")
# api.submit_order(
# symbol=symbol,
# qty=buyqty,
# side= 'buy',
# type= 'market',
# time_in_force= 'day',
# client_order_id=clientid)
#
#