Skip to content

Commit c9f23f1

Browse files
committed
Consistency updates
1 parent c4f887d commit c9f23f1

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

examples/long-short.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import time
44
import datetime
55

6-
API_KEY = "API_KEY"
7-
API_SECRET = "API_SECRET"
6+
API_KEY = "YOUR_API_KEY_HERE"
7+
API_SECRET = "YOUR_API_SECRET_HERE"
88
APCA_API_BASE_URL = "https://paper-api.alpaca.markets"
99

1010

@@ -44,9 +44,6 @@ def run(self):
4444

4545
# Rebalance the portfolio every minute, making necessary trades.
4646
while True:
47-
tRebalance = threading.Thread(target=self.rebalance)
48-
tRebalance.start()
49-
tRebalance.join()
5047

5148
# Figure out when the market will close so we can prepare to sell beforehand.
5249
clock = self.alpaca.get_clock()
@@ -74,13 +71,21 @@ def run(self):
7471
print("Sleeping until market close (15 minutes).")
7572
time.sleep(60 * 15)
7673
else:
74+
# Rebalance the portfolio.
75+
tRebalance = threading.Thread(target=self.rebalance)
76+
tRebalance.start()
77+
tRebalance.join()
7778
time.sleep(60)
7879

7980
# Wait for market to open.
8081
def awaitMarketOpen(self):
8182
isOpen = self.alpaca.get_clock().is_open
8283
while(not isOpen):
83-
print("spinning")
84+
clock = self.alpaca.get_clock()
85+
openingTime = clock.next_open.replace(tzinfo=datetime.timezone.utc).timestamp()
86+
currTime = clock.timestamp.replace(tzinfo=datetime.timezone.utc).timestamp()
87+
timeToOpen = int((openingTime - currTime) / 60)
88+
print(str(timeToOpen) + " minutes til market open.")
8489
time.sleep(60)
8590
isOpen = self.alpaca.get_clock().is_open
8691

@@ -291,13 +296,13 @@ def submitOrder(self, qty, stock, side, resp):
291296
if(qty > 0):
292297
try:
293298
self.alpaca.submit_order(stock, qty, side, "market", "day")
294-
print("Market order of " + '|' + str(qty) + " " + stock + " " + side + '|' + " completed.")
299+
print("Market order of | " + str(qty) + " " + stock + " " + side + " | completed.")
295300
resp.append(True)
296301
except:
297-
print("Order of " + '|' + str(qty) + " " + stock + " " + side + '|' + " did not go through.")
302+
print("Order of | " + str(qty) + " " + stock + " " + side + " | did not go through.")
298303
resp.append(False)
299304
else:
300-
print("Quantity is 0, order of " + '|' + str(qty) + " " + stock + " " + side + '|' + " not completed.")
305+
print("Quantity is 0, order of | " + str(qty) + " " + stock + " " + side + " | not completed.")
301306
resp.append(True)
302307

303308
# Get percent changes of the stock prices over the past 10 days.

0 commit comments

Comments
 (0)