freqtrade / freqtrade

Free, open source crypto trading bot
https://www.freqtrade.io
GNU General Public License v3.0
26.65k stars 5.83k forks source link

Alpaca Integration Query #10404

Closed alisalama closed 2 weeks ago

alisalama commented 2 weeks ago

Hi Matthias, I am looking at contributing to CCXT to add Alpaca and to start building out that functionality. I note your comments here (https://github.com/freqtrade/freqtrade/issues/7953#issuecomment-1366399243) and have implemented fetchTicker & fetchTickers for Alpaca in my working copy. fetchTicker returns the below. It essentially returns last / close / bid / bid volume / ask / ask volume.

I just want to confirm that that is enough data for Freqtrade to pick up the prices. I think from https://github.com/freqtrade/freqtrade/blob/f3e2dcd34205a82c1dfabdaab2aa8f4022564ef1/freqtrade/exchange/exchange.py#L1933C2-L1948C20 it is going to use 'last' which is included so that should be ok.

From ccxt docs (https://github.com/ccxt/ccxt/wiki/Manual#ticker-structure), the fetch_ticker should return 24h weighted values, but the 24h rolling values are not available on the Alpaca api endpoints, so have had to use smaller set of values. I note quite a few other exchanges on ccxt library, including coinbase, have only the last/close/bid/ask on fetch_ticker, so I presume it's ok?

Will push ahead with the pull request to ccxt if so.

My eventual goal is to use Freqtrade for stocks through Alpaca, it's both a Crypto and Stocks api, happy to work on my own copy & unsupported, so no bother. I assume theoretically this will work, just have different symbol names. I have been using Freqtrade for crypto for over a year now, it works brilliantly well and is super robust, and I can't find anything nearly as good for stocks.

There is no fetchL2OrderBook equivalent endpoint.

Thanks, Ali


{
    'symbol': 'BTC/USD',
    'timestamp': 1720252943442,
    'datetime': '2024-07-06T08: 02: 23.442Z',
    'high': None,
    'low': None,
    'bid': 56546.6,
    'bidVolume': 0.28023,
    'ask': 56613.2,
    'askVolume': 0.55196,
    'vwap': None,
    'open': None,
    'close': 56619.023,
    'last': 56619.023,
    'previousClose': None,
    'change': None,
    'percentage': None,
    'average': None,
    'baseVolume': None,
    'quoteVolume': None,
    'info': {
// Full data returned from end point
        'dailyBar': {
            'c': '56592.09',
            'h': '56692.509',
            'l': '56056.7705',
            'n': '15',
            'o': '56056.7705',
            't': '2024-07-06T05: 00: 00Z',
            'v': '0.223029716',
            'vw': '56492.6293752827'
        },
        'latestQuote': {
            'ap': '56613.2',
            'as': '0.55196',
            'bp': '56546.6',
            'bs': '0.28023',
            't': '2024-07-06T08: 02: 23.442102382Z'
        },
        'latestTrade': {
            'i': '6641683653325507660',
            'p': '56619.023',
            's': '0.002148216',
            't': '2024-07-06T07: 57: 26.023919713Z',
            'tks': 'B'
        },
        'minuteBar': {
            'c': '56592.09',
            'h': '56592.09',
            'l': '56592.09',
            'n': '0',
            'o': '56592.09',
            't': '2024-07-06T08: 00: 00Z',
            'v': '0',
            'vw': '0'
        },
        'prevDailyBar': {
            'c': '56089.208',
            'h': '57006.3755',
            'l': '53825.764',
            'n': '267',
            'o': '53875.3225',
            't': '2024-07-05T05: 00: 00Z',
            'v': '22.647476205',
            'vw': '54093.4433016979'
        }
    }
}```
xmatthias commented 2 weeks ago

~No it's not. ~

you'll at least need fetchOHLCV (with history - otherwise we'll have to disable the exchange manually) - otherwise freqtrade will assume the exchange as not suitable.

A list of required endpoints (what ccxt MUST provide) for freqtrade to even consider the exchange is here: https://github.com/freqtrade/freqtrade/blob/f3e2dcd34205a82c1dfabdaab2aa8f4022564ef1/freqtrade/exchange/common.py#L65

You'll also need either fetchTicker or fetchL2Orderbook. Either of the 2 will work - as long as fetchTicker has bid/ask values - which also provide uptodate information (essentially the orderbook).

not having the orderbok will be a bit limiting - and i doubt alpaca really doesn't offer orderbook ... but that's a different topic

For questions about how to integrate to ccxt, please refer to ccxt. ~If they're not interested in integrating alpaca (they'll reject your PR), your endavor will end at that point anyway.~

(funnily, alpaca does seem to be somewhat in ccxt already - though with a ton of missing endpoints) - so whatever you "tried" - is most likely not going to fit it.

image

alisalama commented 2 weeks ago

Thanks Matthias. Seems like got the basis to move forward, so will keep going.

OHLCV CCXT Alpaca already has fetch_ohlcv implemented. I have tested just tested the fetch_ohlcv function, works fine, can grab full history from it.(https://github.com/ccxt/ccxt/blob/be60a09df3a28b10bf8cf243cd3c940e08d08c46/python/ccxt/alpaca.py#L561C9-L561C20).

fetchTicker You'll also need either fetchTicker or fetchL2Orderbook. Either of the 2 will work - as long as fetchTicker has bid/ask values - which also provide uptodate information (essentially the orderbook).

I have implemented fetchTicker + fetchTickers and includes bid/ask/last.

Order Book ccxt Alpaca has fetchOrderBook and fetchL1OrderBook implemented but not FetchL2Orderbook. I will do some investigation on the differences between those, and come back.

I'm fine with the integration to ccxt part, experienced programmer here so no problem. I was mainly looking for data requirement from fetchTicker(s), but looks like what I've implement will be sufficient.

Thanks, Ali

xmatthias commented 2 weeks ago

you don't necessarily need fetchTickers - it's only used for some pairlists - not necessary for basic operations