Begin adding order settings

This commit is contained in:
Mark Veidemanis 2023-02-15 18:33:38 +00:00
parent 1974b19157
commit 660aca44db
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
11 changed files with 220 additions and 19 deletions

View File

@ -29,6 +29,7 @@ from core.views import (
hooks, hooks,
limits, limits,
notifications, notifications,
ordersettings,
positions, positions,
profit, profit,
risk, risk,
@ -284,4 +285,25 @@ urlpatterns = [
assets.AssetRuleDelete.as_view(), assets.AssetRuleDelete.as_view(),
name="assetrule_delete", name="assetrule_delete",
), ),
# Order Settings
path(
"ordersettings/<str:type>/",
ordersettings.OrderSettingsList.as_view(),
name="ordersettings",
),
path(
"ordersettings/<str:type>/create/",
ordersettings.OrderSettingsCreate.as_view(),
name="ordersettings_create",
),
path(
"ordersettings/<str:type>/update/<str:pk>/",
ordersettings.OrderSettingsUpdate.as_view(),
name="ordersettings_update",
),
path(
"ordersettings/<str:type>/delete/<str:pk>/",
ordersettings.OrderSettingsDelete.as_view(),
name="ordersettings_delete",
),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

View File

@ -10,6 +10,7 @@ from .models import ( # AssetRestriction,
AssetRule, AssetRule,
Hook, Hook,
NotificationSettings, NotificationSettings,
OrderSettings,
RiskModel, RiskModel,
Signal, Signal,
Strategy, Strategy,
@ -125,16 +126,17 @@ class StrategyForm(RestrictedFormMixin, ModelForm):
"asset_group", "asset_group",
"risk_model", "risk_model",
"trading_times", "trading_times",
"order_type", "order_settings",
"time_in_force", # "order_type",
# "time_in_force",
"entry_signals", "entry_signals",
"exit_signals", "exit_signals",
"trend_signals", "trend_signals",
"enabled", "enabled",
"take_profit_percent", # "take_profit_percent",
"stop_loss_percent", # "stop_loss_percent",
"trailing_stop_loss_percent", # "trailing_stop_loss_percent",
"trade_size_percent", # "trade_size_percent",
) )
help_texts = { help_texts = {
@ -144,16 +146,16 @@ class StrategyForm(RestrictedFormMixin, ModelForm):
"asset_group": "Asset groups determine which pairs can be traded.", "asset_group": "Asset groups determine which pairs can be traded.",
"risk_model": "The risk model to use for this strategy. Highly recommended.", "risk_model": "The risk model to use for this strategy. Highly recommended.",
"trading_times": "When the strategy will place new trades.", "trading_times": "When the strategy will place new trades.",
"order_type": "Market: Buy/Sell at the current market price. Limit: Buy/Sell at a specified price. Limits protect you more against market slippage.", # "order_type": "Market: Buy/Sell at the current market price. Limit: Buy/Sell at a specified price. Limits protect you more against market slippage.",
"time_in_force": "The time in force controls how the order is executed.", # "time_in_force": "The time in force controls how the order is executed.",
"entry_signals": "Callbacks received to these signals will trigger a trade.", "entry_signals": "Callbacks received to these signals will trigger a trade.",
"exit_signals": "Callbacks received to these signals will close all trades for the symbol on the account.", "exit_signals": "Callbacks received to these signals will close all trades for the symbol on the account.",
"trend_signals": "Callbacks received to these signals will limit the trading direction of the given symbol to the callback direction until further notice.", "trend_signals": "Callbacks received to these signals will limit the trading direction of the given symbol to the callback direction until further notice.",
"enabled": "Whether the strategy is enabled.", "enabled": "Whether the strategy is enabled.",
"take_profit_percent": "The take profit will be set at this percentage above/below the entry price.", # "take_profit_percent": "The take profit will be set at this percentage above/below the entry price.",
"stop_loss_percent": "The stop loss will be set at this percentage above/below the entry price.", # "stop_loss_percent": "The stop loss will be set at this percentage above/below the entry price.",
"trailing_stop_loss_percent": "The trailing stop loss will be set at this percentage above/below the entry price. A trailing stop loss will follow the price as it moves in your favor.", # "trailing_stop_loss_percent": "The trailing stop loss will be set at this percentage above/below the entry price. A trailing stop loss will follow the price as it moves in your favor.",
"trade_size_percent": "Percentage of the account balance to use for each trade.", # "trade_size_percent": "Percentage of the account balance to use for each trade.",
} }
entry_signals = forms.ModelMultipleChoiceField( entry_signals = forms.ModelMultipleChoiceField(
@ -361,3 +363,28 @@ class AssetRuleForm(RestrictedFormMixin, ModelForm):
"trigger_below": "Trigger Bearish when value is below this.", "trigger_below": "Trigger Bearish when value is below this.",
"trigger_above": "Trigger Bullish when value is above this.", "trigger_above": "Trigger Bullish when value is above this.",
} }
class OrderSettingsForm(RestrictedFormMixin, ModelForm):
class Meta:
model = OrderSettings
fields = (
"name",
"description",
"order_type",
"time_in_force",
"take_profit_percent",
"stop_loss_percent",
"trailing_stop_loss_percent",
"trade_size_percent",
)
help_texts = {
"name": "Name of the order settings. Informational only.",
"description": "Description of the order settings. Informational only.",
"order_type": "Market: Buy/Sell at the current market price. Limit: Buy/Sell at a specified price. Limits protect you more against market slippage.",
"time_in_force": "The time in force controls how the order is executed.",
"take_profit_percent": "The take profit will be set at this percentage above/below the entry price.",
"stop_loss_percent": "The stop loss will be set at this percentage above/below the entry price.",
"trailing_stop_loss_percent": "The trailing stop loss will be set at this percentage above/below the entry price. A trailing stop loss will follow the price as it moves in your favor.",
"trade_size_percent": "Percentage of the account balance to use for each trade.",
}

