Ejemplo n.º 1
0
    def __init__(self, stra_barupdate='', stra_onorder=''):
        '''初始所有变量'''
        self.stra_uppdate = stra_barupdate
        self.stra_onorder = stra_onorder
        '''每bar只执行一次交易'''
        self.SingleOrderOneBar = False
        '''每bar只执行一次交易'''
        '''K线序列'''
        self.Bars = []
        '''K线序列'''
        '''合约'''
        self.Instrument = ''
        '''合约'''
        '''合约信息'''
        self.InstrumentInfo = InstrumentField()
        '''合约信息'''
        '''周期'''
        self.Interval = 1
        '''周期'''
        '''周期类型'''
        self.IntervalType = IntervalType.Minute
        '''周期类型'''
        '''分笔数据
        Tick.Instrument用来判断是否有实盘数据'''
        self.Tick = Tick()
        '''分笔数据
        Tick.Instrument用来判断是否有实盘数据'''
        '''买卖信号'''
        self.Orders = []
        '''买卖信号'''
        '''指标字典
        策略使用的指标保存在此字典中
        以便管理程序显示和处理'''
        self.IndexDict = {}
        '''指标字典
        策略使用的指标保存在此字典中
        以便管理程序显示和处理'''
        '''时间'''
        self.D = np.array([])
        '''时间'''
        '''最高价'''
        self.H = np.array([])
        '''最高价'''
        '''最低价'''
        self.L = np.array([])
        '''最低价'''
        '''开盘价'''
        self.O = np.array([])
        '''开盘价'''
        '''收盘价'''
        self.C = np.array([])
        '''收盘价'''
        '''交易量'''
        self.V = np.array([])
        '''交易量'''
        '''持仓量'''
        self.I = np.array([])
        '''持仓量'''

        self._lastOrder = OrderItem()
Ejemplo n.º 2
0
    def on_order(self, stra=Strategy(''), data=Data(), order=OrderItem()):
        """此处调用ctp接口即可实现实际下单"""
        # print('stra order')

        # self.log.write('{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\n'.format(len(p.Orders), stra.Bars[0].D, _order.Direction, _order.Offset, _order.Price, _order.Volume, _order.Remark))

        if stra.EnableOrder:
            # print(order)
            order_id = stra.ID * 1000 + len(stra.GetOrders()) + 1

            # 平今与平昨;逻辑从C# 抄过来;没提示...不知道为啥,只能盲码了.
            if order.Offset != OffsetType.Open:
                key = '{0}_{1}'.format(
                    order.Instrument, DirectType.Sell
                    if order.Direction == DirectType.Buy else DirectType.Buy)
                # 无效,没提示...pf = PositionField()
                pf = self.t.DicPositionField.get(key)
                if not pf or pf.Position <= 0:
                    print('没有对应的持仓')
                else:
                    volClose = min(pf.Position, order.Volume)  # 可平量
                    instField = self.t.DicInstrument[order.Instrument]
                    if instField.ExchangeID == 'SHFE':
                        tdClose = min(volClose, pf.TdPosition)
                        if tdClose > 0:
                            self.t.ReqOrderInsert(order.Instrument,
                                                  order.Direction,
                                                  OffsetType.CloseToday,
                                                  order.Price, tdClose,
                                                  OrderType.Limit, order_id)
                            volClose -= tdClose
                    if volClose > 0:
                        self.t.ReqOrderInsert(order.Instrument,
                                              order.Direction,
                                              OffsetType.Close, order.Price,
                                              volClose, OrderType.Limit,
                                              order_id)
            else:
                self.t.ReqOrderInsert(order.Instrument, order.Direction,
                                      OffsetType.Open, order.Price,
                                      order.Volume, OrderType.Limit, order_id)
