From 8eca0abd13b7dbf773a720295da66f5541e87636 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Tue, 29 Oct 2019 21:13:49 +0800 Subject: [PATCH] [Add] support float number net pos --- vnpy/app/spread_trading/base.py | 11 +++++------ vnpy/trader/utility.py | 12 +++++++++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/vnpy/app/spread_trading/base.py b/vnpy/app/spread_trading/base.py index 2d00579a..fdca426d 100644 --- a/vnpy/app/spread_trading/base.py +++ b/vnpy/app/spread_trading/base.py @@ -1,10 +1,9 @@ from typing import Dict, List -from math import floor, ceil from datetime import datetime from vnpy.trader.object import TickData, PositionData, TradeData 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" @@ -222,10 +221,10 @@ class SpreadData: adjusted_net_pos = net_pos / trading_multiplier 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 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) if not n: @@ -261,9 +260,9 @@ class SpreadData: spread_volume = leg_volume / trading_multiplier if spread_volume > 0: - spread_volume = floor(spread_volume) + spread_volume = floor_to(spread_volume, self.min_volume) else: - spread_volume = ceil(spread_volume) + spread_volume = ceil_to(spread_volume, self.min_volume) return spread_volume diff --git a/vnpy/trader/utility.py b/vnpy/trader/utility.py index 54afc881..256a26f8 100644 --- a/vnpy/trader/utility.py +++ b/vnpy/trader/utility.py @@ -7,7 +7,7 @@ import logging from pathlib import Path from typing import Callable, Dict from decimal import Decimal -from math import floor +from math import floor, ceil import numpy as np import talib @@ -135,6 +135,16 @@ def floor_to(value: float, target: float) -> float: 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: """ For: