Beispiel #1
0
        def Order_At_Boll():
            upper, middle, lower = stock.TDX_BOLL(df_five_hisdat['c'])

            highs, lows, closes = df_five_hisdat['h'], df_five_hisdat[
                'l'], df_five_hisdat['c']

            adx = stock.TDX_ADX(highs, lows, closes)

            self._log('boll : %.2f,%.2f,%.2f' %
                      (upper[-1], middle[-1], lower[-1]))
            boll_w = abs(upper[-1] - lower[-1]) / middle[-1] * 100

            boll_poss = [
                upper[-1],
                (upper[-1] - middle[-1]) / 2 + middle[-1],
                middle[-1],
                (middle[-1] - lower[-1]) / 2 + lower[-1],
                lower[-1],
            ]
            self._log('boll_poss: %.2f, %.2f boll_w=%.2f adx=%.2f' %
                      (boll_poss[0], boll_poss[1], boll_w, adx[-1]))

            user_datas = mydatas[mydatas['symbol'] == code]
            if price > boll_poss[1] * 1.001:
                num = int(user_datas['trade1'])
                if self.order(1, code, price, num):
                    self._log(
                        agl.utf8_to_ascii('一档卖出%s, %.2f, %d' %
                                          (code, price, num)))
            if price > boll_poss[0] * 0.998:
                num = int(user_datas['trade2'])
                if self.order(1, code, price, num):
                    self._log(
                        agl.utf8_to_ascii('二档卖出%s, %.2f, %d' %
                                          (code, price, num)))
            if price < boll_poss[-2] * 0.999:
                num = int(user_datas['trade1'])
                if self.order(0, code, price, num):
                    self._log(
                        agl.utf8_to_ascii('一档买入%s, %.2f, %d' %
                                          (code, price, num)))
            if price < boll_poss[-1] * 1.001:
                num = int(user_datas['trade2'])
                if self.order(0, code, price, num):
                    self._log(
                        agl.utf8_to_ascii('二档买入%s, %.2f, %d' %
                                          (code, price, num)))
            #信号发生时语音播报, 并通知界面回显
            if not self.is_backtesting and (price > boll_poss[1]
                                            or price < boll_poss[-2]):
                codename = stock.GetCodeName(code)
                s = '%s, %.2f' % (codename, price)
                self.data.show(codename)  #通知界面显示
                self.data.speak2(s)  #语音播报
Beispiel #2
0
 def Run(self):
     for strategy in self.policys:
         try:
             strategy.Run()
         except Exception as e:
             s = str(e)
             s += traceback.format_exc()
             strategy._log(agl.utf8_to_ascii(s))
Beispiel #3
0
	def Order_At_Boll():
	    upper, middle, lower = stock.TDX_BOLL(df_five_hisdat['c'])
	    
	    highs, lows, closes = df_five_hisdat['h'], df_five_hisdat['l'], df_five_hisdat['c']
	
	    adx = stock.TDX_ADX(highs, lows, closes)
	
	    self._log('boll : %.2f,%.2f,%.2f'%(upper[-1], middle[-1],lower[-1]))
	    boll_w = abs(upper[-1]-lower[-1])/middle[-1]*100

	    boll_poss = [
	     upper[-1],
	     (upper[-1] - middle[-1])/2+middle[-1],
	     middle[-1],
	     (middle[-1] - lower[-1])/2+lower[-1],	     
	     lower[-1],
	    ]
	    self._log('boll_poss: %.2f, %.2f boll_w=%.2f adx=%.2f'%(boll_poss[0], boll_poss[1], boll_w, adx[-1]))
	    
	    user_datas = mydatas[mydatas['symbol']==code]
	    if price > boll_poss[1]*1.001:
		num = int(user_datas['trade1'])
		if self.order(1, code, price, num):
		    self._log(agl.utf8_to_ascii('一档卖出%s, %.2f, %d'%(code, price, num)))
	    if price > boll_poss[0]*0.998:
		num = int(user_datas['trade2'])
		if self.order(1, code, price, num):
		    self._log(agl.utf8_to_ascii('二档卖出%s, %.2f, %d'%(code, price, num)))
	    if price < boll_poss[-2]*0.999:
		num = int(user_datas['trade1'])
		if self.order(0, code, price, num):
		    self._log(agl.utf8_to_ascii('一档买入%s, %.2f, %d'%(code, price, num)))
	    if price < boll_poss[-1]*1.001:
		num = int(user_datas['trade2'])
		if self.order(0, code, price, num):
		    self._log(agl.utf8_to_ascii('二档买入%s, %.2f, %d'%(code, price, num)))
