コード例 #1
0
ファイル: ceshiwanshua.py プロジェクト: cliicy/okexcode
 def __init__(self):
     self.client = FcoinClient()
     self.fcoin = Fcoin()
     self.fcoin.auth(config.key, config.secret)
     self._sender = MqSender('fcoin', 'kline')
     self.sym = ''
     self.wdata = {}
     self._init_log()
コード例 #2
0
ファイル: trade.py プロジェクト: cliicy/vFcoinWSS
 def __init__(self):
     self.platform = Platform.PLATFORM_FCOIN.value
     self.data_type = PlatformDataType.PLATFORM_DATA_TICKER.value
     BaseSync(self.platform, self.data_type)
     self.client = FcoinClient()
     self._init_log()
     self._sender = MqSender('3', 'trade')
     self.wdata = {}
コード例 #3
0
 def __init__(self):
     self.platform = Platform.PLATFORM_FCOIN.value
     self.data_type = PlatformDataType.PLATFORM_DATA_KLINE.value
     self.interval = 'H1'
     BaseSync(self.platform, self.data_type, self.interval)
     self.client = FcoinClient()
     self._init_log()
     self._sender = MqSender('3', 'kline')
     # self._sender = MqSender('fcoin', 'kline')
     self.wdata = {}
コード例 #4
0
ファイル: ticker.py プロジェクト: cliicy/vFcoinWSS
 def __init__(self):
     self.platform = Platform.PLATFORM_FCOIN.value
     self.data_type = PlatformDataType.PLATFORM_DATA_TICKER.value
     BaseSync(self.platform, self.data_type)
     self.client = FcoinClient()
     self._init_log()
     try:
         self._sender = MqSender('3', 'ticker')
     except Exception as e:
         print('ohhh== ERROR= ', e)
         self._sender = None
     self.wdata = {}
