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,5,'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'])
"--all", "-a", help= "Watch the A.* feed as well, which can overwelm and backup during active times", action='store_true') parser.add_argument("--debug", help="Prints debug messages", action='store_true') opt = parser.parse_args() conn = StreamConn(API_KEY, API_SECRET, APCA_API_BASE_URL) # This is another way to setup wrappers for websocket callbacks, handy if conn is not global. on_minute = conn.on(r'AM$')(on_minute) on_tick = conn.on(r'A$')(on_tick) on_data = conn.on(r'.*')(on_data) # This is an example of how you can add your own async functions into the loop # This one just watches this program for edits and tries to restart it asyncio.ensure_future(reloadWatch(__file__, sys.argv)()) try: if opt.all: # Note to see all these channels, you'd need to add a handler # above or use --debug! conn.run(['Q.*', 'T.*', 'AM.*', 'A.*']) else: conn.run(['AM.*']) except Exception as e: