commit
This commit is contained in:
160
new_prac.py
160
new_prac.py
@@ -1,144 +1,74 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import argparse, socket, json, time
|
import argparse, socket, json
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from state import StateManager
|
from state import StateManager
|
||||||
|
|
||||||
team_name = "HanyangFloorFunction"
|
team_name = "HanyangFloorFunction"
|
||||||
|
|
||||||
ORDER_SIZE = 1
|
|
||||||
MAX_POS = 10
|
|
||||||
KILL_POS = 15
|
|
||||||
REFRESH = 1.0 # 매우 느리게
|
|
||||||
|
|
||||||
class Dir(str, Enum):
|
class Dir(str, Enum):
|
||||||
BUY = "BUY"
|
BUY = "BUY"
|
||||||
SELL = "SELL"
|
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():
|
def main():
|
||||||
args=parse_arguments()
|
parser = argparse.ArgumentParser()
|
||||||
ex=ExchangeConnection(args)
|
parser.add_argument("--test", default="prod-like")
|
||||||
st=StateManager()
|
args = parser.parse_args()
|
||||||
|
|
||||||
hello=ex.read()
|
s = socket.socket()
|
||||||
pos={s["symbol"]:s["position"] for s in hello["symbols"]}
|
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():
|
def nid():
|
||||||
nonlocal oid; oid+=1; return oid
|
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:
|
while True:
|
||||||
m=ex.read()
|
m = json.loads(r.readline())
|
||||||
|
|
||||||
if m["type"]=="close":
|
if m["type"] == "close":
|
||||||
break
|
break
|
||||||
|
|
||||||
elif m["type"]=="book":
|
if m["type"] == "book":
|
||||||
s=m["symbol"]
|
sym = m["symbol"]
|
||||||
|
|
||||||
b=m["buy"][0][0] if m["buy"] else None
|
if not m["buy"] or not m["sell"]:
|
||||||
a=m["sell"][0][0] if m["sell"] else None
|
continue
|
||||||
|
|
||||||
st.update_bid_ask_price(s,b,a)
|
bid = m["buy"][0][0]
|
||||||
|
ask = m["sell"][0][0]
|
||||||
|
|
||||||
now=time.time()
|
# 🔥 spread 충분할 때만
|
||||||
if now-last_refresh>REFRESH:
|
if ask - bid >= 3:
|
||||||
last_refresh=now
|
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
|
if m["type"] == "fill":
|
||||||
for o in list(active.keys()):
|
sym = m["symbol"]
|
||||||
ex.cancel(o)
|
q = m["size"]
|
||||||
active.pop(o,None)
|
|
||||||
|
|
||||||
for sym in ["GS","MS","WFC"]:
|
pos[sym] = pos.get(sym,0)
|
||||||
safe_trade(sym)
|
|
||||||
|
|
||||||
risk()
|
if m["dir"] == "BUY":
|
||||||
|
pos[sym] += q
|
||||||
elif m["type"]=="fill":
|
|
||||||
s=m["symbol"]
|
|
||||||
q=m["size"]
|
|
||||||
|
|
||||||
if m["dir"]==Dir.BUY:
|
|
||||||
pos[s]+=q
|
|
||||||
else:
|
else:
|
||||||
pos[s]-=q
|
pos[sym] -= q
|
||||||
|
|
||||||
if __name__=="__main__":
|
print("POS:", pos)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user