-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from 0xmikemikemike/feat/add-mike-strategy
Adding trading strategy
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2023 Valory AG | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
|
||
"""This module contains the bet amount per threshold strategy.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name: mike_strat | ||
author: valory | ||
version: 0.1.0 | ||
type: custom | ||
description: A simple strategy that bets a fixed amount when the confidence is of a given threshold. | ||
license: Apache-2.0 | ||
aea_version: '>=1.0.0, <2.0.0' | ||
fingerprint: | ||
__init__.py: bafybeidey4syohls5hxmso6qsp5p4uhtzle5txv2mlbym6ktjzknich6oa | ||
mike_s.py: bafybeibqwl52cnz64cysjd2jnjijuakdvyrffapxq65cdzx6g65gu42deq | ||
fingerprint_ignore_patterns: [] | ||
entry_point: mike_strat.py | ||
callable: run | ||
dependencies: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2023 Valory AG | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
|
||
"""This module contains a simple strategy that returns a bet amount based on a mapping.""" | ||
|
||
from typing import Union, List, Dict, Tuple, Any | ||
|
||
REQUIRED_FIELDS = ("confidence", "bet_amount_per_threshold") | ||
|
||
|
||
def check_missing_fields(kwargs: Dict[str, Any]) -> List[str]: | ||
"""Check for missing fields and return them, if any.""" | ||
missing = [] | ||
for field in REQUIRED_FIELDS: | ||
if kwargs.get(field, None) is None: | ||
missing.append(field) | ||
return missing | ||
|
||
|
||
def remove_irrelevant_fields(kwargs: Dict[str, Any]) -> Dict[str, Any]: | ||
"""Remove the irrelevant fields from the given kwargs.""" | ||
return {key: value for key, value in kwargs.items() if key in REQUIRED_FIELDS} | ||
|
||
|
||
def amount_per_threshold( | ||
confidence: float, bet_amount_per_threshold: Dict[str, int] | ||
) -> Dict[str, Union[int, Tuple[str]]]: | ||
"""Get the bet amount per threshold strategy's result.""" | ||
# we need the threshold as a string, because the agent/service overrides cannot be given with `int`s as keys | ||
threshold = str(round(confidence, 1)) | ||
bet_amount = bet_amount_per_threshold.get(threshold, None) | ||
|
||
if bet_amount is None: | ||
return { | ||
"error": ( | ||
f"No amount was found in {bet_amount_per_threshold=} for {confidence=}.", | ||
) | ||
} | ||
return {"bet_amount": bet_amount * confidence} | ||
|
||
|
||
def run(*_args, **kwargs) -> Dict[str, Union[int, Tuple[str]]]: | ||
"""Run the strategy.""" | ||
missing = check_missing_fields(kwargs) | ||
if len(missing) > 0: | ||
return {"error": (f"Required kwargs {missing} were not provided.",)} | ||
|
||
kwargs = remove_irrelevant_fields(kwargs) | ||
return amount_per_threshold(**kwargs) |