def putTraded(request): shift = request.GET["shift"] db = DB(dbName, client, shift) symbol = request.GET["sym"] endTS = request.GET["endTS"] sellPrice = request.GET["sellPrice"] try: db.tradeEND(symbol, endTS, sellPrice) return HttpResponse(str(True)) except OperationalError: ####COMPRUEBA LOS PERMISOS DE LA BASE DE DATOS!!!! return HttpResponse(str(False))
def Traded(request): db = DB(dbName,client, request.GET["shift"]) a = db.getTRADEDdict() assets = [{"name": "ETH", "good": 0, "bad": 0, "percGood": 0, "benefit": 0}, {"name": "BNB", "good": 0, "bad": 0, "percGood": 0, "benefit": 0}, {"name": "BTC", "good": 0, "bad": 0, "percGood": 0, "benefit": 0}] general = {"good": 0, "bad":0,"percGood":0, "duration": timedelta(seconds= 0)} for item in a: evalPrice = Decimal(item["evalPrice"]) endPrice = Decimal(item["sell"]) if endPrice > evalPrice: item["tradeEND"] = True general["good"] = general["good"] +1 else: item["tradeEND"] = False general["bad"] = general["bad"] +1 item["duration"] = item["endTS"]-item["evalTS"] general["duration"] = general["duration"] + item["duration"] totTrades = general["good"]+general["bad"] try: general["percGood"] = f"{(general['good']/totTrades)*100:.2f}" general["duration"] = general["duration"]/totTrades except ZeroDivisionError: general["percGood"] = 0 general["duration"] = 0 for asset in assets: Lass = len(asset["name"]) for item in a: if item["symbol"][Lass-Lass*2:] == asset["name"]: if item["tradeEND"] == True: asset["good"] = asset["good"] + 1 else: asset["bad"] = asset["bad"] + 1 soldAT = Decimal(item["sell"])*Decimal(item["baseQty"]) ben = soldAT-Decimal(item["assetQty"]) asset["benefit"] = asset["benefit"] + ben totTrades = asset["good"]+asset["bad"] try: asset["percGood"] = (asset["good"]/totTrades)*100 except ZeroDivisionError: asset["percGood"] = 0 asset["percGood"] = f"{asset['percGood']:.2f}" asset["benefit"] = f"{asset['benefit']:.8f}" d = {"syms": a, "assets": assets, "general": general} return render(request, "traded.html", d)
def putTrading(request): shift = request.GET["shift"] db = DB(dbName, client, shift) symbol = request.GET["sym"] evalTS = request.GET["evalTS"] evalPrice = request.GET["evalPrice"] stop = request.GET["stop"] limit = request.GET["limit"] assetQty = request.GET["assetQty"] baseQty = request.GET["baseQty"] try: db.tradeSTART(symbol, evalTS, evalPrice, stop, limit, assetQty, baseQty) return HttpResponse(str(True)) except OperationalError: ####COMPRUEBA LOS PERMISOS DE LA BASE DE DATOS!!!! return HttpResponse(str(False))
def Efectivity(request): db = DB(dbName,client, request.GET["shift"]) ##Efectividad por dia/asset graphs = [] data = db.getEFperDay(asset=request.GET["asset"]) colors = ["orange",]*len(data["day"]) for i in range(len(data["day"])): if Decimal(data["perc"][i]) <= Decimal("100") and Decimal(data["perc"][i]) >= Decimal("65"): colors[i] = "green" graph = go.Figure(data=[go.Scatter(x=data["day"], y=data["co"], hovertext=data["text"], mode="markers",marker_color=colors)]) graph.update_layout(title="Efectividad general diaria "+request.GET["asset"]) graphDIV = plot(graph, output_type="div", include_plotlyjs=False, config={"displayModeBar": False, "autosizable": True}) graphs.append(graphDIV) d = {"graphs": graphs} return render(request, "efectivity.html", d)
#!/usr/bin/env python3 from os import environ from datetime import datetime, timedelta from decimal import Decimal from binance.client import Client import mariadb import dateparser from dbOPS import DB2 as DB from dbOPS import parseKline from sys import argv workerTypes = ["dbWorker", "dbMiner"] db = DB() def test_general(): print("|TESTING CONTAINER ENVIRONMENT") a = "" for wT in workerTypes: if wT == "dbWorker": a = dbWorker(argv[1], wT) print(f"--->WorkerType: {a.work}") if len(a.API) > 0: print(f"--->APIKEYS: OK") else: print(f"--->APIKEYS: KO") print("|Conexion con API de BINANCE") kline = a.client.get_historical_klines("BTCEUR", a.client.KLINE_INTERVAL_1DAY,
else: return False else: return False def __init__(self, symbol, cliType, task="simulate", timePoint="now"): self.symbol = symbol['symbol'] self.task = task self.cli = cliType self.timePoint = timePoint if task == "backtest": interval = timedelta(minutes=5) while self.timePoint < datetime.now(): scalpResult = self.run_scalper() if scalpResult == False: self.timePoint = self.timePoint + interval else: print("Bucle Roto") break elif task == "simulate": scalpResult = self.run_scalper() if __name__ == "__main__": db = DB(client) pairs = db.serveScalper() timePoint = datetime.now() - timedelta(days=1) for pair in pairs: print(f'Almacenando: {pair["symbol"]}') db.getHistoricFromAPI(pair=pair["symbol"]) #print(f"END {pair['symbol']}")
def Stats(request): db = DB(dbName,client, request.GET["shift"]) #####Grafico GENERAL percs = db.getPercentage() bestShift = db.getBestShift(66) colors = ["orange",]*24 for i in bestShift["hour"]: colors[int(i)] = "green" perTradeStart = go.Figure(data=[ go.Bar(name="Porcentaje", x=percs["hour"], y=percs["perc"], hovertext=percs["totals"], marker_color=colors)]) perTradeStart.update_layout(barmode="group",title="Efectividad general") startTSdiv = plot(perTradeStart, output_type="div", include_plotlyjs=False, config={"displayModeBar": False, "autosizable": True}) ###Graficos de asset assets = ["ETH","BNB","BTC"] graphs = [] for asset in assets: percs = db.getPercentage(asset=asset) bestShift = db.getBestShift(66, asset=asset) colors = ["orange",]*24 for i in bestShift["hour"]: colors[int(i)] = "green" TradeStart = go.Figure(data=[ go.Bar(name="Porcentaje", x=percs["hour"], y=percs["perc"], hovertext=percs["totals"], marker_color=colors)]) TradeStart.update_layout(barmode="group",title=f"Efectividad {asset}") TSdiv = plot(TradeStart, output_type="div", include_plotlyjs=False, config={"displayModeBar": False, "autosizable": True}) graphs.append(TSdiv) #### ###VARIABLES PARA EL GRAFCO POR PARES '''parList = [] winList = [] losList = [] for item in a: try: ind = parList.index(item["symbol"]) except ValueError: parList.append(item["symbol"]) winList.append(0) losList.append(0) ind = parList.index(item["symbol"]) if item["tradeEND"] == True: winList[ind] = winList[ind] + 1 else: losList[ind] = losList[ind] + 1 ###Grafico de trades por pares. perPair = go.Figure(data=[ go.Bar(name="Ganados", x=winList, y=parList, orientation= "h"), go.Bar(name="Perdidos", x=losList, y=parList, orientation="h")]) perPair.update_layout(barmode="group",title="Resultados agrupados por Pares", height=3000) perPairdiv = plot(perPair, output_type="div", include_plotlyjs=False, config={"displayModeBar": False, "autosizable": True})''' ############################### d = {"perStartTS": startTSdiv, "graphs": graphs} return render(request, "stats.html", d)
def Trading(request): db = DB(dbName,client, request.GET["shift"]) a = db.getTRADINGdict() d = {"syms": a} return render(request, "trading.html", d)
def getBestShift(request): minPerc = request.GET["minPerc"] asset = request.GET["asset"] db = DB(dbName, client, "ALL") bestShift = db.getBestShift(int(minPerc), asset=asset) return HttpResponse(json.dumps(bestShift))
def getTradeable(request): shift = request.GET["shift"] db = DB(dbName, client, shift) tradeable = db.getTRADEABLE(request.GET["sym"]) return HttpResponse(json.dumps(tradeable))