关于空头持仓大于多头内容导航:
1、空头持仓大于多头
持仓,则说明空头持仓做多if self.short_pos > self.long_pos:
return 1
# 反之,说明空头持仓做空
else:
return -1
#----------------------------------------------------------------------
def open(self, direction, price, volume):
"""开仓"""
# 开仓前检查是否有空头持仓
if direction == DIRECTION_LONG:
# 如果有空头持仓,则直接加仓
if self.short_pos:
self.cover(price, self.short_pos)
# 否则增加多头持仓
else:
self.long_pos += volume
# 对于空头开仓,同样需要检查是否有多头持仓
elif direction == DIRECTION_SHORT:
if self.long_pos:
self.sell(price, self.long_pos)
else:
self.short_pos += volume
# 记录开仓时间和价格
self.open_time = self.dt
self.open_price = price
#----------------------------------------------------------------------
def close(self, price):
"""平仓"""
# 平多头仓位
if self.long_pos:
self.sell(price, self.long_pos)
# 平空头仓位
if self.short_pos:
self.cover(price, self.short_pos)
#----------------------------------------------------------------------
def close_today(self, price):
"""平今"""
# 平多头仓位
if self.long_pos:
self.sell(price, self.long_pos)
# 平空头仓位
if self.short_pos:
self.cover(price, self.short_pos)
#----------------------------------------------------------------------
def buy(self, price, volume):
"""买入开多"""
self.long_pos += volume
#----------------------------------------------------------------------
def sell(self, price, volume):
"""卖出平多"""
self.long_pos -= volume
#----------------------------------------------------------------------
def cover(self, price, volume):
"""买入平空"""
self.short_pos -= volume
#----------------------------------------------------------------------
def short(self, price, volume):
"""卖出开空"""
self.short_pos += volume
#----------------------------------------------------------------------
def get_pos(self):
"""查询持仓"""
return self.long_pos - self.short_pos
#----------------------------------------------------------------------
def get_pnl(self, price):
"""查询盈亏"""
pos = self.long_pos - self.short_pos
return pos * (price - self.open_price)
#----------------------------------------------------------------------
def get_pnl_ratio(self, price):
"""查询盈亏比例"""
pos = self.long_pos - self.short_pos
return pos * (price - self.open_price) / self.open_price
#----------------------------------------------------------------------
def get_open_price(self):
"""查询开仓价格"""
return self.open_price
#----------------------------------------------------------------------
def get_close_price(self):
"""查询平仓价格"""
return self.close_price
#----------------------------------------------------------------------
def get_open_time(self):
"""查询开仓时间"""
return self.open_time
#----------------------------------------------------------------------
def get_close_time(self):
"""查询平仓时间"""
return self.close_time
#----------------------------------------------------------------------
def get_long_pos(self):
"""查询多头持仓"""
return self.long_pos
#----------------------------------------------------------------------
def get_short_pos(self):
"""查询空头持仓"""
return self.short_pos
#----------------------------------------------------------------------
def get_trade_cost(self, price, volume):
"""计算交易成本"""
return self.slippage + self.rate * price * volume
#----------------------------------------------------------------------
def get_trade_pnl(self, price, volume):
"""计算交易盈亏"""
return (price - self.open_price) * volume * self.direction
#----------------------------------------------------------------------
def get_trade_pnl_ratio(self, price, volume):
"""计算交易盈亏比例"""
return (price - self.open_price) / self.open_price * volume * self.direction