A function that corrects the "ActionDay"fields from market quotes which is delivered by CTP.

This function is written by myself. It’s purpose is to correct the ActionDay field in the quotes structure delivered by CTP marketdata api.
It belongs to one of my personal projects which may go opensource in the future.
In my work, I find it usefull to put this function into my company’s project.
SO, I extract this function and some related functions, put it here and claim the MIT license for the following lines to avoid copyright issue in the future.
//sepcial functions that used in ctp quotes.
//use the revised date to indicate which file the quote belongs to.
// return a revised tdfdate(actionday)
// on error:
// return -1 if tdftime_action is in the market close time. 
// return 0 if tdftime_action is not valid for the current time(overdue).
inline int fix_actionday(string &exchange, int tdfdate_action, int tdftime_action, int mimic_clock = -1)
{
    using namespace boost::posix_time;
    using namespace boost::gregorian;

    ptime now = microsec_clock::local_time();
    date today = now.date();
    auto tod = now.time_of_day();
    int msec_actiontime = tdftime_to_msec(tdftime_action);
    int msec_10min_period= 1000 * 60 * 10;
    int msec_current_time = tod.total_milliseconds();
    if (mimic_clock >= 0)
        msec_current_time = tdftime_to_msec(mimic_clock);

    // try to get close and settelment price for CF
    // 16 - 17 , for CF
    if ((msec_current_time > tdftime_to_msec(160000000))
        && (msec_current_time < tdftime_to_msec(170000000))
        && tdfdate_action == get_local_tdfdate())
        return tdfdate_action;

    //20 - 21   // for DCE SHFE CZFE
    if ((msec_current_time > tdftime_to_msec(200000000))
        && (msec_current_time < tdftime_to_msec(210000000))
        && tdfdate_action == get_local_tdfdate())
        return tdfdate_action;



    //drop all the playback garbage.
    // 18 - 21 , no need to be processed
    if (msec_current_time > tdftime_to_msec(180000000) && msec_current_time < tdftime_to_msec(205000000))
        return -1;
    // 7 - 9 , no need to be processed
    if (msec_current_time > tdftime_to_msec(70000000) && msec_current_time < tdftime_to_msec(85000000))
        return -1;



    //if (machine time >= 0:00 and machine time < 0:10) and quotes time > 23:50, use machine date -1
    //if (machine time > 23:50 and machine time <= 23:59:59)  and (quotes time > 0:00 and quotes time < 0:10), use machine date + 1
    //otherwise use machine date
    //night market
    if ((msec_current_time <= msec_10min_period) &&
        msec_actiontime >= tdftime_to_msec( 235000000 ) )
    {
        //today - 1
        date yesterday = today - days(1);
        return yesterday.year() * 10000 + yesterday.month() * 100 + yesterday.day();
    }
    else if ((msec_current_time >= tdftime_to_msec(235000000)) &&
        (msec_actiontime <= msec_10min_period))
    {
        date tommorrow = today + days(1);
        return tommorrow.year() * 10000 + tommorrow.month() * 100 + tommorrow.day();
    }

    //ctp will always deliver the last few previous-day-snapshots before the market opening.
    //this behavior will mess up the recording. 
    //we shall discard the expired quotes..
    //in a normal way, tdftime should be always early than the machine_time.
    //give it 10minute margin in case of inaccurate local machine time or latency.
    if ((msec_actiontime - msec_current_time) > msec_10min_period ||
            (msec_actiontime - msec_current_time) < -msec_10min_period)
        return 0;
    return today.year() * 10000 + today.month() * 100 + today.day();
}





void break_splited_cstime(char* cstime, int* _HH, int* _mm, int* _ss)
{
    *_HH = atoi(&cstime[0]);
    *_mm = atoi(&cstime[3]);
    *_ss = atoi(&cstime[6]);
}

void break_cstime(char* cstime, int* _HH, int* _mm, int* _ss)
{
    int HHmmss = atoi(cstime);
    *_HH = HHmmss / 10000;
    *_mm = HHmmss % 10000 / 100;
    *_ss = HHmmss % 100;
}


int csdate_to_tdfdate(char* csdate)
{
    int yyymmdd = atoi(csdate);
    return yyymmdd;
}


int cstime_to_tdftime(char* cstime)
{
    int HH = 0;
    int mm = 0;
    int ss = 0;
    if (strlen(cstime) <= 6)
    {
        break_cstime(cstime, &HH, &mm, &ss);
    }
    else
    {
        break_splited_cstime(cstime, &HH, &mm, &ss);
    }
    return (HH * 10000 + mm * 100 + ss) * 1000;
}

좋은 웹페이지 즐겨찾기