Skip to content

Commit

Permalink
Merge pull request #106 from napse-invest/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
tomjeannesson authored Aug 14, 2023
2 parents aada91b + 48d6270 commit a18c9cd
Show file tree
Hide file tree
Showing 51 changed files with 984 additions and 130 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
ec2-instance-id: ${{ steps.start-ec2-runner.outputs.ec2-instance-id }}
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
Expand All @@ -40,6 +40,7 @@ jobs:
needs:
- start-runner # required to start the main job when the runner is ready
runs-on: ${{ needs.start-runner.outputs.label }} # run the job on the newly created runner
timeout-minutes: 10
strategy:
max-parallel: 4

Expand Down Expand Up @@ -105,7 +106,7 @@ jobs:
if: ${{ always() }} # required to stop the runner even if the error happened in the previous jobs
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
Expand Down
217 changes: 183 additions & 34 deletions django_napse/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.1.7 on 2023-08-12 16:30
# Generated by Django 4.1.7 on 2023-08-14 16:31

import datetime
from django.db import migrations, models
Expand Down Expand Up @@ -252,6 +252,22 @@ class Migration(migrations.Migration):
("description", models.TextField()),
],
),
migrations.CreateModel(
name="Plugin",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("category", models.CharField(max_length=255)),
],
bases=(models.Model, django_napse.utils.findable_class.FindableClass),
),
migrations.CreateModel(
name="Strategy",
fields=[
Expand All @@ -264,6 +280,22 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
(
"architecture",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
related_name="strategy",
to="django_napse_core.architecture",
),
),
(
"config",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
related_name="strategy",
to="django_napse_core.botconfig",
),
),
],
bases=(models.Model, django_napse.utils.findable_class.FindableClass),
),
Expand Down Expand Up @@ -302,6 +334,42 @@ class Migration(migrations.Migration):
],
bases=("django_napse_core.modification",),
),
migrations.CreateModel(
name="DCABotConfig",
fields=[
(
"botconfig_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="django_napse_core.botconfig",
),
),
("setting_timeframe", models.DurationField()),
],
bases=("django_napse_core.botconfig",),
),
migrations.CreateModel(
name="DCAStrategy",
fields=[
(
"strategy_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="django_napse_core.strategy",
),
),
("variable_last_buy_date", models.DateTimeField(blank=True, null=True)),
],
bases=("django_napse_core.strategy",),
),
migrations.CreateModel(
name="EmptyBotConfig",
fields=[
Expand All @@ -320,6 +388,23 @@ class Migration(migrations.Migration):
],
bases=("django_napse_core.botconfig",),
),
migrations.CreateModel(
name="EmptyStrategy",
fields=[
(
"strategy_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="django_napse_core.strategy",
),
),
],
bases=("django_napse_core.strategy",),
),
migrations.CreateModel(
name="EquilibriumFleetOperator",
fields=[
Expand All @@ -337,6 +422,57 @@ class Migration(migrations.Migration):
],
bases=("django_napse_core.defaultfleetoperator",),
),
migrations.CreateModel(
name="LBOPlugin",
fields=[
(
"plugin_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="django_napse_core.plugin",
),
),
],
bases=("django_napse_core.plugin",),
),
migrations.CreateModel(
name="MBPPlugin",
fields=[
(
"plugin_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="django_napse_core.plugin",
),
),
],
bases=("django_napse_core.plugin",),
),
migrations.CreateModel(
name="SBVPlugin",
fields=[
(
"plugin_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="django_napse_core.plugin",
),
),
],
bases=("django_napse_core.plugin",),
),
migrations.CreateModel(
name="SpecificSharesFleetOperator",
fields=[
Expand Down Expand Up @@ -371,6 +507,43 @@ class Migration(migrations.Migration):
],
bases=("django_napse_core.modification",),
),
migrations.CreateModel(
name="TurboDCABotConfig",
fields=[
(
"botconfig_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="django_napse_core.botconfig",
),
),
("setting_timeframe", models.DurationField()),
("setting_percentage", models.FloatField()),
],
bases=("django_napse_core.botconfig",),
),
migrations.CreateModel(
name="TurboDCAStrategy",
fields=[
(
"strategy_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="django_napse_core.strategy",
),
),
("variable_last_buy_date", models.DateTimeField(blank=True, null=True)),
],
bases=("django_napse_core.strategy",),
),
migrations.CreateModel(
name="Transaction",
fields=[
Expand Down Expand Up @@ -408,6 +581,15 @@ class Migration(migrations.Migration):
),
],
),
migrations.AddField(
model_name="plugin",
name="strategy",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="plugins",
to="django_napse_core.strategy",
),
),
migrations.CreateModel(
name="OrderBatch",
fields=[
Expand Down Expand Up @@ -927,39 +1109,6 @@ class Migration(migrations.Migration):
],
bases=("django_napse_core.wallet",),
),
migrations.CreateModel(
name="EmptyStrategy",
fields=[
(
"strategy_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="django_napse_core.strategy",
),
),
(
"architecture",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
related_name="strategy",
to="django_napse_core.singlepairarchitecture",
),
),
(
"config",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
related_name="strategy",
to="django_napse_core.emptybotconfig",
),
),
],
bases=("django_napse_core.strategy",),
),
migrations.CreateModel(
name="Currency",
fields=[
Expand Down
2 changes: 2 additions & 0 deletions django_napse/core/models/bots/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
from .config import *
from .controller import *
from .implementations import *
from .plugin import *
from .plugins import *
from .strategy import *
16 changes: 13 additions & 3 deletions django_napse/core/models/bots/architecture.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from django.db import models

from django_napse.core.models.bots.managers import ArchitectureManager
from django_napse.utils.constants import ORDER_LEEWAY_PERCENTAGE, SIDES
from django_napse.core.models.bots.strategy import Strategy
from django_napse.utils.constants import ORDER_LEEWAY_PERCENTAGE, PLUGIN_CATEGORIES, SIDES
from django_napse.utils.errors.orders import OrderError
from django_napse.utils.findable_class import FindableClass

Expand All @@ -23,6 +24,10 @@ def variables(self):
variables[variable.name[8:]] = getattr(self, variable.name)
return variables

@property
def strategy(self):
return Strategy.objects.get(architecture=self).find()

def get_candles(self): # pragma: no cover
if self.__class__ == Architecture:
error_msg = "get_candles not implemented for the Architecture base class, please implement it in a subclass."
Expand Down Expand Up @@ -68,11 +73,12 @@ def prepare_data(self):
def prepare_db_data(self):
return {
"strategy": self.strategy.find(),
"config": self.strategy.find().config.find().settings,
"architecture": self.find(),
"controllers": self.controllers_dict(),
"connections": self.strategy.bot.get_connections(),
"connection_data": self.strategy.bot.get_connection_data(),
# "plugins": self.strategy.plugins.all(),
"plugins": {category: self.strategy.plugins.filter(category=category) for category in PLUGIN_CATEGORIES},
}

def _get_orders(self, data: dict, no_db_data: Optional[dict] = None) -> list[dict]:
Expand All @@ -84,11 +90,15 @@ def _get_orders(self, data: dict, no_db_data: Optional[dict] = None) -> list[dic
all_orders = []
for connection in connections:
new_data = {**data, **no_db_data, "connection": connection}

# pprint(new_data)
if architecture.skip(data=new_data):
continue
for plugin in no_db_data["plugins"][PLUGIN_CATEGORIES.PRE_ORDER]:
plugin.apply(data=new_data)
orders = strategy.give_order(data=new_data)
for order in orders:
for plugin in no_db_data["plugins"][PLUGIN_CATEGORIES.POST_ORDER]:
plugin.apply(data={**new_data, "order": order})
order["StrategyModifications"] += architecture.strategy_modifications(order=order, data=new_data)
order["ConnectionModifications"] += architecture.connection_modifications(order=order, data=new_data)
order["ArchitectureModifications"] += architecture.architecture_modifications(order=order, data=new_data)
Expand Down
6 changes: 5 additions & 1 deletion django_napse/core/models/bots/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ def _get_orders(self, data: Optional[dict] = None, no_db_data: Optional[dict] =
return self.architecture._get_orders(data=data, no_db_data=no_db_data)

def connect_to(self, wallet):
return Connection.objects.create(owner=wallet, bot=self)
connection = Connection.objects.create(owner=wallet, bot=self)
for plugin in self._strategy.plugins.all():
plugin.connect(connection)
self._strategy.connect(connection)
return connection

def copy(self):
return self.__class__.objects.create(
Expand Down
5 changes: 5 additions & 0 deletions django_napse/core/models/bots/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.db import models

from django_napse.core.models.bots.managers.bot_config import BotConfigManager
from django_napse.core.models.bots.strategy import Strategy
from django_napse.utils.findable_class import FindableClass


Expand Down Expand Up @@ -36,6 +37,10 @@ def settings(self):
settings[setting.name[8:]] = getattr(self, setting.name)
return settings

@property
def strategy(self):
return Strategy.objects.get(config=self).find()

def duplicate_immutable(self):
return self.__class__.objects.create(
space=self.space,
Expand Down
Loading

0 comments on commit a18c9cd

Please sign in to comment.