alpacahq / example-scalping

A working example algorithm for scalping strategy trading multiple stocks concurrently using python asyncio
763 stars 184 forks source link

Trying to set up for paper trading #1

Closed nickjohnson22 closed 4 years ago

nickjohnson22 commented 4 years ago

In an effort to start using this for paper trading, I have modified the main function to stream ticker data using the live credentials and to stream trade data using the paper credentials. However, I have yet to get to a breakpoint set on stream2 in any circumstance.

def main(args):
    api = alpaca.REST(dev_key, dev_secret, base_url=dev_endpoint)
    stream1 = alpaca.StreamConn(live_key, live_secret)
    stream2 = alpaca.StreamConn(dev_key, dev_secret, base_url=dev_endpoint)

    fleet = {}
    symbols = get_tickers()
    for symbol in symbols:
        algo = ScalpAlgo(api, symbol, lot=args.lot)
        fleet[symbol] = algo

    @stream1.on(r'^AM')
     async def on_bars(conn, channel, data):
        if data.symbol in fleet:
            fleet[data.symbol].on_bar(data)

    @stream2.on(r'trade_updates')
     async def on_trade_updates(conn, channel, data):
        logger.info(f'trade_updates {data}')
        symbol = data.order['symbol']
        if symbol in fleet:
            fleet[symbol].on_order_update(data.event, data.order)

     async def periodic():
        while True:
            if not api.get_clock().is_open:
                logger.info('exit as market is not open')
                sys.exit(0)
            await asyncio.sleep(30)
            positions = api.list_positions()
            for symbol, algo in fleet.items():
                pos = [p for p in positions if p.symbol == symbol]
                algo.checkup(pos[0] if len(pos) > 0 else None)
     channels1 = ['AM.' + symbol for symbol in symbols]
     channels2 = ['trade_updates']

     loop1 = stream1.loop
     loop2 = stream2.loop
     loop1.run_until_complete(asyncio.gather(
        stream1.subscribe(channels1),
        periodic(),
    ))
     loop2.run_until_complete(asyncio.gather(
        stream2.subscribe(channels2),
        periodic(),
    ))
     loop1.close()
     loop2.close()

This, however, does seem to work in testing.

import alpaca_trade_api as alpaca
from auth import dev_key, dev_secret, dev_endpoint

stream2 = alpaca.StreamConn(dev_key, dev_secret, dev_endpoint)

@stream2.on(r'trade_updates')
async def on_trade_updates(conn, channel, data):
    print('data', data)

stream2.run(['trade_updates'])

Do you have any thoughts on how I might be able to modify this code to allow trade updates to come from the paper account?

Thank you, Nick

nickjohnson22 commented 4 years ago

I was able to get it to work by doing it this way. Hopefully, this helps someone.

def main(args):
    api = alpaca.REST(dev_key, dev_secret, base_url=dev_endpoint)
    stream1 = alpaca.StreamConn(live_key, live_secret)
    stream2 = alpaca.StreamConn(dev_key, dev_secret, base_url=dev_endpoint)

    fleet = {}
    symbols = args.symbols
    for symbol in symbols:
        algo = ScalpAlgo(api, symbol, lot=args.lot)
        fleet[symbol] = algo

    @stream1.on(r'^AM')
    async def on_bars(conn, channel, data):
        if data.symbol in fleet:
            fleet[data.symbol].on_bar(data)

    @stream2.on(r'trade_updates')
    async def on_trade_updates(conn, channel, data):
        logger.info(f'trade_updates {data}')
        symbol = data.order['symbol']
        if symbol in fleet:
            fleet[symbol].on_order_update(data.event, data.order)

    async def periodic():
        while True:
            if not api.get_clock().is_open:
                logger.info('exit as market is not open')
                sys.exit(0)
            await asyncio.sleep(30)
            positions = api.list_positions()
            for symbol, algo in fleet.items():
                pos = [p for p in positions if p.symbol == symbol]
                algo.checkup(pos[0] if len(pos) > 0 else None)

    channels1 = ['AM.' + symbol for symbol in symbols]
    channels2 = ['trade_updates']

    loop1 = stream1.loop
    loop2 = stream2.loop
    loop1.run_until_complete(stream1.subscribe(channels1))
    loop2.run_until_complete(asyncio.gather(
        stream2.subscribe(channels2),
        periodic(),
    ))
    loop1.close()
    loop2.close()