View File

@ -1,7 +1,7 @@
# Generated by Django 4.1.6 on 2023-02-15 18:11 # Generated by Django 4.1.6 on 2023-02-15 18:11
from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -0,0 +1,30 @@
# Generated by Django 4.1.6 on 2023-02-15 18:31
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0066_remove_account_risk_model_and_more'),
]
operations = [
migrations.CreateModel(
name='OrderSettings',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
('description', models.TextField(blank=True, null=True)),
('order_type', models.CharField(choices=[('market', 'Market'), ('limit', 'Limit')], default='market', max_length=255)),
('time_in_force', models.CharField(choices=[('gtc', 'GTC (Good Til Cancelled)'), ('gfd', 'GFD (Good For Day)'), ('fok', 'FOK (Fill Or Kill)'), ('ioc', 'IOC (Immediate Or Cancel)')], default='gtc', max_length=255)),
('take_profit_percent', models.FloatField(default=1.5)),
('stop_loss_percent', models.FloatField(default=1.0)),
('trailing_stop_loss_percent', models.FloatField(blank=True, default=1.0, null=True)),
('trade_size_percent', models.FloatField(default=0.5)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]

View File

@ -397,6 +397,9 @@ class Strategy(models.Model):
risk_model = models.ForeignKey( risk_model = models.ForeignKey(
"core.RiskModel", on_delete=models.PROTECT, null=True, blank=True "core.RiskModel", on_delete=models.PROTECT, null=True, blank=True
) )
order_settings = models.ForeignKey(
"core.OrderSettings", on_delete=models.PROTECT, null=True, blank=True
)
class Meta: class Meta:
verbose_name_plural = "strategies" verbose_name_plural = "strategies"
@ -481,3 +484,17 @@ class AssetRule(models.Model):
# Ensure that the asset is unique per group # Ensure that the asset is unique per group
class Meta: class Meta:
unique_together = ("asset", "group") unique_together = ("asset", "group")
class OrderSettings(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
description = models.TextField(null=True, blank=True)
order_type = models.CharField(
choices=TYPE_CHOICES, max_length=255, default="market"
)
time_in_force = models.CharField(choices=TIF_CHOICES, max_length=255, default="gtc")
take_profit_percent = models.FloatField(default=1.5)
stop_loss_percent = models.FloatField(default=1.0)
trailing_stop_loss_percent = models.FloatField(default=1.0, null=True, blank=True)
trade_size_percent = models.FloatField(default=0.5)

View File

@ -248,6 +248,9 @@
<a class="navbar-item" href="{% url 'strategies' type='page' %}"> <a class="navbar-item" href="{% url 'strategies' type='page' %}">
Strategies Strategies
</a> </a>
<a class="navbar-item" href="{% url 'ordersettings' type='page' %}">
Order Settings
</a>
<a class="navbar-item" href="{% url 'signals' type='page' %}"> <a class="navbar-item" href="{% url 'signals' type='page' %}">
Signals Signals
</a> </a>

View File

@ -24,7 +24,7 @@
<td>{{ item.id }}</td> <td>{{ item.id }}</td>
<td>{{ item.user }}</td> <td>{{ item.user }}</td>
<td>{{ item.name }}</td> <td>{{ item.name }}</td>
<td>{{ item.description }}</td> <td>{{ item.description|truncatechars:80 }}</td>
<td> <td>
<a <a
href="{% url 'assetrules' type='page' group=item.id %}"> href="{% url 'assetrules' type='page' group=item.id %}">

View File

@ -0,0 +1,69 @@
{% load cache %}
{% load cachalot cache %}
{% get_last_invalidation 'core.OrderSettings' as last %}
{% include 'mixins/partials/notify.html' %}
{% cache 600 objects_ordersettings request.user.id object_list type last %}
<table
class="table is-fullwidth is-hoverable"
hx-target="#{{ context_object_name }}-table"
id="{{ context_object_name }}-table"
hx-swap="outerHTML"
hx-trigger="{{ context_object_name_singular }}Event from:body"
hx-get="{{ list_url }}">
<thead>
<th>id</th>
<th>user</th>
<th>name</th>
<th>description</th>
<th>TP</th>
<th>SL</th>
<th>TSL</th>
<th>size</th>
<th>actions</th>
</thead>
{% for item in object_list %}
<tr>
<td>{{ item.id }}</td>
<td>{{ item.user }}</td>
<td>{{ item.name }}</td>
<td>{{ item.description|truncatechars:80 }}</td>
<td>{{ item.take_profit_percent}}</td>
<td>{{ item.stop_loss_percent }}</td>
<td>{{ item.trailing_stop_loss_percent }}</td>
<td>{{ item.trade_size_percent }}</td>
<td>
<div class="buttons">
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-get="{% url 'ordersettings_update' type=type pk=item.id %}"
hx-trigger="click"
hx-target="#{{ type }}s-here"
hx-swap="innerHTML"
class="button">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-pencil"></i>
</span>
</span>
</button>
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-delete="{% url 'ordersettings_delete' type=type pk=item.id %}"
hx-trigger="click"
hx-target="#modals-here"
hx-swap="innerHTML"
hx-confirm="Are you sure you wish to delete {{ item.name }}?"
class="button">
<span class="icon-text">
<span class="icon">
<i class="fa-solid fa-xmark"></i>
</span>
</span>
</button>
</div>
</td>
</tr>
{% endfor %}
</table>
{% endcache %}

View File

@ -16,8 +16,6 @@
<th>description</th> <th>description</th>
<th>account</th> <th>account</th>
<th>enabled</th> <th>enabled</th>
<th>TP</th>
<th>SL</th>
<th>actions</th> <th>actions</th>
</thead> </thead>
{% for item in object_list %} {% for item in object_list %}
@ -37,8 +35,6 @@
</span> </span>
{% endif %} {% endif %}
</td> </td>
<td>{{ item.take_profit_percent }}</td>
<td>{{ item.stop_loss_percent }}</td>
<td> <td>
<div class="buttons"> <div class="buttons">
<button <button

View File

@ -24,7 +24,7 @@
<td>{{ item.id }}</td> <td>{{ item.id }}</td>
<td>{{ item.user }}</td> <td>{{ item.user }}</td>
<td>{{ item.name }}</td> <td>{{ item.name }}</td>
<td>{{ item.description }}</td> <td>{{ item.description|truncatechars:80 }}</td>
<td>{{ item.get_start_day_display }} at {{ item.start_time }}</td> <td>{{ item.get_start_day_display }} at {{ item.start_time }}</td>
<td>{{ item.get_end_day_display }} at {{ item.end_time }}</td> <td>{{ item.get_end_day_display }} at {{ item.end_time }}</td>
<td> <td>

View File

@ -0,0 +1,37 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from mixins.views import ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate
from core.forms import OrderSettingsForm
from core.models import OrderSettings
from core.util import logs
log = logs.get_logger(__name__)
class OrderSettingsList(LoginRequiredMixin, ObjectList):
list_template = "partials/ordersettings-list.html"
model = OrderSettings
page_title = "List of order settings. Linked to strategies."
list_url_name = "ordersettings"
list_url_args = ["type"]
submit_url_name = "ordersettings_create"
class OrderSettingsCreate(LoginRequiredMixin, ObjectCreate):
model = OrderSettings
form_class = OrderSettingsForm
submit_url_name = "ordersettings_create"
class OrderSettingsUpdate(LoginRequiredMixin, ObjectUpdate):
model = OrderSettings
form_class = OrderSettingsForm
submit_url_name = "ordersettings_update"
class OrderSettingsDelete(LoginRequiredMixin, ObjectDelete):
model = OrderSettings