Beispiel #4
0
 def saveCloseInfo(self):
     """保存收盘信息, 包括资金表和成交表"""
     df = self.ZhiJing()
     df.index = [agl.getCurTime()]
     df.index.name = 't'
     #myredis.set_obj('temp',df)
     db = mysql.Tc()
     df.columns = db.getZhiJinCols()
     db.save(df, tbl_name=mysql.Tc.enum.zhijin)
     df = self.ChengJiao()
     df = df[df[df.columns[-1]]==agl.utf8_to_ascii('普通成交')]
     df.index = df['成交日期'].map(lambda x: x[:4]+'-'+x[4:6]+'-'+x[-2:]) + ' ' + df['成交时间']
     df = df.loc[:,['证券代码','买0卖1','成交数量','成交价格','成交金额','成交编号']]	
     df.columns = db.getChenJiaoCols()
     df.index.name='t'
     db.save(df, tbl_name=mysql.Tc.enum.chenjiao)
Beispiel #5
0
 def Order(self, bSell, code, price, num):
     """return: str 成功则返回委托id号, 失败返回空"""
     #self.delegate.log('-=-----------order')
     #s = ComboArg("bSell, code, price, num")
     s = "Order|"+str(bSell)+"|"+str(code)+"|"+str(price)+"|"+str(num)+"|"
     sReturn = self.delegate.handleRotuer(s)
     #'132493|A5001586|'
     sId = ''
     if sReturn is not None and agl.ascii_to_utf8(sReturn) != "超时":
         sId = sReturn.split('|')[0]
         sSell = '卖出'
         if not bSell:
             sSell = '买入'
         sMsg = '委托下单 %s %s %s %s , 委托单号%s'%(sSell, str(code), agl.FloatToStr(float(price)), str(num), str(sId))
         sMsg = agl.utf8_to_ascii(sMsg)
         self.delegate.log(sMsg)
     return sId  
