示例#1
0
    def sell(self, tick):
        if not self.common['lock'].acquire(timeout=0.1):
            return False
        if self.common['b']:
            rdchicang = redisRW.redisrw(redisRW.db_backtest_chicang)
        else:
            rdchicang = redisRW.redisrw(redisRW.db_chicang)
        cc_tol = 0
        cc_amount = Decimal(0)
        cc_price = Decimal(0)
        try:
            rdsell = redisRW.redisrw(redisRW.db_sell)
            code = tick['code']
            tick_chicang = tick['chicang']
            trade_date = tick['datetime']
            price = tick['price']

            cc_tol = tick_chicang['tol']
            cc_price = price
            cc_amount = comm.sell_money_from_tol(code, cc_price, cc_tol)
            if not self.common['b']:
                r = httpTrade.sell(code, tick['name'], price,
                                   tick_chicang['tol'])
                if r:
                    print('卖出下单成功')
            if cc_tol > 0:
                # 可用金额
                self.common['balance'] += cc_amount
                tick_chicang['tol'] -= cc_tol
                if tick_chicang['tol'] == 0:
                    tick_chicang['status'] = 1
                    tick_chicang['sell_price'] = cc_price

                    zdf = (cc_price / tick_chicang['price'] - 1) * 100
                    if not rdchicang.delete(code):
                        print(code, '删除持仓数据错误。')
                    rdsell.write_json(
                        code, {
                            'code': code,
                            'datetime': trade_date,
                            'price': cc_price,
                            'money': cc_amount,
                            'zdf': zdf
                        })
                    return True
        finally:
            self.common['lock'].release()
示例#2
0
 def _stock_select(self, is_backtest, codes_info, date: datetime = None):
     """
     选股
     date 从指定日期开始选股,不指定则为最新
     """
     # 分析的历史根数
     count_week = 1
     count = 2
     count_short = 15
     rdkline = redisRW.redisrw(redisRW.db_kline)
     rdfinance = redisRW.redisrw(redisRW.db_finance)
     rdxg = redisRW.redisrw(redisRW.db_xg)
     rdindex = redisRW.redisrw(redisRW.db_index)
     if is_backtest:
         rdchicang = redisRW.redisrw(redisRW.db_backtest_chicang)
     else:
         rdchicang = redisRW.redisrw(redisRW.db_chicang)
     for d in codes_info:
         code = d['code']
         if date is not None:
             klines = rdkline.read_klines_from_before_date_count_dec(
                 code, date, count)
         else:
             klines = rdkline.read_klines_from_count_dec(code, count)
         # 持仓
         cc_data = rdchicang.read_dec(d['code'])
         if cc_data:
             # 重设价格
             #price = rdkline.read_klines_from_date_dec(code, datetime.datetime.strptime(cc_data['chicang']['datetime'], '%Y%m%d %H:%M:%S'))[0]['open']
             #cc_data['chicang']['price'] = price
             # 更新市值
             cc_data['chicang']['marketValue'] = comm.sell_money_from_tol(
                 code, klines[0]['close'], cc_data['chicang']['tol'])
             rdchicang.delete(d['code'])
             rdchicang.write_json(d['code'], cc_data)
             continue
         if len(klines) == 0:
             continue
         tj = (klines[0]['dif_c_dea'] == 1
               and klines[0]['ma_5'] > klines[1]['ma_5'])
         if not tj:
             continue
         r_data = comm.xg_data(code, d['name'])
         if not rdxg.write_json(code, r_data):
             print(code, '选股数据写入错误。')
