def setup_realtime_swt(swt, stock_number, qr_date): # 当没有当周数据时,用日线数据补 sdt = SDT.objects(Q(stock_number=stock_number) & Q(date=qr_date)) if not sdt: return list() qr_date_trading = sdt[0] extra_swt = SWT() extra_swt.weekly_close_price = qr_date_trading.today_closing_price extra_swt.last_trade_date = qr_date_trading.date swt = list(swt) swt.insert(0, extra_swt) return swt
def quant_stock(stock_number, stock_name, **kwargs): qr_date = kwargs['qr_date'] if not pre_sdt_check(stock_number, **kwargs): return strategy_name = "depart_long_week" last_trade_date = qr_date + datetime.timedelta(days=7) swt = SWT.objects( Q(stock_number=stock_number) & Q(last_trade_date__lte=last_trade_date)).order_by( '-last_trade_date')[:ema_volume] use_ad_price, swt = is_ad_price(stock_number, qr_date, swt) if not swt: return trading_data = format_trading_data(swt, use_ad_price) df = calculate_macd(DataFrame(trading_data), short_ema, long_ema, dif_ema) df = calculate_ma(df, short_ma, long_ma) this_week = df.iloc[-1] last_week = df.iloc[-2] if last_week['close_price'] < last_week['long_ma'] and this_week['close_price'] > this_week['short_ma']\ and this_week['close_price'] > this_week['long_ma']: if use_ad_price: init_price = swt[0].weekly_close_price else: init_price = this_week['close_price'] increase_rate = round( (this_week['close_price'] - last_week['close_price']) / last_week['close_price'], 4) * 100 short_point = -1 for i in range(1, len(df)): if df.iloc[-i].diff_ma > 0: short_point = i break # if short_point < period: # return if df.iloc[-short_point:].macd.sum() > 0: # print(stock_number) # pd.set_option('display.max_columns', 500) # pd.set_option('display.width', 1000) # print(df.iloc[-short_point:]) qr = QR(stock_number=stock_number, stock_name=stock_name, date=qr_date, strategy_direction='long', strategy_name=strategy_name, init_price=init_price, industry_involved=kwargs.get('industry_involved'), increase_rate=increase_rate) if not check_duplicate_strategy(qr): qr.save() return qr
def quant_stock(stock_number, stock_name, **kwargs): short_ma = kwargs['short_ma'] long_ma = kwargs['long_ma'] qr_date = kwargs['qr_date'] if not pre_sdt_check(stock_number, **kwargs): return if short_ma < long_ma: strategy_direction = 'long' quant_count = long_ma + 5 else: strategy_direction = 'short' quant_count = short_ma + 5 strategy_name = 'week_through_%s_%s_%s' % (strategy_direction, short_ma, long_ma) last_trade_date = qr_date + datetime.timedelta(days=7) swt = SWT.objects( Q(stock_number=stock_number) & Q(last_trade_date__lte=last_trade_date)).order_by( '-last_trade_date')[:quant_count] use_ad_price, swt = is_ad_price(stock_number, qr_date, swt) if not swt: return trading_data = format_trading_data(swt, use_ad_price) # end_date = qr_date.strftime('%Y-%m-%d') # start_date = (qr_date - datetime.timedelta(days=max(short_ma, long_ma) * 7)).strftime('%Y-%m-%d') # trading_data = get_week_trading(stock_number, start_date, end_date) df = calculate_ma(DataFrame(trading_data), short_ma, long_ma) this_week = df.iloc[-1] last_week = df.iloc[-2] if last_week['close_price'] < last_week['long_ma'] and this_week['close_price'] > this_week['short_ma']\ and this_week['close_price'] > this_week['long_ma']: if use_ad_price: init_price = swt[0].weekly_close_price else: init_price = this_week['close_price'] increase_rate = round( (this_week['close_price'] - last_week['close_price']) / last_week['close_price'], 4) * 100 qr = QR(stock_number=stock_number, stock_name=stock_name, date=qr_date, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=init_price, industry_involved=kwargs.get('industry_involved'), increase_rate=increase_rate) if not check_duplicate_strategy(qr): qr.save() return qr return
def quant_stock(stock_number, stock_name, **kwargs): short_ma = kwargs['short_ma'] long_ma = kwargs['long_ma'] qr_date = kwargs['qr_date'] if not pre_sdt_check(stock_number, **kwargs): return if short_ma < long_ma: strategy_direction = 'long' quant_count = long_ma + 5 else: strategy_direction = 'short' quant_count = short_ma + 5 strategy_name = 'maweek_%s_%s_%s' % (strategy_direction, short_ma, long_ma) swt = SWT.objects( Q(stock_number=stock_number) & Q(last_trade_date__lte=qr_date)).order_by( '-last_trade_date')[:quant_count] if not swt: return use_ad_price, swt = is_ad_price(stock_number, qr_date, swt) if not swt: return trading_data = format_trading_data(swt, use_ad_price) df = calculate_ma(DataFrame(trading_data), short_ma, long_ma) this_week = df.iloc[-1] last_week = df.iloc[-2] if this_week['diff_ma'] > 0 > last_week['diff_ma']: if use_ad_price: init_price = swt[0].weekly_close_price else: init_price = this_week['close_price'] increase_rate = round( (this_week['close_price'] - last_week['close_price']) / last_week['close_price'], 4) * 100 qr = QR(stock_number=stock_number, stock_name=stock_name, date=this_week.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=init_price, industry_involved=kwargs.get('industry_involved'), increase_rate=increase_rate) if not check_duplicate_strategy(qr): qr.save() return qr return
def quant_stock(stock_number, stock_name, **kwargs): short_ema = kwargs['short_ema'] long_ema = kwargs['long_ema'] dif_ema = kwargs['dif_ema'] qr_date = kwargs['qr_date'] if not pre_sdt_check(stock_number, **kwargs): return strategy_direction = 'long' quant_count = 150 strategy_name = 'week_macd_%s_%s_%s_%s' % (strategy_direction, short_ema, long_ema, dif_ema) last_trade_date = qr_date + datetime.timedelta(days=7) swt = SWT.objects( Q(stock_number=stock_number) & Q(last_trade_date__lte=last_trade_date)).order_by( '-last_trade_date')[:quant_count] use_ad_price, swt = is_ad_price(stock_number, qr_date, swt) if not swt: return trading_data = format_trading_data(swt, use_ad_price) df = calculate_macd(DataFrame(trading_data), short_ema, long_ema, dif_ema) this_week = df.iloc[-1] last_week = df.iloc[-2] if last_week['macd'] < 0 < this_week['macd']: if use_ad_price: init_price = swt[0].weekly_close_price else: init_price = this_week['close_price'] increase_rate = round( (this_week['close_price'] - last_week['close_price']) / last_week['close_price'], 4) * 100 qr = QR(stock_number=stock_number, stock_name=stock_name, date=qr_date, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=init_price, industry_involved=kwargs.get('industry_involved'), increase_rate=increase_rate) if not check_duplicate_strategy(qr): qr.save()
def check_duplicate(trading_data): if isinstance(trading_data, SDT): cursor = SDT.objects( Q(stock_number=trading_data.stock_number) & Q(date=trading_data.date)) if cursor: return True else: return False elif isinstance(trading_data, SWT): cursor = SWT.objects( Q(stock_number=trading_data.stock_number) & Q(first_trade_date=trading_data.first_trade_date)) if cursor: return True else: return False
def is_week_long(stock_number, qr_date, short_ma, long_ma): if short_ma < long_ma: quant_count = long_ma + 5 else: quant_count = short_ma + 5 swt = SWT.objects( Q(stock_number=stock_number) & Q(last_trade_date__lte=qr_date)).order_by( '-last_trade_date')[:quant_count] if not swt: return False use_ad_price = True trading_data = format_trading_data(swt, use_ad_price) df = calculate_ma(DataFrame(trading_data), short_ma, long_ma) this_week = df.iloc[-1] if this_week['diff_ma'] > 0: return True else: return False
def collect_stock_data(stock_number, start_date, end_date): if not isinstance(start_date, datetime.date) or not isinstance( end_date, datetime.date): return end_date += datetime.timedelta(days=7) url = datayes_week_ad.format(stock_number, start_date.strftime('%Y%m%d'), end_date.strftime('%Y%m%d')) res_data = json.loads(send_request(url, datayes_headers)) if res_data.get('retCode', 0) != 1: return trading_data = res_data.get('data', []) for i in trading_data: trade_days = int(i.get('tradeDays')) if trade_days == 0: continue stock_number = i.get('ticker') try: first_trade_date = datetime.datetime.strptime( i.get('weekBeginDate'), '%Y-%m-%d') except Exception as e: logging.error('Format time failed:' + str(e)) continue former_swt = SWT.objects( Q(stock_number=stock_number) & Q(first_trade_date=first_trade_date)) if former_swt: swt = former_swt.next() swt.ad_open_price = float(i.get('openPrice')) swt.ad_close_price = float(i.get('closePrice')) swt.ad_highest_price = float(i.get('highestPrice')) swt.ad_lowest_price = float(i.get('lowestPrice')) swt.save()
def collect_stock_data(stock_number, start_date, end_date): if not isinstance(start_date, datetime.date) or not isinstance(end_date, datetime.date): return end_date += datetime.timedelta(days=7) url = datayes_week_ad.format(stock_number, start_date.strftime('%Y%m%d'), end_date.strftime('%Y%m%d')) res_data = json.loads(send_request(url, datayes_headers)) if res_data.get('retCode', 0) != 1: return trading_data = res_data.get('data', []) for i in trading_data: trade_days = int(i.get('tradeDays')) if trade_days == 0: continue stock_number = i.get('ticker') try: first_trade_date = datetime.datetime.strptime(i.get('weekBeginDate'), '%Y-%m-%d') end_date = datetime.datetime.strptime(i.get('endDate'), '%Y-%m-%d') last_trade_date = datetime.datetime.strptime(i.get('endDate'), '%Y-%m-%d') except Exception as e: logging.error('Format time failed:' + str(e)) continue former_swt = SWT.objects(Q(stock_number=stock_number) & Q(first_trade_date=first_trade_date)) if len(former_swt): swt = former_swt[0] swt.trade_days = trade_days swt.first_trade_date = first_trade_date swt.last_trade_date = last_trade_date swt.end_date = end_date swt.ad_open_price = float(i.get('openPrice')) swt.ad_close_price = float(i.get('closePrice')) swt.ad_highest_price = float(i.get('highestPrice')) swt.ad_lowest_price = float(i.get('lowestPrice')) else: swt = SWT() swt.stock_number = stock_number swt.stock_name = i.get('secShortName') swt.trade_days = trade_days swt.first_trade_date = first_trade_date swt.last_trade_date = last_trade_date swt.end_date = end_date swt.pre_close_price = float(i.get('preClosePrice')) swt.ad_open_price = float(i.get('openPrice')) swt.ad_close_price = float(i.get('closePrice')) swt.ad_highest_price = float(i.get('highestPrice')) swt.ad_lowest_price = float(i.get('lowestPrice')) swt.increase_rate = str(round(i.get('chgPct') * 100, 2)) + '%' swt.turnover_amount = int(i.get('turnoverValue')) / 10000 swt.turnover_volume = int(i.get('turnoverVol')) / 100 swt.save()