Beispiel #6
0
    def Run(self):
        """
	"""
        #self._log('Strategy_Boll_Pre')

        #以下为交易测试
        code = self.data.get_code()  #当前策略处理的股票
        self.code = code
        if not self.is_backtesting and not self.AllowCode(code):
            return

        self._log(self.getCurTime())
        df_hisdat = self.data.get_hisdat(code)  #日k线
        df_five_hisdat = self.data.get_hisdat(code, dtype='5min')  #5分钟k线

        account = self._getAccount()  #获取交易账户
        price = float(df_five_hisdat.tail(1)['c'])  #当前股价
        closes = df_hisdat['c']
        yestoday_close = closes[-2]  #昨日收盘价
        account_mgr = ac.AccountMgr(account, price, code)
        trade_num = ac.ShouShu(account_mgr.init_money() *
                               self.trade_num_use_money_percent / price)

        # 信号计算
        four = stock.FOUR(df_hisdat['c'])[-1]
        upper, middle, lower = stock.TDX_BOLL(df_five_hisdat['c'])
        highs, lows, closes = df_five_hisdat['h'], df_five_hisdat[
            'l'], df_five_hisdat['c']
        adx = stock.TDX_ADX(highs, lows, closes)
        adx = adx[-1]
        self._log('boll : %.2f,%.2f,%.2f' % (upper[-1], middle[-1], lower[-1]))
        boll_w = abs(upper[-1] - lower[-1]) / middle[-1] * 100

        boll_poss = [
            upper[-1],
            (upper[-1] - middle[-1]) / 2 + middle[-1],
            middle[-1],
            (middle[-1] - lower[-1]) / 2 + lower[-1],
            lower[-1],
        ]
        self._log('boll_poss: %.2f, %.2f boll_w=%.2f adx=%.2f' %
                  (boll_poss[0], boll_poss[1], boll_w, adx))

        pre_price = account_mgr.last_chengjiao_price(is_sell=-1)  #上一个成交的价位
        pre_buy_price = account_mgr.last_chengjiao_price(is_sell=0)
        if np.isnan(pre_buy_price):
            pre_buy_price = pre_price
        pre_sell_num = account_mgr.last_chengjiao_num()  #上次的成交数量
        pre_pre_price = account_mgr.last_chengjiao_price(index=-2)
        sell_count = account_mgr.queryTradeCount(1)
        buy_count = account_mgr.queryTradeCount(0)
        chen_ben = account_mgr.get_BuyAvgPrice()  #买入成本
        yin_kui = account_mgr.yin_kui()  #盈亏成本
        canwei = account_mgr.getCurCanWei()

        #信号判断
        num = 0
        if so.assemble(
                price > boll_poss[1] * 1.001,
                price > pre_price * (1 + self.trade_ratio),
                #price > boll_poss[2],
                #price > self.max_buy_price*(1+self.trade_ratio),
                #boll_w < 2.5,
                #adx > 45,
                #sell_count < 2,
                #pr.horizontal(df_five_hisdat),
                #0,
        ):
            num = trade_num
            #account.Order(1, code, price, num)
        if so.assemble(
                price > boll_poss[0],
                price > pre_price * (1 + self.trade_ratio),
                #price > self.max_buy_price*(1+self.trade_ratio),
                #boll_w > 3,
                #adx>60,
                four > self.trade_four[1],
                #sell_count < 2,
                #self.trade_status == self.enum.nothing,
                #0,
        ):
            if pre_sell_num > 0:
                num = (pre_sell_num * self.trade_num_ratio)
            else:
                num = ac.ShouShu(account_mgr.getCurCanWei() * 0.5)
            #self.trade_status = self.enum.boll_up
            if num > 0 and self.order(1, code, price, num):
                self._log(
                    agl.utf8_to_ascii('二档卖出%s, %.2f, %d' % (code, price, num)))
        if so.assemble(
                price < boll_poss[-2] * 0.999,
                price < pre_price * (1 - self.trade_ratio),
                #price < boll_poss[2],
                #price < self.min_sell_price*(1-0.03),
                #boll_w > 2.5,
                #adx>45,
                #buy_count < 2,
                #pr.horizontal(df_five_hisdat),
                #0,
        ):
            num = trade_num
            #account.Order(0, code, price, num)
        if so.assemble(
                price < boll_poss[-1],
                price < pre_buy_price * (1 - self.trade_ratio),
                #price < self.min_sell_price*(1-0.03),
                #boll_w > 3,
                #buy_count < 2,
                #self.trade_status == self.enum.nothing,
                #adx>60,
                four < self.trade_four[0],
                #0,
        ):
            #加仓买入
            num = account_mgr.getCurCanWei() * self.trade_num_ratio
            #self.trade_status = self.enum.boll_down
            if num > 0 and self.order(0, code, price, num):
                self._log(
                    agl.utf8_to_ascii('二档买入%s, %.2f, %d' % (code, price, num)))

        #建首仓
        if so.assemble(
                four < self.trade_four[0],
                canwei == 0,
        ):
            num = ac.ShouShu(account_mgr.total_money() * self.lowerhold /
                             price)
            bSell = 0
            self.order(bSell, code, price, num)

        #tick report
        if self.is_backtesting and self.is_tick_report:
            self._getAccount().TickReport(df_five_hisdat, 'win')
        return