Skip to content

Commit

Permalink
Merge pull request #7 from midassystems/add_rolls_on_futures
Browse files Browse the repository at this point in the history
UPD: Updated mbn and midas_client.
  • Loading branch information
anthonyb8 authored Jan 27, 2025
2 parents 1e7e290 + 789d52a commit 704cb0a
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 11 deletions.
6 changes: 5 additions & 1 deletion midastrader/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from enum import Enum
from typing import List
from dataclasses import dataclass, field
from mbn import Schema, Parameters as MbnParameters
from mbn import Dataset, Schema, Parameters as MbnParameters, Stype

from midastrader.utils import iso_to_unix
from midastrader.structs import Symbol, SymbolFactory
Expand Down Expand Up @@ -128,6 +128,8 @@ class Parameters:
strategy_name: str
capital: int
schema: Schema
dataset: Dataset
stype: Stype
data_type: LiveDataType
start: str
end: str
Expand Down Expand Up @@ -238,5 +240,7 @@ def from_dict(cls, data: dict) -> "Parameters":
end=data["end"],
symbols=symbols,
schema=data["schema"],
dataset=data["dataset"],
stype=data["stype"],
risk_free_rate=data["risk_free_rate"],
)
2 changes: 2 additions & 0 deletions midastrader/data/adaptors/historical/data_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def get_data(self, parameters: Parameters) -> bool:
parameters.start,
parameters.end,
parameters.schema,
parameters.dataset,
parameters.stype,
)
data = self.database_client.historical.get_records(params)

