def get_ohlc(self, market_id=None, num_ticks=None, interval="HOUR", span=1, from_ts=None, to_ts=None): """ Get the open, high, low, close of a specific market_id :param market_id: market ID :param num_ticks: number of price ticks/data to retrieve :param interval: MINUTE, HOUR or DAY tick interval :param span: it can be a combination of span with interval, 1Hour, 15 MINUTE :param from_ts: from timestamp UTC :param to_ts: to timestamp UTC :return: ohlc dataframe """ if market_id is None: market_id = self.market_id interval = check_interval(interval) span = check_span(interval, span) if from_ts is not None and to_ts is not None: r = self.session.get( self.rest_url + f'/market/{market_id}/barhistorybetween?interval={interval}&span={span}&fromTimeStampUTC={from_ts}&toTimestampUTC={to_ts}' ) else: if not num_ticks: num_ticks = 1 if from_ts is not None: r = self.session.get( self.rest_url + f'/market/{market_id}/barhistorybefore?interval={interval}&span={span}&maxResults={num_ticks}&toTimeStampUTC={to_ts}' ) elif to_ts is not None: r = self.session.get( self.rest_url + f'/market/{market_id}/tickhistoryafter?interval={interval}&span={span}&maxResults={num_ticks}&fromTimeStampUTC={from_ts}' ) else: r = self.session.get( self.rest_url + f'/market/{market_id}/barhistory?interval={interval}&span={span}&PriceBars={num_ticks}' ) resp = json.loads(r.text) try: if num_ticks == 1: return resp.get('PriceBars')[0] else: df_ohlc = pd.DataFrame(resp.get('PriceBars')) df_ohlc['BarDate'] = df_ohlc['BarDate'].map(format_date) return df_ohlc except: raise GCapiException(resp)
def get_prices(self, market_id=None, num_ticks=None, from_ts=None, to_ts=None, price_type=None): """ Get prices :param market_id: market ID :param num_ticks: number of price ticks/data to retrieve :param from_ts: from timestamp UTC :param to_ts: to timestamp UTC :return: price data """ if price_type is None: # Default to mid price price_type = 'MID' else: price_type = price_type.upper() if market_id is None: market_id = self.market_id if from_ts is not None and to_ts is not None: r = self.session.get( self.rest_url + f'/market/{market_id}/tickhistorybetween?fromTimeStampUTC={from_ts}&toTimestampUTC={to_ts}&priceType={price_type}' ) else: if not num_ticks: num_ticks = 1 if from_ts is not None: r = self.session.get( self.rest_url + f'/market/{market_id}/tickhistorybefore?maxResults={num_ticks}&toTimeStampUTC={to_ts}&priceType={price_type}' ) elif to_ts is not None: r = self.session.get( self.rest_url + f'/market/{market_id}/tickhistoryafter?maxResults={num_ticks}&fromTimeStampUTC={from_ts}&priceType={price_type}' ) else: r = self.session.get( self.rest_url + f'/market/{market_id}/tickhistory?PriceTicks={num_ticks}&priceType={price_type}' ) resp = json.loads(r.text) try: if num_ticks == 1: return resp['PriceTicks'][0]['Price'] else: return resp except: raise GCapiException(resp)
def get_margin_info(self, get=None): """ Gets trading account margin information :param get: retrieve specific information (e.g. Cash) :return: trading account margin information """ r = self.session.get(self.rest_url + '/margin/ClientAccountMargin') resp = json.loads(r.text) try: self.cash = resp['Cash'] if get is not None: return resp[get] else: return resp except: raise GCapiException(resp)
def get_account_info(self, get=None): """ Gets trading account general information :param get: retrieve specific information (e.g. TradingAccountId) :return: trading account information """ r = self.session.get(self.rest_url + '/UserAccount/ClientAndTradingAccount') resp = json.loads(r.text) try: self.trading_account_id = resp['TradingAccounts'][0][ 'TradingAccountId'] if get is not None: return resp['TradingAccounts'][0][get] else: return resp except: raise GCapiException(resp)
def get_market_info(self, market_name, get=None): """ Gets market information :param market_name: market name (e.g. USD/CAD) :param get: retrieve specific information (e.g. MarketId) :return: market information """ r = self.session.get(self.rest_url + f'/cfd/markets?MarketName={market_name}') resp = json.loads(r.text) try: self.market_name = market_name self.market_id = resp['Markets'][0]['MarketId'] if get is not None: return resp['Markets'][0][get] else: return resp except: raise GCapiException(resp)
def __init__(self, username, password, appkey, proxies=None): self.rest_url = 'https://ciapi.cityindex.com/TradingAPI' headers = {'Content-Type': 'application/json'} data = {"UserName": username, "Password": password, "AppKey": appkey} r = requests.post(self.rest_url + '/session', headers=headers, proxies=proxies, json=data) resp = json.loads(r.text) if resp['StatusCode'] != 1: raise GCapiException(resp) session = resp['Session'] headers = { 'Content-Type': 'application/json', 'UserName': username, 'Session': session } with requests.Session() as s: s.headers = headers if proxies is not None: s.proxies.update(proxies) self.session = s