[Add] support float number net pos

This commit is contained in:
vn.py 2019-10-29 21:13:49 +08:00
parent bc489d9fab
commit 8eca0abd13
2 changed files with 16 additions and 7 deletions

View File

@ -1,10 +1,9 @@
from typing import Dict, List from typing import Dict, List
from math import floor, ceil
from datetime import datetime from datetime import datetime
from vnpy.trader.object import TickData, PositionData, TradeData from vnpy.trader.object import TickData, PositionData, TradeData
from vnpy.trader.constant import Direction, Offset, Exchange from vnpy.trader.constant import Direction, Offset, Exchange
from vnpy.trader.utility import floor_to from vnpy.trader.utility import floor_to, ceil_to
EVENT_SPREAD_DATA = "eSpreadData" EVENT_SPREAD_DATA = "eSpreadData"
@ -222,10 +221,10 @@ class SpreadData:
adjusted_net_pos = net_pos / trading_multiplier adjusted_net_pos = net_pos / trading_multiplier
if adjusted_net_pos > 0: if adjusted_net_pos > 0:
adjusted_net_pos = floor(adjusted_net_pos) adjusted_net_pos = floor_to(adjusted_net_pos, self.min_volume)
leg_long_pos = adjusted_net_pos leg_long_pos = adjusted_net_pos
else: else:
adjusted_net_pos = ceil(adjusted_net_pos) adjusted_net_pos = ceil_to(adjusted_net_pos, self.min_volume)
leg_short_pos = abs(adjusted_net_pos) leg_short_pos = abs(adjusted_net_pos)
if not n: if not n:
@ -261,9 +260,9 @@ class SpreadData:
spread_volume = leg_volume / trading_multiplier spread_volume = leg_volume / trading_multiplier
if spread_volume > 0: if spread_volume > 0:
spread_volume = floor(spread_volume) spread_volume = floor_to(spread_volume, self.min_volume)
else: else:
spread_volume = ceil(spread_volume) spread_volume = ceil_to(spread_volume, self.min_volume)
return spread_volume return spread_volume

View File

@ -7,7 +7,7 @@ import logging
from pathlib import Path from pathlib import Path
from typing import Callable, Dict from typing import Callable, Dict
from decimal import Decimal from decimal import Decimal
from math import floor from math import floor, ceil
import numpy as np import numpy as np
import talib import talib
@ -135,6 +135,16 @@ def floor_to(value: float, target: float) -> float:
return result return result
def ceil_to(value: float, target: float) -> float:
"""
Similar to math.ceil function, but to target float number.
"""
value = Decimal(str(value))
target = Decimal(str(target))
result = float(int(ceil(value / target)) * target)
return result
class BarGenerator: class BarGenerator:
""" """
For: For: