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 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()