commit
This commit is contained in:
58
prac.py
58
prac.py
@@ -1,34 +1,25 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
from collections import deque
|
||||
from enum import Enum
|
||||
import time
|
||||
import socket
|
||||
import json
|
||||
from state import StateManager
|
||||
from order import OrderManager
|
||||
|
||||
team_name = "HanyangFloorFunction"
|
||||
|
||||
# ==================== 설정 ====================
|
||||
BOND_FAIR_VALUE = 1000
|
||||
BOND_SPREAD = 2
|
||||
BOND_ORDER_SIZE = 20
|
||||
BOND_MAX_POSITION = 100
|
||||
|
||||
STOCK_SPREAD = 6
|
||||
STOCK_ORDER_SIZE = 5
|
||||
STOCK_MAX_POSITION = 100
|
||||
STOCK_REORDER_DELAY = 0.2
|
||||
|
||||
XLF_CONVERSION_FEE = 100
|
||||
VALE_CONVERSION_FEE = 10
|
||||
|
||||
REFRESH_INTERVAL = 1.0
|
||||
MIN_PROFIT_BUFFER = 10
|
||||
MAX_POSITION_SOFT = 50
|
||||
|
||||
XLF_CONVERSION_FEE = 100
|
||||
VALE_CONVERSION_FEE = 10
|
||||
|
||||
# ==================== 방향 ====================
|
||||
class Dir(str, Enum):
|
||||
BUY = "BUY"
|
||||
@@ -41,17 +32,19 @@ def main():
|
||||
|
||||
hello = exchange.read_message()
|
||||
state = StateManager()
|
||||
om = OrderManager()
|
||||
|
||||
# 🔥 자체 order id
|
||||
order_id = 0
|
||||
def next_id():
|
||||
nonlocal order_id
|
||||
order_id += 1
|
||||
return order_id
|
||||
|
||||
implied_fairs = {"GS": None, "MS": None, "WFC": None}
|
||||
|
||||
for sym_info in hello.get("symbols", []):
|
||||
state.update_position(sym_info["symbol"], sym_info["position"])
|
||||
|
||||
implied_fairs = {"GS": None, "MS": None, "WFC": None}
|
||||
active_orders = {}
|
||||
|
||||
def next_id():
|
||||
return om.next_order()
|
||||
|
||||
# ==================== FAIR VALUE ====================
|
||||
def update_fair():
|
||||
try:
|
||||
@@ -75,7 +68,7 @@ def main():
|
||||
implied_fairs["MS"] = int(0.7 * ms_mid + 0.3 * raw_ms)
|
||||
implied_fairs["WFC"] = int(0.7 * wfc_mid + 0.3 * raw_wfc)
|
||||
|
||||
# ==================== BOND ARB ====================
|
||||
# ==================== BOND ====================
|
||||
def bond_arb():
|
||||
bid = state.bid_prices.get("BOND")
|
||||
ask = state.ask_prices.get("BOND")
|
||||
@@ -86,7 +79,7 @@ def main():
|
||||
if bid and bid > 1000:
|
||||
exchange.send_add_message_ioc(next_id(), "BOND", Dir.SELL, bid, 10)
|
||||
|
||||
# ==================== STOCK MM ====================
|
||||
# ==================== MARKET MAKING ====================
|
||||
def market_make(sym):
|
||||
bid = state.bid_prices.get(sym)
|
||||
ask = state.ask_prices.get(sym)
|
||||
@@ -107,13 +100,10 @@ def main():
|
||||
else:
|
||||
buy_size = sell_size = STOCK_ORDER_SIZE
|
||||
|
||||
if buy_size > 0:
|
||||
exchange.send_add_message(next_id(), sym, Dir.BUY, int(buy_price), buy_size)
|
||||
exchange.send_add_message(next_id(), sym, Dir.BUY, int(buy_price), buy_size)
|
||||
exchange.send_add_message(next_id(), sym, Dir.SELL, int(sell_price), sell_size)
|
||||
|
||||
if sell_size > 0:
|
||||
exchange.send_add_message(next_id(), sym, Dir.SELL, int(sell_price), sell_size)
|
||||
|
||||
# ==================== XLF ARB ====================
|
||||
# ==================== XLF ====================
|
||||
def xlf_arb():
|
||||
try:
|
||||
basket_ask = (
|
||||
@@ -131,7 +121,7 @@ def main():
|
||||
if profit > MIN_PROFIT_BUFFER:
|
||||
exchange.send_add_message_ioc(next_id(), "XLF", Dir.SELL, xlf_bid, 10)
|
||||
|
||||
# ==================== VALE ARB ====================
|
||||
# ==================== VALE ====================
|
||||
def vale_arb():
|
||||
vale_bid = state.bid_prices.get("VALE")
|
||||
valbz_ask = state.ask_prices.get("VALBZ")
|
||||
@@ -142,7 +132,7 @@ def main():
|
||||
if vale_bid - valbz_ask - VALE_CONVERSION_FEE > 5:
|
||||
exchange.send_add_message_ioc(next_id(), "VALBZ", Dir.BUY, valbz_ask, 1)
|
||||
|
||||
# ==================== 리스크 관리 ====================
|
||||
# ==================== 리스크 ====================
|
||||
def risk():
|
||||
for sym in ["GS", "MS", "WFC"]:
|
||||
pos = state.get_position(sym)
|
||||
@@ -178,7 +168,6 @@ def main():
|
||||
)
|
||||
|
||||
update_fair()
|
||||
|
||||
bond_arb()
|
||||
xlf_arb()
|
||||
vale_arb()
|
||||
@@ -201,13 +190,11 @@ def main():
|
||||
else:
|
||||
state.update_position(sym, -qty)
|
||||
|
||||
# ==================== 연결 코드 ====================
|
||||
# ==================== 연결 ====================
|
||||
class ExchangeConnection:
|
||||
def __init__(self, args):
|
||||
self.exchange_hostname = args.exchange_hostname
|
||||
self.port = args.port
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.connect((self.exchange_hostname, self.port))
|
||||
s.connect((args.exchange_hostname, args.port))
|
||||
self.reader = s.makefile("r", 1)
|
||||
self.writer = s
|
||||
self._write({"type": "hello", "team": team_name.upper()})
|
||||
@@ -225,8 +212,7 @@ class ExchangeConnection:
|
||||
self._write({"type":"add","order_id":oid,"symbol":sym,"dir":dir,"price":price,"size":size,"tif":"IOC"})
|
||||
|
||||
def _write(self, msg):
|
||||
data = json.dumps(msg) + "\n"
|
||||
self.writer.send(data.encode())
|
||||
self.writer.send((json.dumps(msg)+"\n").encode())
|
||||
|
||||
# ==================== args ====================
|
||||
def parse_arguments():
|
||||
|
||||
Reference in New Issue
Block a user