Last active
November 6, 2024 16:16
-
-
Save wasertech/698ebcad65f3529982d596626a87c64e to your computer and use it in GitHub Desktop.
Freqtrade Custom Stake 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
from freqtrade.strategy.interface import IStrategy | |
from datetime import datetime | |
from typing import Optional | |
class CustomStake(IStrategy): | |
def custom_stake_amount(self, pair: str, current_time: datetime, current_rate: float, | |
proposed_stake: float, min_stake: Optional[float], max_stake: float, | |
entry_tag: Optional[str], side: str, **kwargs) -> float: | |
""" | |
When you want to use position adjustment with unlimited stakes, you must also implement custom_stake_amount to a return a value depending on your strategy. Typical value would be in the range of 25% - 50% of the proposed stakes, but depends highly on your strategy and how much you wish to leave into the wallet as position adjustment buffer. | |
For example if your position adjustment assumes it can do 2 additional buys with the same stake amounts then your buffer should be 66.6667% of the initially proposed unlimited stake amount. | |
Or another example if your position adjustment assumes it can do 1 additional buy with 3x the original stake amount then custom_stake_amount should return 25% of proposed stake amount and leave 75% for possible later position adjustments. | |
""" | |
# proposed_stake: currency_balance / (max_open_trades - current_open_trades) | |
# https://www.freqtrade.io/en/stable/configuration/#dynamic-stake-amount | |
if not min_stake: | |
min_stake = self.get_min_stake(pair) | |
if not max_stake: | |
max_stake = self.get_max_stake(pair) | |
max_open_trades = self.get_max_open_trades() | |
threshold = 1-(1/max_open_trades) | |
return max(min_stake, min(max_stake, proposed_stake * threshold)) | |
def get_max_open_trades(self) -> int: | |
return self.max_open_trades or 1 | |
def get_min_stake(self, pair: str) -> float: | |
return 10.1 | |
def get_max_stake(self, pair: str) -> float: | |
return 100.0 | |
class Stake3x10Kx1K(CustomStake): | |
def get_max_open_trades(self) -> int: | |
return self.max_open_trades or 3 | |
def get_min_stake(self, pair: str) -> float: | |
return 1_000.0 | |
def get_max_stake(self, pair: str) -> float: | |
return 10_000.0 |
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
MIT License | |
Copyright (c) 2024 Danny Waser | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: This strategy will keep enough reserve to open
max_open_trades
number of trades with an even amount. A better strategy is to fill up each trade to its maximum stake before opening a new one. This can be achieved by returningmax(min_stake, min(max_stake, max(threshold, min(proposed_stake, max_stake))))
instead.