Skip to content

Commit

Permalink
Add utilities for DB data loading
Browse files Browse the repository at this point in the history
  • Loading branch information
adolfojmnz committed Jan 3, 2024
1 parent f1e606a commit 5afe8a7
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import csv

from trades.models import Trade

from assets.models import Currency, CurrencyPair


def format_datetime(timestring):
"""Converts the timestring to ISO format"""
date = timestring.split(" ")[0].split(".")
time = timestring.split(" ")[1]
return f"{date[0]}-{date[1]}-{date[2]} {time}"


def load_trades_from_csv(file_path, user):
with open(file_path, "r") as f:
reader = csv.reader(f)
next(reader) # Skip the header row.

operations = []
for row in reader:
try:
operation = Trade(
user=user,
ticket=int(row[0]),
type="S" if row[2] == "sell" else "L",
currency_pair=CurrencyPair.objects.get(symbol__iexact=row[4]),
open_datetime=format_datetime(row[1]),
close_datetime=format_datetime(row[8]),
open_price=float(row[5]),
stop_loss=float(row[6]),
take_profit=float(row[7]),
close_price=float(row[9]),
volume=float(row[3]),
pnl=float(row[10]),
)
operations.append(operation)
except:
continue
Trade.objects.bulk_create(operations)


def load_currencies_from_csv(file_path):
with open(file_path, "r") as f:
reader = csv.reader(f)
next(reader) # Skip the header row.

currencies = []
for row in reader:
try:
currency = Currency(
symbol=row[0],
name=row[1],
description=row[2],
)
currencies.append(currency)
except:
continue
Currency.objects.bulk_create(currencies)


def load_currency_pairs_from_csv(file_path):
with open(file_path, "r") as f:
reader = csv.reader(f)
next(reader) # Skip the header row.

pairs = []
for row in reader:
try:
pair = CurrencyPair(
symbol=row[0],
base_currency=Currency.objects.get(symbol__iexact=row[1]),
quote_currency=Currency.objects.get(symbol__iexact=row[2]),
pip_decimal_position=int(row[3]),
)
pairs.append(pair)
except:
continue
CurrencyPair.objects.bulk_create(pairs)

0 comments on commit 5afe8a7

Please sign in to comment.