commit
This commit is contained in:
160
new_prac.py
160
new_prac.py
@@ -1,144 +1,74 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse, socket, json, time
|
||||
import argparse, socket, json
|
||||
from enum import Enum
|
||||
from state import StateManager
|
||||
|
||||
team_name = "HanyangFloorFunction"
|
||||
|
||||
ORDER_SIZE = 1
|
||||
MAX_POS = 10
|
||||
KILL_POS = 15
|
||||
REFRESH = 1.0 # 매우 느리게
|
||||
|
||||
class Dir(str, Enum):
|
||||
BUY = "BUY"
|
||||
SELL = "SELL"
|
||||
|
||||
class ExchangeConnection:
|
||||
def __init__(self, args):
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.connect((args.exchange_hostname, args.port))
|
||||
self.reader = s.makefile("r", 1)
|
||||
self.writer = s
|
||||
self.send({"type":"hello","team":team_name.upper()})
|
||||
|
||||
def read(self):
|
||||
m = json.loads(self.reader.readline())
|
||||
if "dir" in m:
|
||||
m["dir"] = Dir(m["dir"])
|
||||
return m
|
||||
|
||||
def send(self, m):
|
||||
self.writer.send((json.dumps(m)+"\n").encode())
|
||||
|
||||
def add(self, oid,sym,d,px,sz,tif="DAY"):
|
||||
self.send({"type":"add","order_id":oid,"symbol":sym,"dir":d,"price":px,"size":sz,"tif":tif})
|
||||
|
||||
def cancel(self, oid):
|
||||
self.send({"type":"cancel","order_id":oid})
|
||||
|
||||
def ioc(self, oid,sym,d,px,sz):
|
||||
self.add(oid,sym,d,px,sz,"IOC")
|
||||
|
||||
def parse_arguments():
|
||||
p=argparse.ArgumentParser()
|
||||
g=p.add_mutually_exclusive_group(required=True)
|
||||
g.add_argument("--production",action="store_true")
|
||||
g.add_argument("--test",type=str,default="prod-like")
|
||||
a=p.parse_args()
|
||||
|
||||
if a.production:
|
||||
a.exchange_hostname="production"; a.port=25000
|
||||
else:
|
||||
a.exchange_hostname="test-exch-"+team_name; a.port=22000
|
||||
return a
|
||||
|
||||
def main():
|
||||
args=parse_arguments()
|
||||
ex=ExchangeConnection(args)
|
||||
st=StateManager()
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--test", default="prod-like")
|
||||
args = parser.parse_args()
|
||||
|
||||
hello=ex.read()
|
||||
pos={s["symbol"]:s["position"] for s in hello["symbols"]}
|
||||
s = socket.socket()
|
||||
s.connect(("test-exch-"+team_name, 22000))
|
||||
r = s.makefile("r",1)
|
||||
|
||||
oid=0
|
||||
def send(m):
|
||||
s.send((json.dumps(m)+"\n").encode())
|
||||
|
||||
send({"type":"hello","team":team_name.upper()})
|
||||
|
||||
st = StateManager()
|
||||
|
||||
hello = json.loads(r.readline())
|
||||
|
||||
pos = {}
|
||||
for sym in hello["symbols"]:
|
||||
pos[sym["symbol"]] = sym["position"]
|
||||
|
||||
oid = 0
|
||||
def nid():
|
||||
nonlocal oid; oid+=1; return oid
|
||||
|
||||
active={}
|
||||
last_refresh=0
|
||||
|
||||
def safe_trade(sym):
|
||||
b=st.bid_prices.get(sym)
|
||||
a=st.ask_prices.get(sym)
|
||||
|
||||
if b is None or a is None:
|
||||
return
|
||||
|
||||
spread = a - b
|
||||
|
||||
# 🔥 핵심: 확실할 때만
|
||||
if spread < 4:
|
||||
return
|
||||
|
||||
p = pos.get(sym,0)
|
||||
|
||||
if abs(p) > MAX_POS:
|
||||
return
|
||||
|
||||
# 🔥 절대 무리하지 않음
|
||||
buy_px = b
|
||||
sell_px = a
|
||||
|
||||
o=nid(); ex.add(o,sym,Dir.BUY,buy_px,ORDER_SIZE); active[o]=sym
|
||||
o=nid(); ex.add(o,sym,Dir.SELL,sell_px,ORDER_SIZE); active[o]=sym
|
||||
|
||||
def risk():
|
||||
for s,p in pos.items():
|
||||
if abs(p)>KILL_POS:
|
||||
b=st.bid_prices.get(s)
|
||||
a=st.ask_prices.get(s)
|
||||
if p>0 and b:
|
||||
ex.ioc(nid(),s,Dir.SELL,b,abs(p))
|
||||
elif p<0 and a:
|
||||
ex.ioc(nid(),s,Dir.BUY,a,abs(p))
|
||||
|
||||
while True:
|
||||
m=ex.read()
|
||||
m = json.loads(r.readline())
|
||||
|
||||
if m["type"]=="close":
|
||||
if m["type"] == "close":
|
||||
break
|
||||
|
||||
elif m["type"]=="book":
|
||||
s=m["symbol"]
|
||||
if m["type"] == "book":
|
||||
sym = m["symbol"]
|
||||
|
||||
b=m["buy"][0][0] if m["buy"] else None
|
||||
a=m["sell"][0][0] if m["sell"] else None
|
||||
if not m["buy"] or not m["sell"]:
|
||||
continue
|
||||
|
||||
st.update_bid_ask_price(s,b,a)
|
||||
bid = m["buy"][0][0]
|
||||
ask = m["sell"][0][0]
|
||||
|
||||
now=time.time()
|
||||
if now-last_refresh>REFRESH:
|
||||
last_refresh=now
|
||||
# 🔥 spread 충분할 때만
|
||||
if ask - bid >= 3:
|
||||
send({"type":"add","order_id":nid(),"symbol":sym,
|
||||
"dir":"BUY","price":bid+1,"size":1})
|
||||
send({"type":"add","order_id":nid(),"symbol":sym,
|
||||
"dir":"SELL","price":ask-1,"size":1})
|
||||
|
||||
# 최소 cancel
|
||||
for o in list(active.keys()):
|
||||
ex.cancel(o)
|
||||
active.pop(o,None)
|
||||
if m["type"] == "fill":
|
||||
sym = m["symbol"]
|
||||
q = m["size"]
|
||||
|
||||
for sym in ["GS","MS","WFC"]:
|
||||
safe_trade(sym)
|
||||
pos[sym] = pos.get(sym,0)
|
||||
|
||||
risk()
|
||||
|
||||
elif m["type"]=="fill":
|
||||
s=m["symbol"]
|
||||
q=m["size"]
|
||||
|
||||
if m["dir"]==Dir.BUY:
|
||||
pos[s]+=q
|
||||
if m["dir"] == "BUY":
|
||||
pos[sym] += q
|
||||
else:
|
||||
pos[s]-=q
|
||||
pos[sym] -= q
|
||||
|
||||
if __name__=="__main__":
|
||||
print("POS:", pos)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user