CZCE Futures Contract Symbols

All exchanges are listing contracts with format as: Code+YYMM, e.g. RB2110, while CZCE has different format as Code+YMM, e.g. MA109.

Sina Finance provides standard data API to market data, so that CZCE follows the same convention, such as MA2109. We need to have a conversion logic for CZCE.

There’s couple of tricks on python codes:
1. regular expression
PythonGo has format as CODEYMM, where CODE means contract codes, YMM means 3 digits to represent years. Hence, “(\w+)(\d{3})” will be good to split the symbols.
2. integer to string
Apparently, python doesn’t have autoboxing, so converting int to string requires to use “str()”. 
3. epoch time
“datetime.timestamp()” will return epoch time; it’s optional to convert timestamp to UTC.
4. convert from epoch to datetime
rebuild=datetime.datetime.fromtimestamp(now.timestamp())

        #strategy vtSymbol for CZCE has 3 digit month, while sina has 4 digits.
        #scenario as following: 
        #PythonGo requires code : CZ109
        #SinaFinance has code   : CZ2109
        if exchange=='CZCE':
            tokens=re.split("(\w+)(\d{3})",symbol)
            symbol_code=tokens[1]
            symbol_month=str(int((datetime.datetime.now().year-2000)/10))+tokens[2]
            symbol="{}{}".format(symbol_code,symbol_month)

        bartype = "60" #bartype=60 means 1h.
        timestamp=datetime.datetime.now().timestamp()
        url_template="https://.../futures/api/var%20_{symbol}_{type}_{timestamp}=/InnerFuturesNewService.getFewMinLine?symbol={symbol}&type={type}"
        url=url_template.format(symbol=symbol,type=bartype,timestamp=timestamp)

        

Reference
1. Integer To String
https://pythonexamples.org/python-convert-int-to-string/
2. Timestamp
https://www.delftstack.com/howto/python/python-convert-epoch-to-datetime/