Exemplo n.º 1
0
    def get_bars(self,
                 symbol,
                 period=5,
                 count=100,
                 fields='DTOHLCV',
                 start_time='08:00',
                 end_time='16:30',
                 fill=True):
        """
            SYMBOL,PERIOD,#BARS,[DTOHLCV],{HH:MM-HH:MM},{FILL}
            Example: IBM,15,1000,DTOHLCV,09:30-16:00
        """
        if self.bars_client is None:
            self.bars_client = ddec.DDEClient(self.app_name, 'bars')

        # Create main request message
        message = "{},{},{},{}".format(symbol, period, count, fields)

        # Add start and end times if passed
        if start_time and end_time:
            message += ",{}-{}".format(start_time, end_time)

        # Add fill parameter if passed true
        if fill:
            message += ",FILL"

        response = None
        for i in range(QLinkConn.MAX_TRIES + 1, 0, -1):

            # Send request to QLink DDE server and decode response byte string
            response = self.bars_client.request(message).decode('utf-8')

            # Break from loop if response is not empty
            if response.count('\n') < len(response):
                print("QLinkConn-Bars(%s SUCCESS)" % symbol)
                break

            # If response was empty then wait and try again
            if i <= QLinkConn.MAX_TRIES:
                print(
                    "QLinkConn-Bars(NO DATA RETURNED). Trying %s more time%s..."
                    % (i - 1, 's' if i > 1 else ''))
            time.sleep(QLinkConn.WAIT)

        # Get dtypes
        column_labels = [QLinkConn.BAR_LABELS[x.lower()] for x in fields]

        # Parse response
        bars = pd.DataFrame(
            [bar_str.split('\t') for bar_str in response.split('\n')],
            columns=column_labels).head(count)

        # Convert numerical types
        for f in fields:
            if f.lower() in ['o', 'h', 'l', 'c', 'v']:
                bars[QLinkConn.BAR_LABELS[f.lower()]] = pd.to_numeric(
                    bars[QLinkConn.BAR_LABELS[f.lower()]])

        # Return bars
        return bars
Exemplo n.º 2
0
 def get_other(self, field):
     if field in self.clients.keys():
         return self.clients[field].request(self.symbol)
     elif field in OldDDEConn.FIELDS:
         self.clients[field] = ddec.DDEClient('WINROS', field)
         return self.clients[field].request(self.symbol)
     else:
         print("'%s' is not in the list of acceptable fields: %s" %
               (field, OldDDEConn.FIELDS))
         return None
Exemplo n.º 3
0
    def get_trades(self, symbol, count=100, fields='DTPS'):
        """
            SYMBOL,#ROWS,[DTCEPS]
            Example: IBM,200,DTPSEC
        """
        if self.ts_client is None:
            self.ts_client = ddec.DDEClient(self.app_name, 'ts')

        # Create main request message
        message = "{},{},{}".format(symbol, count, fields)

        response = None
        for i in range(QLinkConn.MAX_TRIES + 1, 0, -1):

            # Send request to QLink DDE server and decode response byte string
            response = self.ts_client.request(message).decode('utf-8')

            # Break from loop if response is not empty
            if response.count('\n') < len(response):
                print("QLinkConn-Trades(%s SUCCESS)" % symbol)
                break

            # If response was empty then wait and try again
            if i <= QLinkConn.MAX_TRIES:
                print(
                    "QLinkConn-Trades(NO DATA RETURNED). Trying %s more time%s..."
                    % (i - 1, 's' if i > 1 else ''))
            print('sleeping...')
            time.sleep(QLinkConn.WAIT)

        # Get dtypes
        column_labels = [QLinkConn.TRADE_LABELS[x.lower()] for x in fields]

        # Parse response
        trades = pd.DataFrame(
            [bar_str.split('\t') for bar_str in response.split('\n')],
            columns=column_labels).head(count)

        # Convert numerical types
        for f in fields:
            if f.lower() in ['p', 's']:
                trades[QLinkConn.TRADE_LABELS[f.lower()]] = pd.to_numeric(
                    trades[QLinkConn.TRADE_LABELS[f.lower()]])

        # Return trades
        return trades
Exemplo n.º 4
0
 def get_totalvol(self):
     if 'totalvol' not in self.clients.keys():
         self.clients['totalvol'] = ddec.DDEClient('WINROS', 'totalvol')
     return self.clients['totalvol'].request(self.symbol)
Exemplo n.º 5
0
 def get_last(self):
     if 'last' not in self.clients.keys():
         self.clients['last'] = ddec.DDEClient('WINROS', 'last')
     return self.clients['last'].request(self.symbol)
Exemplo n.º 6
0
 def get_high(self):
     if 'high' not in self.clients.keys():
         self.clients['high'] = ddec.DDEClient('WINROS', 'high')
     return self.clients['high'].request(self.symbol)
Exemplo n.º 7
0
 def get_open(self):
     if 'open' not in self.clients.keys():
         self.clients['open'] = ddec.DDEClient('WINROS', 'open')
     return self.clients['open'].request(self.symbol)