Exemple #1
0
def return_rest2acc(Order, AccumSumToSell):
    Account2Sell = get_account(user=Order.user, currency=Order.currency1)
    add_trans2(Account2Sell,
               AccumSumToSell,
               Order,
               "deal_return")
    system_notify(order_return_unused(Order, Account2Sell.currency(), AccumSumToSell), Order.user)
Exemple #2
0
def __inner_remove_order(Order, User):
    Order2Remove = OrdersMem.objects.get(user=User.id, id=int(Order), status="processing")
    if not Order2Remove.verify(str(User.id)) :
        return False

    Market = TradePairs.objects.get(id=Order2Remove.trade_pair)

    Order2Remove.status = "canceled"
    Order2Remove.save()
    Title = Market.url_title

    LOCK = "trades" + Title
    TradeLock = my_lock(LOCK)

    try:
        Account = get_account(user=User, currency_id=Order2Remove.currency1)

        add_trans2(Account,
                   Order2Remove.sum1,
                   Order2Remove.currency1,
                   Order2Remove,
                   "order_cancel")

        cache = caching()
        cache.delete_many(["buy_list_" + Title,
                           "sell_list_" + Title,
                           "balance_" + str(User.id),
                           'client_orders_' + str(User.id) + "_" + Title])

        my_release(TradeLock)
        return True
    except:

        my_release(TradeLock)
        return False
Exemple #3
0
def buy(Req, Trade_pair):
    FreqKey = "orders" + str(Req.user.id)
    Start = time.time()
    if not check_freq(FreqKey, 3):
        Response = HttpResponse('{"status":false}')
        Response['Content-Type'] = 'application/json'
        return Response

    getcontext().prec = settings.TRANS_PREC
    try:
        Count = Req.REQUEST.get("count")
        Price = Req.REQUEST.get("price")
        Count = Decimal(Count.replace(",", ".").strip())
        Price = Decimal(Price.replace(",", ".").strip())
        Count = to_prec(Count, settings.TRANS_PREC)
        Price = to_prec(Price, settings.TRANS_PREC)

    except:
        Response = HttpResponse(process_mistake(Req, "invalid_params"))
        Response['Content-Type'] = 'application/json'
        return Response

    if Price <= 0:
        Response = HttpResponse(process_mistake(Req, "SumLess0"))
        Response['Content-Type'] = 'application/json'
        return Response

    if Count <= 0:
        Response = HttpResponse(process_mistake(Req, "CountLess0"))
        Response['Content-Type'] = 'application/json'
        return Response

    TradePair = TradePairs.objects.get(url_title=Trade_pair)
    LOCK = "trades" + TradePair.url_title

    if TradePair.min_trade_base > Count:
        Response = HttpResponse(process_mistake(Req, "MinCount"))
        Response['Content-Type'] = 'application/json'
        return Response

    Custom = "0.0005"  # Req.session["deal_comission"]
    Comission = Decimal(Custom)

    CurrencyOnS = Req.REQUEST.get("currency")
    CurrencyBaseS = Req.REQUEST.get("currency1")

    Amnt1 = Price * Count
    Amnt2 = Count

    CurrencyBase = Currency.objects.get(title=CurrencyBaseS)
    CurrencyOn = Currency.objects.get(title=CurrencyOnS)

    TradeLock = my_lock(LOCK)
    order = OrdersMem(user=Req.user.id,
                      currency1=CurrencyBase.id,
                      sum1_history=Amnt1,
                      price=Price,
                      sum1=Decimal("0.0"),
                      trade_pair=TradePair.id,
                      comission=Comission,
                      status="created")
    order.save()
    i = order.id
    try:
        FromAccount = get_account(user=Req.user, currency=CurrencyBase)
        system_notify_async(deposit_funds(order, CurrencyBase), Req.user.id)
        # TODO Order to Encrypted object
        add_trans2(FromAccount, Amnt1, CurrencyBase, order, "deposit")
        order.status = "processing"
        order.save()
        ResAuto = make_auto_trade(order, TradePair, order.price, CurrencyBase, Amnt1, CurrencyOn)
        Response = HttpResponse(process_auto(ResAuto, TradePair))
        my_release(TradeLock)
        Response['Content-Type'] = 'application/json'
        End = time.time()
        measure = OrderTimer(order=i, time_work=str(End - Start), error="")
        measure.save()

        return Response
    except TransError as e:


        order.status = "canceled"
        order.save()
        Status = e.value
        Response = HttpResponse(process_mistake(Req, Status))
        Response['Content-Type'] = 'application/json'
        my_release(TradeLock)
        End = time.time()
        tb = traceback.format_exc()
        measure = OrderTimer(order=i, time_work=str(End - Start), error=tb)
        measure.save()
        return Response
Exemple #4
0
def process_order_sell(AccountSeller,  AccumSumToSell, OrderBuy, OrderSell, TradePair):
    ## TODO move to settings for every user
    logging.debug("=========================================================================================")
    logging.debug(OrderSell)
    logging.debug("%s" % (AccumSumToSell))
    logging.debug(OrderBuy)
    logging.debug("=========================================================================================")

    if not item.verify(str(OrderBuy.user)):
        logging.debug("Sign FAILED %s" % str(OrderBuy))
        return  AccumSumToSell
    # 7000/3600 = 1.9 BTC
    OrderBuySum = OrderBuy.sum1/OrderBuy.price
    if OrderBuySum > AccumSumToSell:
        ## a danger of low overflow
        TransSum = AccumSumToSell*OrderBuy.price
        AccountBuyer = get_account(user=OrderBuy.user, currency=OrderSell.currency1)
        ##comission
        add_trans2(AccountBuyer,
                   AccumSumToSell*-1,
                   OrderSell.currency1,
                   OrderSell,
                   "deal")

        add_trans2(AccountSeller,
                   TransSum*-1,
                   OrderBuy.currency1,
                   OrderBuy,
                   "deal")

        try:
            system_notify_async(order_description_sell(AccumSum, TransSum, OrderBuy, TradePair),
                                AccountSeller.get_user())
            system_notify_async(order_description_buy(TransSum, AccumSum, Order, item, TradePair),
                                AccountBuyer.get_user())
        except:
            logging.info("something gooing wrong with notification")
            pass

        OrderBuy.make2processed()
        return 0

    if OrderBuySum <= AccumSumToSell:
        TransSum = OrderBuy.sum1
        AccountBuyer = get_account(user=OrderBuy.user, currency=OrderSell.currency1)
        ##comission
        add_trans2(AccountBuyer,
                   OrderBuySum*-1,
                   OrderSell.currency1,
                   OrderSell,
                   "deal")

        add_trans2(AccountSeller,
                   TransSum*-1,
                   OrderBuy.currency1,
                   OrderBuy,
                   "deal")
        try:
            system_notify_async(order_description_sell(NotifySum, TransSum, item, TradePair), AccountSeller.get_user())
            system_notify_async(order_description_buy(TransSum, NotifySum, Order, item, TradePair),
                                AccountBuyer.get_user())
            system_notify_async(order_finish(item), AccountSeller.get_user())

        except:
            logging.info("somthing gooing wrong with notification")
            pass
        return AccumSumToSell - OrderBuySum