kzhdev / alpaca_zorro_plugin

A Zorro Plugin for Alpaca Trade API.
https://zorro-project.com/
MIT License
14 stars 5 forks source link

Alpaca Adjusted Prices #11

Open Algo-Mike opened 2 months ago

Algo-Mike commented 2 months ago

Hi Kun,

Thanks for creating this very useful plugin!

Currently, the Zorro Trader Download.c script, in conjunction with your Alpaca plugin, downloads unadjusted 1 minute prices. Stock splits look like huge drops in price which create all kinds of issues with backtesting. Alpaca does offer adjusted prices, as per this Python script, run on my paper account:

def get_bars(symbol, start, end, limit=1000, timeframe='1Min', adjustment = 'raw'): base_url = f'https://data.alpaca.markets/v2/stocks/{symbol}/bars' request_headers = {'APCA-API-KEY-ID': 'Paper-account-key', 'APCA-API-SECRET-KEY' : 'Paper-account-secret'} request_params = {'start' : start, 'end' : end, 'limit' : limit, 'timeframe' : timeframe, 'adjustment' : adjustment} return requests.get(base_url, params=request_params, headers=request_headers).json()

bars = get_bars('AAPL', '2020-08-27', '2020-09-01', adjustment='raw')['bars'] print(bars) bars = get_bars('AAPL', '2020-08-27', '2020-09-01', adjustment='all')['bars'] print(bars)

The result for a stock split, on split day (AAPL): Apple UNADJUSTED: [{'c': 507, 'h': 507, 'l': 506.1, 'n': 71, 'o': 506.1, 't': '2020-08-24T08:00:00Z', 'v': 5941, 'vw': 506.505578}, ... {'c': 506.1, 'h': 506.1, 'l': 505.74, 'n': 145, 'o': 505.75, 't': '2020-08-24T23:59:00Z', 'v': 9248, 'vw': 505.959286}]

Apple ADJUSTED: [{'c': 124.01, 'h': 124.01, 'l': 123.79, 'n': 71, 'o': 123.79, 't': '2020-08-24T08:00:00Z', 'v': 23764, 'vw': 123.89}, ... {'c': 123.79, 'h': 123.79, 'l': 123.7, 'n': 145, 'o': 123.7, 't': '2020-08-24T23:59:00Z', 'v': 36992, 'vw': 123.75}]

What did the trick here is to add the adjustment parameter and set it to 'all' (adjustment for splits and dividends).

How can I do the same with your Alpaca Zorro plugin? I tried adding brokerCommand(SET_ORDERTEXT, "adjustment = 'all"); to the Download.c script, with no luck. I guess it only works with orders, not downloading data. The log message was "2024-04-28 16:58:21 | DEBUG | Unhandled command: 47 0."

Please advise.

Thank you in advance, Algo Mike

kzhdev commented 1 month ago

You need to replace the xxxx with your alpaca's API KEY and SERET in the above command.

kzhdev commented 1 month ago

Please change the "AlpacaLogType" to 8 in the ZorroFix.ini. It will log order responses.

kzhdev commented 1 month ago

Sorry, the above version is not good. Try this one:

AlpacaZorroPlugin_v.zip

Algo-Mike commented 1 month ago

This last version (above) worked great during trading hours today. No issues to report. Will circle back tomorrow.

Algo-Mike commented 1 month ago

Actually, I just tried to download minute data for today. It kind of works, but I'm getting the too many requests again.

!Failed to get bars. too many requests. Error 035: DECK download 5 errors 167 min Load DFS 2024.. !Failed to get bars. too many requests. !Failed to get bars. too many requests. !Failed to get bars. too many requests. Error 035: DFS download 5 errors 365 min Load DG 2024.. !Failed to get bars. too many requests. !Failed to get bars. too many requests. !Failed to get bars. too many requests. Error 035: DG download 5 errors 395 min

MaxRequests = .1; is set

kzhdev commented 1 month ago

I tried downloading minute data but didn't see "too many requests." Do you have multiple Zorro running at the same time? If you use the Free market data plan, do you have "AlpacaPaidDataPlan = 0" in the ZorroFix.ini file?

Algo-Mike commented 4 weeks ago

I am still getting it, although today not as often. I do not have multiple Zorro instances running at the same time. The flag is set to 0, as it always was. The max requests flag is set as well.

!Failed to get bars. too many requests. !Failed to get bars. too many requests. Error 035: ZBH download 5 errors 370 min Load ZBRA 2024.. !Failed to get bars. too many requests. !Failed to get bars. too many requests.. Error 035: ZBRA download 5 errors 171 min Load ZTS 2024.. !Failed to get bars. too many requests. !Failed to get bars. too many requests. !Failed to get bars. too many requests. Error 035: ZTS download 5 errors 387 min

kzhdev commented 4 weeks ago

Could you set the AlpacaLogType = 56 and update the logs?

Algo-Mike commented 3 weeks ago

Sure. Here's what I get:

!Failed to get bars. too many requests. !Failed to get bars. too many requests. Error 035: ZBH download 5 errors 1158 min Load ZBRA 2024... !Failed to get bars. too many requests. !Failed to get bars. too many requests. !Failed to get bars. too many requests. Error 035: ZBRA download 5 errors 552 min !Failed to get bars. too many requests. Load ZTS 2024.. !Failed to get bars. too many requests. Error 035: ZTS download 5 errors 1154 min

Alpaca_2024-06-13_193248.log WebsocketProxy.log

kzhdev commented 3 weeks ago

Thanks for the log. I suspect the error was in the get bars response JSON, but I don't see the responses logged. Please make sure you set AlpacaLogLevel = 5 and AlpacaLogType = 56.

Are you using Zorro's Download scripts? Can you give me your Asset list? I will try to reproduce the issue.

Algo-Mike commented 3 weeks ago

The problem persists. I set everything as you asked. The script is Download.c with MaxRequests = .1; The asset list is the whole SP500. I am attaching it, and the logs. There is only one instance of Zorro running.

Alpaca_2024-06-17_205206.log WebsocketProxy.log AssetsSP500.csv

kzhdev commented 2 weeks ago

Please try the attached dll. AlpacaZorroPlugin_v.zip

kzhdev commented 2 weeks ago

@Algo-Mike Do you still see the "too many requests" error?

Algo-Mike commented 1 week ago

Have been testing for a few days now, both downloading and paper trading. I didn't get the "too many requests" error anymore. Will keep testing and circle back here.