Giter Site home page Giter Site logo

yarro-s / stratotester Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 1.06 MB

Strategy Tester is a C++ header-only library for testing trading and investing strategies.

License: MIT License

CMake 2.31% C++ 96.88% Shell 0.81%
strategy investing trading backtesting

stratotester's Introduction

StratoTester

Strategy Tester is a C++ header-only library for testing trading and investing strategies.

Quick Start

Include StratoTester.hpp as follows

#define STRATOTESTER_IMPL  // specified only once 
#include <StratoTester.hpp>

using namespace st;  // for convenience 

Here are some QQQ monthly quotes, starting from Jan 01, 2019 ending Dec 01, 2019

auto qqq_hist = st::prices {168.16, 173.19, 179.66, 189.54, 173.95, 186.74,
                            191.10, 187.47, 188.81, 197.08, 205.10, 212.61};

Let's implement a simple momentum trading rule:

auto last_close_up = [=](prices const &price_hist) {
    auto const last_close = price_hist.back();
    auto const first_close = price_hist.front();

    // buy when last close is higer than the first, hold otherwise
    return last_close > first_close ? 1.0 : 0.0;
};

A trading rule takes a rolling price history (represented internally as an std::vector<double>) and calculates weight in the range from -1.0 to 1.0. For instance, returning 0.5 means that the currently required share of the asset in the portfolio is 50% while returning -0.2 means that 20% of the portfolio is used for shorting the asset.

Next, let's create a strategy based on that rule

auto strat = strategy::with(last_close_up)
                .look_back(3)         // 3 samples rolling window
                .rebalance_every(2);  // rebalanace every 2 samples

We specified a three-sample rolling window with look_back(3). Asset rebalancing, specified as rebalance_every(2), is done every two samples. The samples here hold the monthly closing prices of QQQ for the year 2019.

Let's run this strategy with an initial deposit of $10000

auto init_deposit = 10000;
auto test = single_asset(strat, init_deposit).run(qqq_hist);

and log some of the results

std::cout << "Single asset portfolio value history: " << std::endl
          << str_rep(test.results().value_history())
          << std::endl;
std::cout << "Single asset portfolio total return: "
          << test.results().total_return() << std::endl;

After requesting the backtesting results with results(), you can access all the historical values of the portfolio with value_history() and calculate its total return with total_return().

stratotester's People

Contributors

yarro-s avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

zhnathaniellee

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.