Пример #1
0
def client_orders(Req, User_id, Title):
    Dict = {}

    Current = None
    try:
        Current = TradePairs.objects.get(url_title=Title)
    except:
        return json_false500(Req)



    Dict["auth"] = True
    MyOrders = OrdersMem.objects.fitler(user = User_id,
                                        trade_pair = Current,
                                        status='processing')

    MyOrdersList = []
    c = getcontext()
    c.prec = settings.TRANS_PREC

    for i in MyOrders:
        MyOrdersDict = {}
        MyOrdersDict["pub_date"] = formats.date_format(i.pub_date, "DATETIME_FORMAT")
        MyOrdersDict["id"] = i.id
        MyOrdersDict["sum1"] = str(i.sum1)

        if i.currency1 == Current.currency_on.id:
            MyOrdersDict["type"] = "sell"
            MyOrdersDict["price"] = format_numbers10(i.price)
            MyOrdersDict["amnt_trade"] = format_numbers10(i.sum1)
            MyOrdersDict["amnt_base"] = format_numbers10(i.sum1*i.price)
        else:
            MyOrdersDict["type"] = "buy"
            MyOrdersDict["price"] = format_numbers10(i.price)
            MyOrdersDict["amnt_base"] = format_numbers10(i.sum1)
            MyOrdersDict["amnt_trade"] = format_numbers10(i.sum1/i.price)
        MyOrdersList.append(MyOrdersDict)

    balance_sell = get_account(user_id=User_id, currency=Current.currency_on)
    balance_buy = get_account(user_id=User_id, currency=Current.currency_from)
    Dict["balance_buy"] = format_numbers_strong(balance_buy.balance)
    Dict["balance_sell"] = format_numbers_strong(balance_sell.balance)
    Dict["your_open_orders"] = MyOrdersList
    RespJ = json.JSONEncoder().encode(Dict)
    return RespJ
Пример #2
0
def ask(Req, UrlTitle):
    CurrentTradePair = TradePairs.objects.get(url_title=UrlTitle)
    SumList = []
    Amount = Decimal("0")
    TempSum = Decimal('0')
    try:
        Amount = Decimal(Req.REQUEST.get("amount", None))
        Query = "SELECT * FROM main_ordersmem  WHERE  currency1=%i AND currency2=%i \
                          AND status='processing'  \
                          AND user!=%i  ORDER BY price DESC" % (
            CurrentTradePair.currency_on.id,
            CurrentTradePair.currency_from.id,
            Req.user.id)
        List = OrdersMem.objects.raw(Query)
        for item in List:
            if Amount > item.sum1:
                Amount -= item.sum1
                TempSum += item.sum1
                SumList.append({"sum": item.sum1, "price": item.price})
            else:
                TempSum += Amount
                SumList.append({"sum": Amount, "price": item.price})
                break
    except:
        Response = HttpResponse('{"status":false, "description":"amount is incorrect"}')
        Response['Content-Type'] = 'application/json'
        return Response
        # format_numbers_strong(balance_buy.balance )

    AvaragePrice = Decimal("0")
    BuySum = Decimal("0")
    for item in SumList:
        BuySum += item['sum']
        AvaragePrice += ((item['sum'] / TempSum) * item['price'] )
    Dict = {"buy_sum": format_numbers_strong(BuySum),
            "price": format_numbers_strong(AvaragePrice),
            "status": True}
    RespJ = json.JSONEncoder().encode(Dict)
    return cached_json_object(RespJ)
Пример #3
0
def make_auto_trade(OrderSell, TradePair, Price, Currency1, Sum1, Currency2):
    # if we sell
    # Query = "SELECT * FROM main_ordersmem  WHERE  trade_pair_id=%i" % (TradePair.id)

    if int(TradePair.currency_on.id) == int(Currency1.id):
        Query = "SELECT * FROM main_ordersmem  WHERE  currency1=%i AND trade_pair.id=%i \
                           AND status='processing' AND price >= %s  \
                           AND user!=%i  ORDER BY price DESC, id DESC" % (Currency2.id,
                                                                          TradePair.id,
                                                                          format_numbers_strong(Price), OrderSell.user)
    else:
        Query = "SELECT * FROM main_ordersmem WHERE  currency1=%i AND trade_pair=%i \
                           AND status='processing' AND price <= %s \
                           AND user!=%i  ORDER BY price, id DESC " % (Currency2.id,
                                                                      TradePair.id,
                                                                      format_numbers_strong(Price), OrderSell.user )

    List = OrdersMem.objects.raw(Query)
    # ##work on first case
    # CommissionSell = get_account(settings.COMISSION_USER, Currency1)
    # ComnissionBuy = get_account(settings.COMISSION_USER, Currency2)
    AccumSumToSell = Sum1
    AccountBuyer = get_account(user_id=Order.user, currency_id=Currency2.id)
    UserDeals = [int(Order.user)]
    process_order = None

    if TradePair.currency_on.id == Currency1.id :
        process_order = lambda AccountBuyer, AccumSumToSell, OrderBuy, OrderSell, TradePair: process_order_sell(AccountBuyer, AccumSumToSell, OrderBuy, OrderSell, TradePair)
    else:
        process_order = lambda AccountBuyer, AccumSumToSell, OrderBuy, OrderSell, TradePair: process_order_buy(AccountBuyer, AccumSumToSell, OrderBuy, OrderSell, TradePair)

    # TODO in case of exception block OrderSell and OrderBuy and interrupt the cycle
    for OrderBuy in List:
        UserDeals.append(int(OrderBy.user))

        try :
            AccumSumToSell = process_order(AccountBuyer, AccumSumToSell, OrderBuy, OrderSell, TradePair)
        except TransError as e:
            OrderBuy.status = "core_error"
            OrderSell.status = "core_error"
            OrderBuy.save()
            OrderSell.save()
            admin_system_notify_async((OrderBuy, OrderSell))
            ResultSum = finish_create_order(TradePair, AccumSumToSell, OrderSell)
            return {"start_sum": Sum1, "status":False, "last_sum": ResultSum, "users_bothered": UserDeals}



        if AccumSumToSell > 0.00000001:
            continue
        else:
            break

    ResultSum = finish_create_order(TradePair, AccumSumToSell, OrderSell)
    Order.sum1 = AccumSumToSell

    if ResultSum < 0.00000001:
         return_rest2acc(Order, AccumSumToSell)
         Order.sum1 = 0
         Order.make2processed()
    else:
         Order.save()

    return {"start_sum": Sum1, "status":True, "last_sum": ResultSum, "users_bothered": UserDeals}