コード例 #5
0
class H1CandleApp(BaseSync):
    """
    """
    def __init__(self):
        self.platform = Platform.PLATFORM_FCOIN.value
        self.data_type = PlatformDataType.PLATFORM_DATA_KLINE.value
        self.interval = 'H1'
        BaseSync(self.platform, self.data_type, self.interval)
        self.client = FcoinClient()
        self._init_log()
        self._sender = MqSender('3', 'kline')
        # self._sender = MqSender('fcoin', 'kline')
        self.wdata = {}

    def candle(self, data):
        # print('数据:', data)
        name, ml, osym = self.client.channel_config[0].split('.')
        sym = Symbol.convert_to_standard_symbol(Platform.PLATFORM_FCOIN, osym)
        ts = int(round(data['id'] * 1000))  # self.client.get_ts()
        # 从服务器得到的数据中没有ts,只有id,根据文档要求,要把获取到数据的时间存入csv文件及数据库中
        ticks = ts  # int(round(time.time() * 1000))
        data['id'] = ts
        # print("当前时间戳为:", ticks)
        # send to mq
        # if self._sender is not None:
        #     try:
        #         mqdata = {}
        #         tdata = {'symbol': sym, 'ts': ticks, 'tm_intv': m_interval, 'exchange': config.exchange}
        #         mqdata.update(tdata)
        #         mqdata.update(data)
        #         # print(mqdata)
        #         self._sender.send(str(mqdata))
        #     except Exception as error:
        #         print(error)
        #         self._sender.close()
        # else:
        #     # print('fail to connect rabbitmq server')
        #     pass
        # send to mq

        # print('symbol: ', sym)
        # create the no-exist folder to save date
        stime = time.strftime('%Y%m%d', time.localtime())
        skldir = os.path.join(sDir, stime, config.exchange, config.klinedir)
        if not os.path.exists(skldir):
            os.makedirs(skldir)
        # for original data
        # sTfile = '{0}_{1}_{2}{3}'.format(config.klinedir, stime, osym, '.txt')
        # sTfilepath = os.path.join(skldir, sTfile)
        # # write original data to txt files
        # with open(sTfilepath, 'a+', encoding='utf-8') as tf:
        #     tf.writelines(json.dumps(data) + '\n')
        # for no-duplicated csv data
        sfile = '{0}_{1}_{2}_{3}{4}'.format(config.klinedir, stime, ml, osym,
                                            '.csv')
        sfilepath = os.path.join(skldir, sfile)
        if self.wdata:
            if ts in self.wdata.values():
                # self.wdata['ts'] = ts
                pass
            else:
                self.wdata['ts'] = ts
                self.wdata['wlen'] = 0
            # write the current data sent from server to the csv but the position will be changed
        else:
            self.wdata['ts'] = ts
            self.wdata['wlen'] = 0
        self.w2csv(sfilepath, ticks, sym, data)  # 是要把ticks写入csv,而不是和id值一致的ts

    def w2csv(self, sfilepath, ts, sym, data):
        sflag = 'close'
        rFind = False
        kklist = []
        vvlist = []
        # will delete the data from the end if the ts is the same to the previous data
        iseekpos = self.wdata['wlen']
        # print('iseekpos= '+'{0}'.format(iseekpos))
        if iseekpos > 0:
            # print('will call deleteFromMmap')
            self.delline(sfilepath, iseekpos, 0, True)
        # will delete the data from the end if the ts is the same to the one of the previous data
        else:
            pass
        if os.path.exists(sfilepath):
            with open(sfilepath, 'r', encoding='utf-8') as f:
                first_line = f.readline()  # 取第一行
                rFind = sflag in first_line
        with open(sfilepath, 'a+', encoding='utf-8', newline='') as f:
            w = csv.writer(f)
            if rFind is True:
                # vlist = list(data.values())
                self.additem2list(ts, vvlist, sym, self.interval, data)
                w.writerow(vvlist)
            else:  # khead = ['symbol', 'ts', 'tm_intv', 'id', 'open', 'close', 'low', 'high', 'amount', 'vol', 'count']
                klist = list(data.keys())
                # open,close,high,quote_vol,id,count,low,seq,base_vol
                kklist.insert(0, 'symbol')
                kklist.insert(1, 'ts')
                kklist.insert(2, 'tm_intv')
                kklist.insert(3, klist[4])
                kklist.insert(4, klist[0])
                kklist.insert(5, klist[1])
                kklist.insert(6, klist[6])
                kklist.insert(7, klist[2])
                kklist.insert(8, 'amount')
                kklist.insert(9, 'vol')
                kklist.insert(10, klist[5])
                w.writerow(kklist)
                self.additem2list(ts, vvlist, sym, self.interval, data)
                w.writerow(vvlist)

        # update the lenth of data wroten to csv
        prelen = len('{0},{1},{2}'.format(sym, ts, self.interval))
        # print('prelen= ' + '{0}'.format(prelen))
        for i in dkey:
            ss = '{0}{1}'.format(',', data[i])
            prelen += len(ss)
        prelen += len(
            '\t\n')  # because there is a extra '\t\n' which is equal 2 bytes
        # print('w2csv prelen= ' + '{0}'.format(prelen))
        self.wdata['wlen'] = prelen
        # print('w2csv after prelen= ' + '{0}'.format(self.wdata['wlen']))
        # update the lenth of data wroten to csv

    def sync_data(self, *args):
        self.client.stream.stream_klines.subscribe(self.candle)
        self.client.subscribe_candle(args[1], args[0])
コード例 #6
0
ファイル: trade.py プロジェクト: cliicy/vFcoinWSS
class TradeApp(BaseSync):
    """
    """
    def __init__(self):
        self.platform = Platform.PLATFORM_FCOIN.value
        self.data_type = PlatformDataType.PLATFORM_DATA_TICKER.value
        BaseSync(self.platform, self.data_type)
        self.client = FcoinClient()
        self._init_log()
        self._sender = MqSender('3', 'trade')
        self.wdata = {}

    def w2csv(self, sfilepath, sym, data):
        # will delete the data from the end if the ts is the same to the previous data
        iseekpos = self.wdata['wlen']
        # print('iseekpos= '+'{0}'.format(iseekpos))
        if iseekpos > 0:
            # print('will call deleteFromMmap')
            self.delline(sfilepath, iseekpos, 0, True)
        # will delete the data from the end if the ts is the same to the one of the previous data
        else:
            pass

        sflag = 'price'
        rFind = False
        kklist = []
        vvlist = []
        if os.path.exists(sfilepath):
            with open(sfilepath, 'r', encoding='utf-8') as f:
                first_line = f.readline()  # 取第一行
                rFind = sflag in first_line
        with open(sfilepath, 'a+', encoding='utf-8', newline='') as f:
            w = csv.writer(f)
            if rFind is True:
                vvlist.insert(0, sym)
                vvlist.insert(1, data["id"])
                vvlist.insert(2, data["ts"])
                vvlist.insert(3, data["side"])
                vvlist.insert(4, data["amount"])
                vvlist.insert(5, data["price"])
                w.writerow(vvlist)
            else:
                klist = list(data.keys())
                kklist.insert(0, 'symbol')
                kklist.insert(1, klist[2])
                kklist.insert(2, klist[1])
                kklist.insert(3, 'direction')
                kklist.insert(4, klist[0])
                kklist.insert(5, klist[4])
                w.writerow(kklist)
                vlist = list(data.values())
                vvlist.insert(0, sym)
                vvlist.insert(1, vlist[2])
                vvlist.insert(2, vlist[1])
                vvlist.insert(3, vlist[3])
                vvlist.insert(4, vlist[0])
                vvlist.insert(5, vlist[4])
                w.writerow(vvlist)
        # update the lenth of data wroten to csv
        prelen = -1  # 多了一个逗号 所以从-1开始
        for item in vvlist:
            ss = '{0}{1}'.format(',', item)
            prelen += len(ss)
        prelen += len(
            '\t\n')  # because there is a extra '\t\n' which is equal 2 bytes
        # print('w2csv prelen= ' + '{0}'.format(prelen))
        self.wdata['wlen'] = prelen
        # print('w2csv after prelen= ' + '{0}'.format(self.wdata['wlen']))
        # update the lenth of data wroten to csv

    def trade(self, data):
        # print('数据:',data)
        name, osym = self.client.channel_config[0].split('.')
        sym = Symbol.convert_to_standard_symbol(Platform.PLATFORM_FCOIN, osym)
        # send to mq
        if self._sender is not None:
            try:
                mqdata = {}
                tdata = {'symbol': sym, 'exchange': config.exchange}
                mqdata.update(tdata)
                mqdata.update(data)
                # print(mqdata)
                self._sender.send(str(mqdata))
            except Exception as error:
                print(error)
                self._sender.close()
        else:
            # print('fail to connect rabbitmq server')
            pass
        # send to mq
        # print('symbol: ', sym)
        # create the no-exist folder to save date
        stime = time.strftime('%Y%m%d', time.localtime())
        stradeDir = os.path.join(sDir, stime, config.exchange,
                                 config.tradertdir)
        # # for no-duplicated csv data
        if not os.path.exists(stradeDir):
            os.makedirs(stradeDir)
        ts = data['ts']
        # for original data
        sTfile = '{0}_{1}_{2}{3}'.format(config.tradertdir, stime, osym,
                                         '.txt')
        sTfilepath = os.path.join(stradeDir, sTfile)
        # write original data to txt files
        with open(sTfilepath, 'a+', encoding='utf-8') as tf:
            tf.writelines(json.dumps(data) + '\n')

        # for no-duplicated csv data
        sfile = '{0}_{1}_{2}{3}'.format(config.tradertdir, stime, osym, '.csv')
        sfilepath = os.path.join(stradeDir, sfile)
        if self.wdata:
            if ts in self.wdata.values():
                # self.wdata['ts'] = ts
                pass
            else:
                self.wdata['ts'] = ts
                self.wdata['wlen'] = 0
            # write the current data sent from server to the csv but the position will be changed
        else:
            self.wdata['ts'] = ts
            self.wdata['wlen'] = 0
        self.w2csv(sfilepath, sym, data)

    # sync_trades
    def sync_data(self, *args):
        self.client.stream.stream_marketTrades.subscribe(self.trade)
        self.client.subscribe_trade(*args)
