dev progress
This commit is contained in:
+80
-28
@@ -10,11 +10,16 @@ from cvttpy_tools.app import App
|
||||
from cvttpy_tools.logger import Log
|
||||
from cvttpy_tools.config import Config
|
||||
from cvttpy_tools.timer import Timer
|
||||
from cvttpy_tools.timeutils import NanosT, current_seconds
|
||||
from cvttpy_tools.timeutils import NanosT, current_seconds, NanoPerSec
|
||||
from cvttpy_tools.settings.cvtt_types import InstrumentIdT, IntervalSecT
|
||||
|
||||
# ---
|
||||
from cvttpy_trading.trading.mkt_data.historical_md import HistMdBar
|
||||
from cvttpy_trading.trading.instrument import ExchangeInstrument
|
||||
from cvttpy_trading.trading.accounting.exch_account import ExchangeAccountNameT
|
||||
from cvttpy_trading.trading.mkt_data.md_summary import MdTradesAggregate
|
||||
from cvttpy_trading.trading.exchange_config import ExchangeAccounts
|
||||
|
||||
# ---
|
||||
from pairs_trading.lib.live.rest_client import RESTSender
|
||||
|
||||
@@ -60,19 +65,36 @@ class MdSummary(HistMdBar):
|
||||
)
|
||||
return res
|
||||
|
||||
MdSummaryCallbackT = Callable[[List[MdSummary]], Coroutine]
|
||||
def create_md_trades_aggregate(
|
||||
self,
|
||||
exch_acct: ExchangeAccountNameT,
|
||||
exch_inst: ExchangeInstrument,
|
||||
interval_sec: IntervalSecT,
|
||||
) -> MdTradesAggregate:
|
||||
res = MdTradesAggregate(
|
||||
exch_acct=exch_acct,
|
||||
exch_inst=exch_inst,
|
||||
interval_ns=interval_sec * NanoPerSec,
|
||||
)
|
||||
res.set(mdbar=self)
|
||||
return res
|
||||
|
||||
|
||||
MdSummaryCallbackT = Callable[[List[MdTradesAggregate]], Coroutine]
|
||||
|
||||
|
||||
class MdSummaryCollector(NamedObject):
|
||||
sender_: RESTSender
|
||||
exch_acct_: ExchangeAccountNameT
|
||||
instrument_id_: InstrumentIdT
|
||||
exch_inst_: ExchangeInstrument
|
||||
interval_sec_: IntervalSecT
|
||||
history_depth_sec_: IntervalSecT
|
||||
|
||||
history_: List[MdSummary]
|
||||
history_: List[MdTradesAggregate]
|
||||
|
||||
callbacks_: List[MdSummaryCallbackT]
|
||||
timer_: Optional[Timer]
|
||||
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
sender: RESTSender,
|
||||
@@ -83,24 +105,36 @@ class MdSummaryCollector(NamedObject):
|
||||
) -> None:
|
||||
self.sender_ = sender
|
||||
self.exch_acct_ = exch_acct
|
||||
self.instrument_id_ = instrument_id
|
||||
|
||||
exch_inst = ExchangeAccounts.instance().get_exchange_instrument(
|
||||
exch_acct=exch_acct, instrument_id=instrument_id
|
||||
)
|
||||
assert exch_inst is not None, f"Unable to find Exchange instrument for {exch_acct}/{instrument_id}"
|
||||
self.exch_inst_ = exch_inst
|
||||
self.interval_sec_ = interval_sec
|
||||
self.history_depth_sec_ = history_depth_sec
|
||||
|
||||
self.history_ = []
|
||||
self.callbacks_ = []
|
||||
self.timer_ = None
|
||||
|
||||
|
||||
def add_callback(self, cb: MdSummaryCallbackT) -> None:
|
||||
self.callbacks_.append(cb)
|
||||
|
||||
def __hash__(self):
|
||||
return hash((self.exch_acct_, self.instrument_id_, self.interval_sec_, self.history_depth_sec_))
|
||||
|
||||
return hash(
|
||||
(
|
||||
self.exch_acct_,
|
||||
self.exch_inst_.instrument_id(),
|
||||
self.interval_sec_,
|
||||
self.history_depth_sec_,
|
||||
)
|
||||
)
|
||||
|
||||
def rqst_data(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"exch_acct": self.exch_acct_,
|
||||
"instrument_id": self.instrument_id_,
|
||||
"instrument_id": self.exch_inst_.instrument_id(),
|
||||
"interval_sec": self.interval_sec_,
|
||||
"history_depth_sec": self.history_depth_sec_,
|
||||
}
|
||||
@@ -110,7 +144,9 @@ class MdSummaryCollector(NamedObject):
|
||||
endpoint="md_summary", post_body=self.rqst_data()
|
||||
)
|
||||
if response.status_code not in (200, 201):
|
||||
Log.error(f"{self.fname()}: Received error: {response.status_code} - {response.text}")
|
||||
Log.error(
|
||||
f"{self.fname()}: Received error: {response.status_code} - {response.text}"
|
||||
)
|
||||
return []
|
||||
return MdSummary.from_REST_response(response=response)
|
||||
|
||||
@@ -121,19 +157,29 @@ class MdSummaryCollector(NamedObject):
|
||||
endpoint="md_summary", post_body=rqst_data
|
||||
)
|
||||
if response.status_code not in (200, 201):
|
||||
Log.error(f"{self.fname()}: Received error: {response.status_code} - {response.text}")
|
||||
Log.error(
|
||||
f"{self.fname()}: Received error: {response.status_code} - {response.text}"
|
||||
)
|
||||
return None
|
||||
res = MdSummary.from_REST_response(response=response)
|
||||
return None if len(res) == 0 else res[-1]
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
return len(self.history_) == 0
|
||||
|
||||
|
||||
async def start(self) -> None:
|
||||
if self.timer_:
|
||||
Log.error(f"{self.fname()}: Timer is already started")
|
||||
return
|
||||
self.history_ = self.get_history()
|
||||
mdsum_hist = self.get_history()
|
||||
self.history_ = [
|
||||
mdsum.create_md_trades_aggregate(
|
||||
exch_acct=self.exch_acct_,
|
||||
exch_inst=self.exch_inst_,
|
||||
interval_sec=self.interval_sec_,
|
||||
)
|
||||
for mdsum in mdsum_hist
|
||||
]
|
||||
await self.run_callbacks()
|
||||
self.set_timer()
|
||||
|
||||
@@ -148,27 +194,30 @@ class MdSummaryCollector(NamedObject):
|
||||
def next_load_time(self) -> NanosT:
|
||||
curr_sec = int(current_seconds())
|
||||
return (curr_sec - curr_sec % self.interval_sec_) + self.interval_sec_ + 2
|
||||
|
||||
|
||||
async def _load_new(self) -> None:
|
||||
|
||||
|
||||
last: Optional[MdSummary] = self.get_last()
|
||||
if not last:
|
||||
Log.warning(f"{self.fname()}: did not get last update")
|
||||
elif not self.is_empty() and last.ts_ns_ <= self.history_[-1].ts_ns_:
|
||||
Log.info(f"{self.fname()}: Received {last}. Already Have: {self.history_[-1]}")
|
||||
elif not self.is_empty() and last.ts_ns_ <= self.history_[-1].time_ns_:
|
||||
Log.info(
|
||||
f"{self.fname()}: Received {last}. Already Have: {self.history_[-1]}"
|
||||
)
|
||||
else:
|
||||
self.history_.append(last)
|
||||
self.history_.append(last.create_md_trades_aggregate(exch_acct=self.exch_acct_, exch_inst=self.exch_inst_, interval_sec=self.interval_sec_))
|
||||
await self.run_callbacks()
|
||||
self.set_timer()
|
||||
|
||||
|
||||
async def run_callbacks(self) -> None:
|
||||
[await cb(self.history_) for cb in self.callbacks_]
|
||||
|
||||
|
||||
def stop(self) -> None:
|
||||
if self.timer_:
|
||||
self.timer_.cancel()
|
||||
self.timer_ = None
|
||||
|
||||
|
||||
class CvttRestMktDataClient(NamedObject):
|
||||
config_: Config
|
||||
sender_: RESTSender
|
||||
@@ -181,12 +230,13 @@ class CvttRestMktDataClient(NamedObject):
|
||||
self.sender_ = RESTSender(base_url=base_url)
|
||||
self.collectors_ = set()
|
||||
|
||||
async def add_subscription(self,
|
||||
async def add_subscription(
|
||||
self,
|
||||
exch_acct: ExchangeAccountNameT,
|
||||
instrument_id: InstrumentIdT,
|
||||
interval_sec: IntervalSecT,
|
||||
history_depth_sec: IntervalSecT,
|
||||
callback: MdSummaryCallbackT
|
||||
callback: MdSummaryCallbackT,
|
||||
) -> None:
|
||||
mdsc = MdSummaryCollector(
|
||||
sender=self.sender_,
|
||||
@@ -199,14 +249,16 @@ class CvttRestMktDataClient(NamedObject):
|
||||
self.collectors_.add(mdsc)
|
||||
await mdsc.start()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
config = Config(json_src={"cvtt_base_url": "http://cvtt-tester-01.cvtt.vpn:23456"})
|
||||
# config = Config(json_src={"cvtt_base_url": "http://dev-server-02.cvtt.vpn:23456"})
|
||||
|
||||
async def _calback(history: List[MdSummary]) -> None:
|
||||
Log.info(f"MdSummary Hist Length is {len(history)}. Last summary: {history[-1] if len(history) > 0 else '[]'}")
|
||||
async def _calback(history: List[MdTradesAggregate]) -> None:
|
||||
Log.info(
|
||||
f"MdSummary Hist Length is {len(history)}. Last summary: {history[-1] if len(history) > 0 else '[]'}"
|
||||
)
|
||||
|
||||
|
||||
async def __run() -> None:
|
||||
Log.info("Starting...")
|
||||
cvtt_client = CvttRestMktDataClient(config)
|
||||
@@ -215,10 +267,10 @@ if __name__ == "__main__":
|
||||
instrument_id="PAIR-BTC-USD",
|
||||
interval_sec=60,
|
||||
history_depth_sec=24 * 3600,
|
||||
callback=_calback
|
||||
callback=_calback,
|
||||
)
|
||||
while True:
|
||||
await asyncio.sleep(5)
|
||||
|
||||
|
||||
asyncio.run(__run())
|
||||
pass
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
from enum import Enum
|
||||
|
||||
import requests
|
||||
|
||||
# import aiohttp
|
||||
from cvttpy_tools.base import NamedObject
|
||||
from cvttpy_tools.config import Config
|
||||
from cvttpy_tools.logger import Log
|
||||
# ---
|
||||
from cvttpy_trading.trading.trading_instructions import TradingInstructions
|
||||
# ---
|
||||
from pairs_trading.lib.live.rest_client import RESTSender
|
||||
from pairs_trading.apps.pairs_trader import PairsTrader
|
||||
|
||||
|
||||
class TradingInstructionsSender(NamedObject):
|
||||
config_: Config
|
||||
sender_: RESTSender
|
||||
pairs_trader_: PairsTrader
|
||||
|
||||
class TradingInstType(str, Enum):
|
||||
TARGET_POSITION = "TARGET_POSITION"
|
||||
DIRECT_ORDER = "DIRECT_ORDER"
|
||||
MARKET_MAKING = "MARKET_MAKING"
|
||||
NONE = "NONE"
|
||||
|
||||
# config_: Config
|
||||
# ti_method_: str
|
||||
# ti_url_: str
|
||||
# health_check_method_: str
|
||||
# health_check_url_: str
|
||||
|
||||
def __init__(self, config: Config, pairs_trader: PairsTrader) -> None:
|
||||
self.config_ = config
|
||||
base_url = self.config_.get_value("cvtt_base_url", default="")
|
||||
assert base_url
|
||||
self.sender_ = RESTSender(base_url=base_url)
|
||||
self.pairs_trader_ = pairs_trader
|
||||
|
||||
self.book_id_ = self.pairs_trader_.book_id_
|
||||
assert self.book_id_, "book_id is required"
|
||||
|
||||
self.strategy_id_ = config.get_value("strategy_id", "")
|
||||
assert self.strategy_id_, "strategy_id is required"
|
||||
|
||||
|
||||
async def send_trading_instructions(self, ti: TradingInstructions) -> None:
|
||||
|
||||
response: requests.Response = self.sender_.send_post(
|
||||
endpoint="trading_instructions", post_body=ti.to_dict()
|
||||
)
|
||||
if response.status_code not in (200, 201):
|
||||
Log.error(
|
||||
f"{self.fname()}: Received error: {response.status_code} - {response.text}"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user