Skip to content

Commit

Permalink
Migrate forex app to assets app
Browse files Browse the repository at this point in the history
  • Loading branch information
adolfojmnz committed Oct 21, 2023
1 parent e963c9f commit 7a616e9
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 456 deletions.
File renamed without changes.
17 changes: 1 addition & 16 deletions forex/admin.py → assets/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import admin

from forex.models import Currency, CurrencyPair, ForexOperation
from assets.models import Currency, CurrencyPair


@admin.register(Currency)
Expand All @@ -16,19 +16,4 @@ class CurrencyPairAdmin(admin.ModelAdmin):
"base_currency",
"quote_currency",
"pip_decimal_position",
]


@admin.register(ForexOperation)
class ForexOperationAdmin(admin.ModelAdmin):
list_display = [
"user",
"ticket",
"type",
"open_datetime",
"close_datetime",
"stop_loss",
"take_profit",
"volume",
"pnl"
]
4 changes: 2 additions & 2 deletions forex/apps.py → assets/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.apps import AppConfig


class ForexConfig(AppConfig):
class AssetsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "forex"
name = "assets"
File renamed without changes.
11 changes: 4 additions & 7 deletions forex/helpers/csv_loader.py → assets/helpers/csv_loader.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import csv

from django.utils import timezone
from trades.models import Trade

from forex.models import Currency, CurrencyPair, ForexOperation
from assets.models import Currency, CurrencyPair


def format_datetime(timestring):
Expand All @@ -13,15 +13,14 @@ def format_datetime(timestring):


def load_trades_from_csv(file_path, user):
"""Processes a CSV file and saves the data to the database."""
with open(file_path, "r") as f:
reader = csv.reader(f)
next(reader) # Skip the header row.

operations = []
for row in reader:
try:
operation = ForexOperation(
operation = Trade(
user=user,
ticket=int(row[0]),
type="S" if row[2] == "sell" else "L",
Expand All @@ -40,11 +39,10 @@ def load_trades_from_csv(file_path, user):
operations.append(operation)
except:
continue
ForexOperation.objects.bulk_create(operations)
Trade.objects.bulk_create(operations)


def load_currencies_from_csv(file_path):
"""Processes a CSV file and saves the data to the database."""
with open(file_path, "r") as f:
reader = csv.reader(f)
next(reader) # Skip the header row.
Expand All @@ -64,7 +62,6 @@ def load_currencies_from_csv(file_path):


def load_currency_pairs_from_csv(file_path):
"""Processes a CSV file and saves the data to the database."""
with open(file_path, "r") as f:
reader = csv.reader(f)
next(reader) # Skip the header row.
Expand Down
63 changes: 63 additions & 0 deletions assets/helpers/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from assets.models import Currency, CurrencyPair

EUR_DATA = {"symbol": "EUR", "name": "Euro", "description": "..."}


def create_eur():
return Currency.objects.create(
symbol="EUR",
name="Euro",
description="Official currency of 20 of the 27 EU nations",
)


def create_gbp():
return Currency.objects.create(
symbol="GBP",
name="Great Britain Pound",
description="Official currency of the United Kingdom",
)


def create_jpy():
return Currency.objects.create(
symbol="JPY",
name="Japanese Yen",
description="Official currency of Japan",
)


def create_eurgbp_pair(eur=None, gbp=None):
return CurrencyPair.objects.create(
symbol="EUR/GBP",
name="EUR/GBP Forex Pair",
base_currency=eur or create_eur(),
quote_currency=gbp or create_gbp(),
)


def create_eurjpy_pair(eur=None, jpy=None):
return CurrencyPair.objects.create(
name="EUR/JPY Forex Pair",
base_currency=eur or create_eur(),
quote_currency=jpy or create_jpy(),
pip_decimal_position=2,
)


def create_gbpjpy_pair(gbp=None, jpy=None):
return CurrencyPair.objects.create(
name="GBP/JPY Forex Pair",
base_currency=gbp or create_gbp(),
quote_currency=jpy or create_jpy(),
pip_decimal_position=2,
)


def create_currency_pair_list():
eur = create_eur()
gbp = create_gbp()
jpy = create_jpy()
create_eurgbp_pair(eur, gbp)
create_eurjpy_pair(eur, jpy)
create_gbpjpy_pair(gbp, jpy)
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# Generated by Django 4.2.6 on 2023-10-14 23:12
# Generated by Django 4.2.6 on 2023-10-20 22:40

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
dependencies = []

operations = [
migrations.CreateModel(
Expand All @@ -27,7 +24,9 @@ class Migration(migrations.Migration):
),
(
"symbol",
models.CharField(help_text="ISO 4217 Currency Code", max_length=3),
models.CharField(
help_text="ISO 4217 Currency Code", max_length=3, unique=True
),
),
("name", models.CharField(max_length=64)),
("description", models.CharField(max_length=128)),
Expand All @@ -45,8 +44,11 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
("symbol", models.CharField(blank=True, max_length=64, null=True)),
("name", models.CharField(max_length=128)),
(
"symbol",
models.CharField(blank=True, max_length=64, null=True, unique=True),
),
("name", models.CharField(blank=True, max_length=128)),
(
"pip_decimal_position",
models.IntegerField(
Expand All @@ -60,7 +62,7 @@ class Migration(migrations.Migration):
help_text="Currency that is being bought or sold",
on_delete=django.db.models.deletion.PROTECT,
related_name="base_currency",
to="forex.currency",
to="assets.currency",
),
),
(
Expand All @@ -69,55 +71,7 @@ class Migration(migrations.Migration):
help_text="Currency that is used to price the base currency",
on_delete=django.db.models.deletion.PROTECT,
related_name="quote_currency",
to="forex.currency",
),
),
],
),
migrations.CreateModel(
name="ForexOperation",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"ticket",
models.IntegerField(
help_text="ID of the trade on the trading platform"
),
),
(
"type",
models.CharField(
choices=[("L", "Long"), ("S", "Short")], max_length=1
),
),
("open_datetime", models.DateTimeField()),
("close_datetime", models.DateTimeField()),
("open_price", models.FloatField()),
("stop_loss", models.FloatField()),
("take_profit", models.FloatField()),
("close_price", models.FloatField()),
("volume", models.FloatField(default=0.01)),
("pnl", models.FloatField(help_text="Profit/loss in USD")),
(
"currency_pair",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
to="forex.currencypair",
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
to="assets.currency",
),
),
],
Expand Down
File renamed without changes.
32 changes: 3 additions & 29 deletions forex/models.py → assets/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
from accounts.models import User