コード例 #7
0
class DepthApp(BaseSync):
    """
    """
    def __init__(self):

        self.platform = Platform.PLATFORM_FCOIN.value
        self.data_type = PlatformDataType.PLATFORM_DATA_DEPTH.value
        BaseSync(self.platform, self.data_type)
        self.client = FcoinClient()
        self._init_log()
        self._sender = MqSender('3', 'depth')
        self.wdata = {}

    def depth(self, data):
        # print(data)
        name, level, osym = self.client.channel_config[0].split('.')
        sym = Symbol.convert_to_standard_symbol(Platform.PLATFORM_FCOIN, osym)
        # send to mq
        if self._sender is not None:
            try:
                mqdata = {}
                tdata = {
                    'symbol': sym,
                    'level': level,
                    'exchange': config.exchange
                }
                mqdata.update(tdata)
                mqdata.update(data)
                # print(mqdata)
                self._sender.send(str(mqdata))
            except Exception as error:
                print(error)
                self._sender.close()
        else:
            # print('fail to connect rabbitmq server')
            pass
        # send to mq
        # print('symbol: ', sym)
        # create the no-exist folder to save date
        stime = time.strftime('%Y%m%d', time.localtime())
        ts = data['ts']
        depthDir = os.path.join(sDir, stime, config.exchange, config.depthdir)
        if not os.path.exists(depthDir):
            os.makedirs(depthDir)

        # for original data
        sTfile = '{0}_{1}_{2}{3}'.format(config.depthdir, stime, osym, '.txt')
        sTfilepath = os.path.join(depthDir, sTfile)
        # write original data to txt files
        with open(sTfilepath, 'a+', encoding='utf-8') as tf:
            tf.writelines(json.dumps(data) + '\n')

        # for no-duplicated csv data
        dpfile = '{0}_{1}_{2}{3}'.format(config.depthdir, stime, osym, '.csv')
        dpspath = os.path.join(depthDir, dpfile)
        if self.wdata:
            if ts in self.wdata.values():
                # self.wdata['ts'] = ts
                pass
            else:
                self.wdata['ts'] = ts
                self.wdata['wlen'] = 0
            # write the current data sent from server to the csv but the position will be changed
        else:
            self.wdata['ts'] = ts
            self.wdata['wlen'] = 0
        self.w2csv(dpspath, sym, data, level)

    # sync_trades
    def sync_data(self, *args):
        self.client.stream.stream_depth.subscribe(self.depth)
        self.client.subscribe_depth(args[1], args[0])

    def w2csv(self, sfilepath, sym, data, level):
        # 获取最新的深度明细
        # 买(卖)1价, 买(卖)1量
        # 显示列名
        depth_head = [
            'symbol', 'ts', 'depth', 'sell_price', 'buy_price', 'sell_amt',
            'buy_amt'
        ]
        depth_flag = 'depth'
        # 存放要写入CVSV的数据data
        # will delete the data from the end if the ts is the same to the previous data
        iseekpos = self.wdata['wlen']
        # print('iseekpos= '+'{0}'.format(iseekpos))
        if iseekpos > 0:
            # print('will call deleteFromMmap')
            self.delline(sfilepath, iseekpos, 0, True)
        # will delete the data from the end if the ts is the same to the one of the previous data
        else:
            pass
        bidlists = data['bids']
        # print(bidlists)
        asklists = data['asks']
        # print(asklists)
        idp = 0
        nask = len(bidlists)
        rFind = False
        # depth_head = ['symbol', 'ts', 'depth', 'sell_price', 'buy_price', 'sell_amt', 'buy_amt']
        level = int(level.lstrip('L'))
        # print('{0}'.format(level))
        nwdatalen = 0
        while idp < nask:
            if os.path.exists(sfilepath):
                with open(sfilepath, 'r', encoding='utf-8') as f:
                    first_line = f.readline()  # 取第一行
                    rFind = depth_flag in first_line
            with open(sfilepath, 'a+', encoding='utf-8', newline='') as f:
                w = csv.writer(f)
                blst = bidlists[idp:idp + 2]
                alst = asklists[idp:idp + 2]
                idepth = 1 + idp / 2
                balist = [
                    sym, data['ts'], idepth, alst[0], blst[0], alst[1], blst[1]
                ]

                # update the lenth of data wroten to csv
                prelen = -1  # 多了一个逗号 所以从-1开始
                for item in balist:
                    ss = '{0}{1}'.format(',', item)
                    # print('depth数据=', ss.strip())
                    prelen += len(ss)
                nwdatalen += prelen
                nwdatalen += len('\t\n')
                # balist.extend(bidlists[idp:idp + 2])
                # balist.extend(asklists[idp:idp + 2])
                if rFind is True:
                    # if idepth == level:  # only write the depthest data to csv
                    w.writerow(balist)
                    idp += 2
                else:
                    w.writerow(depth_head)
                    # if idepth == level:  # only write the depthest data to csv
                    w.writerow(balist)
                    idp += 2

        self.wdata['wlen'] = nwdatalen
