def quant_stock(stock_number, stock_name, **kwargs): week_long = kwargs.get('week_long', False) if not pre_sdt_check(stock_number, **kwargs): return real_time = kwargs.get('real_time', False) sdt = SDT.objects(Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=kwargs['qr_date'])).order_by('-date')[:ema_volume] if float(sdt[0].increase_rate.replace('%', '')) > 9: return if real_time: sdt = setup_realtime_sdt(stock_number, sdt, kwargs) if not sdt: return trading_data = format_trading_data(sdt) df = calculate_macd(DataFrame(trading_data), kwargs['short_ema'], kwargs['long_ema'], kwargs['dif_ema']) df = calculate_ma(df, kwargs['short_ma'], kwargs['long_ma']) today = df.iloc[-1] yestoday = df.iloc[-2] if short_ma <= long_ma: strategy_direction = 'long' else: strategy_direction = 'short' strategy_name = 'ma_macd_%s_%s_%s' % (strategy_direction, short_ma, long_ma) if week_long: strategy_name = 'weeklong_' + strategy_name if today['diff_ma'] > 0 > yestoday['diff_ma']: increase_rate = round((today['close_price'] - yestoday['close_price']) / yestoday['close_price'], 4) * 100 qr = '' if strategy_direction == 'long': if today['macd'] > 0 > today['dif'] and today['dea'] < 0: qr = QR( stock_number=stock_number, stock_name=stock_name, date=today.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today['close_price'], industry_involved=kwargs.get('industry_involved'), increase_rate=increase_rate ) elif strategy_direction == 'short': if today['macd'] < 0 < today['dif'] and today['dea'] > 0: qr = QR( stock_number=stock_number, stock_name=stock_name, date=today.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today['close_price'], industry_involved=kwargs.get('industry_involved'), increase_rate=increase_rate ) if isinstance(qr, QR): if real_time: return qr if not check_duplicate_strategy(qr): qr.save() return qr return ''
def quant_stock(stock_number, stock_name, **kwargs): real_time = kwargs.get('real_time', False) sdt = SDT.objects(Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=kwargs['qr_date'])).order_by('-date')[:ema_volume] if len(sdt) < ema_volume-50: return if float(sdt[0].increase_rate.replace('%', '')) > 9: return if real_time: today_sdt = SDT.objects(date=kwargs['qr_date']) if kwargs['qr_date'] == datetime.date.today() and not today_sdt: today_trading = kwargs.get('today_trading', {}) if not today_trading.get(stock_number): return sdt = list(sdt) sdt.insert(0, today_trading.get(stock_number)) trading_data = format_trading_data(sdt) df = calculate_macd(DataFrame(trading_data), kwargs['short_ema'], kwargs['long_ema'], kwargs['dif_ema']) df = calculate_ma(df, kwargs['short_ma'], kwargs['long_ma']) today_analysis = df.iloc[-1] yestoday_analysis = df.iloc[-2] if short_ma <= long_ma: strategy_direction = 'long' else: strategy_direction = 'short' strategy_name = 'ma_macd_%s_%s_%s' % (strategy_direction, short_ma, long_ma) if today_analysis['diff_ma'] > 0 > yestoday_analysis['diff_ma']: qr = '' if strategy_direction == 'long': if today_analysis['macd'] > 0 > today_analysis['dif'] and today_analysis['dea'] < 0: qr = QR( stock_number=stock_number, stock_name=stock_name, date=today_analysis.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today_analysis['close_price'] ) elif strategy_direction == 'short': if today_analysis['macd'] < 0 < today_analysis['dif'] and today_analysis['dea'] > 0: qr = QR( stock_number=stock_number, stock_name=stock_name, date=today_analysis.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today_analysis['close_price'] ) if isinstance(qr, QR): if real_time: return qr if not check_duplicate_strategy(qr): qr.save() return qr return ''
def quant_stock(stock_number, stock_name, **kwargs): if not pre_sdt_check(stock_number, **kwargs): return real_time = kwargs.get('real_time', False) sdt = SDT.objects(Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=kwargs['qr_date'])).order_by('-date')[:ema_volume] if float(sdt[0].increase_rate.replace('%', '')) > 9: return '' if real_time: sdt = setup_realtime_sdt(stock_number, sdt, kwargs) if not sdt: return trading_data = format_trading_data(sdt) df = calculate_macd(DataFrame(trading_data), kwargs['short_ema'], kwargs['long_ema'], kwargs['dif_ema']) today = df.iloc[-1] yestoday = df.iloc[-2] strategy_direction = 'long' if yestoday['dif'] < 0 < today['dif']: strategy_name = 'dif_day_%s_%s_%s_%s' % (strategy_direction, kwargs['short_ema'], kwargs['long_ema'], kwargs['dif_ema']) increase_rate = round((today['close_price'] - yestoday['close_price']) / yestoday['close_price'], 4) * 100 qr = QR( stock_number=stock_number, stock_name=stock_name, date=today.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today['close_price'], industry_involved=kwargs.get('industry_involved'), increase_rate=increase_rate ) if real_time and strategy_direction == 'long': return qr if not real_time and not check_duplicate_strategy(qr): qr.save() return qr return ''
def quant_stock(stock_number, stock_name, **kwargs): qr_date = kwargs['qr_date'] week_long = kwargs.get('week_long', False) if not pre_sdt_check(stock_number, **kwargs): return strategy_name = 'goup_staying' if week_long: strategy_name = 'weeklong_' + strategy_name sdt = SDT.objects( Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=qr_date)).order_by('-date')[:quant_count] if len(sdt) < quant_count: return if float(sdt[0].increase_rate.replace('%', '').strip()) > goup_stay: qr = QR(stock_number=stock_number, stock_name=stock_name, date=qr_date, strategy_direction='long', strategy_name=strategy_name, init_price=sdt[0].today_closing_price, industry_involved=kwargs.get('industry_involved'), increase_rate=float(sdt[0].increase_rate.replace('%', '').strip())) if not check_duplicate_strategy(qr): qr.save() return qr
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'] week_long = kwargs.get('week_long', False) if not pre_sdt_check(stock_number, **kwargs): return real_time = kwargs.get('real_time', False) if short_ma <= long_ma: strategy_direction = 'long' quant_count = long_ma + 5 else: strategy_direction = 'short' quant_count = short_ma + 5 strategy_name = 'ma_%s_%s_%s' % (strategy_direction, short_ma, long_ma) if week_long: strategy_name = 'weeklong_' + strategy_name sdt = SDT.objects( Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=qr_date)).order_by('-date')[:quant_count] if len(sdt) < quant_count: # trading data not enough return if real_time: sdt = setup_realtime_sdt(stock_number, sdt, kwargs) if not sdt: return trading_data = format_trading_data(sdt) if not trading_data: return df = calculate_ma(DataFrame(trading_data), short_ma, long_ma) today = df.iloc[-1] yestoday = df.iloc[-2] if today['diff_ma'] > 0 > yestoday['diff_ma']: increase_rate = round( (today['close_price'] - yestoday['close_price']) / yestoday['close_price'], 4) * 100 qr = QR(stock_number=stock_number, stock_name=stock_name, date=today.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today['close_price'], industry_involved=kwargs.get('industry_involved'), increase_rate=increase_rate) if real_time: return qr 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 = '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): real_time = kwargs.get('real_time', False) strategy_direction = 'long' strategy_name = 'break_through_%s_%s_%s' % ( strategy_direction, kwargs['short_ma'], kwargs['long_ma']) sdt = SDT.objects( Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=kwargs['qr_date'])).order_by( '-date')[:kwargs['long_ma'] + 10] if not sdt: return if float(sdt[0].increase_rate.replace('%', '')) > 9: return if sdt[0].today_closing_price <= sdt[0].today_average_price: return if sdt[0].turnover_amount <= sdt[1].turnover_amount: return if real_time: today_sdt = SDT.objects(date=kwargs['qr_date']) if kwargs['qr_date'] == datetime.date.today() and not today_sdt: today_trading = kwargs.get('today_trading', {}) if not today_trading.get(stock_number): return if float( today_trading.get(stock_number).increase_rate.replace( '%', '')) > 9: return sdt = list(sdt) sdt.insert(0, today_trading.get(stock_number)) trading_data = format_trading_data(sdt) df = calculate_ma(DataFrame(trading_data), short_ma, long_ma) today = df.iloc[-1] yestoday = df.iloc[-2] break_through = 1.8 if yestoday['close_price'] < yestoday['short_ma'] and yestoday['close_price'] < yestoday['long_ma']\ and today['close_price'] > today['short_ma'] and today['close_price'] > today['long_ma']: if today['quantity_relative_ratio'] > break_through or\ today['turnover_amount']/float(yestoday['turnover_amount']) > break_through: qr = QR(stock_number=stock_number, stock_name=stock_name, date=today.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today['close_price']) if real_time: return qr if not check_duplicate_strategy(qr): qr.save() return qr return ''
def quant_stock(stock_number, stock_name, **kwargs): sdt_li = SDT.objects( Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=kwargs['date'])).order_by('-date')[:ema_volume] if len(sdt_li) < 20: return trading_data = [] qr_date = kwargs['date'] standard_total_stock = sdt_li[0].total_stock if not standard_total_stock: standard_total_stock = sdt_li[1].total_stock if not standard_total_stock: standard_total_stock = sdt_li[2].total_stock if not standard_total_stock: return for s in sdt_li: total_stock = s.total_stock if total_stock and total_stock != standard_total_stock: today_closing_price = s.today_closing_price * float( total_stock) / float(standard_total_stock) else: today_closing_price = s.today_closing_price trading_data.append({ 'date': s.date, 'price': today_closing_price, 'total_stock': s.total_stock }) trading_data.reverse() df = DataFrame(trading_data).set_index(['date']) df['short_ema'] = df['price'].ewm(span=kwargs['short_ema']).mean() df['long_ema'] = df['price'].ewm(span=kwargs['long_ema']).mean() df['dif'] = df['short_ema'] - df['long_ema'] df['dea'] = df['dif'].ewm(span=kwargs['dif_ema']).mean() df['macd'] = df['dif'] - df['dea'] today_macd = df.iloc[-1] yestoday_macd = df.iloc[-2] if yestoday_macd['macd'] < 0 < today_macd['macd']: strategy_direction = 'long' strategy_name = 'macd_long_%s_%s_%s' % ( kwargs['short_ema'], kwargs['long_ema'], kwargs['dif_ema']) qr = QR(stock_number=stock_number, stock_name=stock_name, date=today_macd.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today_macd['price']) if not check_duplicate(qr): qr.save()
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_ma = kwargs['short_ma'] long_ma = kwargs['long_ma'] qr_date = kwargs['qr_date'] real_time = kwargs.get('real_time', False) if short_ma <= long_ma: strategy_direction = 'long' quant_count = long_ma + 5 else: strategy_direction = 'short' quant_count = short_ma + 5 strategy_name = 'ma_%s_%s_%s' % (strategy_direction, short_ma, long_ma) sdt = SDT.objects( Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=qr_date)).order_by('-date')[:quant_count] if len(sdt) < quant_count: # trading data not enough return if float(sdt[0].increase_rate.replace('%', '')) > 9: return if real_time: today_sdt = SDT.objects(date=kwargs['qr_date']) if kwargs['qr_date'] == datetime.date.today() and not today_sdt: today_trading = kwargs.get('today_trading', {}) if not today_trading.get(stock_number): return sdt = list(sdt) sdt.insert(0, today_trading.get(stock_number)) trading_data = format_trading_data(sdt) if not trading_data: return df = calculate_ma(DataFrame(trading_data), short_ma, long_ma) today_ma = df.iloc[-1] yestoday_ma = df.iloc[-2] if today_ma['diff_ma'] > 0 > yestoday_ma['diff_ma']: qr = QR(stock_number=stock_number, stock_name=stock_name, date=today_ma.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today_ma['close_price']) if real_time: return qr 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 quant_stock(stock_number, stock_name, **kwargs): if not pre_sdt_check(stock_number, **kwargs): return real_time = kwargs.get('real_time', False) week_long = kwargs.get('week_long', False) strategy_direction = 'long' strategy_name = 'break_through_%s_%s_%s' % (strategy_direction, kwargs['short_ma'], kwargs['long_ma']) if week_long: strategy_name = 'weeklong_' + strategy_name sdt = SDT.objects(Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=kwargs['qr_date'])).order_by('-date')[:kwargs['long_ma']+10] if not sdt: return if not real_time: if sdt[0].today_closing_price <= sdt[0].today_average_price: return if real_time: sdt = setup_realtime_sdt(stock_number, sdt, kwargs) if not sdt: return if sdt[0].today_closing_price < sdt[0].today_opening_price: return trading_data = format_trading_data(sdt) df = calculate_ma(DataFrame(trading_data), short_ma, long_ma) today = df.iloc[-1] yestoday = df.iloc[-2] if yestoday['close_price'] < yestoday['long_ma'] and today['close_price'] > today['short_ma']\ and today['close_price'] > today['long_ma']: increase_rate = round((today['close_price'] - yestoday['close_price']) / yestoday['close_price'], 4) * 100 qr = QR( stock_number=stock_number, stock_name=stock_name, date=today.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today['close_price'], industry_involved=kwargs.get('industry_involved'), increase_rate=increase_rate ) if real_time: return qr if not check_duplicate_strategy(qr): qr.save() return qr return ''
def save_quant_result(sdt, strategy_name, strategy_direction='long'): if isinstance(sdt, SDT): stock_number = sdt.stock_number stock_name = sdt.stock_name date = sdt.date strategy_name = strategy_name init_price = sdt.today_closing_price if not check_duplicate(stock_number, date, strategy_name): qr = QR(stock_number=stock_number, stock_name=stock_name, date=date, strategy_name=strategy_name, init_price=init_price, strategy_direction=strategy_direction) qr.save()
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' strategy_name = 'week_boot_long_%s_%s_%s' % (short_ema, long_ema, dif_ema) end_date = qr_date.strftime('%Y-%m-%d') start_date = (qr_date - datetime.timedelta(days=max(short_ema, long_ema) * 7)).strftime('%Y-%m-%d') trading_data = get_week_trading(stock_number, start_date, end_date) if len(trading_data): return df = calculate_macd(DataFrame(trading_data), short_ema, long_ema, dif_ema) this_week = df.iloc[-1] last_week = df.iloc[-2] recent_period = df.iloc[-20:] recent_dif = recent_period['dif'] # print('dif max:%s' % recent_dif.max()) if recent_dif.max() > 0: return # print("%s last:%s, this:%s" % (stock_number, last_week['dif'], this_week['dif'])) if last_week['dif'] < 0 < this_week['dif']: 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 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_day" sdt = SDT.objects( Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=qr_date)).order_by('-date')[:ema_volume] trading_data = format_trading_data(sdt) df = calculate_ma(DataFrame(trading_data), short_ma, long_ma) df = calculate_macd(df, short_ema, long_ema, dif_ema) today = df.iloc[-1] yestoday = df.iloc[-2] if yestoday['close_price'] < yestoday['long_ma'] and today['close_price'] > today['short_ma'] \ and today['close_price'] > today['long_ma']: 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: increase_rate = round( (today['close_price'] - yestoday['close_price']) / yestoday['close_price'], 4) * 100 qr = QR(stock_number=stock_number, stock_name=stock_name, date=today.name, strategy_direction='long', strategy_name=strategy_name, init_price=today['close_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_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' strategy_name = 'month_macd_%s_%s_%s_%s' % (strategy_direction, short_ema, long_ema, dif_ema) end_date = qr_date.strftime('%Y-%m-%d') start_date = (qr_date - datetime.timedelta(days=short_ema * long_ema * 31)).strftime('%Y-%m-%d') trading_data = get_month_trading(stock_number, start_date, end_date) trading_data = trading_data.iloc[-150:] if len(trading_data) < max(short_ema, long_ema): return df = calculate_macd(trading_data, short_ema, long_ema, dif_ema) this_month = df.iloc[-1] last_month = df.iloc[-2] if last_month['macd'] < 0 < this_month['macd']: init_price = this_month['close_price'] increase_rate = round( (this_month['close_price'] - last_month['close_price']) / last_month['close_price'], 4) * 100 qr = QR(stock_number=stock_number, stock_name=stock_name, date=this_month.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): sdt_li = SDT.objects( Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=kwargs['date'])).order_by('-date')[:200] if not sdt_li: return trading_data = [] for s in sdt_li: trading_data.append({ 'date': s.date, 'price': s.today_closing_price, 'total_stock': s.total_stock }) trading_data.reverse() # trading_data = restore_right(trading_data) df = DataFrame(trading_data).set_index(['date']) df['short_ema'] = df['price'].ewm(span=kwargs['short_ema']).mean() df['long_ema'] = df['price'].ewm(span=kwargs['long_ema']).mean() df['dif'] = df['short_ema'] - df['long_ema'] df['dea'] = df['dif'].ewm(span=kwargs['dif_ema']).mean() df['macd'] = df['dif'] - df['dea'] today_macd = df.iloc[-1] yestoday_macd = df.iloc[-2] if today_macd['dif'] < 0 and today_macd['dea'] < 0 < today_macd[ 'macd'] and yestoday_macd['macd'] < 0: strategy_direction = 'long' strategy_name = 'macd_long_%s_%s_%s' % ( kwargs['short_ema'], kwargs['long_ema'], kwargs['dif_ema']) qr = QR(stock_number=stock_number, stock_name=stock_name, date=today_macd.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today_macd['price']) if not check_duplicate(qr): qr.save()
def quant_stock(stock_number, short_ma_num, long_ma_num, qr_date): if short_ma_num <= long_ma_num: strategy_direction = 'long' quant_count = long_ma_num + 5 else: strategy_direction = 'short' quant_count = short_ma_num + 5 strategy_name = 'ma_%s_%s_%s' % (strategy_direction, short_ma_num, long_ma_num) sdt = SDT.objects( Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=qr_date)).order_by('-date')[:quant_count] if len(sdt) < quant_count: # trading data not enough return trading_data = format_trading_data(sdt, qr_date) if not trading_data: return df = DataFrame(trading_data).set_index(['date']) df['short_ma'] = df['price'].rolling(window=short_ma_num, center=False).mean() df['long_ma'] = df['price'].rolling(window=long_ma_num, center=False).mean() df['diff'] = df['short_ma'] - df['long_ma'] today_ma = df.iloc[-1] yestoday_ma = df.iloc[-2] if today_ma['diff'] > 0 > yestoday_ma['diff']: qr = QR(stock_number=stock_number, stock_name=today_ma['stock_name'], date=today_ma.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today_ma['price']) if not check_duplicate(qr): qr.save()
def quant_stock(stock_number, stock_name, **kwargs): length = kwargs['length'] qr_date = kwargs['qr_date'] real_time = kwargs.get('real_time', False) if not pre_sdt_check(stock_number, **kwargs): return strategy_name = 'new_peak_%s' % length strategy_direction = 'long' sdt = SDT.objects( Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=qr_date)).order_by('-date')[:length] if len(sdt) < length: return if real_time: sdt = setup_realtime_sdt(stock_number, sdt, kwargs) trading_data = format_trading_data(sdt) if not trading_data: return df = DataFrame(trading_data) today_data = df.iloc[-1] if df['close_price'].max() <= today_data['close_price']: qr = QR(stock_number=stock_number, stock_name=stock_name, date=today_data.date, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today_data['close_price'], industry_involved=kwargs.get('industry_involved'), increase_rate=float(sdt[0].increase_rate.replace('%', ''))) if real_time: return qr if not check_duplicate_strategy(qr): qr.save() return qr return
increase_rate = '5%' quantity_relative_ratio = 2.5 try: sdt = SDT.objects( Q(date=today) & Q(increase_rate__gte=increase_rate) & Q(quantity_relative_ratio__gte=quantity_relative_ratio)) except Exception, e: logging.error('Query DB failed:%s' % e) raise e for i in sdt: if persist_increase(i.stock_number): continue strategy_name = 'turnover_short' qr = QR() qr.stock_number = i.stock_number qr.stock_name = i.stock_name qr.date = i.date qr.strategy_direction = 'short' qr.strategy_name = strategy_name qr.init_price = i.today_closing_price try: if not is_duplicate(i.stock_number, i.date, strategy_name): qr.save() except Exception, e: logging.error('Save %s quant result failed:%s' % (i.stock_number, e))