Ejemplo n.º 1
0
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()
Ejemplo n.º 2
0
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")
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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()
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
def deletecall(symbol, userid, chatid):
    logger.info('Deleting call... ' + symbol)
    rowcount = Calls.delete().where((Calls.sym == symbol)
                                    & (Calls.chatid == chatid)).execute()
    return rowcount
Ejemplo n.º 11
0
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()
Ejemplo n.º 12
0
def deletependingportfolio(chatid):
    Calls.delete().where((Calls.chatid == str(chatid))
                         & (Calls.qty == 0)).execute()
Ejemplo n.º 13
0
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()