Skip to content

Commit

Permalink
Merge pull request #167 from diogomatoschaves/fix-strategy-validation
Browse files Browse the repository at this point in the history
Fix strategy validation
  • Loading branch information
diogomatoschaves authored Feb 25, 2024
2 parents 9ad5c90 + a3e068a commit 5e581dd
Show file tree
Hide file tree
Showing 11 changed files with 376 additions and 348 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ This page shows all the trades executed by any of the trading bots. It is possib

## Software Requirements

- [Poetry](https://python-poetry.org/docs/) (For creating an environment for local dev.)
- [Poetry](https://python-poetry.org/docs/) (For creating an environment for local development.)
- [Docker](https://docs.docker.com/get-docker/) (For running the app locally.)
- [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli) (For deploying the app to a Heroku.)
- [AWS S3](https://signin.aws.amazon.com/) (For storing machine learning models in the cloud.)
8 changes: 6 additions & 2 deletions dashboard/src/utils/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ export const validateParams = (strategy: any) => {
}
}, {success: true, params: {}})

debugger

return {
success: requiredParams.success && optionalParams.success,
updatedParams: {...requiredParams.params, ...optionalParams.params}
Expand Down Expand Up @@ -288,3 +286,9 @@ export const addLineBreaks = (text: string) => {
const regex = /\s{2}/g;
return text.replace(regex, '<br/>')
}


const Split = (str: string) => {
const re = /\s*(?:,|$)\s*/;
return str.split(re)
}
186 changes: 93 additions & 93 deletions data/poetry.lock

Large diffs are not rendered by default.

130 changes: 65 additions & 65 deletions execution/poetry.lock

Large diffs are not rendered by default.

156 changes: 78 additions & 78 deletions model/poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion model/service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from model.service.cloud_storage import check_aws_config
from model.service.helpers.decorators.handle_app_errors import handle_app_errors
from model.service.helpers.responses import Responses
from model.strategies import compile_strategies
from model.strategies.properties import compile_strategies
from model.worker import conn
from shared.utils.config_parser import get_config
from shared.utils.decorators import handle_db_connection_error
Expand Down
2 changes: 1 addition & 1 deletion model/service/cloud_storage/_upload.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os.path
import os


def upload_file(client, bucket, local_dir, filename):
Expand Down
5 changes: 4 additions & 1 deletion model/signal_generation/_helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from model.service.helpers import LOCAL_MODELS_LOCATION


def convert_signal_to_text(signal):
if signal == 1:
return "BUY"
Expand All @@ -8,5 +11,5 @@ def convert_signal_to_text(signal):


strategies_defaults = {
'MachineLearning': {"verbose": False, "models_dir": 'model/strategies/models'}
'MachineLearning': {"verbose": False, "models_dir": LOCAL_MODELS_LOCATION}
}
74 changes: 47 additions & 27 deletions model/strategies/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,6 @@
)


def map_type(element):

try:
type_ = element.__name__
except AttributeError:
type_ = type(element).__name__

if type_ in ['int', 'float']:
return {
"type": "number",
"func": "Number"
}
elif type_ == 'str':
return {
"type": "string",
"func": "String"
}


def process_ml_strategies(strategies_obj):

estimator = strategies_obj["MachineLearning"]["optionalParamsOrder"][0]
Expand Down Expand Up @@ -65,6 +46,49 @@ def process_ml_strategies(strategies_obj):
return strategies_obj


def check_typing(props):
typing_type = str(props.annotation).replace('typing.', '').split('[')

if len(typing_type) > 1:
return typing_type[0]
return props.annotation


def map_type(type_, args):
if type_ in [int, float]:
return {
"type": {
"type": "number",
"func": "Number"
}
}
elif type_ == 'List':
return {
"type": {
"type": "object",
"func": "Split"
}
}

elif type_ == 'Literal':
return {
"type": {
"type": "string",
"func": "String"
},
"options": args
}
elif type_ == str:
return {
"type": {
"type": "string",
"func": "String"
}
}
else:
return None


def compile_strategies():

# Download models from the cloud
Expand Down Expand Up @@ -96,15 +120,11 @@ def compile_strategies():

args = get_args(props.annotation)

if len(args) != 0:
param_info = {"type": map_type(args[0])}
typing_type = check_typing(props)
param_info = map_type(typing_type, args)

if type(args[0]) is not type:
param_info.update({"options": args})
else:
param_info = {
"type": map_type(props.annotation)
}
if param_info is None:
continue

if is_required:
required_ordering.append(param)
Expand Down
2 changes: 1 addition & 1 deletion model/tests/strategies/test_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
import pandas as pd

from model.strategies import compile_strategies
from model.strategies.properties import compile_strategies
from model.tests.setup.test_data.sample_data import data
from model.tests.setup.fixtures.internal_modules import spy_download_file
from model.tests.setup.fixtures.external_modules import (
Expand Down
Loading

0 comments on commit 5e581dd

Please sign in to comment.