Skip to content

Commit

Permalink
add order_type maker supported
Browse files Browse the repository at this point in the history
  • Loading branch information
51bitquant committed Jul 26, 2022
1 parent b4faea2 commit 73500a5
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 52 deletions.
2 changes: 1 addition & 1 deletion howtrader/app/algo_trading/algos/dma_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DmaAlgo(AlgoTemplate):
"vt_symbol": "",
"direction": [Direction.LONG.value, Direction.SHORT.value],
"order_type": [
OrderType.MARKET.value,
OrderType.TAKER.value,
OrderType.LIMIT.value,
OrderType.STOP.value,
OrderType.FAK.value,
Expand Down
11 changes: 7 additions & 4 deletions howtrader/app/cta_strategy/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,19 +334,21 @@ def send_limit_order(
price: Decimal,
volume: Decimal,
lock: bool,
net: bool
net: bool,
maker: bool = False
) -> list:
"""
Send a limit order to server.
"""
order_type = OrderType.MAKER if maker else OrderType.LIMIT
return self.send_server_order(
strategy,
contract,
direction,
offset,
price,
volume,
OrderType.LIMIT,
order_type,
lock,
net
)
Expand Down Expand Up @@ -462,7 +464,8 @@ def send_order(
volume: Decimal,
stop: bool,
lock: bool,
net: bool
net: bool,
maker: bool = False
) -> list:
"""
"""
Expand Down Expand Up @@ -490,7 +493,7 @@ def send_order(
)
else:
return self.send_limit_order(
strategy, contract, direction, offset, price, volume, lock, net
strategy, contract, direction, offset, price, volume, lock, net, maker
)

def cancel_order(self, strategy: CtaTemplate, vt_orderid: str) -> None:
Expand Down
29 changes: 19 additions & 10 deletions howtrader/app/cta_strategy/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ def buy(
volume: Decimal,
stop: bool = False,
lock: bool = False,
net: bool = False
net: bool = False,
maker: bool = False
) -> list:
"""
Send buy order to open a long position.
Expand All @@ -165,7 +166,8 @@ def buy(
volume,
stop,
lock,
net
net,
maker
)

def sell(
Expand All @@ -174,7 +176,8 @@ def sell(
volume: Decimal,
stop: bool = False,
lock: bool = False,
net: bool = False
net: bool = False,
maker: bool = False
) -> list:
"""
Send sell order to close a long position.
Expand All @@ -186,7 +189,8 @@ def sell(
volume,
stop,
lock,
net
net,
maker
)

def short(
Expand All @@ -195,7 +199,8 @@ def short(
volume: Decimal,
stop: bool = False,
lock: bool = False,
net: bool = False
net: bool = False,
maker: bool = False
) -> list:
"""
Send short order to open as short position.
Expand All @@ -207,7 +212,8 @@ def short(
volume,
stop,
lock,
net
net,
maker
)

def cover(
Expand All @@ -216,7 +222,8 @@ def cover(
volume: Decimal,
stop: bool = False,
lock: bool = False,
net: bool = False
net: bool = False,
maker: bool = False
) -> list:
"""
Send cover order to close a short position.
Expand All @@ -228,7 +235,8 @@ def cover(
volume,
stop,
lock,
net
net,
maker
)

def send_order(
Expand All @@ -239,14 +247,15 @@ def send_order(
volume: Decimal,
stop: bool = False,
lock: bool = False,
net: bool = False
net: bool = False,
maker: bool = False
) -> list:
"""
Send a new order.
"""
if self.trading:
vt_orderids: list = self.cta_engine.send_order(
self, direction, offset, price, volume, stop, lock, net
self, direction, offset, price, volume, stop, lock, net, maker
)
return vt_orderids
else:
Expand Down
6 changes: 4 additions & 2 deletions howtrader/app/tradingview/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ def send_order(
direction: Direction,
offset: Offset,
price: Decimal,
volume: Decimal
volume: Decimal,
maker: bool = False
) -> list:
"""
send order to exchange
Expand All @@ -203,12 +204,13 @@ def send_order(
self.write_log(f"send order failed, order volume: {volume}, required min_volume: {contract.min_volume}")
return []

order_type = OrderType.MAKER if maker else OrderType.LIMIT
original_req: OrderRequest = OrderRequest(
symbol=contract.symbol,
exchange=contract.exchange,
direction=direction,
offset=offset,
type=OrderType.LIMIT,
type=order_type,
price=price,
volume=volume,
reference=f"{APP_NAME}_{strategy.strategy_name}"
Expand Down
68 changes: 39 additions & 29 deletions howtrader/app/tradingview/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class TVTemplate(ABC):
variables: list = []

def __init__(
self,
tv_engine: Any,
strategy_name: str,
tv_id: str,
vt_symbol: str,
setting: dict,
self,
tv_engine: Any,
strategy_name: str,
tv_id: str,
vt_symbol: str,
setting: dict,
) -> None:
""""""
self.tv_engine: Any = tv_engine
Expand Down Expand Up @@ -107,6 +107,7 @@ def on_start(self) -> None:
Callback when strategy is started.
"""
pass

@virtual
def on_stop(self) -> None:
"""callback when strategy is stop"""
Expand Down Expand Up @@ -142,9 +143,10 @@ def on_signal(self, signal: dict) -> None:
"""

def buy(
self,
price: Decimal,
volume: Decimal
self,
price: Decimal,
volume: Decimal,
maker: bool = False
) -> list:
"""
Send buy order to open a long position.
Expand All @@ -153,13 +155,15 @@ def buy(
Direction.LONG,
Offset.OPEN,
price,
volume
volume,
maker=maker
)

def sell(
self,
price: Decimal,
volume: Decimal
self,
price: Decimal,
volume: Decimal,
maker: bool = False
) -> list:
"""
Send sell order to close a long position.
Expand All @@ -168,13 +172,15 @@ def sell(
Direction.SHORT,
Offset.CLOSE,
price,
volume
volume,
maker=maker
)

def short(
self,
price: Decimal,
volume: Decimal
self,
price: Decimal,
volume: Decimal,
maker: bool = False
) -> list:
"""
Send short order to open as short position.
Expand All @@ -183,13 +189,15 @@ def short(
Direction.SHORT,
Offset.OPEN,
price,
volume
volume,
maker=maker
)

def cover(
self,
price: Decimal,
volume: Decimal
self,
price: Decimal,
volume: Decimal,
maker: bool = False
) -> list:
"""
Send cover order to close a short position.
Expand All @@ -198,21 +206,23 @@ def cover(
Direction.LONG,
Offset.CLOSE,
price,
volume
volume,
maker=maker
)

def send_order(
self,
direction: Direction,
offset: Offset,
price: Decimal,
volume: Decimal
self,
direction: Direction,
offset: Offset,
price: Decimal,
volume: Decimal,
maker: bool = False
) -> list:
"""
Send a new order.
"""
if self.trading:
vt_orderids: list = self.tv_engine.send_order(self, direction, offset, price, volume)
vt_orderids: list = self.tv_engine.send_order(self, direction, offset, price, volume, maker=maker)
return vt_orderids
else:
return []
Expand Down Expand Up @@ -265,4 +275,4 @@ def sync_data(self) -> None:
Sync strategy variables value into disk storage.
"""
if self.trading:
self.tv_engine.sync_strategy_data(self)
self.tv_engine.sync_strategy_data(self)
9 changes: 7 additions & 2 deletions howtrader/gateway/binance/binance_inverse_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
TickData,
OrderData,
TradeData,
Offset,
OrderQueryRequest,
AccountData,
ContractData,
Expand Down Expand Up @@ -69,9 +70,10 @@
# Order type map
ORDERTYPE_VT2BINANCES: Dict[OrderType, Tuple[str, str]] = {
OrderType.LIMIT: ("LIMIT", "GTC"),
OrderType.MARKET: ("MARKET", "GTC"),
OrderType.TAKER: ("MARKET", "GTC"),
OrderType.FAK: ("LIMIT", "IOC"),
OrderType.FOK: ("LIMIT", "FOK"),
OrderType.MAKER: ("LIMIT", "GTX")
}
ORDERTYPE_BINANCES2VT: Dict[Tuple[str, str], OrderType] = {v: k for k, v in ORDERTYPE_VT2BINANCES.items()}

Expand Down Expand Up @@ -512,14 +514,17 @@ def send_order(self, req: OrderRequest) -> str:
"newOrderRespType":"RESULT"
}

if req.type == OrderType.MARKET:
if req.type == OrderType.TAKER:
params["type"] = "MARKET"
else:
order_type, time_condition = ORDERTYPE_VT2BINANCES[req.type]
params["type"] = order_type
params["timeInForce"] = time_condition
params["price"] = req.price

if req.offset == Offset.CLOSE:
params['reduceOnly'] = True

path: str = "/dapi/v1/order"

self.add_request(
Expand Down
2 changes: 1 addition & 1 deletion howtrader/gateway/binance/binance_spot_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
# order type mapping
ORDERTYPE_VT2BINANCE: Dict[OrderType, str] = {
OrderType.LIMIT: "LIMIT",
OrderType.MARKET: "MARKET",
OrderType.TAKER: "MARKET",
OrderType.MAKER: "LIMIT_MAKER",
}
ORDERTYPE_BINANCE2VT: Dict[str, OrderType] = {v: k for k, v in ORDERTYPE_VT2BINANCE.items()}
Expand Down
Loading

0 comments on commit 73500a5

Please sign in to comment.