コード例 #8
0
ファイル: ceshiwanshua.py プロジェクト: cliicy/okexcode
class MarketApp:
    """
    """
    def __init__(self):
        self.client = FcoinClient()
        self.fcoin = Fcoin()
        self.fcoin.auth(config.key, config.secret)
        self._sender = MqSender('fcoin', 'kline')
        self.sym = ''
        self.wdata = {}
        self._init_log()

    # self.deleteFromMmap(sfilepath, size-iseekpos,size)
    def deleteFromMmap(self, filename, start, end, lastline=False):
        self.sym = self.sym  # acutally it will not be used just for fix the warnning error
        f = open(filename, "r+")
        VDATA = mmap.mmap(f.fileno(), 0)
        size = len(VDATA)
        if lastline is True:
            start = size - start
            end = size
        else:
            pass
        length = end - start
        newsize = size - length
        VDATA.move(start, end, size - end)
        VDATA.flush()
        VDATA.close()
        f.truncate(newsize)
        f.close()

    def candle(self, data):
        # print('数据:', data)
        name, ml, sym = self.client.channel_config[0].split('.')
        ts = int(round(data['id'] * 1000))  # self.client.get_ts()
        # send to mq
        try:
            mqdata = {}
            tdata = {
                'symbol': sym,
                'ts': ts,
                'tm_intv': m_interval,
                'exchange': config.exchange
            }
            mqdata.update(tdata)
            mqdata.update(data)
            # print(mqdata)
            self._sender.send(str(mqdata))
        except Exception as error:
            print(error)
            self._sender.close()
        # send to mq

        # print('symbol: ', sym)
        # create the no-exist folder to save date
        stime = time.strftime('%Y%m%d', time.localtime())
        stradeDir = os.path.join(sDir, stime, config.exchange, config.klinedir)
        if not os.path.exists(stradeDir):
            os.makedirs(stradeDir)

        # for original data
        sTfile = '{0}_{1}_{2}{3}'.format(config.klinedir, stime, sym, '.txt')
        sTfilepath = os.path.join(stradeDir, sTfile)
        # write original data to txt files
        with open(sTfilepath, 'a+', encoding='utf-8') as tf:
            tf.writelines(json.dumps(data) + '\n')
        # for no-duplicated csv data
        sfile = '{0}_{1}_{2}{3}'.format(config.klinedir, stime, sym, '.csv')
        sfilepath = os.path.join(stradeDir, sfile)
        if self.wdata:
            if ts in self.wdata.values():
                # self.wdata['ts'] = ts
                pass
            else:
                self.wdata['ts'] = ts
                self.wdata['wlen'] = 0
            # write the current data sent from server to the csv but the position will be changed
        else:
            self.wdata['ts'] = ts
            self.wdata['wlen'] = 0
        self.w2csv(sfilepath, ts, sym, data)

    def w2csv(self, sfilepath, ts, sym, data):
        sflag = 'close'
        rFind = False
        kklist = []
        vvlist = []
        # will delete the data from the end if the ts is the same to the previous data
        iseekpos = self.wdata['wlen']
        # print('iseekpos= '+'{0}'.format(iseekpos))
        if iseekpos > 0:
            # print('will call deleteFromMmap')
            self.deleteFromMmap(sfilepath, iseekpos, 0, True)
        # will delete the data from the end if the ts is the same to the one of the previous data
        else:
            pass
        if os.path.exists(sfilepath):
            with open(sfilepath, 'r', encoding='utf-8') as f:
                first_line = f.readline()  # 取第一行
                rFind = sflag in first_line
        with open(sfilepath, 'a+', encoding='utf-8', newline='') as f:
            w = csv.writer(f)
            if rFind is True:
                vlist = list(data.values())
                self.additem2list(ts, vvlist, sym, m_interval, vlist)
                w.writerow(vvlist)
            else:  # khead = ['symbol', 'ts', 'tm_intv', 'id', 'open', 'close', 'low', 'high', 'amount', 'vol', 'count']
                klist = list(data.keys())
                # open,close,high,quote_vol,id,count,low,seq,base_vol
                kklist.insert(0, 'symbol')
                kklist.insert(1, 'ts')
                kklist.insert(2, 'tm_intv')
                kklist.insert(3, klist[4])
                kklist.insert(4, klist[0])
                kklist.insert(5, klist[1])
                kklist.insert(6, klist[6])
                kklist.insert(7, klist[2])
                kklist.insert(8, 'amount')
                kklist.insert(9, 'vol')
                kklist.insert(10, klist[5])
                w.writerow(kklist)
                vlist = list(data.values())
                self.additem2list(ts, vvlist, sym, m_interval, vlist)
                w.writerow(vvlist)

        # update the lenth of data wroten to csv
        prelen = len('{0},{1},{2}'.format(sym, ts, m_interval))
        # print('prelen= ' + '{0}'.format(prelen))
        for i in dkey:
            ss = '{0}{1}'.format(',', data[i])
            prelen += len(ss)
        prelen += len(
            '\t\n')  # because there is a extra '\t\n' which is equal 2 bytes
        # print('w2csv prelen= ' + '{0}'.format(prelen))
        self.wdata['wlen'] = prelen
        # print('w2csv after prelen= ' + '{0}'.format(self.wdata['wlen']))
        # update the lenth of data wroten to csv

    # add extral items to the original list
    def additem2list(self, ts, vvlist, sym, ml, vlist):
        self.sym = sym  # acutally it will not be used just for fix the warnning error
        vvlist.insert(0, sym)
        vvlist.insert(1, ts)
        vvlist.insert(2, ml)
        vvlist.insert(3, vlist[4])
        vvlist.insert(4, vlist[0])
        vvlist.insert(5, vlist[1])
        vvlist.insert(6, vlist[6])
        vvlist.insert(7, vlist[2])
        vvlist.insert(8, vlist[3])
        vvlist.insert(9, vlist[8])
        vvlist.insert(10, vlist[5])

    # 循环
    def loop(self):
        self.client.start()

        while not self.client.isConnected:
            self._log.info('waitting……')
            time.sleep(1)

        self.sync_kline(self.sym)
        while True:
            try:
                pass
            except Exception as error:
                print(error)
            time.sleep(1)

    # sync_trades
    def sync_kline(self, sym):
        self.client.stream.stream_klines.subscribe(self.candle)
        self.client.subscribe_candle(sym, config.mflag)

    # 日志初始化
    def _init_log(self):
        self._log = logging.getLogger(__name__)
        self._log.setLevel(level=logging.INFO)
        formatter = logging.Formatter('%(asctime)s - %(message)s')  # 格式
        '''
        保存文档
        '''
        handler = logging.FileHandler("app.log")
        handler.setLevel(logging.INFO)
        handler.setFormatter(formatter)
        self._log.addHandler(handler)
        '''
        控制台显示
        '''
        console = logging.StreamHandler()
        console.setLevel(logging.INFO)
        console.setFormatter(formatter)
        self._log.addHandler(console)
コード例 #9
0
ファイル: ticker.py プロジェクト: cliicy/vFcoinWSS
class TickerApp(BaseSync):
    """
    """
    def __init__(self):
        self.platform = Platform.PLATFORM_FCOIN.value
        self.data_type = PlatformDataType.PLATFORM_DATA_TICKER.value
        BaseSync(self.platform, self.data_type)
        self.client = FcoinClient()
        self._init_log()
        try:
            self._sender = MqSender('3', 'ticker')
        except Exception as e:
            print('ohhh== ERROR= ', e)
            self._sender = None
        self.wdata = {}

    def ticker(self, data):
        name, osym = self.client.channel_config[0].split('.')
        sym = Symbol.convert_to_standard_symbol(Platform.PLATFORM_FCOIN, osym)
        # ts = self.client.get_ts()
        # 从服务器得到的数据中没有ts,也没有id,根据文档要求,要把获取到数据的时间存入csv文件及数据库中
        ts = int(round(time.time() * 1000))
        # send to mq
        if self._sender is not None:
            # print('aaaa {0}'.format(self._sender))
            try:
                mqdata = {}
                tdata = {'symbol': sym, 'ts': ts, 'exchange': config.exchange}
                mqdata.update(tdata)
                mqdata.update(data)
                # print(mqdata)
                self._sender.send(str(mqdata))
            except Exception as error:
                print(error)
                self._sender.close()
        else:
            print('fail to connect rabbitmq server')
            pass
        # send to mq

        # print('symbol: ', sym)
        # create the no-exist folder to save date
        stime = time.strftime('%Y%m%d', time.localtime())
        tickerDir = os.path.join(sDir, stime, config.exchange, config.tickerdir)
        if not os.path.exists(tickerDir):
            os.makedirs(tickerDir)

        # for original data
        sTfile = '{0}_{1}_{2}{3}'.format(config.tickerdir, stime, osym, '.txt')
        sTfilepath = os.path.join(tickerDir, sTfile)
        # write original data to txt files
        with open(sTfilepath, 'a+', encoding='utf-8') as tf:
            tf.writelines(json.dumps(data) + '\n')

        # for no-duplicated csv data
        tkfile = '{0}_{1}_{2}{3}'.format(config.tickerdir, stime, osym, '.csv')
        tspath = os.path.join(tickerDir, tkfile)

        if self.wdata:
            if ts in self.wdata.values():
                # self.wdata['ts'] = ts
                pass
            else:
                self.wdata['ts'] = ts
                self.wdata['wlen'] = 0
            # write the current data sent from server to the csv but the position will be changed
        else:
            self.wdata['ts'] = ts
            self.wdata['wlen'] = 0
        self.w2csv(tspath, ts, sym, data)

    def w2csv(self, tspath, ts, sym, data):
        # will delete the data from the end if the ts is the same to the previous data
        iseekpos = self.wdata['wlen']
        # print('iseekpos= '+'{0}'.format(iseekpos))
        if iseekpos > 0:
            # print('will call deleteFromMmap')
            self.delline(tspath, iseekpos, 0, True)
        # will delete the data from the end if the ts is the same to the one of the previous data
        else:
            pass

        # 获取最新的深度明细
        ticker_head = []
        ticker_flag = 'latest_price'

        tklist = []
        rFind = False
        if os.path.exists(tspath):
            with open(tspath, 'r', encoding='utf-8') as f:
                first_line = f.readline()  # 取第一行
                rFind = ticker_flag in first_line
        with open(tspath, 'a+', encoding='utf-8', newline='') as f:
            w = csv.writer(f)
            if rFind is True:
                self.addI2list(ts, tklist, sym, data['ticker'])
                w.writerow(tklist)
            else:
                ticker_head.insert(0, 'symbol')
                ticker_head.insert(1, 'ts')
                ticker_head.insert(2, 'latest_price')
                ticker_head.insert(3, 'latest_amount')
                ticker_head.insert(4, 'max_buy1_price')
                ticker_head.insert(5, 'max_buy1_amt')
                ticker_head.insert(6, 'min_sell1_price')
                ticker_head.insert(7, 'min_sell1_amt')
                ticker_head.insert(8, 'pre_24h_price')
                ticker_head.insert(9, 'pre_24h_price_max')
                ticker_head.insert(10, 'pre_24h_price_min')
                ticker_head.insert(11, 'pre_24h_bt_finish_amt')
                ticker_head.insert(12, 'pre_24h_usd_finish_amt')
                w.writerow(ticker_head)
                self.addI2list(ts, tklist, sym, data['ticker'])
                w.writerow(tklist)
        # update the lenth of data wroten to csv
        prelen = len('{0},{1}'.format(sym, ts))
        # print('prelen= ' + '{0}'.format(prelen))
        for i in range(11):
            ss = '{0}{1}'.format(',', data['ticker'][i])
            prelen += len(ss)
        prelen += len('\t\n')  # because there is a extra '\t\n' which is equal 2 bytes
        # print('w2csv prelen= ' + '{0}'.format(prelen))
        self.wdata['wlen'] = prelen
        # print('w2csv after prelen= ' + '{0}'.format(self.wdata['wlen']))
        # update the lenth of data wroten to csv

    # sync_trades
    def sync_data(self, *args):
        self.client.stream.stream_ticker.subscribe(self.ticker)
        self.client.subscribe_ticker(args[0])

    # add extral items to the original list
    # ['symbol', 'ts', 'latest_price', 'latest_amount', 'max_buy1_price', 'max_buy1_amt',
    #        'min_sell1_price', 'min_sell1_amt', 'pre_24h_price', 'pre_24h_price_max', 'pre_24h_price_min',
    #        'pre_24h_bt_finish_amt', 'pre_24h_usd_finish_amt']
    # 最新成交价,最近一笔成交的成交量,最大买一价,最大买一量,最小卖一价,最小卖一量,24小时前成交价,24小时内最高价,
    # 24小时内最低价,24小时内基准货币成交量,24小时内计价货币成交量
    @staticmethod
    def addI2list(ts, vvlist, sym, vlist):
        vvlist.insert(0, sym)
        vvlist.insert(1, ts)
        vvlist.insert(2, vlist[0])
        vvlist.insert(3, vlist[1])
        vvlist.insert(4, vlist[2])
        vvlist.insert(5, vlist[3])
        vvlist.insert(6, vlist[4])
        vvlist.insert(7, vlist[5])
        vvlist.insert(8, vlist[6])
        vvlist.insert(9, vlist[7])
        vvlist.insert(10, vlist[8])
        vvlist.insert(11, vlist[9])
        vvlist.insert(12, vlist[10])

        # 把 下面的实时数据写入 Mongodb中
        # 'latest_price' 最新成交价
        # pre_24h_price_max 24小时内最高价
        # pre_24h_price_min 24小时内最低价
        # pre_24h_usd_finish_amt 24小时内计价货币成交量
        # 货币对
        ybdd = {}
        ybdd['sym'] = sym
        # 价格 Price  latest_price
        ybdd['Price'] = vlist[0]
        # 涨跌幅 Change 需要自己计算 或从网页爬取
        # delta = '0.11'
        ff = (vlist[0] - vlist[6]) / vlist[6]*100
        delta = ('%.2f' % ff)
        ybdd['Change'] = '{0}{1}'.format(delta, '%')
        # High  pre_24h_price_max 24小时内最高价
        ybdd['High'] = vlist[7]
        # Low pre_24h_price_min 24小时内最低价
        ybdd['Low'] = vlist[8]
        ybdd['exchange'] = 'fcoin'
        ybdd['api'] = 'ticker'
        # Volume pre_24h_usd_finish_amt 24小时内计价货币成交量
        ybdd['Volume'] = round(vlist[10], 2)
        ticker_coll.update({'sym': ybdd['sym']}, {'$set': {'Price': ybdd['Price'], 'Change': ybdd['Change'],
                                                           'High': ybdd['High'], 'Low': ybdd['Low'],
                                                           'Volume': ybdd['Volume'], 'exchange': 'fcoin',
                                                           'api': 'ticker'}}, True)