Ejemplo n.º 1
0
    def ensure_profits(cls, user):
        """ 确保生成收益日志 """
        log.info('为用户{}生成收益日志'.format(user))
        ts = list(reversed(Transaction.user_recent_transactions(user)))
        if ts:
            today = datetime.utcnow() + timedelta(hours=8)
            today = today.replace(hour=0, minute=0, second=0, microsecond=0)
            di = 0
            date = ts[di].operated_at
            positions = {}
            realized_profits = defaultdict(float)
            while date <= today:
                profit = 0
                # include new transactions
                while di < len(ts) and ts[di].operated_at <= date:
                    t = ts[di]
                    op = 1 if t.type_ == 'buy' else -1
                    pair = (t.exchange, t.symbol)
                    if pair not in positions:
                        positions[pair] = (t.price, t.quantity * op)
                    else:
                        pv = positions[pair]
                        amount = pv[0] * pv[1] + t.price * t.quantity * op
                        quantity = pv[1] + t.quantity * op
                        if quantity == 0:
                            realized_profits[pair] += pv[1] * (t.price - pv[0])
                            del positions[pair]
                        else:
                            positions[pair] = (amount / quantity, quantity)
                    di += 1

                # calculate profit
                for pair in positions:
                    q = Quote.query_one(
                        {
                            'exchange': pair[0],
                            'symbol': pair[1],
                            'quote_type': '1d',
                            'quote_at': {
                                '$lte': date
                            }
                        },
                        sort=[('quote_at', -1)])
                    if q:
                        pv = positions[pair]
                        profit += (q.close - pv[0]) * pv[1]

                profit += sum(realized_profits.values())

                # update profit
                cls.update_one({
                    'date': date,
                    'user': user
                }, {'$set': {
                    'profit': profit
                }},
                               upsert=True)
                date += timedelta(days=1)
Ejemplo n.º 2
0
Archivo: sms.py Proyecto: sopnic/ybk
def send_sms_yunpian(mobile, text):
    log.info('向 {} 发送 "{}"'.format(mobile, text))
    url = "http://yunpian.com/v1/sms/send.json"
    conf = current_app.config
    data = {"apikey": conf.get("yunpian_apikey", ""), "mobile": mobile, "text": text}
    r = requests.post(url, data=data).json()
    log.info("...发送结果为 {}".format(r))
    if r["code"] == 0:
        r["status"] = 200
    else:
        r["status"] = 500
    r["reason"] = r["msg"]
    return r
Ejemplo n.º 3
0
    def ensure_profits(cls, user):
        """ 确保生成收益日志 """
        log.info('为用户{}生成收益日志'.format(user))
        ts = list(reversed(Transaction.user_recent_transactions(user)))
        if ts:
            today = datetime.utcnow() + timedelta(hours=8)
            today = today.replace(hour=0, minute=0, second=0, microsecond=0)
            di = 0
            date = ts[di].operated_at
            positions = {}
            realized_profits = defaultdict(float)
            while date <= today:
                profit = 0
                # include new transactions
                while di < len(ts) and ts[di].operated_at <= date:
                    t = ts[di]
                    op = 1 if t.type_ == 'buy' else -1
                    pair = (t.exchange, t.symbol)
                    if pair not in positions:
                        positions[pair] = (t.price, t.quantity * op)
                    else:
                        pv = positions[pair]
                        amount = pv[0] * pv[1] + t.price * t.quantity * op
                        quantity = pv[1] + t.quantity * op
                        if quantity == 0:
                            realized_profits[pair] += pv[1] * (t.price - pv[0])
                            del positions[pair]
                        else:
                            positions[pair] = (amount / quantity, quantity)
                    di += 1

                # calculate profit
                for pair in positions:
                    q = Quote.query_one({'exchange': pair[0],
                                         'symbol': pair[1],
                                         'quote_type': '1d',
                                         'quote_at': {'$lte': date}},
                                        sort=[('quote_at', -1)])
                    if q:
                        pv = positions[pair]
                        profit += (q.close - pv[0]) * pv[1]

                profit += sum(realized_profits.values())

                # update profit
                cls.update_one({'date': date, 'user': user},
                               {'$set': {'profit': profit}},
                               upsert=True)
                date += timedelta(days=1)
Ejemplo n.º 4
0
Archivo: sms.py Proyecto: maocis/ybk
def send_sms_yunpian(mobile, text):
    log.info('向 {} 发送 "{}"'.format(mobile, text))
    url = 'http://yunpian.com/v1/sms/send.json'
    conf = current_app.config
    data = {
        'apikey': conf.get('yunpian_apikey', ''),
        'mobile': mobile,
        'text': text,
    }
    r = requests.post(url, data=data).json()
    log.info('...发送结果为 {}'.format(r))
    if r['code'] == 0:
        r['status'] = 200
    else:
        r['status'] = 500
    r['reason'] = r['msg']
    return r