def consumer_thread(): try: # make sure we have an event loop, if not create a new one loop = asyncio.get_event_loop() loop.set_debug(True) except RuntimeError: asyncio.set_event_loop(asyncio.new_event_loop()) global conn conn = StreamConn( ALPACA_API_KEY, ALPACA_SECRET_KEY, base_url=URL('https://paper-api.alpaca.markets'), data_url=URL('https://data.alpaca.markets'), # data_url=URL('http://127.0.0.1:8765'), data_stream='polygon' if USE_POLYGON else 'alpacadatav1') @conn.on(r'^AM\..+$') async def on_minute_bars(conn, channel, bar): print('bars', bar) @conn.on(r'Q\..+') async def on_quotes(conn, channel, quote): print('quote', quote) @conn.on(r'T\..+') async def on_trades(conn, channel, trade): print('trade', trade) conn.run(['alpacadatav1/Q.GOOG'])
def main(): # use the templates for to create alpaca_api_keys.txt and polygon_api_keys.txt alpaca_file_obj = open("api_keys.txt", "r") alpaca_keys = parse_keys(alpaca_file_obj) polygon_file_obj = open("polygon_keys.txt", "r") polygon_keys = parse_keys(polygon_file_obj) # setting this up for a paper trading account trade_url = "https://paper-api.alpaca.markets" # ayment is required to use Polygon streaming API data_url = "wss://alpaca.socket.polygon.io/stocks" # Use the REST API using the keys from Alpaca and the paper trading site api = trade_api.REST(alpaca_keys["key"], alpaca_keys["secret"], trade_url, api_version="v2") # establish the data streaming connection connection = StreamConn(polygon_keys["key"], polygon_keys["secret"], data_url) # create the trading bot object with the api and stream and run it trade_bot = TradingBot(api, connection) trade_bot.run() return None
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'])
def run(self): #On each minute async def on_minute(conn, channel, bar): #Entry 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 Candle!") self.alpaca.submit_order(symbol, 1, 'buy', 'market', 'day') #Take profit at %1 increase (E.g $170 take profit at $171.7) #connect to get streaming stock market data self.conn = StreamConn('polygon key', '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'])
alpaca = tradeapi.REST() def generate_signals(df): # utilize arima from models as well as streamz return signals def execute_trade_strategy(signals, account): # abstract to external, include alpaca.submitorder # https://github.com/alpacahq/alpaca-trade-api-python/blob/master/examples/long-short.py#L66 return account conn = StreamConn() @conn.on(r"AM$") async def on_minute(conn, channel, bar): # bar object is the OHLC print(bar) symbol = bar.symbol close = bar.close try: percent = (close - bar.dailyopen) / close * 100 up = 1 if bar.open > bar.dailyopen else -1 except: percent = 0 up = 0
parser = argparse.ArgumentParser() parser.add_argument( "--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() # 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.*'])
async def on_tick(conn, channel, bar): try: percent = (bar.close - bar.dailyopen) / bar.close * 100 except: percent = 0 print(f""" {channel:<6s} {ms2date(bar.end)} {bar.symbol:<10s} {percent:>8.2f}% {bar.open:>8.2f} {bar.close:>8.2f} {bar.volume:<10d} """) if __name__ == "__main__": logging.basicConfig(level=logging.INFO) # conn and assignment to handlers # TODO: move to the decorator function style conn = StreamConn() on_minute = conn.on(r"AM$")(on_minute) on_tick = conn.on(r'A$')(on_tick) on_data = conn.on(r".*")(on_data) try: conn.run(["Q.*", "T.*", "AM.*", "A.*"]) # else: # conn.run(["AM.*"]) except Exception as e: print(e)
from alpaca_trade_api import StreamConn conn = StreamConn() @conn.on(r'^account_updates$') async def on_account_updates(conn, channel, account): print('account', account) @conn.on(r'^trade_updates$') async def on_status(conn, channel, data): print('trade updates', data) @conn.on(r'^AM$') async def on_minute_bars(conn, channel, bar): print('bars', bar) @conn.on(r'^A$') async def on_second_bars(conn, channel, bar): print('bars', bar) # blocks forever conn.run(['account_updates', 'A.*', 'trade_updates'])
from alpaca_trade_api.common import URL ALPACA_API_KEY = "<YOUR-API-KEY>" ALPACA_SECRET_KEY = "<YOUR-SECRET-KEY>" USE_POLYGON = False if __name__ == '__main__': import logging logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO) conn = StreamConn( ALPACA_API_KEY, ALPACA_SECRET_KEY, base_url=URL('https://paper-api.alpaca.markets'), data_url=URL('https://data.alpaca.markets'), data_stream='polygon' if USE_POLYGON else 'alpacadatav1' ) if USE_POLYGON: @conn.on(r'^status$') async def on_status(conn, channel, data): print('polygon status update', data) sec_agg_count = 0 # don't print too much quotes @conn.on(r'A.*') async def on_agg_sec(conn, channel, bars): global sec_agg_count if sec_agg_count % 1000 == 0: print('sec bars', bars) sec_agg_count += 1
conn.run(channels) except Exception as e: print(f'Exception from websocket connection: {e}') finally: print("Trying to re-establish connection") time.sleep(3) run_connection(conn, channels) if __name__ == '__main__': channels = ['alpacadatav1/Q.GOOG'] conn = StreamConn( ALPACA_API_KEY, ALPACA_SECRET_KEY, base_url=URL('https://paper-api.alpaca.markets'), data_url=URL('https://data.alpaca.markets'), # data_url=URL('http://127.0.0.1:8765'), data_stream='polygon' if USE_POLYGON else 'alpacadatav1') @conn.on(r'^AM\..+$') async def on_minute_bars(conn, channel, bar): print('bars', bar) @conn.on(r'Q\..+') async def on_quotes(conn, channel, quote): print('quote', quote) @conn.on(r'T\..+') async def on_trades(conn, channel, trade): print('trade', trade)
from alpaca_trade_api import StreamConn import threading # conn = tradeapi.stream2.StreamConn('YOUR API KEY','API SECRET',base_url) conn = StreamConn('YOUR API KEY','API SECRET',base_url) @conn.on(r'^account_updates$') async def on_account_updates(conn,channel,account): print("The account displayed is :",account) @conn.on(r'^trade_updates$') async def on_trade_updates(conn,channel,trade): print("The account displayed is :",trade) def ws_start(): conn.run(['account_updates','trade_updates']) ws_thread = threading.Thread(target=ws_start,daemon=True) ws_thread.start()
def construct_logger(filename): log_headers = [logging.FileHandler(filename), logging.StreamHandler()] log_format = '%(asctime)s [%(levelname)s] %(message)s' logging.basicConfig(level=logging.INFO, format=log_format, handlers=log_headers) return logging.getLogger(__name__) if __name__ == '__main__': log = construct_logger('streamer.log') zmq = zmq_msg.Client() if config.use_sandbox: base_url = 'https://paper-api.alpaca.markets' else: base_url = 'https://api.alpaca.markets' conn = StreamConn(key_id=config.api_key, secret_key=config.api_secret, base_url=base_url) @conn.on(r'^trade_updates$') async def on_account_updates(conn, channel, account): log.info(account.order) zmq.write(account.order) conn.run(['trade_updates'])
print('ok') # ticker_list = [ticker.ticker for ticker in tickers] # ticker_list = sorted(ticker_list, key=str.lower) # print(ticker_list) ticker_list = ['AMD'] '''while True: for ticker in ticker_list: ss = api.polygon.snapshot(ticker) lastprice = ss.ticker['lastTrade']['p'] print(f'{ticker} + {lastprice}') time.sleep(60)''' conn = StreamConn( base_url=base_url, key_id=api_key_id, secret_key=api_secret, ) @conn.on(r'^account_updates$') async def on_account_updates(conn, channel, account): print('account', account) @conn.on(r'^status$') async def on_status(conn, channel, data): print('polygon status update', data) @conn.on(r'^AM$')
and gets updates for price changes of different stocks. Based on the events that get triggered it can issue buy and sell orders. Configures the Alpaca API to 1. monitor for changes in real time 2. Trigger purchases or sales from real events """ from trading.config import * from alpaca_trade_api import StreamConn # Used to retrieve stock specific data polygon_data = api.polygon conn = StreamConn(KEY_ID, ALPACA_SECRET_KEY, base_url=BASE_URL) @conn.on(r'account_updates') async def on_account_updates(conn, channel, account): """ Monitors for Alpaca's connection channel and triggers if any trade is made or the account is otherwise updated """ print(f"Account {account} has been modified") @conn.on(r'^AM.') async def on_bars(conn, channel, bar): """
from alpaca_trade_api.common import URL ALPACA_API_KEY = "<YOUR-API-KEY>" ALPACA_SECRET_KEY = "<YOUR-SECRET-KEY>" USE_POLYGON = False if __name__ == '__main__': import logging logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO) conn = StreamConn( ALPACA_API_KEY, ALPACA_SECRET_KEY, base_url=URL('https://paper-api.alpaca.markets'), data_url=URL('https://data.alpaca.markets'), data_stream='polygon' if USE_POLYGON else 'alpacadatav1' ) if USE_POLYGON: @conn.on(r'^status$') async def on_status(conn, channel, data): print('polygon status update', data) @conn.on(r'^A*$') async def on_second_bars(conn, channel, bar): print('bars', bar) @conn.on(r'^AM\..+$') async def on_minute_bars(conn, channel, bar): print('bars', bar)
elif positions != []: for position in positions: if position.symbol != ticker: can_buy = True elif position.symbol == ticker: can_buy = False break return can_buy # create our connection to the api and streamconn for our up to date data api = tradeapi.REST(base_url=base_url, key_id=api_key_id, secret_key=api_secret, api_version='v2') conn = StreamConn(base_url=base_url, key_id=api_key_id, secret_key=api_secret) client = Client(account_sid, auth_token) # Keys for twillio api # global variables calc = Calculations(api) data = [] df_ticker_list = [] ticker_list = [] channels = [] # wait for the market to open async def awaitMarketOpen(): isOpen = api.get_clock().is_open timeList = [] c = 0
parser = argparse.ArgumentParser() parser.add_argument( "--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() # 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.*'])
parser = argparse.ArgumentParser() parser.add_argument( "--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.*'])
def init_streams(): while cls.running: try: # Create a new event loop for this thread. loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) # From alpaca-trade-api. cls.alpaca_stream = StreamConn(data_stream='polygon') @cls.alpaca_stream.on(r'^trade_updates$') async def on_trade_updates(conn, channel, data): # logfeed_data.log(LogLevel.DEBUG, 'Alpaca raw trade update: {0}'.format(data)) try: print('receiving order from ws') order = Order.from_alpaca_api( data.order, logfeed_data) if order is None: logfeed_data.log( LogLevel.WARNING, 'Data stream could not decode an order. Ignoring it' ) print( 'Data stream could not decode an order. Ignoring it' ) cls._queue_update( moment=datetime.now(), update_type=StreamUpdateType.STARTED_UP) else: cls._on_trade_update(order) except Exception as e: logfeed_data.log( LogLevel.ERROR, 'Error handling alpaca trade update:') logfeed_data.log(LogLevel.WARNING, traceback.format_exc()) traceback.print_exc() @cls.alpaca_stream.on(r'^account_updates$') async def on_account_updates(conn, channel, msg): logfeed_data.log( LogLevel.DEBUG, 'Alpaca raw account update: {0}'.format(msg)) print('Alpaca raw account update: {0}'.format(msg)) try: print('receiving acct update from ws') cls._on_account_update(msg, logfeed_data) except Exception as e: logfeed_data.log( LogLevel.ERROR, 'Error handling alpaca account update: ') logfeed_data.log(LogLevel.WARNING, traceback.format_exc()) traceback.print_exc() @cls.alpaca_stream.on(r'^status$') async def on_status(conn, channel, msg): try: cls._on_status_update(msg.message, logfeed_data) except Exception as e: logfeed_data.log( LogLevel.ERROR, 'Error handling polygon status update:') logfeed_data.log(LogLevel.WARNING, traceback.format_exc()) traceback.print_exc() @cls.alpaca_stream.on(r'^A$') async def on_second_bars(conn, channel, data): # start_queue = pytime.monotonic() try: # print(str(data)) # print(f'is {data.start:%Y/%m/%d_%H:%M:%S}') # print(f'at {datetime.now():%Y/%m/%d_%H:%M:%S}') cls._on_data_update(data) except Exception as e: logfeed_data.log( LogLevel.ERROR, 'Error handling polygon candle update:') logfeed_data.log(LogLevel.WARNING, traceback.format_exc()) traceback.print_exc() # queue_time_ms = (pytime.monotonic() - start_queue) * 1000 # moment = datetime.strptime(data.start.strftime(DATE_TIME_FORMAT), DATE_TIME_FORMAT) """ try: if queue_time_ms > 80: print(f'took {queue_time_ms:.0f}ms to queue {data.symbol} {moment:%M:%S} candle') else: print(f'queued {data.symbol} {moment:%M:%S} candle at {datetime.now():%M:%S}') except Exception as e: traceback.print_exc() """ # Subscribe to alpaca and polygon streams channels_to_stream = ['trade_updates', 'account_updates'] channels_to_stream.extend(f'A.{symbol}' for symbol in symbols) logfeed_data.log( LogLevel.INFO, 'Subscribing to polygon and alpaca streams') cls.alpaca_stream.run(channels_to_stream) except Exception as e: logfeed_data.log( LogLevel.ERROR, 'Polygon and alpaca streams disconnected unexpectedly') logfeed_data.log(LogLevel.WARNING, traceback.format_exc()) pytime.sleep(2) logfeed_data.log(LogLevel.INFO, 'Attempting to re-connect data streams')