fixed some bugs, improved the backtesting and strategy table. Possible now to specify which features we want to use in the backtesting api
This commit is contained in:
parent
a51746c255
commit
1b77d66e2c
@ -1,3 +1,4 @@
|
|||||||
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import boto3
|
import boto3
|
||||||
@ -37,101 +38,105 @@ def backtest(event, context):
|
|||||||
no return
|
no return
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if 'body' in event:
|
if 'Records' in event:
|
||||||
event['body'] = json.loads(event['body'])
|
for x in event['Records']:
|
||||||
name = event['body']['name']
|
if 'Sns' in x and 'Message' in x['Sns']:
|
||||||
user = event['body']['user']
|
|
||||||
|
|
||||||
# technically we can get all these from teh strategy table
|
event['body'] = json.loads(x['Sns']['Message'])
|
||||||
stake_currency = event['body']['stake_currency'].upper()
|
name = event['body']['name']
|
||||||
asset = event['body']['asset']
|
user = event['body']['user']
|
||||||
exchange = event['body']['exchange']
|
|
||||||
|
|
||||||
assets = list(map(lambda x: "{}/{}".format(x, stake_currency).upper(), asset))
|
trade_table = get_trade_table()
|
||||||
|
table = get_strategy_table()
|
||||||
|
|
||||||
trade_table = get_trade_table()
|
response = table.query(
|
||||||
table = get_strategy_table()
|
KeyConditionExpression=Key('user').eq(user) &
|
||||||
|
Key('name').eq(name)
|
||||||
|
|
||||||
response = table.query(
|
)
|
||||||
KeyConditionExpression=Key('user').eq(user) &
|
|
||||||
Key('name').eq(name)
|
|
||||||
|
|
||||||
)
|
print(response)
|
||||||
|
if "Items" in response and len(response['Items']) > 0:
|
||||||
|
|
||||||
print(response)
|
today = datetime.datetime.today()
|
||||||
if "Items" in response and len(response['Items']) > 0:
|
yesterday = today - datetime.timedelta(days=1)
|
||||||
|
|
||||||
content = response['Items'][0]['content']
|
content = response['Items'][0]['content']
|
||||||
configuration = {
|
configuration = {
|
||||||
"max_open_trades": 1,
|
"max_open_trades": 1,
|
||||||
"stake_currency": stake_currency,
|
"stake_currency": response['Items'][0]['stake_currency'],
|
||||||
"stake_amount": 1,
|
"stake_amount": 1,
|
||||||
"fiat_display_currency": "USD",
|
"fiat_display_currency": "USD",
|
||||||
"unfilledtimeout": 600,
|
"unfilledtimeout": 600,
|
||||||
"bid_strategy": {
|
"trailing_stop": response['Items'][0]['trailing_stop'],
|
||||||
"ask_last_balance": 0.0
|
"bid_strategy": {
|
||||||
},
|
"ask_last_balance": 0.0
|
||||||
"exchange": {
|
},
|
||||||
"name": exchange,
|
"exchange": {
|
||||||
"enabled": True,
|
"name": response['Items'][0]['exchange'],
|
||||||
"key": "key",
|
"enabled": True,
|
||||||
"secret": "secret",
|
"key": "key",
|
||||||
"pair_whitelist": assets
|
"secret": "secret",
|
||||||
},
|
"pair_whitelist": list(
|
||||||
"telegram": {
|
map(lambda x: "{}/{}".format(x, response['Items'][0]['stake_currency']).upper(),
|
||||||
"enabled": False,
|
response['Items'][0]['assets']))
|
||||||
"token": "token",
|
},
|
||||||
"chat_id": "0"
|
"telegram": {
|
||||||
},
|
"enabled": False,
|
||||||
"initial_state": "running",
|
"token": "token",
|
||||||
"datadir": ".",
|
"chat_id": "0"
|
||||||
"experimental": {
|
},
|
||||||
"use_sell_signal": True,
|
"initial_state": "running",
|
||||||
"sell_profit_only": True
|
"datadir": ".",
|
||||||
},
|
"experimental": {
|
||||||
"internals": {
|
"use_sell_signal": response['Items'][0]['use_sell'],
|
||||||
"process_throttle_secs": 5
|
"sell_profit_only": True
|
||||||
},
|
},
|
||||||
'realistic_simulation': True,
|
"internals": {
|
||||||
"loglevel": logging.DEBUG,
|
"process_throttle_secs": 5
|
||||||
"strategy": "{}:{}".format(name, content)
|
},
|
||||||
|
'realistic_simulation': True,
|
||||||
|
"loglevel": logging.DEBUG,
|
||||||
|
"strategy": "{}:{}".format(name, content),
|
||||||
|
"timerange": "{}-{}".format(yesterday.strftime('%Y%m%d'), today.strftime('%Y%m%d')),
|
||||||
|
"refresh_pairs": True
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
print("generated configuration")
|
print("generated configuration")
|
||||||
print(configuration)
|
print(configuration)
|
||||||
|
|
||||||
print("initialized backtesting")
|
print("initialized backtesting")
|
||||||
backtesting = Backtesting(configuration)
|
backtesting = Backtesting(configuration)
|
||||||
result = backtesting.start()
|
result = backtesting.start()
|
||||||
print("finished test")
|
print("finished test")
|
||||||
|
|
||||||
print("persist data in dynamo")
|
print("persist data in dynamo")
|
||||||
|
|
||||||
print(result)
|
print(result)
|
||||||
for index, row in result.iterrows():
|
for index, row in result.iterrows():
|
||||||
data = {
|
data = {
|
||||||
"id": "{}.{}:{}".format(user, name, row['currency']),
|
"id": "{}.{}:{}".format(user, name, row['currency']),
|
||||||
"trade": "{} to {}".format(row['entry'].strftime('%Y-%m-%d %H:%M:%S'),
|
"trade": "{} to {}".format(row['entry'].strftime('%Y-%m-%d %H:%M:%S'),
|
||||||
row['exit'].strftime('%Y-%m-%d %H:%M:%S')),
|
row['exit'].strftime('%Y-%m-%d %H:%M:%S')),
|
||||||
"pair": row['currency'],
|
"pair": row['currency'],
|
||||||
"duration": row['duration'],
|
"duration": row['duration'],
|
||||||
"profit_percent": row['profit_percent'],
|
"profit_percent": row['profit_percent'],
|
||||||
"profit_stake": row['profit_BTC'],
|
"profit_stake": row['profit_BTC'],
|
||||||
"entry_date": row['entry'].strftime('%Y-%m-%d %H:%M:%S'),
|
"entry_date": row['entry'].strftime('%Y-%m-%d %H:%M:%S'),
|
||||||
"exit_date": row['exit'].strftime('%Y-%m-%d %H:%M:%S')
|
"exit_date": row['exit'].strftime('%Y-%m-%d %H:%M:%S')
|
||||||
}
|
}
|
||||||
|
|
||||||
data = json.dumps(data, use_decimal=True)
|
data = json.dumps(data, use_decimal=True)
|
||||||
data = json.loads(data, use_decimal=True)
|
data = json.loads(data, use_decimal=True)
|
||||||
print(data)
|
print(data)
|
||||||
# persist data
|
# persist data
|
||||||
trade_table.put_item(Item=data)
|
trade_table.put_item(Item=data)
|
||||||
else:
|
else:
|
||||||
raise Exception(
|
raise Exception(
|
||||||
"sorry we did not find any matching strategy for user {} and name {}".format(user, name))
|
"sorry we did not find any matching strategy for user {} and name {}".format(user, name))
|
||||||
else:
|
else:
|
||||||
raise Exception("no body provided")
|
raise Exception("not a valid event: {}".format(event))
|
||||||
|
|
||||||
|
|
||||||
def cron(event, context):
|
def cron(event, context):
|
||||||
|
@ -186,6 +186,22 @@ def __evaluate(data):
|
|||||||
data['stoploss'] = strat.stoploss
|
data['stoploss'] = strat.stoploss
|
||||||
data['ticker'] = strat.ticker_interval
|
data['ticker'] = strat.ticker_interval
|
||||||
|
|
||||||
|
# default variables if not provided
|
||||||
|
if 'trailing_stop' not in data:
|
||||||
|
data['trailing_stop'] = False
|
||||||
|
|
||||||
|
if 'stake_currency' not in data:
|
||||||
|
data['stake_currency'] = "USDT"
|
||||||
|
|
||||||
|
if 'use_sell' not in data:
|
||||||
|
data['use_sell'] = True
|
||||||
|
|
||||||
|
if 'exchange' not in data:
|
||||||
|
data['exchange'] = 'binance'
|
||||||
|
|
||||||
|
if 'assets' not in data:
|
||||||
|
data['assets'] = ["BTC", "ETH", "LTC"]
|
||||||
|
|
||||||
# force serialization to deal with decimal number
|
# force serialization to deal with decimal number
|
||||||
data = json.dumps(data, use_decimal=True)
|
data = json.dumps(data, use_decimal=True)
|
||||||
data = json.loads(data, use_decimal=True)
|
data = json.loads(data, use_decimal=True)
|
||||||
|
@ -13,6 +13,9 @@ def get_trade_table():
|
|||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if 'tradeTable' not in os.environ:
|
||||||
|
os.environ['tradeTable'] = "FreqTradeTable"
|
||||||
|
|
||||||
table_name = os.environ['tradeTable']
|
table_name = os.environ['tradeTable']
|
||||||
existing_tables = boto3.client('dynamodb').list_tables()['TableNames']
|
existing_tables = boto3.client('dynamodb').list_tables()['TableNames']
|
||||||
if table_name not in existing_tables:
|
if table_name not in existing_tables:
|
||||||
@ -55,6 +58,9 @@ def get_strategy_table():
|
|||||||
provides us access to the strategy table and if it doesn't exists creates it for us
|
provides us access to the strategy table and if it doesn't exists creates it for us
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
if 'strategyTable' not in os.environ:
|
||||||
|
os.environ['strategyTable'] = "FreqStrategyTable"
|
||||||
|
|
||||||
table_name = os.environ['strategyTable']
|
table_name = os.environ['strategyTable']
|
||||||
existing_tables = boto3.client('dynamodb').list_tables()['TableNames']
|
existing_tables = boto3.client('dynamodb').list_tables()['TableNames']
|
||||||
|
|
||||||
@ -62,7 +68,7 @@ def get_strategy_table():
|
|||||||
if table_name not in existing_tables:
|
if table_name not in existing_tables:
|
||||||
try:
|
try:
|
||||||
db.create_table(
|
db.create_table(
|
||||||
TableName=os.environ[table_name],
|
TableName=table_name,
|
||||||
KeySchema=[
|
KeySchema=[
|
||||||
{
|
{
|
||||||
'AttributeName': 'user',
|
'AttributeName': 'user',
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
|
import os
|
||||||
from base64 import urlsafe_b64encode
|
from base64 import urlsafe_b64encode
|
||||||
|
|
||||||
|
import boto3
|
||||||
import pytest
|
import pytest
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
from freqtrade.aws.backtesting_lambda import backtest, cron
|
from freqtrade.aws.backtesting_lambda import backtest, cron
|
||||||
@ -64,15 +66,21 @@ class MyFancyTestStrategy(IStrategy):
|
|||||||
"body": json.dumps(request)
|
"body": json.dumps(request)
|
||||||
}, {})
|
}, {})
|
||||||
|
|
||||||
|
# build sns request
|
||||||
request = {
|
request = {
|
||||||
"user": "GCU4LW2XXZW3A3FM2XZJTEJHNWHTWDKY2DIJLCZJ5ULVZ4K7LZ7D23TG",
|
"user": "GCU4LW2XXZW3A3FM2XZJTEJHNWHTWDKY2DIJLCZJ5ULVZ4K7LZ7D23TG",
|
||||||
"name": "MyFancyTestStrategy",
|
"name": "MyFancyTestStrategy"
|
||||||
"stake_currency": "usdt",
|
|
||||||
"asset": ["ETH", "BTC", "XRP", "LTC"],
|
|
||||||
"exchange": "binance"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
backtest({"body": json.dumps(request)}, {})
|
backtest({
|
||||||
|
"Records": [
|
||||||
|
{
|
||||||
|
"Sns": {
|
||||||
|
"Subject": "backtesting",
|
||||||
|
"Message": json.dumps(request)
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
}, {})
|
||||||
|
|
||||||
|
|
||||||
def test_cron(lambda_context):
|
def test_cron(lambda_context):
|
||||||
@ -135,4 +143,7 @@ class MyFancyTestStrategy(IStrategy):
|
|||||||
}, {})
|
}, {})
|
||||||
|
|
||||||
print("evaluating cron job")
|
print("evaluating cron job")
|
||||||
|
|
||||||
cron({}, {})
|
cron({}, {})
|
||||||
|
|
||||||
|
#TODO test receiving of message some how
|
||||||
|
@ -628,13 +628,15 @@ def lambda_context():
|
|||||||
lamb.start()
|
lamb.start()
|
||||||
|
|
||||||
session = boto3.session.Session()
|
session = boto3.session.Session()
|
||||||
|
|
||||||
client = session.client('sns')
|
|
||||||
dynamodb = boto3.resource('dynamodb')
|
|
||||||
os.environ["strategyTable"] = "StrategyTable"
|
os.environ["strategyTable"] = "StrategyTable"
|
||||||
os.environ["tradeTable"] = "TradeTable"
|
os.environ["tradeTable"] = "TradeTable"
|
||||||
os.environ["topic"] = "UnitTestTopic"
|
os.environ["topic"] = "UnitTestTopic"
|
||||||
|
|
||||||
|
client = session.client('sns')
|
||||||
|
client.create_topic(Name=os.environ["topic"])
|
||||||
|
|
||||||
|
dynamodb = boto3.resource('dynamodb')
|
||||||
|
|
||||||
import responses
|
import responses
|
||||||
|
|
||||||
# do not mock requests to these urls
|
# do not mock requests to these urls
|
||||||
|
@ -490,7 +490,7 @@ def test_processed(default_conf, mocker) -> None:
|
|||||||
|
|
||||||
def test_backtest_pricecontours(default_conf, fee, mocker) -> None:
|
def test_backtest_pricecontours(default_conf, fee, mocker) -> None:
|
||||||
mocker.patch('freqtrade.optimize.backtesting.exchange.get_fee', fee)
|
mocker.patch('freqtrade.optimize.backtesting.exchange.get_fee', fee)
|
||||||
tests = [['raise', 17], ['lower', 0], ['sine', 17]]
|
tests = [['raise', 17], ['lower', 0], ['sine', 16]]
|
||||||
for [contour, numres] in tests:
|
for [contour, numres] in tests:
|
||||||
simple_backtest(default_conf, contour, numres, mocker)
|
simple_backtest(default_conf, contour, numres, mocker)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user