示例#3
0
 def _xg_hot_p(self, rdchicang, codes_info, date: datetime = None):
     """
     选股-热点股票
     date 从指定日期开始选股,不指定则为最新
     """
     # 分析的历史根数
     count_week = 1
     count = 2
     count_short = 15
     rdkline = redisRW.redisrw(redisRW.db_kline)
     rdfinance = redisRW.redisrw(redisRW.db_finance)
     rdxg = redisRW.redisrw(redisRW.db_xg)
     rdindex = redisRW.redisrw(redisRW.db_index)
     for d in codes_info:
         code = d['code']
         if date is not None:
             klines = rdkline.read_klines_from_before_date_count_dec(
                 code, date, count)
         else:
             klines = rdkline.read_klines_from_count_dec(code, count)
         # 持仓
         cc_data = rdchicang.read_dec(d['code'])
         if cc_data:
             # 重设价格
             price = rdkline.read_klines_from_date_dec(
                 code,
                 datetime.datetime.strptime(cc_data['chicang']['datetime'],
                                            '%Y%m%d %H:%M:%S'))[0]['open']
             cc_data['chicang']['price'] = price
             # 更新市值
             cc_data['chicang']['marketValue'] = comm.sell_money_from_tol(
                 code, klines[0]['close'], cc_data['chicang']['tol'])
             rdchicang.delete(d['code'])
             rdchicang.write_json(d['code'], cc_data)
             continue
         if len(klines) == 0:
             continue
         tj = klines[0]['pctChg'] > 0.1 and \
              klines[0]['ma_5'] > klines[1]['ma_5']
         if not tj:
             continue
         r_data = comm.xg_data(code, d['name'], 0, None, None, None, None)
         if r_data and (rdxg.read_str(code) is None):
             if not rdxg.write_json(code, r_data):
                 print(code, '选股数据写入错误。')
示例#4
0
 def buy(self, tick, fg_data):
     if not self.common['lock'].acquire(timeout=0.1):
         return False
     if self.common['b']:
         rdchicang = redisRW.redisrw(redisRW.db_backtest_chicang)
     else:
         rdchicang = redisRW.redisrw(redisRW.db_chicang)
     try:
         cc_tol = 0
         cc_amount = Decimal(0)
         cc_price = Decimal(0)
         code = tick['code']
         tick_chicang = tick['chicang']
         trade_date = tick['datetime']
         price = tick['price']
         cangwei = fg_data['cangwei']
         cc_max = fg_data['cc_max']
         # 加仓
         cc_data = rdchicang.read_dec(code)
         # 新开仓
         if not cc_data:
             if len(rdchicang.read_codes()) >= cc_max:
                 return False
         buy_tol = comm.buy_tol_from_money(code, self.common['balance'],
                                           price, cangwei)
         if buy_tol is None:
             #print('余额不足。')
             return False
         cc_price = price
         cc_tol = buy_tol
         cc_amount = comm.sell_money_from_tol(code, cc_price, cc_tol, False)
         if not self.common['b']:
             r = httpTrade.buy(code, tick['name'], price, buy_tol)
             if r:
                 print('买入下单成功')
         if cc_tol > 0:
             # 可用金额
             self.common['balance'] -= cc_amount
             chicang = {
                 'code': code,
                 'datetime': trade_date,
                 'price': cc_price,
                 'tol': cc_tol,
                 'marketValue': cc_amount,
                 'status': 0
             }
             # 加仓
             if cc_data:
                 c = cc_data['chicang']
                 all_tol = c['tol'] + cc_tol
                 chicang['price'] = (
                     (c['price'] * c['tol'] + cc_tol * cc_price) /
                     all_tol).quantize(Decimal('0.01'), ROUND_HALF_UP)
                 chicang['tol'] = all_tol
                 chicang['marketValue'] = comm.sell_money_from_tol(
                     code, chicang['price'], chicang['tol'], False)
                 if not rdchicang.delete(code):
                     print(code, '删除持仓数据错误。')
             for k in fg_data.keys():
                 if k not in ['cangwei', 'price_buy']:
                     chicang[k] = fg_data[k]
             for k in chicang.keys():
                 tick_chicang[k] = chicang[k]
             r_data = comm.xg_data(code, tick['name'], chicang)
             rdchicang.write_json(code, r_data)
             return True
     finally:
         self.common['lock'].release()
