Giter VIP home page Giter VIP logo

Comments (6)

thedatadavis avatar thedatadavis commented on July 28, 2024 1

I had this issue as well.

The culprit appears to be in the alpaca-trade-api itself. Update the stream2.py module to create a new event loop if one is not running.

Change this:

# /site-packages/alpaca_trade_api/stream2.py
class StreamConn(object):
    def __init__(self, key_id=None, secret_key=None, base_url=None):
        self._key_id, self._secret_key = get_credentials(key_id, secret_key)
        base_url = re.sub(r'^http', 'ws', base_url or get_base_url())
        self._endpoint = base_url + '/stream'
        self._handlers = {}
        self._handler_symbols = {}
        self._base_url = base_url
        self._ws = None
        self.polygon = None
        self.loop = asyncio.get_event_loop() # where Runtime Error is occuring

To this:

# /site-packages/alpaca_trade_api/stream2.py
class StreamConn(object):
    def __init__(self, key_id=None, secret_key=None, base_url=None):
        self._key_id, self._secret_key = get_credentials(key_id, secret_key)
        base_url = re.sub(r'^http', 'ws', base_url or get_base_url())
        self._endpoint = base_url + '/stream'
        self._handlers = {}
        self._handler_symbols = {}
        self._base_url = base_url
        self._ws = None
        self.polygon = None

        # Add try block for get_event_loop()
        # If no current loop then create one
        try:
            self.loop = asyncio.get_event_loop()
        except:
            self.loop = asyncio.new_event_loop()
            asyncio.set_event_loop(self.loop)

Edit Re: Plotting

The sample backtest shows that the exactbars parameter should be set to either 0 or -1 or -2 to enable plotting.

Once you make this change, cerebro.plot() will return an array of charts. You can access the one from this example using cerebro.plot()[0][0] --

# example_backtest.py

# ...

data0 = DataFactory(dataname='AAPL', historical=True, fromdate=datetime(2015, 1,1), timeframe=bt.TimeFrame.TFrame("Days"))  # Supported timeframes: "Days"/"Minutes"
cerebro.adddata(data0)

cerebro.run(exactbars=0) # or -1 or -2
cerebro.plot()[0][0]

from alpaca-backtrader-api.

alexonab avatar alexonab commented on July 28, 2024

Try cerebro.setbroker() instead of cerebro.setbroker(broker). It seems like the alpaca broker only works with live trading. The default broker will do for backtesting.

from alpaca-backtrader-api.

fjctp avatar fjctp commented on July 28, 2024

I tried cerebro.setbroker() as you suggested, but I got this error.

setbroker() missing 1 required positional argument: 'broker'

Then I removed the line cerebro.setbroker(broker). The script finished without error, but I did not see a plot at the end.

from alpaca-backtrader-api.

alexonab avatar alexonab commented on July 28, 2024

It looks like commenting out lines 108 and 109 in alpacastore.py works also. I don't see the need for the event loop when calling conn.run.

from alpaca-backtrader-api.

kevinmarkwardt avatar kevinmarkwardt commented on July 28, 2024

The only way I could get this working is by updating the stream2.py as well but I changed it like this

# /site-packages/alpaca_trade_api/stream2.py
class StreamConn(object):
    def __init__(self, key_id=None, secret_key=None, base_url=None):
        self._key_id, self._secret_key = get_credentials(key_id, secret_key)
        base_url = re.sub(r'^http', 'ws', base_url or get_base_url())
        self._endpoint = base_url + '/stream'
        self._handlers = {}
        self._handler_symbols = {}
        self._base_url = base_url
        self._ws = None
        self.polygon = None

        # Add try block for get_event_loop()
        # If no current loop then create one
        try:
            self.loop = asyncio.get_event_loop()
        except:
            self.loop = asyncio.new_event_loop()
            asyncio.set_event_loop(self.loop)

TO

# /site-packages/alpaca_trade_api/stream2.py
class StreamConn(object):
    def __init__(self, key_id=None, secret_key=None, base_url=None):
        self._key_id, self._secret_key = get_credentials(key_id, secret_key)
        base_url = re.sub(r'^http', 'ws', base_url or get_base_url())
        self._endpoint = base_url + '/stream'
        self._handlers = {}
        self._handler_symbols = {}
        self._base_url = base_url
        self._ws = None
        self.polygon = None

        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)

Setting a new event loop and then setting that new one as the current event loop. Once I did that the threads connected and data streamed in. Hope this helps someone else.

from alpaca-backtrader-api.

shlomiku avatar shlomiku commented on July 28, 2024

this issue should be resolved with the updated version on pypi.
let me know if you still encounter issues.
please note - you must have a funded account to do paper/live trading.
you could do backtesting by getting the data not through polygon (polygon access requires a funded account)
let me know if there are still issues

from alpaca-backtrader-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.