Comments (6)
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.
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.
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.
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.
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.
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)
- Not a reliable API
- backtrader-api uses deprecated alpaca-trade-api - will it be migrated to alpaca-py?
- Documentation for creating new Alpaca is broken. HOT 1
- Trading Calender - Package Issue HOT 3
- Cannot import alpaca-backtrader-api (Python 3.9.9 MacOS) HOT 5
- backtest not going back far enough HOT 2
- Multiple datas has connection limited error?
- alpaca-trade-api uses UTC while `_clear_out_of_market_hours` assumes NY Time. HOT 1
- Alpaca AttributeError: 'Asset' has not attribute 'status'
- Data websocket error with paper trading. Error code = 1002 (protocol error)
- Multiple WebSocket Open or Proxy - for multiple strategies
- Cannot purchase Long and create a trailing stop
- The Default Value for 'stocklike'
- only getting minute data until 12pm EST everyday in historical mode HOT 1
- Multiple datas sample no longer works HOT 1
- alpaca-trade-api-python is being deprecated by alpaca-py HOT 1
- Replay with historical data only retrieve daily bars instead replaying every minute
- module 'finplot' has no attribute 'add_order' HOT 1
- error installing on Windows 11, Python 3.9 HOT 3
- Wrong timezone for DataFactory
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alpaca-backtrader-api.