Tradingene's package for algorithm backtest
Project description
Tradingene package for backtesting algorithms.
Overview
This package is project of Tradingene.com team. It was developed for stand-alone backtesting of trading algorithms mimicking Tradingene's API (see documentaion here).
Our package allows you to test your trading ideas. You write a code that reflects your trading logic and after backtest you are able to check whether your idea was good or not based on our backtest statistics. The best algorithms will have an opportunity to run live on an exchange (see Tradingene.com web site).
At the moment this package is able to mimic Tradingene's API, but very soon it will be possible to test your strategies using machine learning algorithms!
Getting started
Notice: There are little differences in a preparation stage between web platform and this package while setting up all needed parameters. We will explain here only package's setting. Platform setting is a little bit easier due to user interface.
All needed functionality is contained in a TNG class, class constructor takes 4 parameters (in the order of appearance):
- Name of the algorithm (string);
- Trading regime ("SP" or "MP", to be explained further)
- Date that backtest starts (datetime);
- Date that backtest ends (datetime).
If you don't know what class and/or constructor is -- don't worry! It is easy to follow the logic and meaning of this variables, particular implementation is not important!
Fisrt you have to specify name of your algorithm, regime that algorithm will use, start and end of backtest (other possible initializations see here).
from tng
from datetime import datetime
name = "Cornucopia"
regime = "SP"
start_date = datetime(2018, 1, 1)
end_date = datetime(2018, 2, 1)
alg = tng.TNG(name, regime, start_date, end_date)
After this code you will code an object named alg
, which contains all the functions you need. When you will run backtest your algorithm will be simulated from the January 1st 2018 till February 1st 2018.
After initialization like above you are able to add instrument and timeframe to your algorithm.
alg.addInstrument("btcusd")
alg.addTimeframe("btcusd", 1440)
Instrument is the name of the asset that your algorithm will use. Timeframe is measured in minutes. In the example above user-defined onBar function will call every 1440 minutes (one day) if there is a fully formed candle. Please see detailed explanation see here.
The next step is to implement onBar function. This function contains the core idea of your algorithm. Trading idea in this example is very clear.
- If the daily price moved down we sell hoping that this falling will continue;
- If the daily price raised during the day we buy hoping that the price will continue to go up;
- If an open price is equal to a close price do nothing;
- If several raising (falling) candles appears in a row then algorithm will buy (sell) only on the first candle (you will see a warning);
- If you are in a short position after buy you will have long position and vice versa.
In this example you are always "in a position", what position you have depends on the last daily candle.
Code that implements trading logic described above:
def onBar(instrument):
if instrument.open[1] > instrument.close[1]:
# If price goes down during the day then sell;
alg.sell()
elif instrument.open[1] < instrument.close[1]:
# If price goes up during the day then buy;
alg.buy()
else:
# If price did not change then do nothing
pass
This trading logic is very easy though you can find more [example here](where are examples).
After all needed steps are done we are able to backtest our strategy finally! Idea behind backtest is easy: backtest goes through historical data and invokes user-defined onBar function after timeframe
(specified in a addTimeframe
function) minutes left.
Backtest your algorithm like this:
alg.run_backtest(onBar)
That's it! Now you can see results of your backtest (not implemented yet)!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for tradingene-0.0.dev10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | adb5497fdd010230b76965876b214d0f76db82c7762c86eeaf5d42fd3431973d |
|
MD5 | 0c76373117d621cd36669c58123175c8 |
|
BLAKE2b-256 | ca3bbe38814cdc654bcbf7e0d716e91b7a55c8562fe8ecf8a89ff9da24c94fef |