def createorupdateportfolio(sym, state, chatid, qty, querysymbol, price=None): call = getCall(sym, "PORTFOLIO", chatid) if call: oldprice = float(call.callrange) if call.callrange else '0' oldqty = call.qty qty = int(qty) if price is not None: price = float(price) newqty = qty + oldqty newprice = (oldprice * oldqty + price * qty) / newqty if price != 0 else oldprice call.callrange = "{:.2f}".format(newprice) call.qty = newqty call.time = datetime.datetime.now() call.chatid = chatid call.misc = str(state) call.querysymbol = querysymbol call.save() return call else: Calls.insert(sym=sym, callrange=(str(price) if price else '0'), qty=int(qty), chatid=chatid, type=PORTFOLIO_TYPE, querysymbol=querysymbol, misc=str(state)).execute()
def getCall(sym, type, chatid): try: call = Calls.get(Calls.sym == sym, Calls.type == type, Calls.chatid == chatid) return call except DoesNotExist as de: logger.error("Not exists")
def getportfolio(chatid): portfolio = list() for call in Calls.select().where((Calls.chatid == chatid) & (Calls.type == PORTFOLIO_TYPE) & (Calls.qty > 0)): portfolio.append(call) return portfolio
def getwatchlist(chatid): watchlist = list() for call in Calls.select().where((Calls.chatid == chatid) & (Calls.type == WATCH_TYPE) & (Calls.sym.is_null(False))): watchlist.append(call) return watchlist
def lastupdatedportfolio(chatid): try: call=Calls.select()\ .where((Calls.chatid==chatid) & (Calls.type==PORTFOLIO_TYPE) & (Calls.misc==str(PORTFOLIO_STATE_PENDING)))\ .order_by(Calls.time.desc()).get() except DoesNotExist as de: return None return call
def createcall(type, symbol, user, chatid, userid, callrange=None, misc=None, desc=None, querysymbol=None): Calls.insert(sym=symbol, type=type, callrange=callrange, querysymbol=querysymbol, chatid=chatid, user=user, userid=userid, misc=misc, desc=desc).upsert().execute()
def getcalls(chatid, symbol=None): clauses = [(Calls.chatid == chatid), (Calls.type.not_in([WATCH_TYPE, PORTFOLIO_TYPE]))] if symbol: clauses.append((Calls.sym == symbol)) callstxt = '' for call in Calls.select().where(reduce(operator.and_, clauses)): callstxt += call.user + " : " + call.type + " " + call.sym + "@" + call.callrange + \ " " +(call.desc if call.desc else " ")+ \ " on <i>" + call.time.strftime('%b %d %H:%M') + "</i>\n" return callstxt
def deleteportfolio(symbol, chatid): logger.info('Deleting portfolio... ' + symbol) rowcount = Calls.delete().where((Calls.sym == symbol) & ( Calls.chatid == chatid) & (Calls.type == PORTFOLIO_TYPE)).execute() return rowcount
def deletewatchlist(symbol, chatid): logger.info('Deleting watchlist... ' + symbol) rowcount = Calls.delete().where((Calls.sym == symbol) & (Calls.chatid == chatid) & (Calls.type == WATCH_TYPE)).execute() return rowcount
def deletecall(symbol, userid, chatid): logger.info('Deleting call... ' + symbol) rowcount = Calls.delete().where((Calls.sym == symbol) & (Calls.chatid == chatid)).execute() return rowcount
def deleteoldwatchlist(): calls = Calls.select(Calls.time).where(Calls.type == WATCH_TYPE).order_by( Calls.time.desc()).limit(LIMIT) Calls.delete().where((Calls.type == WATCH_TYPE) & (Calls.time.not_in(calls))).execute()
def deletependingportfolio(chatid): Calls.delete().where((Calls.chatid == str(chatid)) & (Calls.qty == 0)).execute()
def deleteoldcalls(): calls = Calls.select(Calls.time).where( (Calls.type.not_in([WATCH_TYPE, PORTFOLIO_TYPE ]))).order_by(Calls.time.desc()).limit(LIMIT) Calls.delete().where((Calls.type.not_in([WATCH_TYPE, PORTFOLIO_TYPE])) & (Calls.time.not_in(calls))).execute()