Ejemplo n.º 1
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
Ejemplo n.º 2
0
def __inner_remove_order(Order, User):
    Order2Remove = Orders.objects.get(user=User,
                                      id=int(Order),
                                      status="processing")
    Title = Order2Remove.trade_pair.url_title
    LOCK = "trades" + Title
    TradeLock = my_lock(LOCK)

    try:
        Order2Remove.status = "canceled"
        Order2Remove.save()
        Account = Accounts.objects.get(user=User,
                                       currency=Order2Remove.currency1)
        add_trans(Order2Remove.transit_1, Order2Remove.sum1,
                  Order2Remove.currency1, Account, 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
Ejemplo n.º 3
0
def __inner_remove_order(Order, User):
    Order2Remove  = Orders.objects.get(user = User, id = int(Order), status="processing" )
    Title =  Order2Remove.trade_pair.url_title
    LOCK = "trades" + Title
    TradeLock = my_lock(LOCK)

    try :
       Order2Remove.status = "canceled"
       Order2Remove.save()
       Account = Accounts.objects.get(user = User, currency = Order2Remove.currency1)
       add_trans(Order2Remove.transit_1, 
                 Order2Remove.sum1, 
                 Order2Remove.currency1,
                 Account,
                 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
Ejemplo n.º 4
0
  def handle(self, *args, **options):
        LOCK = "merged_command_"
        
        try:
               lock = my_lock(LOCK)
#		*  *  *   *   *     cd crypton;python ./manage_lock.py clean_small_orders btc_uah >> clean_small.log
#*  *  *   *   *     cd crypton;python ./manage_lock.py clean_small_orders ltc_uah >> clean_small.log
#*  *  *   *   *     cd crypton;python ./manage_lock.py clean_small_orders nvc_uah >> clean_small.log
#*/1  *  *   *   *   cd crypton;python ./manage_lock.py clear_online >> clear_online.log
#*/2  *  *   *   *     cd crypton;python ./manage_lock.py stock_stat  1>>stock_stat.log 2>>stock_stat.log
#*/3   *  *   *   *     cd crypton;python ./manage.py top_prices >> top_prices.log
	       call_command('uah_balance')
	       call_command('liqapy_async')
	       call_command('async_p24_one_payment')

	       call_command('clear_online')
	       call_command('top_prices')
	       call_command('stock_stat')
	       call_command('clean_small_orders', 'btc_uah')
	       call_command('clean_small_orders', 'ltc_uah')
	       call_command('clean_small_orders', 'nvc_uah')
	       call_command('clean_small_orders', 'doge_uah')
	       call_command('rates_uah_usd')	
	
        except LockBusyException as e:
               print "operation is locked", e.value
        except:
               print "Unexpected error:", str(sys.exc_info())
        finally:
	        my_release(lock)
Ejemplo n.º 5
0
    def handle(self, *args, **options):

        LOCK = "p2p_lock_auto"
        lock = my_lock(LOCK)
        try:
            process_command()
            my_release(lock)
        except:
            print "Unexpected error:", sys.exc_info()[0]
Ejemplo n.º 6
0
 def handle(self, *args, **options):
     
     LOCK = "partners_pay"
     LOCK = LOCK
     lock = my_lock(LOCK)
     try:
         process_partners_program()
     except :
         print "Unexpected error:", sys.exc_info()[0]    
     
     my_release(lock)
Ejemplo n.º 7
0
    def handle(self, *args, **options):
	
        LOCK = "p2p_lock"
        lock = my_lock(LOCK)
                
            #try :
        if not check_global_lock():
            print "start process"
            process_command()
            my_release(lock)                       
        else:
            print "global check" 
Ejemplo n.º 8
0
    def handle(self, *args, **options):

        LOCK = "check_crypto"
        lock = None
        try:
            lock = my_lock(LOCK)
            process()
        except LockBusyException as e:
            print "operation is locked", e.value
            sys.exit(0)
        except:
            print "Unexpected error:", str(sys.exc_info())
        finally:
            my_release(lock)
Ejemplo n.º 9
0
    def handle(self, *args, **options):
        
	LOCK = "out_crypto"
	lock = None
	try:
        	lock = my_lock(LOCK)
		for CurrencyTitle in CryptoSettings.keys():
			if CurrencyTitle != "BTC":
       			     process_out(CurrencyTitle)
		my_release(lock)
	except LockBusyException as e:
               print "operation is locked", e.value
	       sys.exit(0)
	except:
	       print "Unexpected error:", str(sys.exc_info())
Ejemplo n.º 10
0
 def handle(self, *args, **options):
       URL_TITLE = args[0]
       LOCK = "small_orders"
       LOCK +=  URL_TITLE
       lock = my_lock(LOCK)
       
       try:
              lock = my_lock(LOCK)
              process_small_orders(URL_TITLE)
       except LockBusyException as e:
              print "operation is locked", e.value
       except:
              print "Unexpected error:", str(sys.exc_info())
       
       my_release(lock)
Ejemplo n.º 11
0
 def handle(self, *args, **options):
     Url = args[0]
     Type = args[1]
     
     socket.setdefaulttimeout(10)
     Interval = int(args[2])
     LOCK = "btce"
     LOCK = LOCK + Type
     Lock = my_lock(LOCK)
     try:
             Decoder = json.JSONDecoder()
             Last = btce_trade_stat_minute_usd.objects.all().aggregate(Max('btc_tid'))
             #Last{'unixtime__max': None}
             if Last["btc_tid__max"] is None:
                Last = 0
             else: 
                Last = Last["btc_tid__max"]
             bulk_add  = []
     
             D = urllib2.urlopen(Url, timeout=10)
             Str = D.read()
     
             Res = Decoder.decode(Str)
             Res.reverse()
             
             for i in   Res:
                     
                     if  i['tid']>Last :
                             Last = i['tid']   
                             generated_date = datetime.fromtimestamp(int(i['date']))    
                             bulk_add.append( btce_trade_stat_minute_usd(unixtime = i['date'],
                                                                     amount = i['amount'],
                                                                     price = i['price'],
                                                                     ask_bid = i['trade_type'],
                                                                     datetime = generated_date,
                                                                     btc_tid = Last,
                                                                     stock_type = Type
                                                                     ) )
                             
             btce_trade_stat_minute_usd.objects.bulk_create(bulk_add)          
             d = len(bulk_add)
             bulk_add  = []   
             print "add %i" % (d)
     except :
             print "Unexpected error:", sys.exc_info()[0]       
     my_release(Lock)    
     sys.exit(0)
Ejemplo n.º 12
0
    def handle(self, *args, **options):
 	CurrencyTitle = args[0]    
        
        try :
              Time = args[1]    
              Time = int(Time)
        except :
              Time = 0  

        LOCK = "in_cryptoblck_info"
        LOCK = LOCK + CurrencyTitle
        lock = my_lock(LOCK)
        try:
            process_in_crypto(Time, CurrencyTitle)
        except :
        print "Unexpected error:", sys.exc_info()[0]	
        my_release(lock)
Ejemplo n.º 13
0
 def handle(self, *args, **options):
     CurrencyTitle = args[0]
     try:
         Time = args[1]
         Time = int(Time)
     except:
         Time = 0
     print "process %s time %i" % (CurrencyTitle, Time)
     LOCK = "in_cryptoblck_info"
     LOCK = LOCK + CurrencyTitle
     lock = None
     try:
         lock = my_lock(LOCK)
         process_in_crypto(Time, CurrencyTitle)
     except:
         print "Unexpected error:", sys.exc_info()[0]
     finally:
         my_release(lock)
Ejemplo n.º 14
0
    def handle(self, *args, **options):
        CurrencyTitle = args[0]    
        
        try :
              Time = int(Time)
        except :
              Time = 0  

	LOCK = "out_crypto"
	LOCK +=  CurrencyTitle
	lock = None
	#try:
        lock = my_lock(LOCK)
        process_out(CurrencyTitle)
	#except LockBusyException as e:
         #      print "operation is locked", e.value
#	except:
#	       print "Unexpected error:", str(sys.exc_info())
	my_release(lock)
Ejemplo n.º 15
0
    def handle(self, *args, **options):
        
        Time =  0   
        try :
              Time = int(args[0])
        except :
              Time = 0  
        print "from %i " % (Time)
        LOCK = "in_cryptoblck_info"
#	lock = None
#	try:
 #               lock = my_lock(LOCK)        
        process_block_info(Time)
	
#	except LockBusyException as e:
 #              print "operation is locked", e.value
#	except :
#		print "Unexpected error:",str( sys.exc_info())

        my_release(lock)
Ejemplo n.º 16
0
    def handle(self, *args, **options):

        Time = 0
        try:
            Time = int(args[0])
        except:
            Time = 0
        print "from %i " % (Time)
        LOCK = "incomin_btc_blockchaint_txid"
        lock = None
        try:
            lock = my_lock(LOCK)
            process_block_info(Time)

        except LockBusyException as e:
            print "operation is locked", e.value
        except:
            print "Unexpected error:", str(sys.exc_info())
            traceback.print_exc(file=sys.stdout)
        finally:
            my_release(lock)
    def handle(self, *args, **options):
        
        Time =  0   
        try :
              Time = int(args[0])
        except :
              Time = 0  
        print "from %i " % (Time)
	LOCK = "incomin_btc_blockchaint_txid"
	lock = None
	try:
                lock = my_lock(LOCK)        
		process_block_info(Time)
	
	except LockBusyException as e:
               print "operation is locked", e.value
	except :
		print "Unexpected error:",str( sys.exc_info())
		traceback.print_exc(file=sys.stdout)
	finally:
	        my_release(lock)
Ejemplo n.º 18
0
    def handle(self, *args, **options):
        CurrencyTitle = args[0]    
        
        try :
                Time = int(Time)
        except :
                Time = 0  

        LOCK = "out_crypto"
        LOCK +=  CurrencyTitle
        lock = None
        lock = my_lock(LOCK)
        try:
                process_out(CurrencyTitle)
                my_release(lock)
        except LockBusyException as e:
                    print "operation is locked", e.value
        except urllib2.HTTPError:
                print "url error"
                my_release(lock)
        except APIException as e:
            print e.message
            if not e.message.startswith(u"Insufficient Funds"):
                print "release lock"
                my_release(lock)
        except:
            print "Unexpected error:", str(sys.exc_info())
Ejemplo n.º 19
0
    def handle(self, *args, **options):
        try :
              Time = args[0]    
              Time = int(Time)
        except :
              Time = 0 
	print "process  time %i" % ( Time) 
	LOCK = "in_cryptoblck_info"
	LOCK = LOCK 
	lock = None
	try:
      		lock = my_lock(LOCK)
		
		for CurrencyTitle in  CryptoSettings.keys():
			
			print "start proces %s" % CurrencyTitle
			if CurrencyTitle !="BTC":
				process_in_crypto(Time, CurrencyTitle)
	except :
		print "Unexpected error:", sys.exc_info()[0]	
        finally:
		my_release(lock)
Ejemplo n.º 20
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 = Orders(user = Req.user,
                                currency1 = CurrencyBase,
                                currency2 = CurrencyOn, 
                                price = Price,
                                sum1_history = Amnt1,
                                sum2_history = Amnt2,
                                sum1 = Amnt1, 
                                sum2 = Amnt2,
                                transit_1 = TradePair.transit_from,
                                transit_2 = TradePair.transit_on,
                                trade_pair = TradePair,
                                comission = Comission
                                )
                order.save()

                try: 
                        FromAccount = Accounts.objects.get(user = Req.user, currency = CurrencyBase)
                        add_trans(FromAccount, Amnt1, CurrencyBase, TradePair.transit_from, order, "deposit")                      
                        order.status = "processing"
                        order.save()
                        system_notify(deposit_funds(order), Req.user)                  
                        
                        ResAuto = make_auto_trade(order, TradePair, order.price, CurrencyBase, Amnt1, CurrencyOn, Amnt2)
                        
                        Response = HttpResponse(process_auto(Req, ResAuto, TradePair))

                        my_release(TradeLock)
                        Response['Content-Type'] = 'application/json'
                        End = time.time()
                        measure = OrderTimer(order = order,time_work = str(End - Start))
                        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()
                        measure = OrderTimer(order = order,time_work = str(End - Start))
                        measure.save()
                        
                        return Response 
Ejemplo n.º 21
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 = Orders(user=Req.user,
                   currency1=CurrencyBase,
                   currency2=CurrencyOn,
                   price=Price,
                   sum1_history=Amnt1,
                   sum2_history=Amnt2,
                   sum1=Amnt1,
                   sum2=Amnt2,
                   transit_1=TradePair.transit_from,
                   transit_2=TradePair.transit_on,
                   trade_pair=TradePair,
                   comission=Comission)
    order.save()

    try:
        FromAccount = Accounts.objects.get(user=Req.user,
                                           currency=CurrencyBase)
        add_trans(FromAccount, Amnt1, CurrencyBase, TradePair.transit_from,
                  order, "deposit")
        order.status = "processing"
        order.save()
        system_notify(deposit_funds(order), Req.user)

        ResAuto = make_auto_trade(order, TradePair, order.price, CurrencyBase,
                                  Amnt1, CurrencyOn, Amnt2)

        Response = HttpResponse(process_auto(Req, ResAuto, TradePair))

        my_release(TradeLock)
        Response['Content-Type'] = 'application/json'
        End = time.time()
        measure = OrderTimer(order=order, time_work=str(End - Start))
        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()
        measure = OrderTimer(order=order, time_work=str(End - Start))
        measure.save()

        return Response
Ejemplo n.º 22
0
def sell(Req, Trade_pair):
    FreqKey = "orders" + str(Req.user.id)
    Start = time.time()
    if not check_freq(FreqKey, 3):
        Response = HttpResponse('{"status":false,"description":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 = Count
    Amnt2 = Count * Price
    CurrencyBase = Currency.objects.get(title=CurrencyBaseS)
    CurrencyOn = Currency.objects.get(title=CurrencyOnS)
    TradeLock = my_lock(LOCK)
    order = OrdersMem(user=Req.user.id,
                      currency1=CurrencyOn.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=CurrencyOn)
        system_notify_async(deposit_funds(order, CurrencyOn), Req.user.id)
        add_trans2(FromAccount, Amnt1, CurrencyOn, order, "deposit")
        order.status='processing'
        order.save()
        ResAuto = make_auto_trade(order, TradePair, order.price, CurrencyOn, Amnt1, CurrencyBase)

        # adding locks
        my_release(TradeLock)
        Response = HttpResponse(process_auto(ResAuto, TradePair))
        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
        my_release(TradeLock)
        Response = HttpResponse(process_mistake(Req, Status))
        Response['Content-Type'] = 'application/json'
        End = time.time()
        tb = traceback.format_exc()
        measure = OrderTimer(order=i, time_work=str(End - Start),error=tb)
        measure.save()
        return Response