From 7b101f17b6aa79544d012c88ba0eb236d7dac577 Mon Sep 17 00:00:00 2001 From: Gert Wohlgemuth Date: Mon, 4 Jun 2018 13:40:06 -0700 Subject: [PATCH] minor adjustements --- Dockerfile | 2 +- freqtrade/aws/backtesting_lambda.py | 54 ++++++++++++--------- freqtrade/tests/aws/test_strategy_lambda.py | 4 +- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/Dockerfile b/Dockerfile index afafd93c1..62003930c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM python:3.6.5-slim-stretch # Install TA-lib -RUN apt-get update && apt-get -y install curl build-essential && apt-get clean +RUN apt-get update && apt-get -y install curl build-essential git && apt-get clean RUN curl -L http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz | \ tar xzvf - && \ cd ta-lib && \ diff --git a/freqtrade/aws/backtesting_lambda.py b/freqtrade/aws/backtesting_lambda.py index 33aea9e8e..81e02ff94 100644 --- a/freqtrade/aws/backtesting_lambda.py +++ b/freqtrade/aws/backtesting_lambda.py @@ -2,6 +2,7 @@ import datetime import logging import os import tempfile +from base64 import urlsafe_b64encode import boto3 import simplejson as json @@ -68,30 +69,18 @@ def backtest(event, context): try: if "Items" in response and len(response['Items']) > 0: - print("backtesting from {} till {} for {} with {} vs {}".format(fromDate, till, name, - event['body'][ - 'stake_currency'], - event['body']['assets'])) + print("schedule backtesting from {} till {} for {} with {} vs {}".format(fromDate, till, name, + event['body'][ + 'stake_currency'], + event['body'][ + 'assets'])) configuration = _generate_configuration(event, fromDate, name, response, till) - backtesting = Backtesting(configuration) - result = backtesting.start() - for index, row in result.iterrows(): - data = { - "id": "{}.{}:{}:test".format(user, name, row['currency'].upper()), - "trade": "{} to {}".format(row['entry'].strftime('%Y-%m-%d %H:%M:%S'), - row['exit'].strftime('%Y-%m-%d %H:%M:%S')), - "pair": row['currency'], - "duration": row['duration'], - "profit_percent": row['profit_percent'], - "profit_stake": row['profit_BTC'], - "entry_date": row['entry'].strftime('%Y-%m-%d %H:%M:%S'), - "exit_date": row['exit'].strftime('%Y-%m-%d %H:%M:%S') - } + print("configuration: \n{}\n".format( + urlsafe_b64encode(json.dumps(configuration).encode('utf-8')).decode('utf-8'))) - _submit_result_to_backend(data) - - # fire request message to aggregate this strategy now + # fire AWS fargate instance now + run_backtest(configuration, name, user) return { "statusCode": 200 @@ -113,6 +102,25 @@ def backtest(event, context): raise Exception("not a valid event: {}".format(event)) +def run_backtest(configuration, name, user): + backtesting = Backtesting(configuration) + result = backtesting.start() + for index, row in result.iterrows(): + data = { + "id": "{}.{}:{}:test".format(user, name, row['currency'].upper()), + "trade": "{} to {}".format(row['entry'].strftime('%Y-%m-%d %H:%M:%S'), + row['exit'].strftime('%Y-%m-%d %H:%M:%S')), + "pair": row['currency'], + "duration": row['duration'], + "profit_percent": row['profit_percent'], + "profit_stake": row['profit_BTC'], + "entry_date": row['entry'].strftime('%Y-%m-%d %H:%M:%S'), + "exit_date": row['exit'].strftime('%Y-%m-%d %H:%M:%S') + } + + _submit_result_to_backend(data) + + def _submit_result_to_backend(data): """ submits the given result to the backend system for further processing and analysis @@ -163,7 +171,7 @@ def _generate_configuration(event, fromDate, name, response, till): "chat_id": "0" }, "initial_state": "running", - "datadir": tempfile.gettempdir(), + "datadir": os.environ("FREQ_DATA_DIR", tempfile.gettempdir()), "experimental": { "use_sell_signal": response['Items'][0]['use_sell'], "sell_profit_only": True @@ -172,7 +180,7 @@ def _generate_configuration(event, fromDate, name, response, till): "process_throttle_secs": 5 }, 'realistic_simulation': True, - "loglevel": logging.DEBUG, + "loglevel": logging.INFO, "strategy": "{}:{}".format(name, content), "timerange": "{}-{}".format(fromDate.strftime('%Y%m%d'), till.strftime('%Y%m%d')), "refresh_pairs": True diff --git a/freqtrade/tests/aws/test_strategy_lambda.py b/freqtrade/tests/aws/test_strategy_lambda.py index 3c349cac3..8afab16d3 100644 --- a/freqtrade/tests/aws/test_strategy_lambda.py +++ b/freqtrade/tests/aws/test_strategy_lambda.py @@ -64,14 +64,14 @@ class TestStrategy(IStrategy): } # db should be empty - assert (len(json.loads(aws.names({}, {})['body'])) == 0) + assert (len(json.loads(aws.names({}, {})['body']['result'])) == 0) # now we add an entry aws.submit({ "body": json.dumps(request) }, {}) # now we should have items - assert (len(json.loads(aws.names({}, {})['body'])) == 1) + assert (len(json.loads(aws.names({}, {})['body']['result'])) == 1) # able to add a second strategy with the sample name, but different user