Skip to content

Commit

Permalink
Refactor equity timeseries to show current date
Browse files Browse the repository at this point in the history
  • Loading branch information
diogomatoschaves committed Feb 26, 2024
1 parent aaee449 commit 2cf50c5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 41 deletions.
27 changes: 8 additions & 19 deletions data/service/helpers/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,31 +240,22 @@ def extract_request_params(request):

def resample_equity_data(df, timeframes, max_size):

original_size = df.shape[0]
now = pd.Timestamp(datetime.datetime.now(tz=pytz.utc))

timeframe = list(timeframes)[0]
df_now = pd.DataFrame(data={df.columns[0]: [None], "time": [now]}).set_index('time')

df = pd.concat([df, df_now], axis=0).ffill()

for timeframe in timeframes:

df = df.resample(timeframe).mean().ffill()

if df.shape[0] > max_size or df.shape[0] > original_size:
if df.shape[0] > max_size:
continue

else:
break

return df, timeframe


def add_current_timestamp(df, time_frame):

now = pd.Timestamp(datetime.datetime.now(tz=pytz.utc)).round(time_frame) - pd.Timedelta(time_frame)

df_now = pd.DataFrame(data={df.columns[0]: [None], "time": [now]}).set_index('time')

df = pd.concat([df, df_now], axis=0)

return df.ffill()
return df


def get_pipeline_equity_timeseries(pipeline_id=None, account_type=None, max_items=500):
Expand All @@ -279,9 +270,7 @@ def get_pipeline_equity_timeseries(pipeline_id=None, account_type=None, max_item

df = pd.DataFrame(timeseries).set_index('time').rename(columns={"value": "$"})

df, timeframe = resample_equity_data(df, const.CANDLE_SIZES_MAPPER.values(), max_items)

df = add_current_timestamp(df, timeframe)
df = resample_equity_data(df, const.CANDLE_SIZES_MAPPER.values(), max_items)

df = df.reset_index()

Expand Down
86 changes: 65 additions & 21 deletions data/tests/service/blueprints/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,27 +615,50 @@ def test_pipelines_metrics(
"extra_url,response",
[
pytest.param(
'/1',
'/1?maxItems=32',
{
"data": [
{"$": 1012.5, "time": 1696172400000},
{"$": 1010.0, "time": 1696174200000},
{"$": 1020.0, "time": 1696176000000},
{"$": 1015.0, "time": 1696177800000},
{"$": 1025.0, "time": 1696179600000},
{"$": 1030.0, "time": 1696181400000},
{"$": 1030.0, "time": 1698795000000},
{"$": 1011.25, "time": 1696161600000},
{"$": 1026.25, "time": 1696176000000},
{"$": 1030.0, "time": 1696190400000},
{"$": 1030.0, "time": 1696204800000},
{"$": 1030.0, "time": 1696219200000},
{"$": 1030.0, "time": 1696233600000},
{"$": 1030.0, "time": 1696248000000},
{"$": 1030.0, "time": 1696262400000},
{"$": 1030.0, "time": 1696276800000},
{"$": 1030.0, "time": 1696291200000},
{"$": 1030.0, "time": 1696305600000},
{"$": 1030.0, "time": 1696320000000},
{"$": 1030.0, "time": 1696334400000},
{"$": 1030.0, "time": 1696348800000},
{"$": 1030.0, "time": 1696363200000},
{"$": 1025.0, "time": 1696377600000},
],
"success": True,
},
id="pipeline-equity-timeseries_no-max-items",
),
pytest.param(
'/1?maxItems=1',
'/1?maxItems=16',
{
"data": [
{"$": 1016.875, "time": 1696118400000},
{"$": 1016.875, "time": 1698710400000},
{"$": 1011.25, "time": 1696161600000},
{"$": 1026.25, "time": 1696176000000},
{"$": 1030.0, "time": 1696190400000},
{"$": 1030.0, "time": 1696204800000},
{"$": 1030.0, "time": 1696219200000},
{"$": 1030.0, "time": 1696233600000},
{"$": 1030.0, "time": 1696248000000},
{"$": 1030.0, "time": 1696262400000},
{"$": 1030.0, "time": 1696276800000},
{"$": 1030.0, "time": 1696291200000},
{"$": 1030.0, "time": 1696305600000},
{"$": 1030.0, "time": 1696320000000},
{"$": 1030.0, "time": 1696334400000},
{"$": 1030.0, "time": 1696348800000},
{"$": 1030.0, "time": 1696363200000},
{"$": 1025.0, "time": 1696377600000},
],
"success": True,
},
Expand All @@ -647,25 +670,46 @@ def test_pipelines_metrics(
id="equity-timeseries_non-existent-pipeline",
),
pytest.param(
'?maxItems=3',
'?maxItems=5',
{
"data": {
"live": [
{"$": 1011.25, "time": 1696172400000},
{"$": 1017.5, "time": 1696176000000},
{"$": 1027.5, "time": 1696179600000},
{"$": 1027.5, "time": 1698793200000},
{"$": 1022.5, "time": 1696118400000},
{"$": 1030.0, "time": 1696204800000},
{"$": 1030.0, "time": 1696291200000},
{"$": 1025.0, "time": 1696377600000},
],
"testnet": [
{"$": 1011.25, "time": 1696172400000},
{"$": 1017.5, "time": 1696176000000},
{"$": 1027.5, "time": 1696179600000},
{"$": 1027.5, "time": 1698793200000},
{"$": 1022.5, "time": 1696118400000},
{"$": 1030.0, "time": 1696204800000},
{"$": 1030.0, "time": 1696291200000},
{"$": 1025.0, "time": 1696377600000},
],
},
"success": True,
},
id="portfolio-equity-timeseries_max-items=3",
id="portfolio-equity-timeseries_max-items=5",
),
pytest.param(
'?maxItems=2',
{
"data": {
"live": [
{"$": 1022.5, "time": 1696118400000},
{"$": 1030.0, "time": 1696204800000},
{"$": 1030.0, "time": 1696291200000},
{"$": 1025.0, "time": 1696377600000},
],
"testnet": [
{"$": 1022.5, "time": 1696118400000},
{"$": 1030.0, "time": 1696204800000},
{"$": 1030.0, "time": 1696291200000},
{"$": 1025.0, "time": 1696377600000},
],
},
"success": True,
},
id="portfolio-equity-timeseries_max-items=2",
),
],
)
Expand Down
3 changes: 2 additions & 1 deletion data/tests/setup/fixtures/external_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import flask_jwt_extended
import pytest
import pytz
import redis
from binance import ThreadedWebsocketManager
from binance.client import Client
Expand Down Expand Up @@ -79,7 +80,7 @@ def spy_db_connection(mocker):
return mocker.spy(db.connections, 'all')


FAKE_TIME = datetime.datetime(2023, 11, 1, )
FAKE_TIME = datetime.datetime(2023, 10, 4, tzinfo=pytz.utc)


@pytest.fixture
Expand Down

0 comments on commit 2cf50c5

Please sign in to comment.