def test_Trade(self): """[summary] Testing Trade with only backetest. """ createdf.connectandsave(num_data=300) api_key = key.api_key api_secret = key.api_secret t = ai.Trade(api_key=api_key, api_secret=api_secret, backtest=True) algo = "Ema" t.Trade(algo=algo)
def handle(self, *args, **options): api_key = key.api_key api_secret = key.api_secret backtest = True print("Start backtest") logging.info("start backtest") while True: A = ai.Trade(api_key=api_key, api_secret=api_secret, backtest=backtest, duration="h") A.Trade(algo=options["algo"], time_sleep=0.15)
def test_GetClose(self): createdf.connectandsave(num_data=300) api_key = key.api_key api_secret = key.api_secret t = ai.Trade(api_key=api_key, api_secret=api_secret, backtest=True) t.GetClose() self.assertIsNot(t.now_position, None) self.assertIsNot(t.price, None) self.assertIsNot(t.latest_close, None) self.assertIsNot(t.before_close, None)
def handle(self, *args, **options): api_key = key.api_key api_secret = key.api_secret backtest = False print(f"Start Trading backtest={backtest}") while True: A = ai.Trade(api_key=api_key, api_secret=api_secret, backtest=backtest, duration="h") A.Trade(algo=options["algo"], time_sleep=45)
def test_SendOrders(self): createdf.connectandsave(num_data=300) api_key = key.api_key api_secret = key.api_secret t = ai.Trade(api_key=api_key, api_secret=api_secret, backtest=True) SELLSIGNAL = True BUYSIGNAL = False t.SendOrders(SELLSIGNAL, BUYSIGNAL) SELLSIGNAL = False BUYSIGNAL = True t.SendOrders(SELLSIGNAL, BUYSIGNAL) SELLSIGNAL = False BUYSIGNAL = False t.SendOrders(SELLSIGNAL, BUYSIGNAL)
def test_startbacktest(self): createdf.connectandsave() # createdf.csv2models() api_key = key.api_key api_secret = key.api_secret b = get_data.Balance(api_key=api_key, api_secret=api_secret) b.GetExecutions() # algo_name = "Ema" # time_sleep = 0.02 # rep = 2 backtest = True A = ai.Trade(api_key=api_key, api_secret=api_secret, backtest=backtest, duration="h") # A.Trade(algo=algo_name, rep=rep, time_sleep=time_sleep) th = threading.Thread(group=None, target=A.Trade) th.start() th.join() self.assertEqual(threading.active_count(), 1)
def AutoTrade(request): """[summary] from autotrade.html. this view switch autotrading ON/OFF, and select autotrading algo. models.UsingALgo detect using algo. ON/OFF switch is global stop. When POST request, TradeThread running and trading per 1/60/1440 minutes. Args: request ([type]): [description] Returns: [type]: [description] """ api_key = key.api_key api_secret = key.api_secret tradetime = {"m": 1, "h": 60, "d": 1440} backtest = True code = models.UsingProductCode.objects.last().code if not models.SignalEvents.objects.exists(): get_data.Balance(api_key=api_key, api_secret=api_secret).GetExecutions() signalevents = models.SignalEvents.objects.order_by("-time")[:5] algolists = models.AlgoList.objects.all() algolistsform = models.AlgoListForm() if request.method == "POST": # POST request has 2 pattern. 'select autotrading algo' or 'cancel or re-select autotrading algo'. if request.POST.get("stop_algo") is None: # 'select autotrading algo' pattern. add UsingAlgo objects. algo_name = request.POST.get("select_algo") algo = models.AlgoList.objects.get(algo=algo_name) duration = request.POST.get("select_duration") usingalgo = models.UsingALgo usingalgo(algo=algo, duration=duration).save() A = ai.Trade(api_key=api_key, api_secret=api_secret, backtest=backtest, duration=duration, product_code=code) try: pertrade = tradetime[duration] except KeyError as e: logger.error(e) pertrade = 60 th = TradeThread(algo_name=algo_name, sleep_time=pertrade, TradeInstance=A) th.name = "TradeThread" th.start() return render( request, "chart/autotrade.html", { "usealgo": algo, "duration": duration, "signalevents": signalevents, }) else: # 'cancel or re-select autotrading algo' pattern. delete UsingAlgo objects. event.set() models.UsingALgo.objects.all().delete() isAutoTrade(request) return render( request, "chart/autotrade.html", { "signalevents": signalevents, "algolists": algolists, "form": algolistsform, "durations": ["h", "d", "m"], }) elif models.UsingALgo.objects.exists(): algo = models.UsingALgo.objects.first().algo duration = models.UsingALgo.objects.first().duration return render(request, "chart/autotrade.html", { "usealgo": algo, "signalevents": signalevents, "duration": duration, }) else: # if UsingAlgo has no object and not POST request, send html to usacble algolists, history and durations list. return render( request, "chart/autotrade.html", { "signalevents": signalevents, "algolists": algolists, "form": algolistsform, "durations": ["h", "d", "m"], })