def __init__(self, instrument, granularity, units, clargs): self.accountID, token = exampleAuth() self.client = API(access_token=token) self.units = units self.clargs = clargs self.pt = PriceTable(instrument, granularity) mavgX = MAx(self.pt, clargs.shortMA, clargs.longMA, clargs.SOK, clargs.SOD, clargs.SOF) self.pt.setHandler("onAddItem", mavgX.calculate) self.indicators = [mavgX] self.state = NEUTRAL # overall state based on calculated indicators # fetch initial historical data params = {"granularity": granularity, "count": self.clargs.longMA} r = instruments.InstrumentsCandles(instrument=instrument, params=params) rv = self.client.request(r) # and calculate indicators for crecord in rv['candles']: if crecord['complete'] is True: # def addItem(self, dt, ao, ah, al, ac, av, bo, bh, bl, bc, bv): self.pt.addItem(crecord['time'], float(crecord['mid']['o']), float(crecord['mid']['h']), float(crecord['mid']['l']), float(crecord['mid']['c']), int(crecord['volume']), float(crecord['mid']['o']), float(crecord['mid']['h']), float(crecord['mid']['l']), float(crecord['mid']['c']), int(crecord['volume'])) self._botstate()
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
setattr(header_text, 'struw', _header_text) urwid.AttrMap(header_text, 'titlebar') return header_text if __name__ == "__main__": from console.config import Config low = OrderedDict() # Queues Q_PRICE = Queue() # Price queue Q_NAV = Queue() # Net Asset Value queue accountID, access_token = exampleAuth() cfg = Config() api = API(access_token=access_token, environment="practice") # list of widgets x = 0 loIw = OrderedDict() for G in cfg.instrument_groups(): groupName, groupItems = G for t in groupItems: loIw.update({t: urwid.Text(('streak', t))}) setattr(loIw[t], "instrument", t) loIw.update({"D%d" % x: urwid.Divider('-')}) x += 1
# -*- coding: utf-8 -*- """retrieve the tradable instruments for account.""" import json import oandapyV20 import oandapyV20.endpoints.accounts as accounts from exampleauth import exampleAuth accountID, token = exampleAuth() client = oandapyV20.API(access_token=token) r = accounts.AccountInstruments(accountID=accountID) rv = client.request(r) print(json.dumps(rv, indent=2))