Skip to content

Commit

Permalink
Improve Strategies' Jumping
Browse files Browse the repository at this point in the history
- Use group for determining siblings instead of strategy
- Adjust time for overlapping
- Allow overlapping in CatchTheWave
  • Loading branch information
math-a3k committed Aug 22, 2023
1 parent 736f84e commit 97bb4f4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
37 changes: 15 additions & 22 deletions base/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ def get_param(self, param, kwargs):
else:
return int(param_value)

def get_symbols_with_siblings(self):
return self.bot.user.bots.filter(
Q(
timestamp_buying__gt=timezone.now()
- timezone.timedelta(hours=3)
)
| Q(
timestamp_start__gt=timezone.now()
- timezone.timedelta(hours=3)
),
status__gte=self.bot.Status.INACTIVE,
group=self.bot.group,
).values_list("symbol", flat=True)


class ACMadness(TradingStrategy):
"""
Expand Down Expand Up @@ -146,21 +160,7 @@ def evaluate_sell(self):
def evaluate_jump(self):
if self.time_safeguard:
return False, None
symbols_with_siblings = list(
self.bot.user.bots.filter(
Q(
timestamp_buying__gt=timezone.now()
- timezone.timedelta(hours=1)
)
| Q(
timestamp_start__gt=timezone.now()
- timezone.timedelta(hours=1)
),
status__gte=self.bot.Status.INACTIVE,
strategy=self.bot.strategy,
strategy_params=self.bot.strategy_params,
).values_list("symbol", flat=True)
)
symbols_with_siblings = self.get_symbols_with_siblings()
symbols = self.bot.symbol._meta.concrete_model.objects.top_symbols()
symbols = sorted(
symbols, key=lambda s: s.others["stp"]["next_n_sum"], reverse=True
Expand Down Expand Up @@ -300,13 +300,6 @@ def evaluate_jump(self):
return True, symbol
return False, None

def get_symbols_with_siblings(self):
return self.bot.user.bots.filter(
status__gte=self.bot.Status.INACTIVE,
strategy=self.bot.strategy,
strategy_params=self.bot.strategy_params,
).values_list("symbol", flat=True)

def all_positive(self, ts):
return all([t > 0 for t in ts])

Expand Down
44 changes: 37 additions & 7 deletions base/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,26 +1129,43 @@ def setUpClass(cls):
},
},
)
cls.s3, _ = Symbol.objects.update_or_create(
symbol="S3BUSD",
status="TRADING",
base_asset="S3",
quote_asset="BUSD",
defaults={
"model_score": 0.99,
"volume_quote_asset": Decimal(1000000),
"variation_range_24h": Decimal(9),
"info": {
"filters": [
{},
{"stepSize": "0.10000000", "filterType": "LOT_SIZE"},
]
},
},
)
cls.user1, _ = User.objects.get_or_create(
username="user1", password="secret", email="admin@example.com"
)
cls.group1, _ = TraderoBotGroup.objects.get_or_create(
user=cls.user1, name="Test Group 1"
user=cls.user1, name="BotTest Group 1"
)
cls.bot1 = TraderoBot(
cls.bot1, _ = TraderoBot.objects.get_or_create(
name="test_bot1",
symbol=cls.s1,
user=cls.user1,
group=cls.group1,
strategy="acmadness",
strategy_params="",
fund_quote_asset_initial=20,
is_dummy=True,
is_jumpy=True,
)
cls.bot1.save()
cls.bot1.on()
cls.bot2 = TraderoBot(
symbol=cls.s2,
cls.bot2, _ = TraderoBot.objects.get_or_create(
name="test_bot2",
symbol=cls.s3,
user=cls.user1,
group=cls.group1,
strategy="acmadness",
Expand All @@ -1157,7 +1174,6 @@ def setUpClass(cls):
is_dummy=True,
is_jumpy=True,
)
cls.bot2.save()
cls.bot2.on()
with requests_mock.Mocker() as m:
with open("base/fixtures/klines_response_mock.json") as f:
Expand All @@ -1172,13 +1188,21 @@ def setUpClass(cls):
f"&interval={settings.TIME_INTERVAL}m",
text=content,
)
m.get(
f"{BINANCE_API_URL}/api/v3/klines?symbol=S3BUSD"
f"&interval={settings.TIME_INTERVAL}m",
text=content,
)
Symbol.update_all_indicators()
cls.s1.refresh_from_db()
cls.s2.refresh_from_db()
cls.s3.refresh_from_db()
cls.s1.volume_quote_asset = 1000000
cls.s2.volume_quote_asset = 1000000
cls.s3.volume_quote_asset = 1000000
cls.s1.save()
cls.s2.save()
cls.s3.save()
super().setUpClass()


Expand All @@ -1190,6 +1214,7 @@ def test_update_all_bots(self):
json=[
{"symbol": "S1BUSD", "price": "1.0"},
{"symbol": "S2BUSD", "price": "1.0"},
{"symbol": "S3BUSD", "price": "1.0"},
],
)
TraderoBot.update_all_bots()
Expand Down Expand Up @@ -1365,6 +1390,7 @@ def test_acmadness(self):
json={"symbol": "S1BUSD", "price": "1.0"},
)
self.bot1.strategy = "acmadness"
self.bot1.symbol = self.s1
#
self.s1.others["stp"]["next_n_sum"] = 4
self.s1.last_updated = timezone.now() - timezone.timedelta(
Expand Down Expand Up @@ -1412,6 +1438,8 @@ def test_acmadness(self):
"no other symbol to go", self.bot1.others["last_logs"][-1]
)
self.bot1.jumpy_whitelist = "S2BUSD"
self.s2.others["outliers"]["o1"] = False
self.s2.save()
self.bot1.decide()
self.assertIn("Jumped", self.bot1.others["last_logs"][-2])
self.assertIn("Bought", self.bot1.others["last_logs"][-1])
Expand All @@ -1438,6 +1466,7 @@ def test_catchthewave(self):
f"{BINANCE_API_URL}/api/v3/ticker/price",
json={"symbol": "S1BUSD", "price": "1.0"},
)
self.bot1.symbol = self.s1
self.bot1.strategy = "catchthewave"
self.bot1.strategy_params = "use_local_memory=0"
#
Expand Down Expand Up @@ -1594,6 +1623,7 @@ def test_update_all_bots(self):
json=[
{"symbol": "S1BUSD", "price": "1.0"},
{"symbol": "S2BUSD", "price": "1.0"},
{"symbol": "S3BUSD", "price": "1.0"},
],
)
task = tasks.update_all_bots_job.delay()
Expand Down

0 comments on commit 97bb4f4

Please sign in to comment.