alpacahq/alpaca-backtrader-api

How do you test the Websocket?

xs005 opened this issue · 12 comments

xs005 commented

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

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

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.

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.

@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?

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

@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 :

  • We should add an argument that allows us to turn on/off this mocking layer when launching the proxy agent
  • The mock orders could be generated by a random number generator by default, and we should also allow users to use their own custom order generators.

Let's make some definitions first:

  • Websocket data:

    • quotes
    • general trades
    • bars
    • account actions (user trade responses)
  • User actions: go through the REST api, not the proxy-agent

    • orders
    • account management
    • portfolio actions

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

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

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

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

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)

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.