Stock trading in Elixir

This might be interesting as well: https://duckdb.org/

I don’t think we should make things too dependant on any particular DB, it would be nice to make the backtesting/ohlc parts of the project modular so that you can just pass in the data and get results with whatever DB setup.

Also, not to add to the DB confusion :slight_smile: , but it looks like Redis could be another good choice https://redislabs.com/blog/unlocking-timeseries-data-redis/.

1 Like

@LostKobrakai looks very cool, but also looks like someone will have to write a NIF for this (probably over my head for now).

@GunnarPDX in principle I agree, but when I attempt to abstract away from the database, I start to see my progress slowing. I looked to see if I can simply use Ecto and back it with SQLite for my own purposes, and it seems that I can… Maybe?

Making the library abstract enough to use most any database is a challenge that I’m kind of short on ideas for.

I decided to push on ahead with sqlite. Depo has been very straightforward to use. I wrote some functions tonight to seed my DB from polygon:

And I discovered that jupyter notebook works with elixir! So I am working towards backtesting in JN with SQLite.

@GunnarPDX I’m sorry, I still haven’t ran your support/resistance code. I will get to it. Planning on adding unit tests?

1 Like

@rm-rf-etc Hey, I went ahead and added a unit test as well as decimals.


Right now it returns a list of tuples like:
[
    {:support, %{h: 208.94, l: 208.94, t: 1597657204}},
    {:resistance, %{h: 210.16, l: 209.84, t: 1597656664}}
]

I can’t remember why I opened a pr last week, I closed that and can open a new one if it’s ready. I’ll also try to mess around with the polygon seeder and JN to see how that works with the S/R levels.

1 Like

@GunnarPDX that’s awesome, thank you.

Polygon’s data may give you some hassles. There can be holes in the data for some intervals, I posted about the issue here:

So I’m gonna try it with TD Ameritrade’s API.

Oh I see what the issue is now. Yeah, that’s a tricky one… I never ran into this issue with the portfolio stuff I worked on since it was all EOD data which is pretty easy to come by. I’m going to try out a company called TwelveData which looks promising.


If that doesn’t work and you can’t make a brokers API work then maybe take a look at DTN IQfeed. You would need to stream IQfeed’s data off a windows box because of their client, which is annoying, but people say good things about their reliability and their prices are reasonable.

Thanks for the info, I’ll take a look.

I think Ameritrade’s historical data will work well. I haven’t yet made start_date and end_date work, since an extra calculation step will be needed for Ameritrade’s params. You can see the endpoint doc here, the issue is that params period and periodType seem to define the start and end dates (unless I’m mistaken).

But otherwise what I added is working and you can try it out.

%Seeder.Conf{
    ticker: "MSFT",
    start: %Date{year: 2018, month: 1, day: 1},
    end: %Date{year: 2020, month: 10, day: 1},
    period_size: 1,
    period_unit: :minute,
    db_root: "/your/data/path",
    api_key: "****"
}
|> Seeder.dl_to_db(:ameritrade)

Nice! I haven’t tried it yet so this may not be the issue, but It looks like you aren’t supposed to provide a period/periodType if you use the startDate and endDate. Their naming is kinda weird, I’m guessing that period and periodType are for when you just want a variable number of the most recent weeks/months/years data. And frequency would be the bar interval size, eg: :minute.

1 Like

Migrating to https://github.com/ElixirTradingTools, will push some commits after work to fix names that I changed, and redirect people from the old location to the new. @GunnarPDX, check your github for the invite :slight_smile:

1 Like

Checking in, since some time has passed with no updates.

I’ve been spending my time lately with TradingView, testing strategies in pinescript. This has the advantage of being very quick for prototyping.

I’ve landed on two strategies that I’m pretty sure will translate to consistent profit, and I’m switching back to elixir now to collect larger samples, refine the strategy, and produce a bot that’s ready to run:

  • opening range breakout
  • trend following

Here’s a helpful video on building a trend following strategy.

I’ve been doing some work on the seeder repo to make the Ameritrade downloader work right (it’s in a separate branch).

2 Likes

Here’s a client library for streaming market data. No unit tests yet, but the examples work. Supported brokers are Polygon, Binance, and Coinbase.

To get forex data from Polygon, you have to buy the entry-level tier. I tried using a free key from Alpaca but Polygon blocks those. I will update this to support stocks too. Enjoy.

2 Likes

Sweet, I have been working on a Robinhood client and some nice financial charts. Robinhood doesn’t offer stream data and their auth is a bit annoying but they are the only broker with a usable API that offers fractional-shares and zero-commission if you needed that. I Might try and set up the stock data streaming with some of the charts I made in live view this weekend, still working on adding indicators to them.

Also made a correlation matrix library:

2 Likes

FYI Interactive Brokers expose a local API via Trader Workstation which can be supervised via IBController. Worth a try.

1 Like

Thanks @evadne, I didn’t know about this. I will give it a test drive.

@GunnarPDX thanks, I was thinking about writing a charting client to help me verify my data is right, but I’ll try yours first. Does it currently support autoscaling and repaint on update? Will take a look.

I wrote a small module to encapsulate the functionality for forming candlesticks in whatever given timeframe. Will add tests. The idea is that you use this module to generate your chart data from either full tick data or from lower timeframes.

@rm-rf-etc They currently have autoscaling and should repaint on update with a few small changes. Right now a lot of stuff is hardcoded so you will have to add some props for passing data in. I’ll make an effort to go in and add that stuff since I’ll be using them as real components soon.

The data format I’m using is:

[ {
    "date" : "2020-10-30 16:00:00",
    "open" : 388.625000000000,
    "low" : 388.290000000000,
    "high" : 389.190000000000,
    "close" : 388.790000000000,
    "volume" : 41147080
} ]

everything else like axis scaling and bar color gets handled by the component.

1 Like

I’m working on something pretty fun right now, it’s a rust NIF for performant chart analysis. PineScript is a really efficient language for testing ideas, but its usefulness falls off as soon as you get past the basics, so I think there’s a neat opportunity to build an equivalent in elixir and rust.

The way that PineScript works under the hood is that it generates a table, where every candlestick in the chart is a new row in the table, and each variable in your script generates a column in the table. So I prototyped a fixed-length rolling table in elixir, but I think the performance boost from using rust and a NIF would be worthwhile. I’ve also wanted the opportunity to write a NIF, and just needed a use case I’m invested in.

Some additional motivation: PineScript also has zero support for strategies that involve more than one asset, so strategies like stat-arb are out of the question. I’m planning on solving that, as well as portfolio-wide testing.

I’ve spent a lot of time with QuantConnect lately but I keep coming back to elixir because python and C# both irritate me.

I will probably reimplement the trade indicators in rust and provide a NIF for elixir. I’m also thinking about the potential to write elixir macros to make indicators and trading rules really concise, but I don’t know if that will be warranted, I’ll see when I get there.

Just wanted to share this in case anybody is interested.

6 Likes