Giter Site home page Giter Site logo

algotrader's Introduction

algotrader

Paused: Currently paused while I work to set up a good set of cTrader bots and learn all I can from that experience.

WIP: Work in progress.

Intro

Open source algorithmic trader with fast backtesting written in Rust.

What works so far

  • Running make shows backtest results.
  • Running make test runs tests.

You can support the development of this tool (give me more time to work on it) by using my referal link to ig.com or send bitcoin to 1TqbToWRV41o8879d8zNectrVHz5H97zV.

Goal 1: Proof of concept (backtest, two strategies, hardcoded config)

Step 1

  • TradingView import for stock data.
  • Be able to run on 1 stock.
    • Be able to run one stock without considering days.
    • Separating into days and running days in parallel.
    • Simulator (src/simulator.rs)
      • Rework parallelism to be about variations and fuzz rather than days
      • Rework so that current account size is always available and fees applied to next day
      • Review all todos in the code
      • One simple strategy.
        • Basic buy and sell structure
        • Make trades based on the strategy
      • Don't provide full current day data in backtests (e.g. high/low/close since that is future knowledge that won't be available in real trading).
      • Figure out why the previous day bug produced so much better results (it knew the future)
      • Rework data so that the strategies can't access any future data accidentally.
      • Implement moving averages
      • Implement stop loss helpers
      • Select a market and implement strategy that works on a short time frame
      • Display trades and manually verify they makes sense in reality.
    • Run with live data
      • Live data API using FIX/Skilling
      • Backtest and adapt with Skilling data
      • Run with live data.
      • Manual buy and sell notifications as default fallback API.

Step 2

  • Implement buy and sell APIs to let it run for real (with tiny sums of money).
    • The idea here is to fix the most common error states as they occur over time.

Step 3

  • Be able to run on multiple stocks
  • Two different stategies (probably one for ranging and one for trending markets).

Bonus

  • Full system testing setup (once I have explored what data the system will return, etc.)

Goal 2: Use the data to implement proper backtesting tools and initial strategies

  • More backtesting tools, fuzzing, optimization, etc.
  • Implement and verify different strategies.

Core concepts and design ideas

  • Loss is always a possible outcome.

    • Never trade anything you can't loose. Even if everything works correctly, perhaps a technical issue somewhere causes orders not to go through properly, or something happens where the market gaps up or down unexpectedly during the day. This is why you want to risk a fixed amount and not the entire account.
  • Fixed amount positions by default.

    • This will make an account grow slower but safer since it avoids huge losses if you start going exponential in the wrong direction.
  • Backtests

    • Backtests are essential.
      • Only trade what has been proven to work historically.
    • Backtests should be nearly instant.
      • You should not feel like skipping backtesting because it's slow, or have to iterate slowly when developing algorithms.
    • Backtests are two step verification.
      • Only use all available data for final verification to avoid tailoring algorithms to the specific historic data.
    • Backtests fuzz.
      • By changing initial conditions (like date) it can reveal if a strategy only works when run on a specific day.
  • Day trading

    • The market might be a bit more unreliable on lower timeframes, but at the same time it allows you to work with many different timescales down to seconds.
    • Gives faster feedback on what works and what does not since trades play out over the day instead of days or weeks.
    • Allows for parallel backtesting of multiple days at the same time.
    • Avoids issues with holding positions over multiple days like fees, gap up/down, leverage rebaseing (using say 10x leverage, the underlying market goes down 5% the first day and then up 5% the next, you loose 50% the first day and then gain 50% the next means you now have 75% of the original amount, e.g. 1000 * 0.5 * 1.5 = 750), ...
    • Using max loss per day by default since some days just don't behave like you expect, better to just wait for the next one.
  • Strategies

    • No magic values in strategies, everything is configurable.
      • This allows different settings per market and changing settings over time.
      • This also allows for automatically figuring out the best config (possibly by brute force testing).
    • Many strategies can be active at the same time looking at the same or different markets. There can be many concurrent trades but it's limited to 1 by default.
    • Ideally it should be able to select strategies and settings based on the recent market conditions, or just what works best in backtests recently.
  • Testing

    • This will be tested (as in automated tests) in a way that allows for a lot of flexibility of implementation. I'm biasing this towards full system tests, e.g. given this config and this data, assert which trades where executed.
    • Unit tests will be used where it makes a lot of sense like for calculations like moving average, ATR, etc.

Various notes

  • Properties to show in result
    • Maximum drawdown
    • Exposure time by strategy
      • Adjusted result given it would be exposed 100% of the time.

Development

Test data in this project is from TradingView but you could use any data that provides timestamp/open/close/high/low and volume.

If you want to export data from TradingView you will need to have a paid account. I have confirmed with TradingView support that it is okay to include data from their exports in a public project like this.

Import data.

make import

Run backtests and show results.

make

Run tests

make test

Contributions

Contributions are very welcome, but be aware I don't always have a lot of spare time for this project so it could take some time before I can review it. The more well made, consistent with the style of the existing project, and well tested it is the easier it is to include it.

Broker integration ideas

  • The simplest possible API would be one that provides market data during the day and then notifies you to buy or sell when needed. You probably want to run such an integration on the hourly timeframe to not get too many notifications.

Unfinished broker integrations

  • Fix API
    • It is interesting since it can work with many brokers and seems to be free (?) to use on for example Skilling when using c-trader.
    • Skilling also has the benefit of very small spreads, almost no fees and very reliable TP/Stop orders (market orders will stop out almost exactly where you say except in exceptional cases where the market moves very quicky).
    • "A FIX API can be obtained via cTrader with no conditions applied such as minimum trading volume or minimum deposit size.".
  • Nordnet

Related resources

Various backtest inspiration

Various algorithm inspiration

algotrader's People

Contributors

joakimk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.