Beispiel #1
0
    def on_market_prepare_open(self, protfolio: Portfolio, today: datetime):
        """
            市场准备开始(比如:竞价).
        """
        indicator = Indicator(40)
        for code in self.codes:
            bars = self.market.getHistory().getKbars(code, 100)
            indicator.update_bar(bars)
            count = 20
            aroon_down, aroon_up = indicator.aroon(count)
            need_hold = aroon_up > 50 and aroon_up > aroon_down
            position = protfolio.getLongPosition(code)

            if need_hold:
                if position.pos_total < 1:
                    targetPrice = bars[-1].close_price * 1.05  # 上一个交易日的收盘价作为买如价
                    ok = protfolio.buyAtPercentage(code, targetPrice, 1)
                    print(f"buy: price = {targetPrice} , {ok}")

            else:
                if position.pos_total > 0:
                    targetPrice = bars[-1].close_price * 0.92  # 上一个交易日的收盘价作为买如价
                    ok = protfolio.sellAll(code, targetPrice)
                    print(f"sell: price = {targetPrice} , {ok}")

        pass
Beispiel #2
0
    def on_market_prepare_close(self, protfolio: Portfolio):
        """
            市场准备关市.
        """
        self.write_log("on_market_prepare_close")
        protfolio.sell("000034", 344.23, 200)

        pass
Beispiel #3
0
    def on_market_prepare_open(self, protfolio: Portfolio):
        """
            市场准备开始(比如:竞价).
        """
        self.write_log("on_market_prepare")

        protfolio.buy("000034", 344.23, 200)

        pass
Beispiel #4
0
    def on_bar_per_minute(self, time: datetime, protfolio: Portfolio):
        """
            市场开市后的每分钟。
        """
        #每天两点半的后尝试去做交易。
        if time.hour == 14 and time.minute == 30:
            self.__history_bar100 = self.market.getHistory().getKbars(
                self.code, 100)
            assert len(self.__history_bar100) == 100
            bars = self.__history_bar100

            todayBar = self.market.getRealTime().getKBar(self.code)

            indicator = Indicator(40)
            indicator.update_bar(bars)
            dif, dea, macd_bar = indicator.macd(fast_period=12,
                                                slow_period=26,
                                                signal_period=9,
                                                array=True)

            if (not self.today_has_buy):
                # 预测金叉
                todayBar.close_price = todayBar.close_price * 1.01
                indicator.update_bar(todayBar)
                dif, dea, predict_macd_bar = indicator.macd(fast_period=12,
                                                            slow_period=26,
                                                            signal_period=9,
                                                            array=True)
                print(
                    f"[{self.market.getToday()}]:bar={macd_bar[-1]},predic_bar={predict_macd_bar[-1]}"
                )

                if (predict_macd_bar[-1] > 0 and macd_bar[-1] <= 0):
                    targetPrice = todayBar.close_price  # 上一个交易日的收盘价作为买如价
                    print(f"   gold cross!!!")
                    if protfolio.buy(self.code, targetPrice, 100):
                        self.today_has_buy = True
            elif (not self.today_has_sell):
                todayBar.close_price = todayBar.close_price * 0.99
                indicator.update_bar(todayBar)
                dif, dea, predict_macd_bar = indicator.macd(fast_period=12,
                                                            slow_period=26,
                                                            signal_period=9,
                                                            array=True)
                print(
                    f"[{self.market.getToday()}]:bar={macd_bar[-1]},predic_bar={predict_macd_bar[-1]}"
                )
                if (predict_macd_bar[-1] <= 0 and macd_bar[-1] > 0):
                    targetPrice = todayBar.close_price
                    print(f"   dead cross!!!")
                    if protfolio.sell(self.code, targetPrice, 100):
                        self.today_has_sell = True
