def __init__(self, instruments, granularity, prob_threshold, target, account_name, *args, **kwargs): PricingStream.__init__(self, *args, **kwargs) print "loading model..." self.model = pickle.load(open('mpv12_0025_const.sav', 'rb')) self.instruments = instruments.split(',') self.target = target self.connected = False self.account_name = account_name self.granularity = granularity self.prob_threshold = prob_threshold self.status = {} for inst in self.instruments: self.status[inst] = { "bid": None, "ask": None, "time": None, "trade_pred": None, "current_pred": None, "long": 0, "short": 0, "unrealizedPL": 0, "pending_order": None, "made_prediction": False, "tradeID": None } print "connecting..." self.client = API(access_token=access_token, environment=environment)
def stream(count, instruments, nice, timeout=None): accountID, access_token = exampleAuth() request_params = {} if timeout: request_params = {"timeout": timeout} # fetch MAXREC stream records MAXREC = count api = API(access_token=access_token, environment="practice", request_params=request_params) # setup the stream request r = PricingStream(accountID=accountID, params={"instruments": ",".join(instruments)}) n = 0 R_list = list() while True: try: for R in api.request(r): if nice: R = json.dumps(R, indent=2) print('Collecting Data.. Please wait..') R_list.append(R) n += 1 if MAXREC and n >= MAXREC: r.terminate("maxrecs received: {}".format(MAXREC)) except V20Error as e: # catch API related errors that may occur with open("LOG", "a") as LOG: LOG.write("V20Error: {}\n".format(e)) break except ConnectionError as e: with open("LOG", "a") as LOG: LOG.write("Error: {}\n".format(e)) except StreamTerminated as e: with open("LOG", "a") as LOG: LOG.write("Stopping: {}\n".format(e)) break except Exception as e: with open("LOG", "a") as LOG: LOG.write("??? : {}\n".format(e)) break return R_list
def main(clargs): accountID, access_token = exampleAuth() request_params = {} if clargs['--timeout']: request_params = {"timeout": clargs['--timeout']} # fetch MAXREC stream records MAXREC = int(clargs['--count']) api = API(access_token=access_token, environment="practice", request_params=request_params) # setup the stream request r = PricingStream(accountID=accountID, params={"instruments": ",".join(clargs['<instrument>'])}) n = 0 _m = {"PRICE": PriceRecord, "HEARTBEAT": HeartBeat} while True: try: for rv in api.request(r): # create a Pydantic record based on the type rec = _m[rv['type']](**rv) n += 1 if MAXREC and n >= MAXREC: r.terminate("maxrecs received: {}".format(MAXREC)) print(rec.json() if clargs['--nice'] else rec) except V20Error as e: # catch API related errors that may occur logger.error("V20Error: %s", e) break except ConnectionError as e: logger.error("%s", e) except StreamTerminated as e: logger.error("Stopping: %s", e) break except Exception as e: logger.error("%s", e) break
def get_realtime_ticker(self, callback): req = PricingStream(accountID=self.account_id, params={'instruments': settings.product_codes}) try: for resp in self.client.request(req): if resp['type'] == 'PRICE': timestamp = datetime.timestamp( dateutil.parser.parse(resp['time'])) instrument = resp['instrument'] bid = float(resp['bids'][0]['price']) ask = float(resp['asks'][0]['price']) volume = self.get_candle_volume() ticker = Ticker(instrument, timestamp, bid, ask, volume) callback(ticker) with open(ticker_log_path, mode='w') as f: f.write(str(timestamp)) except V20Error as e: requests.post( settings.WEB_HOOK_URL, data=json.dumps({ 'text': u'oanda streaming stops', # 通知内容 'username': u'Market-Signal-Bot', # ユーザー名 'icon_emoji': u':smile_cat:', # アイコン 'link_names': 1, # 名前をリンク化 })) logger.error(f'action=get_realtime_ticker error={e}') raise
def get_streaming_price(client, accountID, ls_instrument): print('getting {} instruments'.format(len(ls_instrument))) params = {"instruments": ",".join(ls_instrument)} r = PricingStream(accountID=accountID, params=params) price_ls = [] n = 0 max_iter = len(ls_instrument) for R in client.request(r): price_ls.append(R) n += 1 if n >= max_iter: break price_data = pd.DataFrame(price_ls) for c in ['asks', 'bids']: price_data[c] = price_data[c].apply(lambda x: x[0]['price']) price_data['time'] = pd.to_datetime(price_data['time']) price_data['time'] = price_data['time'].apply(lambda t: convert_ToLocal(t)) instrument_to_ask = dict(zip(price_data['instrument'], price_data['asks'])) instrument_to_bid = dict(zip(price_data['instrument'], price_data['bids'])) return instrument_to_ask, instrument_to_bid
def stream(self, instrument, window): request_params = {"timeout": 100} params = {"instruments": instrument, "granularity": window} self.api = API(access_token=token, environment=env, request_params=request_params) r = PricingStream(accountID=accountID, params=params) while True: try: api.request(r) for R in r.response: data = { "time": R['time'], "closeoutAsk": R['closeoutAsk'], "closeoutBid": R['closeoutBid'], "status": R['status'], "instrument": R['instrument'], "ask": R['ask'], "bid": R['bid'] } print(data) except Exception as e: print(e) continue
def connect_v20(access_token, accountID, instruments): api = API(access_token=access_token, environment="practice") s = PricingStream(accountID=accountID, params={"instruments": instruments}) response = api.request(s) try: n = 0 for R in api.request(s): # print (msg['type']) if R['type'] == 'HEARTBIT': print(json.dumps(R['type'], indent=2), json.dumps(R['type']['time'], indent=2)) if R['type'] == 'PRICE': instrument = json.loads(json.dumps(R['instrument'], indent=2)) status = json.loads(json.dumps(R['status'], indent=2)) timestamp = json.loads(json.dumps(R['time'], indent=2)) closeoutBid = json.loads(json.dumps(R['closeoutBid'], indent=2)) closeoutAsk = json.loads(json.dumps(R['closeoutAsk'], indent=2)) askBook = json.loads(json.dumps(R['asks'], indent=2)) # influx_record(R['type']) print(instrument, timestamp, closeoutBid, closeoutAsk) position_open(instrument, closeoutBid, closeoutAsk) # for line in askBook: # price = json.loads(json.dumps(line["price"], indent=2)) # liquidity = json.loads(json.dumps(line["liquidity"], indent=2)) # print (price,liquidity) # return instrument,closeoutBid, closeoutAsk # n += 1 # if n > 125: # s.terminate("maxrecs received: {}".format(MAXREC)) except V20Error as e: print("Error: {}".format(e))
def _monitor_prices(self): # Set up price stream endpoint = PricingStream( self.account_id, {'instruments': ','.join(self.instruments)}, ) logging.info('Starting price stream') while True: try: for response in self.api.request(endpoint): # Store any price updates and inform the appropriate trader if response['type'] == 'PRICE': # NOTE: This method of getting the max/min bid/ask may # be unnecessary as they appear to be ordered such that # the appropriate value is first. However, this is not # documented anywhere, so doing it this way is more # futureproof at the cost of some performance. highest_bid = max( response['bids'], key = lambda x: float(x['price']) ) lowest_ask = min( response['asks'], key = lambda x: float(x['price']) ) # Update the appropriate trader self.traders[response['instrument']].tick({ 'bid': float(highest_bid['price']), 'ask': float(lowest_ask['price']), }) except (V20Error, OSError) as e: logging.error(e)
def print_stream_pricing(order_instrument): """ Stream price data for given instrument. Note: Gives a "HEARTBEAT" to maintain an active HTTP connection, i.e if no new pricing info is available. """ maxrec = 100 # not exactly sure what this value means s = PricingStream(accountID=accountID, params={"instruments": order_instrument}) try: n = 0 for R in api.request(s): print(json.dumps(R, indent=2)) n += 1 if n > maxrec: s.terminate("maxrecs received: {}".format(maxrec)) except V20Error as e: print("Error: {}".format(e))
def _run(self): tickMsg = "write tick record ...{}\n" while True: r = PricingStream( accountID=accountID, params={"instruments": ",".join(self.instruments)}) se = None # to save the exception if it occurs with open("prices.txt", "a") as O: n = 0 try: for R in api.request(r): now = datetime.now() sys.stderr.write(tickMsg.format(now)) if self.nice: O.write(json.dumps(R, indent=2) + "\n") else: O.write(json.dumps(R) + "\n") O.flush() gevent.sleep(0) n += 1 if self.maxrec and n >= self.maxrec: r.terminate("maxrecs received: {}".format(n)) except V20Error as e: # catch API related errors that may occur se = e # save for reraise logger.error("V20Error: %s %d", e, n) break except ConnectionError as e: logger.error("ConnectionError: %s %d", e, n) time.sleep(3) except StreamTerminated as e: se = e # save for reraise logger.error("StreamTerminated: %s %d", e, n) break except Exception as e: se = e # save for reraise logger.error("Some exception: %s %d", e, n) break
def fetch(): access_token = Config.get('oanda')['api_key'] account_id = Config.get('oanda')['account_id'] api = API(access_token=access_token, environment="practice") instruments = "DE30_EUR,EUR_USD,EUR_JPY" s = PricingStream(accountID=account_id, params={"instruments": instruments}) try: n = 0 for R in api.request(s): print(json.dumps(R, indent=2)) n += 1 if n > 10: s.terminate("maxrecs received: {}".format(MAXREC)) except V20Error as e: print("Error: {}".format(e))
def processStreamPrices(self): """获取价格推送""" # 未订阅 symbols = self.subscribed_symbols if not symbols: #print 'exit price stream thread' return req = PricingStream(accountID=self.accountId, params={'instruments':','.join(symbols)}) resp = self.v20_api.request(req) if resp: try: for line in resp: if line: if self.DEBUG: print self.onPrice.__name__ if line['type'] == 'PRICE': if self.spi: self.spi.onPrice(line) else: if self.spi: self.spi.onHeartbeat(line) if not self.active or not self.started: break except V20Error as e: self.new_state(BrokenState) if self.spi: self.spi.onError(e, -1) #print 'exit price stream thread' return except ConnectionError as e: self.new_state(BrokenState) if self.spi: self.spi.onError(e, -1) #print 'exit price stream thread' return except StreamTerminated as e: self.new_state(BrokenState) if self.spi: self.spi.onError(e, -1) #print 'exit price stream thread' return except Exception as e: self.new_state(BrokenState) if self.spi: self.spi.onError(e, -1) #print 'exit price stream thread' return else: if self.spi: self.spi.onError(error, -1)
def run(self): c = 0 r = PricingStream(accountID=account.mt4, params={"instruments": ",".join(self.instruments)}) try: for ticks in api.request(r): self.callback(ticks) c += 1 if self.max_rec and c >= self.max_rec: r.terminate(f"Maximum heartbeats received: {c}") except V20Error as e: # catch API related errors that may occur logger.error(f"V20Error: {e}") except ConnectionError as e: logger.error(f"ConnectionError: {e}") time.sleep(2) except StreamTerminated as e: logger.error(f"StreamTerminated: {e}") except Exception as e: logger.error(f"Some exception: {e}")
def stream_prices(pair, config_file, count=0): """Stream price data. Parameters ---------- pair: str The instrument pair in which to fetch prices. config_file : str Location of configuration file. count: int The number of price bars to get; infinite bars if count=0. """ conf = get_config(config_file) api = API(access_token=conf['token'], environment=conf['environment']) r = PricingStream(accountID=conf['account'], params={'instruments': pair}) api.request(r) if 'JPY' in pair: spread_multiplier = 100 else: spread_multiplier = 10000 n = 0 print('\n{}'.format(pair)) while True: try: for _ in api.request(r): if _['type'] == 'PRICE': d = dict(bid=_['bids'][0]['price'], ask=_['asks'][0]['price']) d['spread'] = round(float(d['ask']) * spread_multiplier - float(d['bid']) * spread_multiplier, 1) print('Bid: {}'.format(d['bid'])) print('Ask: {}'.format(d['ask'])) print('Spread: {}\n'.format(d['spread'])) if count: n += 1 else: # Keep looping if no count was specified n = -1 if n >= count: break break except KeyboardInterrupt: break
def streamTick(self, callback, currency): params = {'instruments': currency} req = PricingStream(accountID=self.account_id, params=params) try: for res in self.client.request(req): tick = Tick.parse(res) if tick is not None: tick.volume = self.currentVolume(currency) callback(tick) else: print(res) except V20Error as e: logger.error('in candles() ... {e}', e) return
def oanda_stream_api(account, instrument, action): from oandapyV20 import API from oandapyV20.endpoints.pricing import PricingStream from oandapyV20.exceptions import V20Error params = {"instruments": instrument} api = API(access_token=account.token) ps = PricingStream(account.id, params) try: for rsp in api.request(ps): action(rsp) except V20Error as e: print("Error: {}".format(e))
def stream_rates(ACCESS_TOKEN, ACCOUNT_ID, INSTRUMENTS): api = API(access_token=ACCESS_TOKEN, environment="practice") stream = PricingStream(accountID=ACCOUNT_ID, params={"instruments": INSTRUMENTS}) try: item = 0 for response in api.request(stream): print(json.dumps(response, indent=2)) item += 1 # if n > 10: # stream.terminate("maxrecs received: {}".format(MAXREC)) except V20Error as e: print("Error: {}".format(e))
def get_current_ask_price(order_instrument): """ Return the current ask price for an instrument at the time the function is called """ maxrec = 10 # Try to get the stream price data 10 times n = 0 s = PricingStream(accountID=accountID, params={"instruments": order_instrument}) try: for R in api.request(s): n += 1 if n > maxrec: s.terminate("maxrecs received: {}".format(maxrec)) if R["type"] == "PRICE": #print(R["asks"][0]["price"]) ask_price = R["asks"][0]["price"] return ask_price else: print(R) except V20Error as e: print("Error: {}".format(e)) # If we fail to get price data return False
def connect_to_stream(self): """ Converts CCY pair to OANDA style pair. Creates OADAN V20 API. Raises PricingStream request Output: client.request(request) """ pairs_oanda = ["%s_%s" % (p[:3], p[3:]) for p in self.pairs] pairs_list = ','.join(pairs_oanda) params = {"instruments": pairs_list} client = API(access_token=self.access_token, environment=self.domain) request = PricingStream(accountID=self.account_id, params=params) return client.request(request)
def get_realtime_ticker(self, callback): req = PricingStream(accountID=self.account_id, params={'instruments': settings.product_code}) try: for resp in self.client.request(req): if resp['type'] == 'PRICE': timestamp = datetime.timestamp( dateutil.parser.parse(resp['time'])) instrument = resp['instrument'] bid = float(resp['bids'][0]['price']) ask = float(resp['asks'][0]['price']) volume = self.get_candle_volume() ticker = Ticker(instrument, timestamp, bid, ask, volume) callback(ticker) except V20Error as e: logger.error(f'action=get_realtime_ticker error={e}') raise
def stream(instrument, window): request_params = {"timeout": 100} params = {"instruments": instrument, "granularity": window} api = API(access_token=token, environment=env, request_params=request_params) r = PricingStream(accountID=accountID, params=params) while True: try: api.request(r) for R in r.response: time = R['time'] ask = R['asks'][0]['price'] bid = R['bids'][0]['price'] return time, ask, bid except Exception as e: print(e) continue
def lambda_handler(event, context): environment = event['environment'] instruments = event['instruments'] statusCode = 200 message = "OK" api = API(access_token=ac.access_token, environment=environment) params = {"instruments": instruments} ps = PricingStream(ac.account_number, params) dynamoDB = boto3.resource("dynamodb") table = dynamoDB.Table(instruments) try: for rsp in api.request(ps): if "bids" in rsp.keys(): table.put_item( Item={ "DATETIME": datetime.datetime.now( datetime.timezone(datetime.timedelta( hours=9))).strftime('%Y%m%d%H%M%S'), "bid": rsp["bids"][0]["price"], "ask": rsp["asks"][0]["price"] }) break except V20Error as e: statusCode = 500 message = "Error: {}".format(e) return { "statusCode": statusCode, "body": json.dumps({"message": message}), }
def _run(self): se = None # save exception for reraise while True: # setup the stream-request r = PricingStream( accountID=self.accountID, params={"instruments": ",".join(self.instruments)}) n = 0 try: for R in self.api.request(r): # now = datetime.now() self.queue.put_nowait(R) gevent.sleep(0) n += 1 except V20Error as e: # catch API related errors that may occur se = e logger.error("V20Error: %s %s %d", e.code, e.msg, n) break except ConnectionError as e: logger.error("ConnectionError: %s %d", e, n) time.sleep(3) except StreamTerminated as e: se = e logger.error("StreamTerminated: %s %d", e, n) break except Exception as e: se = e logger.error("Exception: %s %d", e, n) break raise se
class Streaming: """Script for streaming of rates Example use: streaming.py --inst EUR_USD --inst EUR_JPY Can be one or more instruments Will keep going until stopped or error occurs """ # Get credentials from .env access_token = os.getenv("TOKEN") accountID = os.getenv("ACCOUNT_ID") # Parser for tickers parser = argparse.ArgumentParser(prog="streaming") parser.add_argument("function", type=str, action="store", nargs=1) parser.add_argument("-i", "--instruments", type=str, action="append") args = parser.parse_args() # Generate for the api-calls api = API(access_token=access_token, environment=os.getenv("OandaEnv"), request_params={}) r = PricingStream(accountID=accountID, params={"instruments": ",".join(args.instruments)}) # Continious streaming of rates while True: try: for R in api.request(r): R = json.dumps(R) print(R) except Exception as e: print("Error occured: {}\n".format(e)) break
def quoteStreamer(self): AccID, api = self.accountDetails() if not os.path.exists(os.path.join(self.path['mainPath'], 'TICKERS')): os.makedirs(os.path.join(self.path['mainPath'], 'TICKERS')) try: while True: n = 0 s = PricingStream( accountID=AccID, params={"instruments": self.path['instruments']}) tickers = [] try: for R in api.request(s): if R['type'] == 'PRICE': rec = { 'tickers': R['instrument'], 'bids': R['bids'][0]['price'], 'asks': R['asks'][0]['price'], 'direction': 'v' } if len(tickers) + 1 <= len( self.path['instruments'].split(',')): tickers.append(rec) else: for enum, ii in enumerate(tickers): previous_bid = tickers[enum]['bids'] if tickers[enum]['tickers'] == R[ 'instrument']: tickers[enum]['bids'] = R['bids'][0][ 'price'] tickers[enum]['asks'] = R['asks'][0][ 'price'] tickers[enum][ 'direction'] = self.arrowHead( previous_bid, tickers[enum]['bids']) df = pd.DataFrame([x for x in tickers], columns=[ 'tickers', 'bids', 'asks', 'direction' ]) df.to_csv( os.path.join(self.path['mainPath'], 'TICKERS/streams.csv')) print(tickers) else: rec = { 'tickers': R['instrument'], 'bids': R['bids'][0]['price'], 'asks': R['asks'][0]['price'], 'direction': 'v' } if len(tickers) + 1 <= len( self.path['instruments'].split(',')): tickers.append(rec) else: for enum, ii in enumerate(tickers): previous_bid = tickers[enum]['bids'] if tickers[enum]['tickers'] == R[ 'instrument']: tickers[enum]['bids'] = R['bids'][0][ 'price'] tickers[enum]['asks'] = R['asks'][0][ 'price'] tickers[enum][ 'direction'] = self.arrowHead( previous_bid, tickers[enum]['bids']) df = pd.DataFrame([x for x in tickers], columns=[ 'tickers', 'bids', 'asks', 'direction' ]) df.to_csv( os.path.join(self.path['mainPath'], 'TICKERS/streams.csv')) print(tickers) except: pass n += 1 try: if n > 10: time.sleep(10) except: pass continue except: pass
# OANDA Config accountID = config_trading['DataOandaAccount'] access_token = config_trading['DataOandaToken'] # Telegram Config TOKEN = config_trading['DataTgrChatID'] chatid = config_trading['DataTgrToken'] # tb = telebot.TeleBot(TOKEN) # Do Not Touch pairs_traded = '%s,%s' % (str(instrument_1), str(instrument_2)) pairs_traded_dict = {instrument_1, instrument_2} api = API(access_token=access_token, environment="practice") stream = PricingStream(accountID=accountID, params={"instruments": pairs_traded}) orders_list = orders.OrderList(accountID) trades_list = trades.TradesList(accountID) current_holding = 0 df_x_ = 0 df_y_ = 0 def get_src_cls(source_name): return getattr(sys.modules[__name__], source_name) class EGCointegration(Strategy): def __init__(self, x, y, on, col_name, is_cleaned=True): if is_cleaned is not True:
# # author たっきん # # 事前準備 : # oandapyV20のインストール (pip install oandapyV20) # ============================================================================== import json from oandapyV20 import API from oandapyV20.endpoints.pricing import PricingStream from oandapyV20.exceptions import V20Error from fx import oanda_common as oc from fx import your_account as ya api = API(access_token=ya.access_token, environment=oc.OandaEnv.PRACTICE) params = {"instruments": "USD_JPY,EUR_JPY,EUR_USD"} ps = PricingStream(ya.account_number, params) try: for rsp in api.request(ps): print("■リストの取得") print(json.dumps(rsp, indent=2)) print("■bidsのみ抽出:") if "bids" in rsp.keys(): print(rsp["bids"][0]["price"]) except V20Error as e: print("Error: {}".format(e))
if not clargs.instruments: parser.parse_args(["--help"]) request_params = {} if clargs.timeout: request_params = {"timeout": clargs.timeout} # fetch MAXREC stream records MAXREC = clargs.count api = API(access_token=access_token, environment="practice", request_params=request_params) # setup the stream request r = PricingStream(accountID=accountID, params={"instruments": ",".join(clargs.instruments)}) n = 0 while True: try: for R in api.request(r): if clargs.nice: R = json.dumps(R, indent=2) print(R) n += 1 if MAXREC and n >= MAXREC: r.terminate("maxrecs received: {}".format(MAXREC)) except V20Error as e: # catch API related errors that may occur with open("LOG", "a") as LOG:
import json from oandapyV20 import API from oandapyV20.exceptions import V20Error from oandapyV20.endpoints.pricing import PricingStream import configparser config = configparser.ConfigParser() config.read('config.ini') api = API(access_token=config['account']['token'], environment="practice") instruments = "XAU_USD" s = PricingStream(accountID=config['account']['accountID'], params={"instruments": instruments}) try: n = 0 for R in api.request(s): print(json.dumps(R, indent=2)) # n += 1 # if n > 10: # s.terminate("maxrecs received: {}".format(n)) except V20Error as e: print("Error: {}".format(e))
# 'NZD_USD',\ # 'USD_CAD','USD_CHF'] symbols = ['AUD_CAD','AUD_CHF','AUD_JPY','AUD_NZD','AUD_USD',\ 'CAD_CHF','CAD_JPY',\ 'CHF_JPY',\ 'EUR_AUD','EUR_CAD','EUR_CHF','EUR_GBP','EUR_JPY','EUR_NZD','EUR_USD',\ 'GBP_AUD','GBP_CAD','GBP_CHF','GBP_JPY','GBP_NZD','GBP_USD',\ 'NZD_CAD','NZD_CHF','NZD_JPY','NZD_USD',\ 'USD_CAD','USD_CHF','USD_JPY'] params = { 'instruments': 'AUD_CAD,AUD_CHF,AUD_JPY,AUD_NZD,AUD_USD,CAD_CHF,CAD_JPY,CHF_JPY,EUR_AUD,EUR_CAD,EUR_CHF,EUR_GBP,EUR_JPY,EUR_NZD,EUR_USD,GBP_AUD,GBP_CAD,GBP_CHF,GBP_JPY,GBP_NZD,GBP_USD,NZD_CAD,NZD_CHF,NZD_JPY,NZD_USD,NZD_USD,USD_CAD,USD_CHF,USD_JPY' } #params = {'instruments':'AUD_NZD,AUD_USD,EUR_AUD,EUR_GBP,EUR_USD,GBP_USD,NZD_USD,USD_CAD,USD_CHF'} price = PricingStream(accountID=accountID, params=params) ohlcd = {'count': 163, 'granularity': 'H1'} buyTrades = {} sellTrades = {} ma30 = {} ma50 = {} ma100 = {} atr = {} class Breakout(): def prepare(): for symbol in symbols: candle = InstrumentsCandles(instrument=symbol, params=ohlcd)