Skip to content

Commit

Permalink
Fix ohlcv data sources tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Mar 22, 2024
1 parent f1b70a3 commit c6defde
Show file tree
Hide file tree
Showing 11 changed files with 1,010 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def get_data(self, index_datetime=None, **kwargs):
if index_datetime is None:
index_datetime = datetime.utcnow()

index_datetime = pd.to_datetime(index_datetime, utc=True)
df = pd.read_csv(self._csv_file_path)

# Convert the 'Datetime' column to datetime type if
Expand All @@ -161,6 +162,10 @@ def get_data(self, index_datetime=None, **kwargs):

# Filter rows based on the start and end dates
filtered_df = df[(df['Datetime'] <= index_datetime)]

if len(filtered_df) == 0:
return None

last_row = filtered_df.iloc[-1]
return {
"symbol": self.symbol,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from datetime import datetime
from dateutil.tz import tzutc
from queue import PriorityQueue

from investing_algorithm_framework.domain import OrderType, OrderSide, \
Expand Down Expand Up @@ -139,7 +140,7 @@ def update(self, object_id, data):
if "updated_at" in data:
created_at = data["updated_at"]
else:
created_at = datetime.now()
created_at = datetime.now(tz=tzutc())

self.create_snapshot(portfolio.id, created_at=created_at)
return new_order
Expand Down Expand Up @@ -187,15 +188,15 @@ def execute_order(self, order_id, portfolio):

data = external_order.to_dict()
data["status"] = OrderStatus.OPEN.value
data["updated_at"] = datetime.now()
data["updated_at"] = datetime.now(tz=tzutc())
return self.update(order_id, data)
except Exception as e:
logger.error("Error executing order: {}".format(e))
return self.update(
order_id,
{
"status": OrderStatus.REJECTED.value,
"updated_at": datetime.now()
"updated_at": datetime.now(tz=tzutc())
}
)

Expand Down Expand Up @@ -744,7 +745,7 @@ def _close_trades(self, amount_to_close, sell_order):
def create_snapshot(self, portfolio_id, created_at=None):

if created_at is None:
created_at = datetime.utcnow()
created_at = datetime.now(tz=tzutc())

portfolio = self.portfolio_repository.get(portfolio_id)
pending_orders = self.get_all(
Expand Down
2 changes: 1 addition & 1 deletion tests/app/algorithm/test_close_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def setUp(self) -> None:
csv_file_path=os.path.join(
self.resource_dir,
"market_data_sources",
"TICKER_BTC-EUR_BITVAVO_2021-06-02:00:00_2021-06-26:00:00.csv"
"TICKER_BTC-EUR_BINANCE_2023-08-23:22:00_2023-12-02:00:00.csv"
)
))
self.app.add_algorithm(Algorithm())
Expand Down
2 changes: 1 addition & 1 deletion tests/app/algorithm/test_close_trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def setUp(self) -> None:
csv_file_path=os.path.join(
self.resource_dir,
"market_data_sources",
"TICKER_BTC-EUR_BITVAVO_2021-06-02:00:00_2021-06-26:00:00.csv"
"TICKER_BTC-EUR_BINANCE_2023-08-23:22:00_2023-12-02:00:00.csv"
)
))
self.app.add_algorithm(Algorithm())
Expand Down
2 changes: 1 addition & 1 deletion tests/app/algorithm/test_get_closed_trades.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def setUp(self) -> None:
csv_file_path=os.path.join(
self.resource_dir,
"market_data_sources",
"TICKER_BTC-EUR_BITVAVO_2021-06-02:00:00_2021-06-26:00:00.csv"
"TICKER_BTC-EUR_BINANCE_2023-08-23:22:00_2023-12-02:00:00.csv"
)
))
self.app.add_market_credential(
Expand Down
2 changes: 1 addition & 1 deletion tests/app/algorithm/test_get_open_trades.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def setUp(self) -> None:
csv_file_path=os.path.join(
self.resource_dir,
"market_data_sources",
"TICKER_BTC-EUR_BITVAVO_2021-06-02:00:00_2021-06-26:00:00.csv"
"TICKER_BTC-EUR_BINANCE_2023-08-23:22:00_2023-12-02:00:00.csv"
)
))
self.app.add_market_credential(
Expand Down
2 changes: 1 addition & 1 deletion tests/app/algorithm/test_get_trades.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def setUp(self) -> None:
csv_file_path=os.path.join(
self.resource_dir,
"market_data_sources",
"TICKER_BTC-EUR_BITVAVO_2021-06-02:00:00_2021-06-26:00:00.csv"
"TICKER_BTC-EUR_BINANCE_2023-08-23:22:00_2023-12-02:00:00.csv"
)
))
self.app.add_algorithm(Algorithm())
Expand Down
12 changes: 7 additions & 5 deletions tests/domain/models/test_trade.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime, timedelta
from unittest import TestCase
from dateutil.tz import tzutc
import pandas as pd
import os

Expand Down Expand Up @@ -29,7 +30,7 @@ def test_trade(self):
self.assertEqual(trade.opened_at, trade_opened_at)

def test_stop_loss_manual(self):
current_datetime = datetime(2021, 6, 20, 00, 00, 0)
current_datetime = datetime(2023, 8, 26, 00, 00, 0, tzinfo=tzutc())
resource_dir = os.path.abspath(
os.path.join(
os.path.join(
Expand All @@ -54,18 +55,19 @@ def test_stop_loss_manual(self):
end_date=current_datetime,
csv_file_path=f"{resource_dir}/"
"market_data_sources/"
"OHLCV_BTC-EUR_15m_2021-05-17:00:00_2021-06-26:00:00.csv"
"OHLCV_BTC-EUR_BINANCE_2h_2023-08-07:07"
":59_2023-12-02:00:00.csv"
)
csv_ticker_market_data_source = CSVTickerMarketDataSource(
identifier="BTC",
market="BITVAVO",
symbol="BTC/EUR",
csv_file_path=f"{resource_dir}"
"/market_data_sources/"
"TICKER_BTC-EUR_BITVAVO_2021-06-02:00"
":00_2021-06-26:00:00.csv"
"TICKER_BTC-EUR_BINANCE_2023-08"
"-23:22:00_2023-12-02:00:00.csv"
)
trade_opened_at = datetime(2021, 6, 17, 12, 0, 0)
trade_opened_at = datetime(2023, 8, 17, 12, 0, 0, tzinfo=tzutc())
open_price = 32589
trade = Trade(
buy_order_id=1,
Expand Down
Loading

0 comments on commit c6defde

Please sign in to comment.