Ejemplo n.º 1
0
 def down_quote(self, conid, period, bar):
   ib_cfg = ib_web_api.Configuration()
   ib_cfg.verify_ssl = False
   api = MarketDataApi(ib_web_api.ApiClient(ib_cfg))
   res = None
   for i in range(1, 6):
     try:
       # Download conid
       res = api.iserver_marketdata_history_get(
         conid,
         period=period,
         bar=bar
       )
     except Exception as e:
       if i == 6:
         raise Exception('Could not get quote: %s\n' % e)
       else:
         continue
   # If three days, pick the last day
   # TODO: Do we need it like this?
   if period == '3d' and bar == '1d':
     if hasattr(res, 'points'):
       point = int(res.points)
       res = res.data[point].to_dict()
     else:
       raise Exception('Missing "points" for %s' % self.conid)
   else:
     if res is not None:
       res = [ i.to_dict() for i in res.data ]
     else:
       raise Exception('Could not get quote', self.conid)
   return res
 def run(self, symbols):
     symbols = [i.upper() for i in symbols]
     # Instantiate API class
     config = ib_web_api.Configuration()
     config.verify_ssl = False
     client = ib_web_api.ApiClient(config)
     api = MarketDataApi(client)
     for symbol in symbols:
         print('===> ' + symbol)
         # Get last 15 minutes data
         # TODO: Catch here
         points = api.iserver_marketdata_history_get(
             Company(symbol).get_conid(), '15min', bar='1min').data
         # Get just volume
         vol = [p.to_dict()['v'] for p in points]
         # Min/max/mean volume points, perc increase from mean to hi
         lo = min(i for i in vol if i > 0)
         hi = max(vol)
         m = mean(vol)
         perc = ((hi - m) / m) * 100
         # Print stats
         print('LO: ' + str(lo))
         print('HI: ' + str(hi))
         print('ME: ' + str(round(m, 2)))
         print('PERC: ' + str(round(perc, 2)) + '%')
         print('CONID: ' + Company(symbol).get_conid())
config.verify_ssl = False
client = ib_web_api.ApiClient(config)

try:
    # Get conids
    api = ContractApi(client)
    conids = {}
    for ticker in args.tickers:
        conid = int
        response = api.iserver_secdef_search_post({"symbol": ticker})
        for item in response:
            if item.description == 'NASDAQ':
                conids[ticker] = item.conid
except ApiException as e:
    print("Could not find conid: %s\n" % e)
    exit(1)

try:
    # Get market data
    for symbol, conid in conids.items():
        api = MarketDataApi(ib_web_api.ApiClient(config))
        response = api.iserver_marketdata_history_get(conid, '1d').data
        datapoints = []
        for datapoint in response:
            datapoints.append(datapoint.to_dict())
        f = open('data-5/' + symbol + '.json', 'w')
        f.write(json.dumps(datapoints))
        f.close()
except ApiException as e:
    print("Exception: %s\n" % e)
Ejemplo n.º 4
0
                    print('Could not get symbol %s' % ticker)
                    failed = True
                    continue
except ApiException as e:
    print("Could not get conids: %s\n" % e)
    exit(1)
if len(conids) == 0:
    print('Could not find any conids')
    exit(1)
# Print summary
eprint('Conids found: %s/%s' % (len(conids), len(args.tickers)))

# Get market data
try:
    eprint('Get market data')
    api = MarketDataApi(client)
    for symbol, conid in conids.items():
        response = api.iserver_marketdata_history_get(
          conid,
          '2d',
          bar='5m'
        ).data
        datapoints = []
        # Convert to dict
        for datapoint in response:
            datapoints.append(datapoint.to_dict())
        # Split into two days by date
        dates = {}
        for datapoint in datapoints:
            cur_date = datetime .fromtimestamp(int(datapoint['t']/1000)) \
                .strftime('%Y-%m-%d')