51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
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_tools.web.rest_client import RESTSender
|
|
# ---
|
|
from cvttpy_trading.trading.trading_instructions import TradingInstructions
|
|
# ---
|
|
from pairs_trading.apps.pair_trader import PairTrader
|
|
|
|
|
|
class TradingInstructionsSender(NamedObject):
|
|
config_: Config
|
|
sender_: RESTSender
|
|
pairs_trader_: PairTrader
|
|
|
|
class TradingInstType(str, Enum):
|
|
TARGET_POSITION = "TARGET_POSITION"
|
|
DIRECT_ORDER = "DIRECT_ORDER"
|
|
MARKET_MAKING = "MARKET_MAKING"
|
|
NONE = "NONE"
|
|
|
|
def __init__(self, config: Config, pairs_trader: PairTrader) -> 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:
|
|
Log.info(f"{self.fname()}: sending {ti=}")
|
|
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}"
|
|
)
|
|
|