|
3 | 3 | import time |
4 | 4 | import datetime |
5 | 5 |
|
6 | | -API_KEY = "API_KEY" |
7 | | -API_SECRET = "API_SECRET" |
| 6 | +API_KEY = "YOUR_API_KEY_HERE" |
| 7 | +API_SECRET = "YOUR_API_SECRET_HERE" |
8 | 8 | APCA_API_BASE_URL = "https://paper-api.alpaca.markets" |
9 | 9 |
|
10 | 10 |
|
@@ -44,9 +44,6 @@ def run(self): |
44 | 44 |
|
45 | 45 | # Rebalance the portfolio every minute, making necessary trades. |
46 | 46 | while True: |
47 | | - tRebalance = threading.Thread(target=self.rebalance) |
48 | | - tRebalance.start() |
49 | | - tRebalance.join() |
50 | 47 |
|
51 | 48 | # Figure out when the market will close so we can prepare to sell beforehand. |
52 | 49 | clock = self.alpaca.get_clock() |
@@ -74,13 +71,21 @@ def run(self): |
74 | 71 | print("Sleeping until market close (15 minutes).") |
75 | 72 | time.sleep(60 * 15) |
76 | 73 | else: |
| 74 | + # Rebalance the portfolio. |
| 75 | + tRebalance = threading.Thread(target=self.rebalance) |
| 76 | + tRebalance.start() |
| 77 | + tRebalance.join() |
77 | 78 | time.sleep(60) |
78 | 79 |
|
79 | 80 | # Wait for market to open. |
80 | 81 | def awaitMarketOpen(self): |
81 | 82 | isOpen = self.alpaca.get_clock().is_open |
82 | 83 | 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.") |
84 | 89 | time.sleep(60) |
85 | 90 | isOpen = self.alpaca.get_clock().is_open |
86 | 91 |
|
@@ -291,13 +296,13 @@ def submitOrder(self, qty, stock, side, resp): |
291 | 296 | if(qty > 0): |
292 | 297 | try: |
293 | 298 | 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.") |
295 | 300 | resp.append(True) |
296 | 301 | 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.") |
298 | 303 | resp.append(False) |
299 | 304 | 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.") |
301 | 306 | resp.append(True) |
302 | 307 |
|
303 | 308 | # Get percent changes of the stock prices over the past 10 days. |
|
0 commit comments