Skip to content

Commit

Permalink
Merge pull request #197 from 0xmikemikemike/feat/add-mike-strategy
Browse files Browse the repository at this point in the history
Adding trading strategy
  • Loading branch information
Adamantios authored Jan 8, 2024
2 parents cbfd215 + ab67d64 commit 4e686ec
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
20 changes: 20 additions & 0 deletions strategies/mike_strat/__init__.py
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."""
14 changes: 14 additions & 0 deletions strategies/mike_strat/component.yaml
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: {}
65 changes: 65 additions & 0 deletions strategies/mike_strat/mike_strat.py
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)

0 comments on commit 4e686ec

Please sign in to comment.