Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration of other marketdata vendors / alpaca.markets #167

Closed
iocron opened this issue Jul 1, 2024 · 9 comments · Fixed by #179
Closed

Integration of other marketdata vendors / alpaca.markets #167

iocron opened this issue Jul 1, 2024 · 9 comments · Fixed by #179
Assignees

Comments

@iocron
Copy link

iocron commented Jul 1, 2024

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
Copy link
Owner

cinar commented Jul 3, 2024

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

@cinar cinar self-assigned this Jul 3, 2024
@cinar
Copy link
Owner

cinar commented Jul 16, 2024

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
Copy link
Author

iocron commented Jul 19, 2024

@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
Copy link
Owner

cinar commented Jul 19, 2024

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
Copy link
Author

iocron commented Jul 19, 2024

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
Copy link
Owner

cinar commented Jul 19, 2024

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
Copy link
Owner

cinar commented Jul 19, 2024

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

@iocron
Copy link
Author

iocron commented Jul 21, 2024

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
Copy link
Owner

cinar commented Jul 21, 2024

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.

cinar added a commit that referenced this issue Jul 21, 2024
# Describe Request

Alpaca Markets Repository added.

Fixed #167 

# Change Type

New feature.
@cinar cinar reopened this Jul 21, 2024
@cinar cinar closed this as completed Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants