-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathPythonTradingBot.py
More file actions
46 lines (44 loc) · 1.52 KB
/
PythonTradingBot.py
File metadata and controls
46 lines (44 loc) · 1.52 KB
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
import alpaca_trade_api as tradeapi
from alpaca_trade_api import StreamConn
import threading
import time
import datetime
import logging
import argparse
# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
# API KEYS
#region
API_KEY = "ALPACA API KEY HERE"
API_SECRET = "ALPACA API SECRET HERE"
APCA_API_BASE_URL = "https://paper-api.alpaca.markets"
#endregion
#Buy a stock when a doji candle forms
class BuyDoji:
def __init__(self):
self.alpaca = tradeapi.REST(API_KEY, API_SECRET, APCA_API_BASE_URL, api_version='v2')
def run(self):
#On Each Minute
async def on_minute(conn, channel, bar):
symbol = bar.symbol
print("Close: ", bar.close)
print("Open: ", bar.open)
print("Low: ", bar.low)
print(symbol)
#Check for Doji
if bar.close > bar.open and bar.open - bar.low > 0.1:
print('Buying on Doji!')
self.alpaca.submit_order(symbol,1,'buy','market','day')
#TODO : Take profit
#Connect to get streaming market data
conn = StreamConn('Polygon Key Here', 'Polygon Key Here', 'wss://alpaca.socket.polygon.io/stocks')
on_minute = conn.on(r'AM$')(on_minute)
# Subscribe to Microsoft Stock
conn.run(['AM.MSFT'])
# Run the BuyDoji class
ls = BuyDoji()
ls.run()