Beispiel #5
0
    def on_bar_per_minute(self, time: datetime, protfolio: Portfolio):
        """
            市场开市后的每分钟。
        """
        self.on_bar_per_minute_count = self.on_bar_per_minute_count + 1

        assert is_same_minitue(time, self.market.getToday())
        assert time.hour >= 9  #9点后开市
        if (time.hour > 9 or (time.hour == 9 and time.minute > 32)):
            ##开市之后的实时信息不应该为none
            bar = self.market.getRealTime().getKBar("601318")
            assert not bar is None

            tickData: BarData = self.market.getRealTime().getTick("601318")
            preTickData: BarData = self.daily_pre_tick

            assert not tickData is None
            assert not preTickData is None
            deltaFloat = preTickData.close_price * 0.015
            assert utils.isEqualFloat(preTickData.close_price,
                                      tickData.open_price, deltaFloat)

        self.daily_pre_tick = self.market.getRealTime().getTick("601318")

        # 中国平安601318
        # 2019-03-25 10:35:00:open = 71.03,close=70.96 一天最高点
        # 2019-03-25 13:12:00:open = 69.97,close=69.79  下午开盘的一个低点
        # 2019-03-25 13:47:00:open = 70.33,close=70.41   下午的一个高点
        sell_time_01 = datetime(2019, 3, 25, 13, 12)
        if not self.sell_time_01_tag:
            if (utils.is_same_minitue(sell_time_01, time)):
                protfolio.sell("601318", 70.35, 120)
                self.sell_time_01_tag = True

        #self.write_log(f"     on_bar_per_minute:{time}" )
        # 中国平安601318 在datetime(2019, 2, 26, 10, 28)时刻,最低到达 low_price=67.15
        if (utils.is_same_time(datetime(2019, 2, 26, 10, 28),
                               self.market.getToday(),
                               deltaSecond=30)):
            protfolio.buy("601318", 70.75, 20)  ##测试交割价格在67.15附近

        # 中国平安601318 在datetime(2019, 2, 27, 9, 48)时刻,最高到达 high_price=68.57
        if (utils.is_same_time(datetime(2019, 2, 27, 9, 48),
                               self.market.getToday(),
                               deltaSecond=30)):
            protfolio.sell("601318", 60.75, 20)  ##测试交割价格在68.57附近

        ###开始做空买入在datetime(2019, 2, 27, 9, 48)时刻,最高到达 high_price=68.57
        # 中国平安601318 在datetime(2019, 2, 28)时刻,9点40分左右到最低点66.8,10:47最高到达68.08,然后14:00后面出现新的第二低点67.2-67.4
        if (utils.is_same_time(datetime(2019, 2, 28, 11, 00),
                               self.market.getToday(),
                               deltaSecond=30)):
            assert protfolio.cover("601318", 67.3,
                                   10) == True  ## 11点后开始平仓,以当天第二低价格平仓

        # 4月1日 - 20日随机交易
        today = self.market.getToday()
Beispiel #6
0
    def on_market_close(self, protfolio: Portfolio):
        """
            市场关市.
        """
        time = self.market.getToday()
        assert time.hour == 15 and time.minute == 0

        assert self.on_bar_per_minute_count > 200

        self.final_valid_capital = protfolio.getValidCapital()

        # 中国平安601318 在datetime(2019, 2, 26, 10, 28)时刻,最低到达 low_price=67.15
        if utils.is_same_day(datetime(2019, 2, 26), self.market.getToday()):
            #当天已经买入121*100股,持有仓位资金不为0
            assert protfolio.getTotalHoldCapital() > 810700
            position = protfolio.getLongPosition("601318")
            assert position.is_long == True
            assert position.pos_total == 121 * 100
            assert position.getPosAvailable() == 0  # 因为今天才交易,可用仓位为0
        pass
Beispiel #7
0
    def on_market_prepare_close(self, protfolio: Portfolio):
        """
            市场准备关市.
        """
        time = self.market.getToday()
        assert time.hour == 14 and time.minute == 57

        # 最开始datetime(2019, 2, 26, 10, 28)买入100股,由于A股T+1的限制,是不可以当天卖的
        if (utils.is_same_day(datetime(2019, 2, 26, 10, 28), time)):
            assert protfolio.sell("601318", 67.00, 100) == False

        pass
Beispiel #8
0
 def __randomTrade(self, protfolio: Portfolio):
     happen = random.random()
     code = "601318"
     price = self.market.getRealTime().getTick(code).close_price
     trade_price = price * random.uniform(0.94, 1.06)
     volume = random.randint(3, 100)
     if happen <= 0.25:
         protfolio.buy(code, trade_price, volume)
     elif happen <= 0.5:
         protfolio.sell(code, trade_price, volume)
     elif happen <= 0.75:
         protfolio.short(code, trade_price, volume)
     else:
         protfolio.cover(code, trade_price, volume)