OPERATION_TYPE_CHOICE = [
("L", "Long"),
("S", "Short")
]


class Currency(models.Model):
symbol = models.CharField(
unique=True,
Expand All @@ -31,6 +25,8 @@ class CurrencyPair(models.Model):
)
name = models.CharField(
blank=True,
null=True,
default=None,
max_length=128,
)
base_currency = models.ForeignKey(
Expand Down Expand Up @@ -58,26 +54,4 @@ def save(self, *args, **kwargs):
return super().save(*args, **kwargs)

def __str__(self) -> str:
return self.symbol


class ForexOperation(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
ticket = models.IntegerField(
unique=True,
db_index=True,
help_text="ID of the trade on the trading platform"
)
type = models.CharField(max_length=1, choices=OPERATION_TYPE_CHOICE)
currency_pair = models.ForeignKey(CurrencyPair, on_delete=models.PROTECT)
open_datetime = models.DateTimeField()
close_datetime = models.DateTimeField()
open_price = models.FloatField()
stop_loss = models.FloatField()
take_profit = models.FloatField()
close_price = models.FloatField()
volume = models.FloatField(default=0.01)
pnl = models.FloatField(help_text="Profit/loss in USD")

def __str__(self) -> str:
return f"{self.type} on {self.currency_pair.symbol}"
return self.symbol
14 changes: 14 additions & 0 deletions assets/routers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.urls import path

from assets import views


urlpatterns = [
# Currency
path("assets/currencies", views.CurrencyListView.as_view(), name="currency-list"),
path("assets/currencies/<int:pk>", views.CurrencyDetailView.as_view(), name="currency-detail"),

# Currency Pair
path("assets/currency-pairs", views.CurrencyPairListView.as_view(), name="currency-pair-list"),
path("assets/currency-pairs/<int:pk>", views.CurrencyPairDetailView.as_view(), name="currency-pair-detail"),
]
9 changes: 1 addition & 8 deletions forex/serializers.py → assets/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import serializers

from forex.models import Currency, CurrencyPair, ForexOperation
from assets.models import Currency, CurrencyPair


class CurrencySerializer(serializers.ModelSerializer):
Expand All @@ -14,11 +14,4 @@ class CurrencyPairSerializer(serializers.ModelSerializer):

class Meta:
model = CurrencyPair
fields = "__all__"


class ForexOperationSerializer(serializers.ModelSerializer):

class Meta:
model = ForexOperation
fields = "__all__"
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from rest_framework import status
from rest_framework.test import APIClient

from forex.models import Currency
from forex.serializers import CurrencySerializer
from assets.models import Currency
from assets.serializers import CurrencySerializer

from accounts.utils import create_test_admin, create_test_user
from forex.utils import EUR_DATA, create_eur, create_currency_pair_list
from assets.helpers.test_utils import EUR_DATA, create_eur, create_currency_pair_list


class SetUpTestCase(TestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from rest_framework import status
from rest_framework.test import APIClient

from forex.models import CurrencyPair
from forex.serializers import CurrencyPairSerializer
from assets.models import CurrencyPair
from assets.serializers import CurrencyPairSerializer

from accounts.utils import create_test_admin, create_test_user
from forex.utils import (
from assets.helpers.test_utils import (
create_eur,
create_gbp,
create_eurgbp_pair,
Expand Down
Loading

0 comments on commit 7a616e9

Please sign in to comment.