alpacahq / alpaca-backtrader-api

Alpaca Trading API integrated with backtrader
https://pypi.org/project/alpaca-backtrader-api/
Apache License 2.0
627 stars 147 forks source link

How do you test the Websocket? #124

Open xs005 opened 4 years ago

xs005 commented 4 years ago

I am playing with WebSocket streaming data. How could you do testing aftermarket hours when there are no data from the WebSocket?

shlomiku commented 4 years ago

what do you mean playing with the websocket? do you mean this package or that one: https://github.com/alpacahq/alpaca-trade-api-python

if you mean this package (alpaca-backtrader-api) you have a backtester which allows you to run tests on past data if you mean the alpaca-trade-api package, there's no way to currently simulate data outside market hours

xs005 commented 4 years ago

Sorry about the confusing question, I mean the alpaca-trade-api. I like your backtrader-api, but I cannot run paper-trader with alpaca-backtrader-api, even though I used the sample codes, it just pull the streaming data and then got warning to wait 3 sec for more data and then stopped.

I am thinking to build a simple mock class to simulate the websocket streaming using historical data, with an order handler for testing.

BTW, I read your blog about the websocket proxy, that is awesome work.

shlomiku commented 4 years ago

Hi, so I was actually thinking on using the alpaca-proxy-agent to create a mock server to simulate data for testing. the issue with that is writing a wrapper for mock orders.

dsqx71 commented 3 years ago

@shlomikushchi Hi, I'm quite interested to generate simulation data using alpaca-proxy-agent, could you please share some code pointers or hints about how to add such a mocking layer?

shlomiku commented 3 years ago

of course. let's start by defining your requirements first?

dsqx71 commented 3 years ago

@shlomikushchi, I'd like to build a mocking layer that could send mock orders to the subscribers of the proxy-agent, and whether it's sending real orders or mock orders remains agnostic to the subscribers. Such a tool could allow us to test our strategies after the market is closed.

Requirements :

shlomiku commented 3 years ago

Let's make some definitions first:

The proxy-agent could be used for mocking websocket data. It can't be used to mock user actions. for that, you need to mock the REST API. And you need to create a portfolio instance to hold "positions" You probably want to do both to make a complete simulation

dsqx71 commented 3 years ago

@shlomikushchi Thanks for clarifying, I totally agree that it's users's job to handle user actions.

shlomiku commented 3 years ago

you could do a mock for both, but it's not with the proxy-agent, this is only for stream data.

KalebMills commented 3 years ago

Greetings, I have a backtesting harness for my bot that does exactly this. It seems what you will want to use is use Polygons historic trades as seen here. Load all of that into a list, and start emitting on some random interval through a websocket, that way you can abstract this through a websocket, and the user doesn't need to change their code.

7ruth commented 2 years ago

I just found out that there is an undocumented feature, using alpaca python sdk, you can connect to the 'test' websocket and use "FAKEPACA" ticker.

Here is my set up that got me 'fake' bars when market was closed:


conn = Stream(ALPACA_API_KEY,
                  ALPACA_SECRET_KEY,
                  data_stream_url=URL(
                      "wss://stream.data.alpaca.markets/"),
                  data_feed='test',
                  )

conn.subscribe_bars(print_quote, 'FAKEPACA')

(I was getting an issue when trying to stream trades)

aakashthakkar commented 1 year ago

I just found out that there is an undocumented feature, using alpaca python sdk, you can connect to the 'test' websocket and use "FAKEPACA" ticker.

Here is my set up that got me 'fake' bars when market was closed:


conn = Stream(ALPACA_API_KEY,
                  ALPACA_SECRET_KEY,
                  data_stream_url=URL(
                      "wss://stream.data.alpaca.markets/"),
                  data_feed='test',
                  )

conn.subscribe_bars(print_quote, 'FAKEPACA')

(I was getting an issue when trying to stream trades)

Ahh. Was stuck in the middle of the night trying to generate test data. Man you saved the day. Exactly what I was looking for.