示例#5
0
 def buy(self, tick):
     if not self.commobj.lock.acquire(timeout=0.1):
         return False
     try:
         cc_tol = 0
         cc_amount = Decimal(0)
         cc_price = Decimal(0)
         code = tick['code']
         tick_chicang = tick['chicang']
         price = tick['price']
         trade_date = tick['datetime']
         if redisRW.redisrw(redisRW.db_sell).read_str(code):
             return
         # 限定持仓数量
         # if len(self.commobj.rdchicang.read_codes()) >= 1:
         #    return False
         cangwei = '10000'
         """elif cc_tol == 1:
             cangwei = '0.6'
         elif cc_tol == 2:
             cangwei = '0.9'"""
         buy_tol = comm.buy_tol_from_money(code, self.commobj.balance,
                                           price, cangwei)
         if buy_tol is None:
             # print('余额不足。。')
             return False
         if not self.commobj.is_backtest:
             ht_bh = rpcTrade.buy(code, tick['name'], str(price), buy_tol)
             print('买入合同编号:', ht_bh)
             if ht_bh:
                 tm = datetime.datetime.now()
                 e = False
                 while not cc_tol == buy_tol:
                     time.sleep(0.5)
                     if (datetime.datetime.now() - tm).total_seconds() > 10:
                         # 撤单
                         rpcTrade.cancel_oder_all()
                         time.sleep(6)
                         e = True
                     cj_list = rpcTrade.get_chengjiao(ht_bh)
                     if cj_list:
                         cc_tol = 0
                         cc_amount = Decimal(0)
                         cc_price = Decimal(0)
                         for cj in cj_list:
                             cc_tol += cj['tol']
                             cc_price += Decimal(str(cj['price']))
                             cc_amount += Decimal(str(cj['amount']))
                         cc_price /= Decimal(len(cj_list))
                     if e:
                         print('已撤')
                         break
                 if cc_tol > 0:
                     print('已成交:', cc_tol, cc_price, cc_amount)
         else:
             cc_price = price
             cc_tol = buy_tol
             cc_amount = comm.sell_money_from_tol(code, cc_price, cc_tol)
         if cc_tol > 0:
             # 可用金额
             self.commobj.balance -= cc_amount + comm.trade_fee(cc_amount)
             chicang = {
                 'code': code,
                 'datetime': trade_date,
                 'price': cc_price,
                 'base_price': tick['price_low'],
                 'r_price': cc_price,
                 'sell_price': 0,
                 'tol': cc_tol,
                 'marketValue': cc_amount,
                 'status': 0
             }
             for k in chicang.keys():
                 tick_chicang[k] = chicang[k]
             r_data = comm.xg_data(code, tick['name'], None,
                                   tick['zs_date'], tick['zy_date'],
                                   tick['price_zs'], tick['price_zy'],
                                   chicang)
             self.commobj.rdchicang.write_json(code, r_data)
             return True
     finally:
         self.commobj.lock.release()
示例#6
0
    def sell(self, tick):
        if not self.commobj.lock.acquire(timeout=0.1):
            return False
        cc_tol = 0
        cc_amount = Decimal(0)
        cc_price = Decimal(0)
        try:
            rdsell = redisRW.redisrw(redisRW.db_sell)
            code = tick['code']
            tick_chicang = tick['chicang']
            trade_date = tick['datetime']
            price = tick['price']
            if not self.commobj.is_backtest:
                ht_bh = rpcTrade.sell(code, tick['name'], str(price),
                                      tick_chicang['tol'])
                print('卖出合同编号:', ht_bh)
                if ht_bh:
                    tm = datetime.datetime.now()
                    e = False
                    while not cc_tol == tick_chicang['tol']:
                        time.sleep(0.5)
                        if (datetime.datetime.now() - tm).total_seconds() > 30:
                            # 撤单
                            rpcTrade.cancel_oder_all()
                            time.sleep(6)
                            e = True
                        cj_list = rpcTrade.get_chengjiao(ht_bh)
                        if cj_list:
                            cc_tol = 0
                            cc_amount = Decimal(0)
                            cc_price = Decimal(0)
                            for cj in cj_list:
                                cc_tol += cj['tol']
                                cc_price += Decimal(str(cj['price']))
                                cc_amount += Decimal(str(cj['amount']))
                            cc_price /= Decimal(len(cj_list))
                        if e:
                            print('已撤')
                            break
                    if cc_tol > 0:
                        print('已成交:', cc_tol, cc_price, cc_amount)
            else:
                cc_tol = tick_chicang['tol']
                cc_price = price
                cc_amount = comm.sell_money_from_tol(code, cc_price, cc_tol)
            if cc_tol > 0:
                # 可用金额
                self.commobj.balance += cc_amount
                tick_chicang['tol'] -= cc_tol
                if tick_chicang['tol'] == 0:
                    tick_chicang['status'] = 1
                    tick_chicang['sell_price'] = cc_price

                    zdf = (cc_price / tick_chicang['price'] - 1) * 100
                    if not self.commobj.rdchicang.delete(code):
                        print(code, '删除持仓数据错误。')
                    rdsell.write_json(
                        code, {
                            'code': code,
                            'datetime': trade_date,
                            'price': cc_price,
                            'money': cc_amount,
                            'zdf': zdf
                        })
                    return True
        finally:
            self.commobj.lock.release()