An algorithmic trading system written in Elixir, supporting basic backtesting and live execution (currently only via the Alpaca brokerage).
To get started, you'll need an Alpaca account, and the associated app and secret keys:
ALPACA_API_KEY=XXXXX
ALPACA_API_SECRET=XXXXX
To execute a backtest, use the mix backtest
task along with a symbol and strategy:
mix backtest ./config.json
See the simple MacdCross strategy for an example:
defmodule Shiny.Strategy.MacdCross do
# A simple demo strategy using an MACD cross. Probably not a good idea for live trading.
def execute(state, portfolio, symbol, bars) do
current_bar = hd(bars)
closes = Enum.map(bars, & &1.close) |> Enum.slice(0, 100)
macd_histogram = TAlib.Indicators.MACD.histogram(closes)
position = Shiny.Portfolio.position(portfolio, symbol)
if position do
if(macd_histogram < 0) do
{state, %Shiny.Order{type: :close, symbol: symbol}}
end
else
if(macd_histogram > 0) do
{state, %Shiny.Order{type: :buy, symbol: symbol, shares: 100}}
end
end
end
end
Disclaimer: All investments and trading in the stock market involve risk. Any decisions to place trades in the financial markets, including trading in stock or options or other financial instruments is a personal decision that should only be made after thorough research, including a personal risk and financial assessment and the engagement of professional assistance to the extent you believe necessary. The trading strategies or related information mentioned in this article is for informational purposes only.
Signal - mostly stateless, gives a -1.0 to 1.0 buy/sell signal or other info
Manager - makes decisions on position entry and exit based on signals and other factors such as implementing stops or taking profits.
Executor - responsible for opening and closing positions, including choosing position size and strategy. For example, a "short SPY" strategy could choose to short the underlying, buy put options, short futures, etc.
Broker - a connector for different brokerages and exchanges, including