Hi All,
I’m delighted to announce Trading.jl, an event-driven algorithmic trading and backtesting package.
It provides an easy-to-use framework for defining and executing trading strategies based on technical indicators, as well as backtesting these strategies on historical data. The backtesting is set up in a way to be as equivalent to realtime trading as possible.
It builds on the ease of use, extensibility, inherent performance and elegance of the ECS (Entity-Component-System) paradigm, implemented through Overseer.jl, and keeps interoperability with the Julia ecosystem by interfacing with TimeSeries.jl.
Features
- Highly customizable and extensible by being based on ECS
- Backtesting and Realtime trading treated on completely equal footing
- Several built in indicators such as
SMA
,EMA
,MovingStdDev
,RSI
andBollinger
- Interaction with brokers such as Alpaca
- High performance, with backtesting on 100k data points within seconds
Illustrative Example
To define a trading strategy, all you need to do is implement a Julia struct
that subtypes System
with an update
function that defines the trading logic. The update
function is called periodically by the framework and has access to tick data for the tickers that the strategy was created for, as well as any technical indicators requested by the strategy. The package includes several built-in technical indicators
such as simple moving averages, relative strength index, and exponential moving averages. Users can also define their own custom indicators
.
struct MyStrategy <: System end
Overseer.requested_components(::MyStrategy) = (Open, Close, SMA{20, Close}, SMA{200, Close})
function Overseer.update(s::MyStrategy, trader, ticker_ledgers)
for ledger in ticker_ledgers
for e in new_entities(ledger, s)
#Trading logic goes here
end
end
end
To execute a trading strategy in real-time, users can create a Trader
object with the desired strategies, and connect it to a real-time data source through the different broker APIs:
broker = AlpacaBroker("<key_id>", "<secret>")
strategy = Strategy(:my_strategy, [MyStrategy()], tickers=["AAPL"])
trader = Trader(broker, strategies=[strategy])
start(trader)
If you want to backtest a trading strategy on historical data, you can use BackTester
instead of Trader
with the desired data range, interval, and strategies. The BackTester
will simulate the behavior of a realtime Trader
on the specified data. Afterward, a TimeArray
can be created with the data from the trader, and used for performance analysis.
trader = BackTester(HistoricalBroker(broker),
strategies=[strategy],
start = <start date>,
stop = <stop date>,
dt = <data timeframe>)
start(trader)
The Documentation is relatively complete and ever improving.
Have a look, let me know if you have any feedback, and happy trading!
Cheers