[Mod] round float number position to desired decimals for inverse contracts

This commit is contained in:
vn.py 2019-11-07 10:41:35 +08:00
parent fde4c74ca5
commit 47b256ea90
3 changed files with 23 additions and 4 deletions

View File

@ -2,6 +2,7 @@ from typing import Any
from vnpy.trader.constant import Direction, Offset
from vnpy.trader.object import (TickData, OrderData, TradeData)
from vnpy.trader.utility import round_to
from .template import SpreadAlgoTemplate
from .base import SpreadData
@ -111,19 +112,27 @@ class SpreadTakerAlgo(SpreadAlgoTemplate):
# Calcualte spread volume to hedge
active_leg = self.spread.active_leg
active_traded = self.leg_traded[active_leg.vt_symbol]
active_traded = round_to(active_traded, self.spread.min_volume)
hedge_volume = self.spread.calculate_spread_volume(
active_leg.vt_symbol,
active_traded
)
self.write_log(
f"active {active_leg.vt_symbol} traded: {active_traded} hedge_volume: {hedge_volume}")
# Calculate passive leg target volume and do hedge
for leg in self.spread.passive_legs:
passive_traded = self.leg_traded[leg.vt_symbol]
passive_traded = round_to(passive_traded, self.spread.min_volume)
passive_target = self.spread.calculate_leg_volume(
leg.vt_symbol,
hedge_volume
)
self.write_log(
f"hedge {leg.vt_symbol} traded: {passive_traded} target: {passive_target}")
leg_order_volume = passive_target - passive_traded
if leg_order_volume:

View File

@ -39,11 +39,13 @@ class LegData:
# Contract data
self.size: float = 0
self.net_position: bool = False
self.min_volume: float = 0
def update_contract(self, contract: ContractData):
""""""
self.size = contract.size
self.net_position = contract.net_position
self.min_volume = contract.min_volume
def update_tick(self, tick: TickData):
""""""

View File

@ -1,12 +1,11 @@
from collections import defaultdict
from typing import Dict, List, Set
from math import floor, ceil
from copy import copy
from vnpy.trader.object import TickData, TradeData, OrderData, ContractData
from vnpy.trader.constant import Direction, Status, Offset
from vnpy.trader.utility import virtual
from vnpy.trader.utility import virtual, floor_to, ceil_to, round_to
from .base import SpreadData, calculate_inverse_volume
@ -204,6 +203,10 @@ class SpreadAlgoTemplate:
else:
volume = volume * price / size
# Round order volume to min_volume of contract
leg = self.spread.legs[vt_symbol]
volume = round_to(volume, leg.min_volume)
vt_orderids = self.algo_engine.send_order(
self,
vt_symbol,
@ -241,12 +244,17 @@ class SpreadAlgoTemplate:
leg_traded = self.leg_traded[leg.vt_symbol]
trading_multiplier = self.spread.trading_multipliers[
leg.vt_symbol]
adjusted_leg_traded = leg_traded / trading_multiplier
adjusted_leg_traded = round_to(
adjusted_leg_traded, self.spread.min_volume)
if adjusted_leg_traded > 0:
adjusted_leg_traded = floor(adjusted_leg_traded)
adjusted_leg_traded = floor_to(
adjusted_leg_traded, self.spread.min_volume)
else:
adjusted_leg_traded = ceil(adjusted_leg_traded)
adjusted_leg_traded = ceil_to(
adjusted_leg_traded, self.spread.min_volume)
if not n:
self.traded = adjusted_leg_traded