From d66196a29028713e89ec8867fc42c57a4441905c Mon Sep 17 00:00:00 2001 From: Gert Wohlgemuth Date: Wed, 23 May 2018 23:11:11 -0700 Subject: [PATCH 1/2] added functionality to render trades --- freqtrade/aws/backtesting_lambda.py | 9 +++++- freqtrade/aws/strategy.py | 45 ++++++++++++++++++++++++++-- freqtrade/tests/aws/test_backtest.py | 24 ++++++++++++--- serverless.yml | 22 ++++++++++++++ 4 files changed, 93 insertions(+), 7 deletions(-) diff --git a/freqtrade/aws/backtesting_lambda.py b/freqtrade/aws/backtesting_lambda.py index 47e353298..0ae9be001 100644 --- a/freqtrade/aws/backtesting_lambda.py +++ b/freqtrade/aws/backtesting_lambda.py @@ -114,6 +114,7 @@ def backtest(event, context): print("persist data in dynamo") print(result) + result_data = [] for index, row in result.iterrows(): data = { "id": "{}.{}:{}".format(user, name, row['currency']), @@ -129,9 +130,15 @@ def backtest(event, context): data = json.dumps(data, use_decimal=True) data = json.loads(data, use_decimal=True) - print(data) + # persist data trade_table.put_item(Item=data) + result_data.append(data) + + return { + "statusCode": 200, + "body": json.dumps(result_data) + } else: raise Exception( "sorry we did not find any matching strategy for user {} and name {}".format(user, name)) diff --git a/freqtrade/aws/strategy.py b/freqtrade/aws/strategy.py index 9227bc36c..7c0562d3f 100644 --- a/freqtrade/aws/strategy.py +++ b/freqtrade/aws/strategy.py @@ -8,7 +8,7 @@ from boto3.dynamodb.conditions import Key, Attr from jsonschema import validate from freqtrade.aws.schemas import __SUBMIT_STRATEGY_SCHEMA__ -from freqtrade.aws.tables import get_strategy_table +from freqtrade.aws.tables import get_strategy_table, get_trade_table from freqtrade.strategy.resolver import StrategyResolver import requests @@ -144,7 +144,7 @@ def submit(event, context): :return: """ - print(event) + # print(event) # get data data = json.loads(event['body']) @@ -253,3 +253,44 @@ def submit_github(event, context): print("imported/updated: {} strategies".format(strategies)) else: print("invalid response received \n{}\n".format(result)) + + +def get_trades(event, context): + """ + this function retuns all the knowns trades for a user, strategy and pair + :param event: + :param context: + :return: + """ + + assert 'pathParameters' in event + assert 'user' in event['pathParameters'] + assert 'name' in event['pathParameters'] + assert 'stake' in event['pathParameters'] + assert 'asset' in event['pathParameters'] + + table = get_trade_table() + + response = table.query( + KeyConditionExpression=Key('id').eq( + "{}.{}:{}/{}".format( + event['pathParameters']['user'], + event['pathParameters']['name'], + event['pathParameters']['asset'], + event['pathParameters']['stake'] + ) + ) + ) + + if "Items" in response and len(response['Items']) > 0: + + return { + "statusCode": response['ResponseMetadata']['HTTPStatusCode'], + "body": json.dumps(response['Items']) + } + + else: + return { + "statusCode": response['ResponseMetadata']['HTTPStatusCode'], + "body": json.dumps(response) + } diff --git a/freqtrade/tests/aws/test_backtest.py b/freqtrade/tests/aws/test_backtest.py index 770339e6c..18b0fc690 100644 --- a/freqtrade/tests/aws/test_backtest.py +++ b/freqtrade/tests/aws/test_backtest.py @@ -5,7 +5,7 @@ import boto3 import pytest import simplejson as json from freqtrade.aws.backtesting_lambda import backtest, cron -from freqtrade.aws.strategy import submit +from freqtrade.aws.strategy import submit, get_trades def test_backtest(lambda_context): @@ -72,7 +72,7 @@ class MyFancyTestStrategy(IStrategy): "name": "MyFancyTestStrategy" } - backtest({ + data = json.loads(backtest({ "Records": [ { "Sns": { @@ -80,7 +80,23 @@ class MyFancyTestStrategy(IStrategy): "Message": json.dumps(request) } }] - }, {}) + }, {})['body']) + + # evaluate that we now have trades in the database + # sadly not always a given at this tage + # due to the dynamic nature. Should pick a strategy for testing + # which generates a lot of trades + if len(data) > 0: + data = get_trades({ + 'pathParameters': { + 'user': "GCU4LW2XXZW3A3FM2XZJTEJHNWHTWDKY2DIJLCZJ5ULVZ4K7LZ7D23TG", + "name": "MyFancyTestStrategy", + 'stake': "USDT", + 'asset': "{}".format(data[0]['pair'].split("/")[0]) + } + }, {})['body'] + print(data) + assert len(json.loads(data)) > 0 def test_cron(lambda_context): @@ -146,4 +162,4 @@ class MyFancyTestStrategy(IStrategy): cron({}, {}) - #TODO test receiving of message some how + # TODO test receiving of message some how diff --git a/serverless.yml b/serverless.yml index 6bf7aa341..1264038c1 100644 --- a/serverless.yml +++ b/serverless.yml @@ -158,6 +158,28 @@ functions: environment: strategyTable: ${self:custom.strategyTable} + # loads all trades for a strategy and it's associated pairs + trades: + memorySize: 128 + handler: freqtrade/aws/strategy.get_trades + events: + - http: + path: strategies/{user}/{name}/{stake}/{asset} + method: get + cors: true + request: + parameter: + paths: + user: true + name: true + stake: true + asset: true + + environment: + strategyTable: ${self:custom.strategyTable} + tradeTable: ${self:custom.tradeTable} + + #submits a new strategy to the system submit: memorySize: 128 From 669f596ce7caab250fb24ddab47c8145f010080b Mon Sep 17 00:00:00 2001 From: Gert Wohlgemuth Date: Wed, 23 May 2018 23:49:57 -0700 Subject: [PATCH 2/2] getting rid of hyperopt must, should be optional --- freqtrade/arguments.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/freqtrade/arguments.py b/freqtrade/arguments.py index 676fd9e71..c9e0bbe9a 100644 --- a/freqtrade/arguments.py +++ b/freqtrade/arguments.py @@ -194,7 +194,7 @@ class Arguments(object): Builds and attaches all subcommands :return: None """ - from freqtrade.optimize import backtesting, hyperopt + from freqtrade.optimize import backtesting subparsers = self.parser.add_subparsers(dest='subparser') @@ -205,10 +205,14 @@ class Arguments(object): self.backtesting_options(backtesting_cmd) # Add hyperopt subcommand - hyperopt_cmd = subparsers.add_parser('hyperopt', help='hyperopt module') - hyperopt_cmd.set_defaults(func=hyperopt.start) - self.optimizer_shared_options(hyperopt_cmd) - self.hyperopt_options(hyperopt_cmd) + try: + from freqtrade.optimize import hyperopt + hyperopt_cmd = subparsers.add_parser('hyperopt', help='hyperopt module') + hyperopt_cmd.set_defaults(func=hyperopt.start) + self.optimizer_shared_options(hyperopt_cmd) + self.hyperopt_options(hyperopt_cmd) + except ImportError as e: + logging.warn("no hyper opt found - skipping support for it") @staticmethod def parse_timerange(text: str) -> Optional[Tuple[List, int, int]]: