def common_deal_list(Pair, User_id=None): Current = None try: Current = TradePairs.objects.get(url_title=Pair) except: return json_false500(Req) ldeals = None if User_id is None: ldeals = DealsMemory.objects.filter(trade_pair=Current.id).order_by("-id") else: ldeals = DealsMemory.objects.filter(trade_pair=Current.id, user_id=User_id).order_by("-id") ResList = [] for item in ldeals: new_item = {} rate = item.price new_item['pub_date'] = formats.date_format(item.pub_date, "DATETIME_FORMAT") new_item["type"] = item.type_deal new_item["user"] = item.user new_item["price"] = format_numbers10(rate) new_item["amnt_base"] = format_numbers10(item.amnt_base) new_item["amnt_trade"] = format_numbers10(item.amnt_trade) ResList.append(new_item) return ResList
def japan_stat(Req, Pair): Current = None try: Current = TradePairs.objects.get(url_title=Pair) except: return json_false500(Req) List = StockStat.objects.raw("SELECT * FROM main_stockstat WHERE main_stockstat.Stock_id=%i \ ORDER BY id DESC LIMIT 48 " % Current.id) ListJson = [] VolumeBase = 0 VolumeTrade = 0 for item in List: StartDate = item.start_date VolumeTrade = VolumeTrade + item.VolumeTrade VolumeBase = VolumeBase + item.VolumeBase Key = "%i:%i" % (StartDate.hour, StartDate.minute) ListJson.append( [Key, float(item.Start), float(item.Max), float(item.Min), float(item.End), float(item.VolumeTrade)]) OnlineUsersCount = OnlineUsers.objects.count() ListJson.reverse() Dict = {"trades": ListJson, "online": OnlineUsersCount, "volume_base": str(VolumeBase), "volume_trade": str(VolumeTrade)} RespJ = json.JSONEncoder().encode(Dict) return RespJ
def cache_control(Req): do = Req.REQUEST.get("do", None) cache = caching() if do == "flush": return json_false500(Req) if do == "get": key = Req.REQUEST.get("key") return HttpResponse(str(cache.get(key,""))) if do == "del": key = Req.REQUEST.get("key") value = str(cache.get(key,"")) cache.delete(key) return HttpResponse(value) return json_false500(Req)
def sell_list(Req, Pair): Current = None try: Current = TradePairs.objects.get(url_title=Pair) except: return json_false500(Req) SellList = OrdersMem.objects.filter(status="processing", currency1=Current.currency_on.id, currency2=Current.currency_from.id) getcontext().prec = 8 Currency1Title = Current.currency_from.title Currency2Title = Current.currency_on.title AccumSellSum = 0 GroupSellDict = {} for item in SellList: SellSum = item.sum1 ##LTC BuySum = item.sum2 ## UAH Rate = item.price AccumSellSum += SellSum if GroupSellDict.has_key(Rate): GroupSellDict[Rate][Currency2Title] = GroupSellDict[Rate][Currency2Title] + SellSum GroupSellDict[Rate][Currency1Title] = GroupSellDict[Rate][Currency1Title] + BuySum else: GroupSellDict[Rate] = {Currency2Title: SellSum, Currency1Title: BuySum} ResSellList = [] LL = GroupSellDict.keys() L = [] for i in LL: Temp = Decimal(i) GroupSellDict[Temp] = GroupSellDict[i] L.append(Temp) L.sort() Price = 0 MinPrice = 0 for i in L: Price = format_numbers10(i) ResSellList.append({"price": Price, "currency_trade": format_numbers10(GroupSellDict[i][Currency2Title]), "currency_base": format_numbers10(GroupSellDict[i][Currency1Title])}) if len(ResSellList): MinPrice = ResSellList[0]["price"] Dict = {"orders_sum": format_numbers10(AccumSellSum), "list": ResSellList, "min_price": MinPrice, "max_price": Price} RespJ = json.JSONEncoder().encode(Dict) return RespJ
def buy_list(Req, Pair): Current = None try: Current = TradePairs.objects.get(url_title=Pair) except: return json_false500(Req) BuyList = OrdersMem.objects.filter(status="processing", currency1=Current.currency_from.id, currency2=Current.currency_on.id) getcontext().prec = settings.TRANS_PREC Currency1Title = Current.currency_from.title Currency2Title = Current.currency_on.title List1 = {} AccumBuySum = 0 for item in BuyList: SellSum = item.sum1 ## UAH BuySum = item.sum2 ## LTC Rate = item.price AccumBuySum += SellSum if List1.has_key(Rate): List1[Rate][Currency1Title] = List1[Rate][Currency1Title] + SellSum List1[Rate][Currency2Title] = List1[Rate][Currency2Title] + BuySum else: List1[Rate] = {Currency1Title: SellSum, Currency2Title: BuySum} ResBuyList = [] LL = List1.keys() L = [] for i in LL: Temp = Decimal(i) List1[Temp] = List1[i] L.append(Temp) L.sort() L.reverse() Price = 0 MaxPrice = 0 for i in L: Price = format_numbers10(i) ResBuyList.append({"price": Price, "currency_trade": format_numbers10(List1[i][Currency2Title]), "currency_base": format_numbers10(List1[i][Currency1Title])}) if len(ResBuyList): MaxPrice = ResBuyList[0]["price"] Dict = {"orders_sum": format_numbers10(AccumBuySum), "list": ResBuyList, "max_price": MaxPrice, "min_price": Price} RespJ = json.JSONEncoder().encode(Dict) return RespJ
def auth(Req): Nonce = Req.REQUEST.get("nonce", None) if Nonce is None: return json_false500(Req) Sign = Req.META.get('HTTP_API_SIGN', None) if Sign is None: return json_false500(Req, {"description": "invalid_params", "key": "api_sign"}) PublicKey = Req.META.get('HTTP_PUBLIC_KEY', None) if PublicKey is None: return json_false500(Req, {"description": "invalid_params", "key": "public_key"}) try: Req.user = check_api_sign(PublicKey, Sign, Req.body) Cache = caching() Cache.set("nonce_" + PublicKey, int(Nonce), 50000) Nonce = Cache.get("nonce_" + PublicKey) return json_true(Req, {"nonce": Nonce, "public_key": PublicKey}) except: return json_false500(Req, {"description": "auth_faild"})
def last_price(Req, Pair): Current = None try: Current = TradePairs.objects.get(url_title=Pair) except: return json_false500(Req) Dict = None try: deal = DealsMemory.objects.filter(trade_pair=Current.id).latest("id") Dict = {"price": format_numbers4(deal.price), "price_10": format_numbers10(deal.price)} except: Dict = {"price": "0", "price_10": "0.000000000"} RespJ = json.JSONEncoder().encode(Dict) return RespJ
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