from vnpy.app.position_manager.tools.position_operator.position_operator import PositionModel
class SymbolOperator:
@classmethod
def get_strategy_position(cls, strategy_data, market_vt_symbol):
if market_vt_symbol in cls.__get_strategy_vt_symbols(strategy_symbols=strategy_data.keys()):
market_vt_symbol = cls.__find_strategy_symbol_with_vt_symbol(
strategy_symbols=strategy_data.keys(),
vt_symbol=market_vt_symbol
)
strategy_position = strategy_data[market_vt_symbol][PositionModel.position_key()]
else:
strategy_position = 0
return strategy_position
@staticmethod
def __get_vt_symbol(strategy_symbol):
return strategy_symbol.split(".")[0] + "." + strategy_symbol.split(".")[1]
@classmethod
def __get_strategy_vt_symbols(cls, strategy_symbols):
strategy_vt_symbols = []
for strategy_symbol in strategy_symbols:
vt_symbol = cls.__get_vt_symbol(strategy_symbol=strategy_symbol)
strategy_vt_symbols.append(vt_symbol)
return strategy_vt_symbols
@classmethod
def __find_strategy_symbol_with_vt_symbol(cls, strategy_symbols, vt_symbol):
find_strategy_symbol = None
for strategy_symbol in strategy_symbols:
strategy_vt_symbol = cls.__get_vt_symbol(strategy_symbol=strategy_symbol)
if vt_symbol == strategy_vt_symbol:
find_strategy_symbol = strategy_symbol
return find_strategy_symbol