cinar / indicator

Indicator is a Golang module providing various stock technical analysis indicators for trading.
GNU Affero General Public License v3.0
477 stars 96 forks source link

Integration of other marketdata vendors / alpaca.markets #167

Closed iocron closed 1 month ago

iocron commented 3 months ago

Is your feature request related to a problem? Please describe. Marketdata from other vendors would be awesome, especially alpaca.markets

Describe the solution you'd like Marketdata Integration by using the official alpaca.markets api: https://github.com/alpacahq/alpaca-trade-api-go

cinar commented 3 months ago

Thank you very much for the feature request! Let me add support for it definitely!

cinar commented 2 months ago

Now that I am back, I will take care of this one. I may need your help testing it, as I don't have an Alpaca subscription unless they offer free tier.

iocron commented 2 months ago

@cinar happy to help wherever I can. The alpaca marketdata can be freely accessed :) You only need to create a account (https://alpaca.markets/) and api key/secret. I've written a simplified example of receiving the bars from alpaca below (in hour format and of the last x days):

package main

import (
    "fmt"
    "github.com/alpacahq/alpaca-trade-api-go/v3/marketdata"
    "time"
)

func main() {
    clientOpts := marketdata.ClientOpts{
        APIKey:    "myapikey",
        APISecret: "mysecretkey",
    }
    client := marketdata.NewClient(clientOpts)

    lastNDays := 7
    barsRequest := marketdata.GetBarsRequest{
        TimeFrame: marketdata.NewTimeFrame(1, marketdata.Hour),
        Start:     time.Now().AddDate(0, 0, -(lastNDays)),
    }

    bars, err := client.GetBars("AAPL", barsRequest)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(bars)
}
cinar commented 2 months ago

This is super helpful! I was trying to find the right API to use. Is GetBars will be enough or should I try to add support for the streaming APIs too?

iocron commented 2 months ago

Yeah, I had the same problem when going trough the API Docs 😅 GetBars would be enough for me, others might find the streaming API very useful too. I had no chance to look into the streaming API yet so I am not sure how much extra work it will take to implement.

Btw./Offtopic, I am currently playing around with the backtesting of v2 as well, but I couldn't get it running without issues yet. I always get "Best outcome for brk-b is 0.00% with Apo Strategy." for some reason, but thats a different issue I might open soon if I am not missing something important on my part.

cinar commented 2 months ago

Awesome, yes, they have a ton of API, so I wasn't sure which one to use. Let me start with the GetBars then, and hopefully adding the streaming one is not a big deal later on. I am thinking of doing this as a separate Go module, so that I don't introduce a dependency to Alpaca directly. As soon as I have something functional, I'll share it here.

Regarding the backtest problem. I've seen that happening before. At that time, I believe I had old data, and backtest was looking for a time window that I had no data points. Not sure if it is the same problem for you. Definitely let me know, I am also very curious.

cinar commented 2 months ago

@iocron please take a look at https://github.com/cinar/indicatoralpaca. I hope it works the way you envisioned?

iocron commented 2 months ago

Receiving the data seems to work as expected 👍 :) But the backtest seems off (I had to increase the date range to get any trading signals at all, otherwise i always get 0.00% as outcome).

I created a fork and added a example to examples/alpaca_example_macd.go (https://github.com/iocron/indicatoralpaca). Running the example go run examples/alpaca_example_macd.go results in only 1 buy and 1 sell signal (output/AAPL - MACD Strategy (12,26,9).html). But there should be more trades in a 180day range on the daily timeframe unit (verified by using the official macd strategy on tradingview with the daily timeframe unit + macd 12,26,9).

Btw, how can I choose multiple Timeframe Units the way it is implemented right now (e.g. for HTF/HigherTimeFrame Trading)?

cinar commented 2 months ago

That's correct, I also only get 2 transactions, one buy, and one sell. It is normalized, so it could be Sell, Sell, Buy, Buy, Sell, but you'll only get Buy, Sell. But looking at the chart, it seems to be acting according to the logic in the strategy.

MACD > Signal and MACD < 0 results in a Buy Signal > MACD and MACD > 0 results in a Sell

image

Their logic may be different. For a long time, I didn't have the MACD < 0 and MACD > 0 checks, and it was generating more transactions but those were unnecessary, so had to add this change.

Certainly we can define a new strategy without that check also if that helps.

Currently the library is assuming days as the time unit for the periods. However, it should work for other time units also with some tweaking. Can you give an example? I am also curious how we can enable that.