Pages - Menu

標籤

AWS (1) bash (1) Boost (2) C (2) CMake (2) Concurrency_Programming (3) CPP (37) Database (2) DNS (1) Docker (4) Docker-Compose (1) ELK (1) emacs (4) gcp (1) gdrive (1) git (1) gitbash (2) gitlab (1) kvm (4) Linux (5) MT4 (4) MT5 (4) Multicast (2) MySQL (2) Nijatrader8 (1) OpenCV (1) Python (4) QT5 (1) R (1) rdp (3) screenshot (1) ssh (3) Tabnine (1) TCP (1) TensorFlow (1) Tools (12) Ubuntu_1904 (11) Ubuntu_20_04 (5) UDP (1) VS2010 (1) VS2015 (1) VS2019 (1) WebServer (1) Win10 (1) winmerge (1) WSL (1) xrdp (1)

搜尋此網誌

2020年4月30日星期四

A Clock for trading strategy backtest for Linux

Aim

One may want to simulate a clock during a trading strategy backtest. With this class, one may select if this is a debug period, enable and disable to tune the clock. This class support multi-threading. This class on linux.

Source



#include <iostream>

#include "NLClock.h"

void StartStopDebug()
{
    std::cout << __FUNCTION__ << "-------------" << std::endl;
    std::cout << TSCLOCK_IS_DEBUG() << std::endl;
    TSCLOCK_SET_DEBUG(true);
    std::cout << TSCLOCK_IS_DEBUG() << std::endl;
    TSCLOCK_SET_DEBUG(false);
    std::cout << TSCLOCK_IS_DEBUG() << std::endl;
}
void SetClock()
{
    std::cout << std::endl;
    std::cout << __FUNCTION__ << "-------------" << std::endl;
    TSCLOCK_SET_CLOCK_DATE(2020, 3, 20, 1, 20, 34);
    std::cout << TSCLOCK_GET_TIME_STRING() << std::endl;
}
void AddSecExample()
{
    std::cout << std::endl;
    std::cout << __FUNCTION__ << "-------------" << std::endl;
    TSCLOCK_SET_CLOCK_DATE(2020, 3, 20, 1, 20, 34);
    std::cout << "Set clock: " << TSCLOCK_GET_TIME_STRING() << std::endl;
    TSCLOCK_ADD_SEC(1); // fail to add sec since currently is not be able to tune
    std::cout << "Fail example: " << TSCLOCK_GET_TIME_STRING() << std::endl;

    TSCLOCK_SET_ENABLE_TO_TUNE(true);
    TSCLOCK_ADD_SEC(1); // able to tune
    std::cout << "Success example: " << TSCLOCK_GET_TIME_STRING() << std::endl;
}
void AddSecExampleAdvance()
{
    std::cout << std::endl;
    std::cout << __FUNCTION__ << "-------------" << std::endl;
    TSCLOCK_SET_CLOCK_DATE(2020, 3, 20, 1, 20, 34);
    std::cout << "Set clock: " << TSCLOCK_GET_TIME_STRING() << std::endl;
    // we may have different threads to share the same clock while backtesting a trading strategy
    // we may only tune the clock time when all the threads are ready
    // we firest set the clock untunable by counting the number
    // following is an example to show 3 threads ask the clock to wait
    TSCLOCK_SET_ENABLE_TO_TUNE(false);
    TSCLOCK_SET_ENABLE_TO_TUNE(false);
    TSCLOCK_SET_ENABLE_TO_TUNE(false);

    // we need 3 enable before we can tune the clock
    TSCLOCK_SET_ENABLE_TO_TUNE(true);
    TSCLOCK_ADD_SEC(1); // fail to add
    std::cout << "Fail example: " << TSCLOCK_GET_TIME_STRING() << std::endl;

    TSCLOCK_SET_ENABLE_TO_TUNE(true);
    TSCLOCK_SET_ENABLE_TO_TUNE(true);
    TSCLOCK_ADD_SEC(1); // able to add
    std::cout << "Success example: " << TSCLOCK_GET_TIME_STRING() << std::endl;
}
// usage: ./NLClock
int main(int argc, char *argv[])
{
    StartStopDebug();
    SetClock();
    AddSecExample();
    AddSecExampleAdvance();

    return 0;
}

Output

$ ./src/NLClock 
StartStopDebug-------------
0
1
0

SetClock-------------
2020-03-20 01:20:34

AddSecExample-------------
Set clock: 2020-03-20 01:20:34
Fail example: 2020-03-20 01:20:34
Success example: 2020-03-20 01:20:35

AddSecExampleAdvance-------------
Set clock: 2020-03-20 01:20:34
Fail example: 2020-03-20 01:20:34
Success example: 2020-03-20 01:20:35

沒有留言:

發佈留言