#pragma warning(suppress : 4996)
#include <string>
#include <vector>
#include <numeric>
#include <iostream>
#include "strategy.h"
#include <unordered_map>
using namespace std;
struct tick_data {
double UTCTime;
char Symbol[20];
double TradePrice;
int Volume;
int Position;
};
struct get_tickdata {
struct tick_data data[300000];
int tick_num;
};
struct xbar {
double MinuPrice;
int MinuVolume;
int MinuPosition;
};
struct get_xbardata {
struct xbar bar[30000];
int xbar_num;
};
std::unordered_map<std::string, long> TickTime;
std::unordered_map<std::string, vector<int>> Volume;
std::unordered_map<std::string, vector<int>> Posistion;
std::unordered_map<std::string, struct get_tickdata*> MyTick;
std::unordered_map<std::string, struct get_xbardata*> MyXbax;
int i = 0, j = 0;
class MyStrategy :public Strategy
{
public:
MyStrategy() {}
~MyStrategy(){}
void on_init(){
cout << "on_init" << endl;
subscribe("SHFE.rb2205,SHFE.fu2205", "tick");
return;
}
void on_tick(Tick* tick){
long TradTime = floor(long(tick->created_at) / 60);
auto ftick = MyTick.find(tick->symbol);
if (ftick == MyTick.end()) {
struct get_tickdata* its = new get_tickdata;
its->tick_num = 0;
MyTick[tick->symbol] = its;
}
auto get_tick = MyTick.find(tick->symbol);
tick_data* tb = get_tick->second->data;
int& i = get_tick->second->tick_num;
int idx = get_tick->second->tick_num;
tb[i].UTCTime = tick->created_at;
strcpy_s(tb[i].Symbol, tick->symbol);
tb[i].TradePrice = tick->price;
tb[i].Volume = tick->last_volume;
tb[i].Position = tick->cum_position;
auto fxbar = MyXbax.find(tick->symbol);
if (fxbar == MyXbax.end()) {
struct get_xbardata* its = new get_xbardata;
its->xbar_num = 0;
MyXbax[tick->symbol] = its;
}
auto get_xbar = MyXbax.find(tick->symbol);
xbar* td = get_xbar->second->bar;
int& j = get_xbar->second->xbar_num;
int jdx = get_xbar->second->xbar_num;
if (i == 0) {
Volume[tick->symbol].push_back(tb[i].Volume);
Posistion[tick->symbol].push_back(0);
}
else {
Volume[tick->symbol].push_back(tb[i].Volume);
Posistion[tick->symbol].push_back(tb[i].Position - tb[i - 1].Position);
}
if (TickTime[tick->symbol] != TradTime) {
td[j].MinuPrice = tick->price;
td[j].MinuVolume = accumulate(Volume[tick->symbol].begin(), Volume[tick->symbol].end(), 0);
td[j].MinuPosition = accumulate(Posistion[tick->symbol].begin(), Posistion[tick->symbol].end(), 0);
Volume.erase(tick->symbol);
Posistion.erase(tick->symbol);
j++;
}
TickTime[tick->symbol] = TradTime;
if (j != jdx) {
//cout << TradTime<<" "<<tick->symbol << " " << td[jdx].MinuPrice << " " << td[jdx].MinuVolume <<" "<< td[jdx].MinuPosition << endl;
//信号处理&下单模块
}
i++;
}
private:
};
int main(int argc, char *argv[])
{
MyStrategy s;
s.set_strategy_id("a95d0ab6-6c5a-11ec-88c4-5c80b6db920b");//策略id
s.set_token("1edacf253f599e7eb0b1ab3c58c3c5ac95142ed9");//账号id
s.set_mode(MODE_BACKTEST);
s.set_backtest_config("2021-11-30 20:50:00", "2022-01-05 15:00:00",1000000, 1, 0, 0, 0, 1);
s.run();
return 0;
}
交易日期:
2021-12-01|2021-11-30 20:50:00|2021-12-01 15:00:00
2021-12-02|2021-12-01 20:50:00|2021-12-02 15:00:00
2021-12-03|2021-12-02 20:50:00|2021-12-03 15:00:00
2021-12-06|2021-12-03 20:50:00|2021-12-06 15:00:00
2021-12-07|2021-12-06 20:50:00|2021-12-07 15:00:00
2021-12-08|2021-12-07 20:50:00|2021-12-08 15:00:00
2021-12-09|2021-12-08 20:50:00|2021-12-09 15:00:00
2021-12-10|2021-12-09 20:50:00|2021-12-10 15:00:00
2021-12-13|2021-12-10 20:50:00|2021-12-13 15:00:00
2021-12-14|2021-12-13 20:50:00|2021-12-14 15:00:00
2021-12-15|2021-12-14 20:50:00|2021-12-15 15:00:00
2021-12-16|2021-12-15 20:50:00|2021-12-16 15:00:00
2021-12-17|2021-12-16 20:50:00|2021-12-17 15:00:00
2021-12-20|2021-12-17 20:50:00|2021-12-20 15:00:00
2021-12-21|2021-12-20 20:50:00|2021-12-21 15:00:00
2021-12-22|2021-12-21 20:50:00|2021-12-22 15:00:00
2021-12-23|2021-12-22 20:50:00|2021-12-23 15:00:00
2021-12-24|2021-12-22 20:50:00|2021-12-24 15:00:00
2021-12-27|2021-12-24 20:50:00|2021-12-27 15:00:00
2021-12-28|2021-12-27 20:50:00|2021-12-28 15:00:00
2021-12-29|2021-12-28 20:50:00|2021-12-29 15:00:00
2021-12-30|2021-12-29 20:50:00|2021-12-30 15:00:00
2021-12-31|2021-12-30 20:50:00|2021-12-31 15:00:00
2022-01-04|2022-01-04 08:50:00|2022-01-04 15:00:00
2022-01-05|2022-01-04 20:50:00|2022-01-05 15:00:00
说明:1、第一列为交易日期,第二和第三列为业务日期。
2、第二列为夜盘交易,第三列为日盘交易,夜盘+日盘作为一个交易日期。
3、如果需要用到策略id和账号id可到掘金官网下载:https://www.myquant.cn/
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。
因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。