策略示例(C++)

buySellSymbol-股票买卖策略

config.json配置文件

{
  // 用户
  "user": "SDK-User",
  // 令牌
  "token": "SDK-Token",
  // 登录地址
  "login_endpoint": "https -h prx-01.upoem1.com -p 443",
  //实例id
  "instance_id": "xxxxxxxxxxxxxxxxxxxxxxxxx",

  "initialize": {
    //"symbol_sets": [ "000300.SH" ],
    "symbols": [
      "000001.SZ"
    ],
    "bars": [
      {
        "interval": "1day",
        "count": 100,
        "match": 1
      }
    ],
    "backtest": {
        "start_date": 20181201,
        "end_date": 20181230,
        "cash": {"CS": 1000000}
    },
    "mode": "both",
    "commission": 0.0002,
  }
}

buySellSymbol.cpp策略文件

#include <iostream>
#include "StrategyApi.h"
#include "TimeUtil.h"

/*
股票策略:买卖策略
本策略是一个简单的买卖策略,主要是体现如何下单的。当没有持仓时就进行买入操作,当有持仓时,就进行卖出平仓操作
回测标的:平安银行(000001.SZ)
回测周期:20180702-20181101
撮合周期:日K
 */

using namespace std;
using namespace xQuant;

//定义策略分析器,实现相应的回调函数
class MyMulAnalyzer : public StrategyAnalyzer
{
public:
    //初始化函数
    void on_initialize(std::shared_ptr<StrategyApi> sApi) final
    {
        cout << sApi->get_version() << endl;
    }

    //开盘前准备,必须设置要关注的标的
    void on_before_market_open(std::shared_ptr<StrategyApi> sApi, const MarketParam& marketParam) final
    {
        set<string> symbols_set;
        symbols_set.insert("000001.SZ");
        //设置需要关注的标的
        sApi->set_focus_symbols(symbols_set);
    }

    void on_bar(std::shared_ptr<StrategyApi> sApi, const Bar& bar) final
    {
        cout << "========>on_bar" << endl;
        vector<SymbolPosition> symbol_positions = sApi->get_symbol_positions();

        //如果有持仓
        if (symbol_positions.size() > 0)
        {
            //平仓
            cout << "sell all" << endl;
            sApi->target_position(bar.symbol, 0,bar.close);
        }
        else
        {
            //市价下单50000股
            cout << "buy to 5000" << endl;
            sApi->target_position(bar.symbol, 50000,bar.close);
        }
    }

};

/////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
    auto app = StrategyApi::create_strategy_api();
    do
    {
        //先注册分析器
        std::unique_ptr<MyMulAnalyzer> pAnalyzer(new MyMulAnalyzer());

        app->reg_analyzer(pAnalyzer.get());

        int rc = app->init(argc, argv);
        if (rc != 0)
        {
            std::cout << "init failed|rc=" << rc << std::endl;
            break;
        }
        app->wait_for_shutdown();
        break;

    } while (0);


    return 0;
}

buySellFutures-期货买卖策略

config.json配置文件

{
  // 用户
  "user": "SDK-User",
  // 令牌
  "token": "SDK-Token",
  // 登录地址
  "login_endpoint": "https -h prx-01.upoem1.com -p 443",
  //实例id
  "instance_id": "xxxxxxxxxxxxxxxxxxxxxxxxx",

  "initialize": {
    "symbol_sets": [ "IF.PRD" ],
    "symobls": [ "IFZ0.CFE" ],
    "bars": [
      {
        "interval": "1day",
        "count": 100,
        "match": 1
      }
    ],
    "backtest": {
        "start_date": 20181201,
        "end_date": 20181230,
        "cash": {"CF": 1000000}
    },
    "mode": "both",
    "commission": 0.0002,
  }
}

buySellFutures.cpp策略文件

#include <iostream>
#include <thread>
#include <chrono>
#include "StrategyApi.h"
#include "TimeUtil.h"
#include "PrintUtil.h"

/*
期货策略:买卖策略
本策略是一个简单的期货买卖策略,主要是体现期货简单买卖
*/

using namespace std;
using namespace xQuant;

//定义策略分析器,实现相应的回调函数
class MyMulAnalyzer : public StrategyAnalyzer
{
public:
    //开盘前准备,必须设置要关注的标的
    void on_before_market_open(std::shared_ptr<StrategyApi> sApi, const MarketParam& marketParam) final
    {
        string futures_code = sApi->get_continuous_symbol("IFZ0.CFE", marketParam.trade_date);
        cout << "futures_code : " << futures_code << endl;

        set<string> symbols_set;
        symbols_set.insert(futures_code);
        symbols_set.insert("IFZ0.CFE");
        sApi->set_focus_symbols(symbols_set);
    }

    void on_handle_data(std::shared_ptr<StrategyApi> sApi, int64_t timeExch)
    {
        //获取主力合约
        string futures_code = sApi->get_continuous_symbol("IFZ0.CFE", sApi->date_now());
        auto bars = sApi->get_bars_history(futures_code, "1day", 1);
        double factor = bars->get_double_column("pre_close")[0] / bars->get_double_column("open")[0];
        cout << "=======>factor!!!" << factor << endl;
        ///获取标的的仓位,并对仓位中的股数进行判断。
        vector<SymbolPosition> symbolPositions = sApi->get_symbol_positions();
        //如果有持仓
        if (symbolPositions.size() != 0)
        {
            //cout << "持仓" << endl;
            if (symbolPositions[0].symbol == futures_code)
            {
                //cout << "主力合约不变" << endl;
                if (symbolPositions[0].pos_high / symbolPositions[0].pos_price > 1.03 || symbolPositions[0].pos_low / symbolPositions[0].pos_price < 0.95)
                {
                    sApi->target_position(symbolPositions[0].symbol, 0,symbolPositions[0].pos_high, "short");
                }
            }
            else
            {
                //cout << "主力合约发生变化" << endl;
                auto p_bars = sApi->get_bars_history(symbolPositions[0].symbol, "1day", 1);
                sApi->target_position(symbolPositions[0].symbol, 0,p_bars->get_double_column("close")[0], "short");
                sApi->target_position(futures_code, 2,p_bars->get_double_column("close")[0], "short");
            }
        }
        //如果没有持仓
        else
        {
            //cout << "没有持仓" << endl;
            if (factor < 1)
            {
                sApi->target_position(futures_code, 2, bars->get_double_column("close")[0], "short");
            }
        }
    }
};

/////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
    auto app = StrategyApi::create_strategy_api();
    do
    {
        //先注册分析器
        std::unique_ptr<MyMulAnalyzer> pAnalyzer(new MyMulAnalyzer());

        app->reg_analyzer(pAnalyzer.get());

        int rc = app->init(argc, argv);
        if (rc != 0)
        {
            std::cout << "init failed|rc=" << rc << std::endl;
            break;
        }
        app->wait_for_shutdown();
        break;

    } while (0);

    return 0;
}

results matching ""

    No results matching ""