Backtester for C++

Discussion in 'App Development' started by thefairarbiter, Jul 29, 2020.

  1. Does anybody have a C++ project for backtesting trading strategies and is looking for collaborators?

    Is anybody looking for a C++ backtesting project to collaborate with?
     
  2. 931

    931

    How much is done and what components do you have missing in your codebase and would be interested in collaberating on?
     
  3. First the status of my own work.

    The backtester as a whole can run a strategy using stock and option data, use indicators, and record unrealized and realized PnL using a non-margin or margin account. The backtester is implemented as a pipeline, listed below.
    Data -> Stock or option data. Reads csv of a specific format. Creates a global time map made available to the strategy that maps a real timepoint to every data vector with a price point at that time.
    Strategy -> Current implementation is a time-driven decision system that has access to all stages of the pipeline. Supports multiple contract data vectors (where contract represents a stock or option), open and pending orders in the broker, existing positions within the account, and outputs a contract,order pair to the broker for trade execution.
    Broker -> Simulates trades on the open market. Current functionality supports holding open orders, but considers the same timepoint that the strategy saw when executing trades (even though real Brokers consider timepoints a few steps ahead because of the inherent delay in data retrieval). Simulates trade commission, taxes and fees for multiple security types. Interfaces with the account for available funds.
    Account -> holds position objects (contract, orders, executions). Accepts executed trades from the broker.
    Brain -> Facilitates pipeline of the above four stages.

    Currently, no documentation is available.

    Second, missing components:
    1.) A few known bugs in the Broker, Account and Position implementations
    2.) graphing capability for visual back-testing representation (prefer to keep this within C++, GNUplot is an option)
    3.) Implement new indicators (currently simple moving average is the only working one)
    4.) unit tests for development verification
    5.) Collaborators/users for input and ideas to improve the functionality and robustness of the code, develop new strategies and share perspectives on current global markets (a robust group of open-source developers)

    The pipe stages listed above are the base classes of a trading bot that uses Interactive Brokers C++ api to trade on live global markets. Because of this, they use a few implementations from the IB C++ API (contract, order, execution). Their paper trading account is especially useful for strategy development and that repository is coming soon.

    Currently both repositories are private.
     
  4. traider

    traider

    Any reason for using c++?
    Do you have any working strategies? You should work on platform last and just use one of the many open source ones if you dont already have gd strategies.
     
    thefairarbiter likes this.
  5. 1.) familiarity
    2.) IB API support compatible with my needs

    In short, no.

    Though I need more information about what you mean by working. If you mean to ask if I have a strategy that works all the time on any trading day in any market condition, the answer is absolutely not. If you mean to ask if I have a strategy working with the backtester that produces meaningful results, the answer is yes (its a simple SMA momentum strategy that's supposed to test the platform thoroughly, it loses money). But with certain conditions, the answer will be wrong.

    I decided to build my own implementation of a backtester after using BackTrader in python. It was a fine platform, but I wanted something with more functionality and configurability, and I wanted to develop with C++ (I spent a week trying to get the IB python api to work with me, but I couldn't get it to do what I wanted so I gave up). After a fruitless search for an open source usable C++ backtester I started on my own (the ones I found were proprietary, targeted at fund managers). I could also use the programming experience.

    In short, I think BackTrader was missing a few things:
    1.) seamless integration with a live trading API (python only integrates with python. Shared objects and binaries work with just about any platform)
    2.) pairing data vectors together (ex. derivative and underlying)
    3.) contracts that aren't just stocks
    4.) data that isn't just candles
    5.) support for many data vectors at once
    6.) indicators on multiple dimensions (indicator for a single vector, indicator for many vectors)
    Essentially, I think a backtester should be able to be implemented as a trading bot, but with the API calls and callbacks integrated into it. This is easily done with class hierarchy in C++, and this is how I've built the current implementation.
     
  6. 931

    931

    Can you name few open C++ projects?
    Would be interesting to see how others approach.
     
  7. 931

    931

    Been on similar path.
    Id guess you are <1year in ,from the info provided.


    I used Qt to build all from ground up.
    And much of code is interconnected and not following object oriented guidlines where beneficial.
    Not many modular components and most would be hard to root out+also no documentation.

    Not easy path but in the end you'll have maximal flexibility.
    +Reinventing the wheel is going to give lot more insight and opportunity to develop your own tools that propietary platforms cant support.

    But have you thought about that if your breakthrough's are not going to happen in few years?

    Will you just keep going or give up soon?
     
    Last edited: Jul 29, 2020
  8. traider

    traider

    I think you might underestimate how much work is needed for a full backtester especially when you do not know what type of strategies you are going to implement.
    Check out lean from quantconnect. Take a look at just the portion where they download data , adjust for dividends etc. Its a crazy amount of work unless you have a team.
    If you knew exactly what type of strategies you are pursuing then you can cut down the work a lot and might be ok with rolling your own but I still dont think its necessary.

    IB API supports python now
     
    trader99 likes this.
  9. I checked out Lean. Super awesome project! I'd heard of quantconnect before starting on building a backtester. I didn't really look into them because at the time, I didn't want to learn C# and I thought that developing a C++ backtester like BackTrader would only take like one summer. The codebase they've developed looks massive, and its probably supposed to support literally any kind of strategy. That's not what I'm going for. The strategies my backtester is supposed to support have a lot to do with statistical signal processing and discrete events (hence the time map). This system has been especially useful when running intra-day strategies on lots of data lines from IB. As you could probably guess, that SP has a lot of FFT and linear algebra in it, which plenty of C++ apis will easily integrate into.

    Accurate. Implementing a backtester like Lean is simply untractable for a single shmuck like myself. But I don't want the kind of generality that they're going for. My backtester will have enough stuff to support data-driven strategies for momentum, volatility, and others. The detailed stuff like reading data will come as it needs to.

    If enough people care about a C++ backtester and I'm able to recruit interested parties, then one day a C++ backtester like theirs could be possible.

    A valuable perspective to consider. I'm open to any kind of strategy that works, but according to my reading, volatility bets and momentum strategies can work well with sufficient signal processing. I'm definitely going to see what quantconnect/lean has to offer and learn some C# in the near future though. They seem to support a lot of broker APIs, and I like the idea of being to move between brokers if the current one isn't being cooperative.

    Just to confirm @traider, you're not interested in developing a C++ backtester?
     
  10. Yes. I started seriously developing the backtester in March.

    Perhaps we've been trying to invent something new. I think there's a need for modern C++ in the open-source quantitative finance world. Such a useful and universal language ought to be brought into the picture. Many of the world's best APIs are in the C++ and they undoubtedly will help anybody who uses them. Some will probably argue that C# and java are sufficient, but not all of use are backend web developers!

    Are you interested in sharing code @931?
    Yes. The current state of the backtester already supports live trading, but with bugs like I said. I'm not giving up soon. It probably requires about 50 more hours of development effort to get all the features I listed earlier. More features will come to the backtester as strategy development demands, though I suspect that the way the backtester will grow will not be through actually building more backtester, it will be through building more indicators and more complicated decision systems, which is orthogonal to the actual backtester.

    @931 are you interested in sharing code? I have a lot of cleaning to do, but I plan on making the backtester public in about 1 week.

    There is a C++ algorithmic trading project on github @931 @traider , checkout Raymond Burkholder's trade-frame. It's C++, but certainly not modern C++. He uses DTN IQ and I don't need that level of data (it's also like $150/m, too expensive for me at this point, whereas IB gives me all the data I need for $14.50/m). He also uses Wine because DTN IQ is windows only. I don't dig it.
     
    #10     Jul 29, 2020