Expand Down
1 change: 1 addition & 0 deletions midastrader/data/adaptors/ib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ def stream_quote_data(self, contract: Contract) -> None:
bbo_obj = BboMsg(
instrument_id=instrument_id,
ts_event=0,
rollover_flag=0,
price=0,
size=0,
side=Side.NONE,
Expand Down
1 change: 1 addition & 0 deletions midastrader/data/adaptors/ib/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def realtimeBar(
bar = OhlcvMsg(
instrument_id=instrument_id,
ts_event=int(time * 1e9),
rollover_flag=0,
open=int(open_ * 1e9),
high=int(high * 1e9),
low=int(low * 1e9),
Expand Down
33 changes: 33 additions & 0 deletions midastrader/structs/events/rollover_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from dataclasses import dataclass, field
from midastrader.structs.events.base import SystemEvent


@dataclass
class RolloverEvent(SystemEvent):
"""
Represents end of historical data stream.
An EOD event is triggered at the end of a trading day to initiate tasks such as
mark-to-market evaluations, portfolio reconciliation, and daily summary calculations.
This event captures the date for which the end-of-day processing applies.
Attributes:
timestamp (date): The date representing the end-of-day moment.
type (str): Event type, automatically set to 'END-OF-DAY'.
"""

timestamp: int
instrument: int
old_price: float
new_price: float
type: str = field(init=False, default="END-STREAM")

def __str__(self) -> str:
"""
Returns a human-readable string representation of the `EODEvent`.
Returns:
str: A formatted string showing the event type and timestamp.
"""
string = f"\n{self.type} EVENT"
return string
Binary file removed tests/integration/hogs_corn_ohlcv1m.bin
Binary file not shown.
9 changes: 6 additions & 3 deletions tests/integration/strategy/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ output_path = "tests/integration/backtest/output/"
url = "http://192.53.120.167:8080" #"http://127.0.0.1:8080"
key = "your_database_key"
# data_file= "tests/integration/primer.bin"
data_file="tests/integration/test_ohlcv-1h.bin"
# data_file="tests/integration/test_ohlcv-1h.bin"
data_file= "tests/integration/test_HE.c.0_ZC.c.0_ohlcv-1h_2024-09-01_2024-12-31.bin"

# [vendor.interactive_brokers]
# tick_interval = 5 # only matters for tick data
Expand Down Expand Up @@ -42,6 +43,8 @@ tick_interval = 5 # only matters for tick data
schema = "ohlcv-1h"
start = "2024-09-01"
end = "2024-12-10"
stype= "continuous"
dataset= "futures"
test_start = ""
test_end = "2024-12-11" #2024-05-04 00:00:00"
missing_values_strategy = "drop"
Expand All @@ -53,7 +56,7 @@ type= "Future"
instrument_id=43
broker_ticker= "HE"
data_ticker= "HE"
midas_ticker="HE.n.0"
midas_ticker="HE.c.0"
security_type = "FUTURE"
currency= "USD"
exchange= "CME"
Expand Down Expand Up @@ -81,7 +84,7 @@ type= "Future"
instrument_id=70
broker_ticker= "ZC"
data_ticker= "ZC"
midas_ticker="ZC.n.0"
midas_ticker="ZC.c.0"
security_type= "FUTURE"
currency= "USD"
exchange= "CBOT"
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ output_path = "output/"
[vendor.historical]
url = "http://127.0.0.1:8080"
key = "your_database_key"
data_file= "tests/unit/hogs_corn_ohlcv1h.bin"
data_file= "tests/unit/test_HE.c.0_ZC.c.0__ohlcv-1h_2024-09-01_2024-12-31.bin"

[vendor.interactive_brokers]
host="127.0.0.1"
Expand Down Expand Up @@ -41,6 +41,8 @@ tick_interval = 5 # only matters for tick data
schema = "ohlcv-1h"
start = "2024-01-01"
end = "2024-01-31"
stype= "continuous"
dataset= "futures"
missing_values_strategy = "drop"
risk_free_rate = 0.04

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/core/performance/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def setUp(self) -> None:

# Parameters
self.schema = "Ohlcv-1s"
self.stype = "continuous"
self.dataset = "futures"
self.strategy_name = "Testing"
self.capital = 1000000
self.data_type = random.choice([LiveDataType.BAR, LiveDataType.TICK])
Expand All @@ -126,6 +128,8 @@ def setUp(self) -> None:
data_type=self.data_type,
start=self.start,
end=self.end,
stype="continuous",
dataset="futures",
# test_start=self.test_start,
# test_end=self.test_end,
risk_free_rate=0.9,
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/core/test_base_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def setUp(self) -> None:
self.bar = OhlcvMsg(
instrument_id=1,
ts_event=self.timestamp,
rollover_flag=0,
open=int(80.90 * 1e9),
close=int(9000.90 * 1e9),
high=int(75.90 * 1e9),
Expand Down Expand Up @@ -85,6 +86,7 @@ def test_process(self):
self.timestamp = 1707221160000000000
self.bar = OhlcvMsg(
instrument_id=1,
rollover_flag=0,
ts_event=self.timestamp,
open=int(80.90 * 1e9),
close=int(9000.90 * 1e9),
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/core/test_order_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def setUp(self) -> None:
self.timestamp = 1707221160000000000
self.bar = OhlcvMsg(
instrument_id=1,
rollover_flag=0,
ts_event=self.timestamp,
open=int(80.90 * 1e9),
close=int(9000.90 * 1e9),
Expand All @@ -41,6 +42,7 @@ def setUp(self) -> None:

self.bar2 = OhlcvMsg(
instrument_id=1,
rollover_flag=0,
ts_event=self.timestamp,
open=int(90.90 * 1e9),
close=int(9000.90 * 1e9),
Expand All @@ -51,6 +53,7 @@ def setUp(self) -> None:

self.tick = BboMsg(
instrument_id=2,
rollover_flag=0,
ts_event=self.timestamp,
price=int(12 * 1e9),
size=12345,
Expand Down Expand Up @@ -193,6 +196,7 @@ def setUp(self) -> None:
self.timestamp = 1707221160000000000
self.bar = OhlcvMsg(
instrument_id=1,
rollover_flag=0,
ts_event=self.timestamp,
open=int(80.90 * 1e9),
close=int(9000.90 * 1e9),
Expand All @@ -204,6 +208,7 @@ def setUp(self) -> None:
self.tick = BboMsg(
instrument_id=2,
ts_event=self.timestamp,
rollover_flag=0,
price=int(12 * 1e9),
size=12345,
side=Side.NONE,
Expand Down
22 changes: 16 additions & 6 deletions tests/unit/data/historical/test_data_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from time import sleep
from datetime import datetime, time
from unittest.mock import Mock, MagicMock
from mbn import OhlcvMsg
from mbn import Dataset, OhlcvMsg, Schema, Stype

from midastrader.config import LiveDataType, Parameters, Mode
from midastrader.structs.events import EODEvent
Expand Down Expand Up @@ -90,8 +90,9 @@ def setUp(self) -> None:

# Dataclient instance
self.bus = MessageBus()

kwargs = {"data_file": "tests/unit/hogs_corn_ohlcv1h.bin"}
kwargs = {
"data_file": "tests/unit/test_HE.c.0_ZC.c.0__ohlcv-1h_2024-09-01_2024-12-31.bin"
}
self.adaptor = HistoricalAdaptor(
self.symbols_map,
self.bus,
Expand All @@ -104,15 +105,19 @@ def test_get_data_file(self):
params = Parameters(
strategy_name="Testing",
capital=10000000,
schema="Ohlcv-1d",
schema=Schema.OHLCV1_D,
dataset=Dataset.FUTURES,
stype=Stype.CONTINUOUS,
data_type=LiveDataType.BAR,
start="2024-10-01",
end="2024-10-05",
risk_free_rate=0.9,
symbols=self.symbols,
)

kwargs = {"data_file": "tests/unit/hogs_corn_ohlcv1h.bin"}
kwargs = {
"data_file": "tests/unit/test_HE.c.0_ZC.c.0__ohlcv-1h_2024-09-01_2024-12-31.bin"
}
adaptor = HistoricalAdaptor(self.symbols_map, self.bus, **kwargs)

# Test
Expand All @@ -127,7 +132,9 @@ def test_get_data_database(self):
params = Parameters(
strategy_name="Testing",
capital=10000000,
schema="Ohlcv-1d",
schema=Schema.OHLCV1_D,
dataset=Dataset.FUTURES,
stype=Stype.CONTINUOUS,
data_type=LiveDataType.BAR,
start="2024-10-01",
end="2024-10-05",
Expand Down Expand Up @@ -161,6 +168,7 @@ def test_check_eod(self):
bar = OhlcvMsg(
instrument_id=1,
ts_event=1727916834000000000,
rollover_flag=0,
open=int(80.90 * 1e9),
close=int(9000.90 * 1e9),
high=int(75.90 * 1e9),
Expand All @@ -185,6 +193,7 @@ def test_check_eod_not(self):
bar = OhlcvMsg(
instrument_id=1,
ts_event=1727888034000000000,
rollover_flag=0,
open=int(80.90 * 1e9),
close=int(9000.90 * 1e9),
high=int(75.90 * 1e9),
Expand All @@ -210,6 +219,7 @@ def test_data_stream(self):
record = OhlcvMsg(
instrument_id=1,
ts_event=1707221160000000000,
rollover_flag=0,
open=int(80.90 * 1e9),
close=int(9000.90 * 1e9),
high=int(75.90 * 1e9),
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/data/ib/test_data_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def test_tickPrice(self):
bbo_obj = BboMsg(
instrument_id=1,
ts_event=0,
rollover_flag=0,
price=0,
size=0,
side=Side.NONE,
Expand Down Expand Up @@ -152,6 +153,7 @@ def test_tickSize(self):
bbo_obj = BboMsg(
instrument_id=1,
ts_event=0,
rollover_flag=0,
price=0,
size=0,
side=Side.NONE,
Expand Down Expand Up @@ -199,6 +201,7 @@ def test_tickString(self):
bbo_obj = BboMsg(
instrument_id=1,
ts_event=0,
rollover_flag=0,
price=0,
size=0,
side=Side.NONE,
Expand Down Expand Up @@ -233,6 +236,7 @@ def test_push_market_event(self):
bbo_obj = BboMsg(
instrument_id=1,
ts_event=0,
rollover_flag=0,
price=0,
size=0,
side=Side.NONE,
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/execution/dummy/test_dummy_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def test_update_account(self):
return_value=OhlcvMsg(
instrument_id=1,
ts_event=1777700000000000,
rollover_flag=0,
open=int(80.90 * 1e9),
close=int(90 * 1e9),
high=int(75.90 * 1e9),
Expand Down Expand Up @@ -442,6 +443,7 @@ def test_liquidate_positions(self):
return_value=OhlcvMsg(
instrument_id=1,
ts_event=1777700000000000,
rollover_flag=0,
open=int(80.90 * 1e9),
close=int(90 * 1e9),
high=int(75.90 * 1e9),
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/structs/events/test_market_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def setUp(self) -> None:
self.bar = OhlcvMsg(
instrument_id=1,
ts_event=self.timestamp,
rollover_flag=0,
open=int(80.90 * 1e9),
close=int(9000.90 * 1e9),
high=int(75.90 * 1e9),
Expand All @@ -21,6 +22,7 @@ def setUp(self) -> None:
self.tick = BboMsg(
instrument_id=1,
ts_event=self.timestamp,
rollover_flag=0,
price=int(12 * 1e9),
size=12345,
side=Side.NONE,
Expand Down
Loading

0 comments on commit 704cb0a

Please sign in to comment.