added functionality to render trades

This commit is contained in:
Gert Wohlgemuth 2018-05-23 23:11:11 -07:00
parent 07155801bb
commit d66196a290
4 changed files with 93 additions and 7 deletions

View File

@ -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))

View File

@ -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)
}

View File

@ -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):

View File

@ -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