Alpaca-py examples

I’m experiencing many problems migrating our applications to the new API.

In particular, I’m having a hard placing and canceling Bracket, OTO and OCO orders in the paper environment.

Does anybody have/know of examples how to place and cancel those order types?

1 Like

Did you ever figure this out? I’m confused too…

same. almost no example provided what so ever.

I figured it out, and getting more comfortable with the syntax

2 Likes

Thanks for the code; I’m still struggling with getting stream market data though:

async def handler(data):
    print("received data: ", data)
    

def main():
    stream = StockDataStream(api_key = API_KEY, secret_key = SECRET_KEY, feed=DataFeed.IEX, url_override="wss://data.alpaca.markets/stream")
    # stream = CryptoDataStream(api_key = API_KEY, secret_key = SECRET_KEY)
    # the crypto stream works perfectly fine; but the stock data stream just get stuck when I don't use url_override, and print "data websocket error, restarting connection: server rejected WebSocket connection: HTTP 403" when I do.

    stream.subscribe_bars(handler, "BABA")
    print("Start streaming")
    stream.run()

I figured it out… the documentation is just so lack luster. I can’t seem to utilize the Bar object from alpaca in any reasonable form (not indexing, not function calling, not bar.close parameter reference…) So I use raw data and unpack it myself… essentailly not able to utilize the api at all.

raw_data = True
async def bar_handler(bar):
    print(bar)
    mapped_bar = {
        BAR_MAPPING[key]: val for key, val in bar.items() if key in BAR_MAPPING
    }
    print(mapped_bar)

stream = StockDataStream(api_key = API_KEY, secret_key = SECRET_KEY, raw_data=raw_data, feed=DataFeed.IEX)
    # stream = CryptoDataStream(api_key = API_KEY, secret_key = SECRET_KEY)

    stream.subscribe_bars(bar_handler, "BABA")