You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This directory contains example trading algorithms that connect to the paper-trading API. These scripts are meant to be run in a Node.js app, where you first install the Alpaca node module, then run the trading algorithm. Please note you will need to replace the `API_KEY` and `API_SECRET` parameters at the top of the file with your own information from the [Alpaca dashboard](https://app.alpaca.markets/). Please also note that the performance of these scripts in a real trading environment is not guaranteed. While they are written with the goal of showing realistic uses of the SDK, there is no guarantee that the strategies they outline are a good fit for your own brokerage account.
4
+
5
+
## Long-Short Equity
6
+
7
+
This trading algorithm implements the long-short equity strategy. This means that the algorithm will rank a given universe of stocks based on a certain metric, and long the top ranked stocks and short the lower ranked stocks. More specifically, the algorithm uses the frequently used 130/30 percent equity split between longs and shorts (130% of equity used for longs, 30% of equity used for shorts). The algorithm will then grab the top and bottom 25% of stocks, and long or short them accordingly. The algorithm will purchase equal quantities across a bucket of stocks, so all stocks in the long bucket are ordered with the same quantity (same with the short bucket). After every minute, the algorithm will re-rank the stocks and make adjustments to the position if necessary.
8
+
9
+
Some stocks cannot be shorted. In this case, the algorithm uses the leftover equity from the stocks that could not be shorted and shorts the stocks have already been shorted.
10
+
11
+
The algorithm uses percent change in stock price over the past 10 minutes to rank the stocks, where the stocks the rose the most are longed and the ones that sunk the most are shorted.
print(str(timeToOpen) +" minutes til market open.")
89
+
time.sleep(60)
90
+
isOpen=self.alpaca.get_clock().is_open
91
+
92
+
defrebalance(self):
93
+
tRerank=threading.Thread(target=self.rerank)
94
+
tRerank.start()
95
+
tRerank.join()
96
+
97
+
# Clear existing orders again.
98
+
orders=self.alpaca.list_orders(status="open")
99
+
fororderinorders:
100
+
self.alpaca.cancel_order(order.id)
101
+
102
+
print("We are taking a long position in: "+str(self.long))
103
+
print("We are taking a short position in: "+str(self.short))
104
+
# Remove positions that are no longer in the short or long list, and make a list of positions that do not need to change. Adjust position quantities if needed.
105
+
executed= [[], []]
106
+
positions=self.alpaca.list_positions()
107
+
self.blacklist.clear()
108
+
forpositioninpositions:
109
+
if(self.long.count(position.symbol) ==0):
110
+
# Position is not in long list.
111
+
if(self.short.count(position.symbol) ==0):
112
+
# Position not in short list either. Clear position.
0 commit comments