Loading ...

MACD Cpp Demo

Author: Zero, Date: 2019-03-28 15:26:08
Tags: MACD C++


/*backtest
start: 2019-02-26 09:00:00
end: 2019-03-27 15:00:00
period: 1d
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
*/

double position = 0;

bool onTick(string symbol) {
    auto ct = exchange.SetContractType(symbol);
    if (ct == false) {
        return false;
    }
    auto r = exchange.GetRecords();
    if (!r.Valid || r.size() < 20) {
        return false;
    }

    auto macd = TA.MACD(r);
    auto slow = macd[0][macd[0].size() - 2];
    auto fast = macd[1][macd[1].size() - 2];
    string action;
    if (fast >= slow && position <= 0) {
        action = "buy";
    } else if (fast <= slow && position >= 0) {
        action = "sell";
    }
    if (action.size() > 0) {
        if (position != 0) {
            ext::Trade("cover", symbol);
        }
        position = ext::Trade(action, symbol, 1);
    }
    return true;
}

void main() {
    while (true) {
        if (exchange.IO("status") == 0) {
            Sleep(1000);
            continue;
        }
        if (!onTick("MA888")) {
            Sleep(1000);
        }
    }
}


Related

More