def update_position(self):
for _, pos in self.__shares.items():
inst = Instrument.objects.filter(
exchange=pos['ExchangeID'],
product_code=re.findall('[A-Za-z]+', pos['InstrumentID'])[0]).first()
tick = json.loads(self.redis_client.get(pos['InstrumentID']))
if pos['Direction'] == ApiStruct.D_Buy:
profit = Decimal(tick['LastPrice']) - Decimal(pos['OpenPrice'])
else:
profit = Decimal(pos['OpenPrice']) - Decimal(tick['LastPrice'])
profit = profit * Decimal(pos['Volume']) * inst.volume_multiple
Trade.objects.update_or_create(
broker=self.__broker, strategy=self.__strategy, instrument=inst,
code=pos['InstrumentID'],
direction=DirectionType.LONG if pos['Direction'] == ApiStruct.D_Buy else DirectionType.SHORT,
close_time__isnull=True,
defaults={
'open_time': datetime.datetime.strptime(
pos['OpenDate']+'09', '%Y%m%d%H').replace(tzinfo=pytz.FixedOffset(480)),
'shares': pos['Volume'], 'filled_shares': pos['Volume'],
'avg_entry_price': Decimal(pos['OpenPrice']),
'cost': pos['Volume'] * Decimal(pos['OpenPrice']) * inst.fee_money *
inst.volume_multiple + pos['Volume'] * inst.fee_volume,
'profit': profit, 'frozen_margin': Decimal(pos['Margin'])})
评论列表
文章目录