Ejemplo n.º 3
0
    def __order__(self, direction, offset, price, volume, remark):
        """策略执行信号"""

        if self.SingleOrderOneBar and (self.LastEntryDateLong == self.D[-1]
                                       or self.LastEntryDateShort == self.D[-1]
                                       or self.ExitDateLong == self.D[-1]
                                       or self.ExitDateShort == self.D[-1]):
            return
        order = OrderItem()
        order.Instrument = self.Instrument
        order.DateTime = self.D[-1]
        order.Direction = direction
        order.Offset = offset
        order.Price = price
        order.Volume = volume
        order.Remark = remark

        self.Orders.append(order)

        # 处理策略相关属性
        order.IndexEntryLong = self._lastOrder.IndexEntryLong
        order.IndexExitLong = self._lastOrder.IndexExitLong
        order.IndexEntryShort = self._lastOrder.IndexEntryShort
        order.IndexExitShort = self._lastOrder.IndexExitShort
        order.IndexLastEntryLong = self._lastOrder.IndexLastEntryLong
        order.IndexLastEntryShort = self._lastOrder.IndexLastEntryShort
        order.AvgEntryPriceLong = self._lastOrder.AvgEntryPriceLong
        order.AvgEntryPriceShort = self._lastOrder.AvgEntryPriceShort
        order.PositionLong = self._lastOrder.PositionLong
        order.PositionShort = self._lastOrder.PositionShort
        order.EntryDateLong = self._lastOrder.EntryDateLong
        order.EntryDateShort = self._lastOrder.EntryDateShort
        order.EntryPriceLong = self._lastOrder.EntryPriceLong
        order.EntryPriceShort = self._lastOrder.EntryPriceShort
        order.ExitDateLong = self._lastOrder.ExitDateLong
        order.ExitDateShort = self._lastOrder.ExitDateShort
        order.ExitPriceLong = self._lastOrder.ExitPriceLong
        order.ExitPriceShort = self._lastOrder.ExitPriceShort
        order.LastEntryDateLong = self._lastOrder.LastEntryDateLong
        order.LastEntryDateShort = self._lastOrder.LastEntryDateShort
        order.LastEntryPriceLong = self._lastOrder.LastEntryPriceLong
        order.LastEntryPriceShort = self._lastOrder.LastEntryPriceShort

        diroff = '{0}-{1}'.format(order.Direction.name, order.Offset.name)
        for case in switch(diroff):
            if case('Buy-Open'):
                order.PositionLong += order.Volume
                order.AvgEntryPriceLong = (
                    self._lastOrder.PositionLong *
                    self._lastOrder.AvgEntryPriceLong + order.Volume *
                    order.Price) / (self._lastOrder.Volume + order.Volume)
                if self._lastOrder.PositionLong == 0:
                    order.IndexEntryLong = len(self.Bars) - 1
                    order.EntryDateLong = self.D[-1]  # str '20160630 21:25:00'
                    order.EntryPriceLong = order.Price
                order.IndexLastEntryLong = len(self.Bars) - 1
                order.LastEntryPriceLong = order.Price
                order.LastEntryDateLong = self.D[-1]
                break

            if case('Buy-Close'):
                c_lots = min(self._lastOrder.PositionShort,
                             order.Volume)  # 能够平掉的数量
                if c_lots <= 0:  # 无仓可平
                    print('平仓量>持仓量')
                    break
                order.PositionShort -= c_lots

                order.IndexExitShort = len(self.Bars) - 1
                order.ExitDateShort = self.D[-1]
                order.ExitPriceShort = order.Price
                break

            if case('Sell-Open'):
                order.PositionShort += order.Volume
                order.AvgEntryPriceShort = (
                    self._lastOrder.PositionShort *
                    self._lastOrder.AvgEntryPriceShort + order.Volume *
                    order.Price) / (self._lastOrder.Volume + order.Volume)
                if self._lastOrder.PositionShort == 0:
                    order.IndexEntryShort = len(self.Bars) - 1
                    order.EntryDateShort = self.D[
                        -1]  # time or double or str ???
                    order.EntryPriceShort = order.Price
                order.IndexLastEntryShort = len(self.Bars) - 1
                order.LastEntryPriceShort = order.Price
                order.LastEntryDateShort = self.D[-1]
                break

            if case('Sell-Close'):
                c_lots = min(self._lastOrder.PositionLong,
                             order.Volume)  # 能够平掉的数量
                if c_lots <= 0:  # 无仓可平
                    print('平仓量>持仓量')
                    break
                order.PositionLong -= c_lots

                order.IndexExitLong = len(self.Bars) - 1
                order.ExitDateLong = self.D[-1]
                order.ExitPriceLong = order.Price
                break

        self._lastOrder = order
        self.stra_onorder(self, order)
Ejemplo n.º 4
0
 def __OnOrder(self, data=Data(), order=OrderItem()):
     """调用外部接口的reqorder"""
     self._data_order(self, data, order)
Ejemplo n.º 5
0
 def _data_order(self, stra, data=Data(), order=OrderItem()):
     """继承类中实现此函数,有策略信号产生时调用"""
     pass