class CGActualHandler(CSHandler): def get(self): apikey = self.request.get('apikey', None) self.record_stat(apikey, 'cg-actual') date_from = self.request.get('from', None) date_to = self.request.get('to', None) debug = self.request.get('debug', False) ltg_threshold = self.request.get('ltg_threshold', False) error = False errmsg = "" buys = [] sells = [] if not (apikey or date_from or date_to): template = JENV.get_template('cg-actual.html') self.response.write(template.render({'cgs': None, 'error': False})) return if not (apikey and date_from and date_to): template = JENV.get_template('cg-actual.html') self.response.write( template.render({ 'cgs': None, 'error': True, 'errmsg': "All form fields are mandatory" })) return try: buys = self.fetch_buys(apikey) sells = self.fetch_sells(apikey) error = False except Exception, e: error = True logging.error(str(e)) errmsg = str(e) p = Portfolio(buys, sells, ltg_threshold=int(ltg_threshold)) dt = datetime.strptime(date_from, "%Y-%m-%d") from_ts = ts_ms_from_dt(dt) dt = datetime.strptime(date_to, "%Y-%m-%d") to_ts = ts_ms_from_dt(dt) + cg.ONEDAY cgs = p.cg(from_ts, to_ts) result = {'cgs': cgs, 'cgmod': cg, 'error': error, 'errmsg': errmsg} if debug: self.response.headers['Content-Type'] = 'application/json' self.response.out.write(jsonpickle.encode(result)) else: template = JENV.get_template('cg-actual.html') self.response.write(template.render(result))
def gen_buy (nbuy, day): rate = random.randint(RATE_MIN, RATE_MAX) fiat = long(rate * (BUY_VOL / cg.SATOSHIS_F)) return { "time": cg.ts_ms_from_dt(day), "vol": BUY_VOL, "rate": rate, "orderID": "test_buy_order_%03d" % nbuy, "fiat": fiat, "tradeID": "test_buy_trade_%03d" % nbuy, "rateSpecified": rate }
def gen_buy(nbuy, day): rate = random.randint(RATE_MIN, RATE_MAX) fiat = long(rate * (BUY_VOL / cg.SATOSHIS_F)) return { "time": cg.ts_ms_from_dt(day), "vol": BUY_VOL, "rate": rate, "orderID": "test_buy_order_%03d" % nbuy, "fiat": fiat, "tradeID": "test_buy_trade_%03d" % nbuy, "rateSpecified": rate }
def gen_sell (nsell, day, total_buy_vol, total_sell_vol): ## FIXME: To fix thes balance = total_buy_vol - total_sell_vol vol = random.randint(1, balance/SELL_SATOSHI_MUL) * SELL_SATOSHI_MUL rate = random.randint(RATE_MIN, RATE_MAX) fiat = long(rate * (vol / cg.SATOSHIS_F)) return { "time": cg.ts_ms_from_dt(day), "vol": vol, "rate": rate, "orderID": "test_sell_order_%03d" % nsell, "fiat": fiat, "tradeID": "test_sell_trade_%03d" % nsell, "rateSpecified": rate }
def gen_sell(nsell, day, total_buy_vol, total_sell_vol): ## FIXME: To fix thes balance = total_buy_vol - total_sell_vol vol = random.randint(1, balance / SELL_SATOSHI_MUL) * SELL_SATOSHI_MUL rate = random.randint(RATE_MIN, RATE_MAX) fiat = long(rate * (vol / cg.SATOSHIS_F)) return { "time": cg.ts_ms_from_dt(day), "vol": vol, "rate": rate, "orderID": "test_sell_order_%03d" % nsell, "fiat": fiat, "tradeID": "test_sell_trade_%03d" % nsell, "rateSpecified": rate }
class CGProjHandler(CSHandler): ## FIXME: Lot of code duplication betwen this and the CGActual ## handler. ... def get(self): apikey = self.request.get('apikey', None) self.record_stat(apikey, 'cg-proj') sell_qty = long(self.request.get('sell_qty', 0)) price = float(self.request.get('sell_price', 0.0)) sell_price = long(price * 100) # Paisa debug = self.request.get('debug', False) ltg_threshold = self.request.get('ltg_threshold', False) error = False errmsg = "" buys = [] sells = [] if not apikey: template = JENV.get_template('cg-proj.html') self.response.write(template.render({'cgs': None, 'error': False})) return if not (apikey and sell_qty and sell_price): template = JENV.get_template('cg-proj.html') self.response.write( template.render({ 'cgs': None, 'error': True, 'errmsg': "All form fields are mandatory" })) return try: buys = self.fetch_buys(apikey) sells = self.fetch_sells(apikey) error = False except Exception, e: error = True logging.error(str(e)) errmsg = str(e) p = Portfolio(buys, sells, ltg_threshold=int(ltg_threshold)) dt = datetime(1980, 01, 01) from_ts = ts_ms_from_dt(dt) dt = datetime.now() to_ts = ts_ms_from_dt(dt) cgs = p.cg(from_ts, to_ts) if p.end_balance < sell_qty: error = True errmsg = ("Cannot sell %f BTC as your current balance (%f BTC) " + "is insufficient.") % (sell_qty / cg.SATOSHIS_F, p.end_balance / cg.SATOSHIS_F) else: p.gen_sells(sell_qty, sell_price, to_ts - 1) cgs = p.cg(to_ts - 2, to_ts) result = {'cgs': cgs, 'cgmod': cg, 'error': error, 'errmsg': errmsg} if debug: self.response.headers['Content-Type'] = 'application/json' self.response.out.write(jsonpickle.encode(result)) else: template = JENV.get_template('cg-proj.html') self.response.write(template.render(result))