Skip to content

Commit

Permalink
Improve CatchTheWave Strategy
Browse files Browse the repository at this point in the history
- Add support for the Early Onset status
  • Loading branch information
math-a3k committed Aug 8, 2023
1 parent cccec2b commit 585663d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
23 changes: 19 additions & 4 deletions base/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,20 @@ def __init__(self, bot, symbol=None, **kwargs):
self.bot = bot
self.symbol = symbol or self.bot.symbol
self.scg = self.symbol.others["scg"]
self.cg = self.scg["current_good"]
self.diff_sm = self.scg["line_diff_sm"] # Short - Middle tendency
self.s_var = self.scg["line_s_var"] # Var of the Short tendency
self.l_var = self.scg["line_l_var"] # Var of the Long tendency
#
self.early_onset = bool(int(kwargs.get("early_onset", "0")))
self.sell_on_maxima = bool(int(kwargs.get("sell_on_maxima", "1")))
self.onset_periods = int(kwargs.get("onset_periods", "2"))
self.maxima_tol = Decimal(kwargs.get("maxima_tol", "0.1"))
self.sell_safeguard = Decimal(kwargs.get("sell_safeguard", "0.3"))

def evaluate_buy(self):
if self.cg and self.is_on_wave_onset():
if self.is_on_good_status() and self.is_on_wave_onset():
return True, None
return (False, "Symbol is not in CG and ascending...")
return (False, "Symbol is not in good status and ascending...")

def evaluate_sell(self):
if self.bot.price_current > self.get_min_selling_threshold():
Expand All @@ -197,9 +198,13 @@ def evaluate_sell(self):
def evaluate_jump(self):
symbols_with_siblings = self.get_symbols_with_siblings()
symbols = self.bot.symbol._meta.concrete_model.objects.top_symbols()
if self.early_onset:
key = lambda s: s.others["scg"]["seo_index"]
else:
key = lambda s: s.others["scg"]["scg_index"]
symbols = sorted(
symbols,
key=lambda s: s.others["scg"]["scg_index"],
key=key,
reverse=True,
)
symbols_blacklist = self.bot.jumpy_blacklist.split(",")
Expand All @@ -224,6 +229,13 @@ def get_symbols_with_siblings(self):
def all_positive(self, ts):
return all([t > 0 for t in ts])

def is_on_good_status(self):
if self.early_onset:
return self.scg["early_onset"] and self.not_decreasing(
self.l_var[-self.onset_periods :] # long-term line
)
return self.scg["current_good"]

def is_local_maxima(self):
return self.s_var[-1] * 100 < -self.maxima_tol

Expand All @@ -233,5 +245,8 @@ def is_on_wave(self):
def is_on_wave_onset(self):
return self.all_positive(self.s_var[-self.onset_periods :])

def not_decreasing(self, line):
return all([l * 100 > -self.maxima_tol for l in line])

def get_min_selling_threshold(self):
return self.bot.price_buying * (1 + self.sell_safeguard / 100)
10 changes: 10 additions & 0 deletions base/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,16 @@ def test_catchthewave(self):
self.assertIn(
"no other symbol to go", self.bot1.others["last_logs"][-1]
)
self.bot1.strategy_params = "early_onset=1"
self.bot1.symbol.others["scg"]["early_onset"] = True
self.bot1.symbol.others["scg"]["line_l_var"] = [0.1, 0, -0.11]
self.s1.others["scg"]["current_good"] = False
self.s1.others["scg"]["early_onset"] = True
self.s1.others["scg"]["line_l_var"] = [1, 1, 2]
self.s1.save()
self.bot1.decide()
self.assertIn("Jumped", self.bot1.others["last_logs"][-2])
self.assertIn("Bought", self.bot1.others["last_logs"][-1])


@pytest.mark.usefixtures("celery_session_app")
Expand Down
7 changes: 6 additions & 1 deletion docs/trading_bots.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,16 @@ CatchTheWave

Also known as *dolphin bots*, this strategy [4]_ relies on the :ref:`scg` indicator for buying.

Given a Symbol, the bot will buy when it is in "Current Good" and the short-term tendency is increasing ("wave onset"), while selling when the short-term line crosses the middle-term one ("end of the wave").
Given a Symbol, the bot will buy when it is on "good status" while selling when the short-term line crosses the middle-term one ("end of the wave").

*Good Status* is either when Symbol is at "Current Good" and the short-term tendency is increasing ("wave onset") or at "Early Onset" and the long-term line is not decreasing.

Parameters
^^^^^^^^^^

``early_onset``
Use the *Early Onset* status (``1`` | ``0``, defaults to ``0``).

``sell_on_maxima``
Sell after the short-term tendency has a local maxima (``1`` | ``0``, defaults to ``1``).

Expand Down

0 comments on commit 585663d

Please sign in to comment.