Beispiel #9
0
    def on_market_prepare_open(self, protfolio: Portfolio, today: datetime):
        """
            市场准备开始(比如:竞价).
        """
        indicator = Indicator(40)
        for code in self.codes:
            bars = self.market.getHistory().getKbars(code, 100)
            indicator.update_bar(bars)
            dif, dea, macd_bar = indicator.kdj()

            ##金叉出现
            if (macd_bar[-1] >= 0 and macd_bar[-2] <= 0):
                tradePrice = bars[-1].close_price * 1.01  # 上一个交易日的收盘价作为买如价
                protfolio.buy(code, tradePrice, 1)
                protfolio.cover(code, tradePrice, 1)  ##平仓做空
                ##死叉出现
            if (macd_bar[-1] <= 0 and macd_bar[-2] >= 0):
                targetPrice = bars[-1].close_price * 0.99  # 上一个交易日的收盘价作为买如价
                protfolio.sell(code, targetPrice, 1)
                protfolio.short(code, targetPrice, 1)  ##开仓做空

        pass
Beispiel #10
0
    def on_market_open(self, protfolio: Portfolio):
        """
            市场开市.
        """
        self.market_open_count = self.market_open_count + 1
        self.on_bar_per_minute_count = 0

        if (is_same_day(datetime(2019, 2, 23), self.market.getToday())):
            ###刚刚开始,没有任何持仓
            assert protfolio.sell("601318", 67.15, 100) == False
            assert protfolio.cover("601318", 67.15, 100) == False

        # 中国平安601318 在datetime(2019, 2, 26, 10, 28)时刻,最低到达 low_price=67.15
        # 中国平安601318 在datetime(2019, 2, 27, 9, 48)时刻,最高到达 high_price=68.57
        # 中国平安601318 在datetime(2019, 2, 28)时刻,9点40分左右到最低点66.8,10:47最高到达68.08,然后14:00后面出现新的第二低点67.2-67.4

        self.daily_pre_tick = self.market.getRealTime().getTick("601318")

        if (is_same_day(datetime(2019, 2, 26, 10, 28),
                        self.market.getToday())):
            assert protfolio.buy("601318", 67.15, 100) == True
            assert protfolio.buy("601318", 67.15, 100) == False  ##钱不够
            assert protfolio.buy("601318", 67.15, 1) == True
            assert protfolio.buy("601318", 67.10,
                                 1) == True  ##价格过低,能委托成功,但没发成交
            assert protfolio.buy("601318", 66.10,
                                 1) == True  ##价格过低,能委托成功,但没发成交
            assert protfolio.buy("601318", 66.10,
                                 1) == True  ##价格过低,能委托成功,但没发成交

        if (is_same_day(datetime(2019, 2, 27, 9, 48), self.market.getToday())):
            position = protfolio.getLongPosition("601318")
            assert position.is_long == True
            assert position.pos_total == 121 * 100
            assert position.getPosAvailable() == 121 * 100  # 昨天买入的,所以今天可用

            assert protfolio.sell("601318", 68.54, 500) == False  ##持仓数不够
            assert protfolio.sell("601318", 68.54, 55) == True
            assert protfolio.sell("601318", 68.54, 46) == True

            assert protfolio.short(
                "601318", 68.54, 10
            ) == True  ###开始做空买入在datetime(2019, 2, 27, 9, 48)时刻,最高到达 high_price=68.57
            assert protfolio.short("601318", 68.70,
                                   1) == True  ##价格过低,能委托成功,但没发成交

        if (is_same_day(datetime(2019, 2, 28, 10, 48),
                        self.market.getToday())):
            assert protfolio.buy("601318", 67.40, 120) == True

        # 4月23日,清空持仓
        if (is_same_day(datetime(2019, 4, 23), self.market.getToday())):
            longPos = protfolio.getLongPosition("601318")
            shortPos = protfolio.getShortPosition("601318")
            assert longPos.pos_lock == 0 and shortPos.pos_lock == 0  #这个时间是没有冻结的
            #high: 2019 - 04 - 23  13: 47:00: open = 83.77, close = 83.88
            #low: 2019 - 04 - 23  09: 31:00: open = 81.38, close = 81.35
            protfolio.cover("601318", 83.77, shortPos.pos_total / 100)
            protfolio.sell("601318", 81.35, longPos.pos_total / 100)

        pass