Home > Mobile >  I have this code using the alpaca websocket but there is a function which is not defined
I have this code using the alpaca websocket but there is a function which is not defined

Time:04-12

from time import sleep
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import requests
plt.style.use("fivethirtyeight")
import alpaca_trade_api as tradeapi
import threading
from bs4 import BeautifulSoup
import datetime
import logging

api_key = 'YOUR API KEY'
api_secret = 'YOUR API SECRET KEY'
base_url = 'https://paper-api.alpaca.markets'
data_url = 'wss://data.alpaca.markets'
ws_url = 'wss://data.alpaca.markets'

# instantiate REST API
api = tradeapi.REST(api_key, api_secret, base_url, api_version='v2')


# init WebSocket
conn = tradeapi.stream2.StreamConn(
    api_key,
    api_secret,
    base_url=base_url,
    data_url=data_url,
    data_stream='alpacadatav1',
)

I get the error conn = tradeapi.stream2.StreamConn( AttributeError: module 'alpaca_trade_api' has no attribute 'stream2'

This code is coming from https://algotrading101.com/learn/alpaca-trading-api-guide/ on the part where he shows how he made a trading algorithm near the end of the page.

Do I need to import any extra libraries or am I wrong somewhere In the code.

CodePudding user response:

That tutorial is slightly out of date. Newer versions of alpaca_trade_api are using the Stream class:

conn = tradeapi.stream.Stream(
    key_id=api_key,
    secret_key=api_secret,
    base_url='https://paper-api.alpaca.markets',
    data_feed='iex'
)

Note that the free data feed is 'iex' and the paid data feed is 'sip'. Once you establish a connection, you will need to subscribe to trades or quotes using conn.subscribe_trades() or conn.subscribe_quotes(). See the github page for details.

  • Related