def handle(self, *args, **options): if not Option.objects.exists(): Option.objects.create(name="site_name", value='TdxStock') Option.objects.create(name="site_host", value='localhost') Option.objects.create(name="site_description", value='沪深股票分析') options = [ Option(name=name, value=None) for name in [ 'site_seo_description', 'site_keywords', 'open_site_comment', 'beian_code', 'gongan_beian_code', 'show_gongan_code' ] ] Option.objects.bulk_create(options) self.stdout.write(self.style.SUCCESS('填充 options 表成功')) else: raise CommandError('options 表中已经有数据')
def get_option_list(request, future_code): status = {'code': 0, 'message': 'unknown'} result = {'status': status} if request.method == 'GET': try: future = Future.objects.get(code=future_code) except Future.DoesNotExist: status['code'] = 404 status['message'] = '期货代码不存在' return JsonResponse(result, status=404) result['option_list'] = Option.get_option_list(future) status['message'] = '获取成功' return JsonResponse(result, status=200) else: # http方法不支持 status['code'] = 405 status['message'] = 'http method not supported' return JsonResponse(result, status=405)
def insert_option(self, Code_option): option = Option(Code_Option= Code_option) option.save() pass
def data_transfer(rootdir): for parent, dirnames, filenames in os.walk(rootdir): for filename in filenames: print(filename) # filename = "m1709-c-2800.csv" if "-" not in filename: with open(rootdir + "/" + filename, 'r') as f: data_list = f.read().split('\n') data_list = data_list[1:-3] iterator = 0 first_row_data = data_list[iterator].split(',') try: first_row_time = datetime.datetime(*(time.strptime( first_row_data[0], "%Y/%m/%d %H:%M")[:6])) except: first_row_time = datetime.datetime(*(time.strptime( first_row_data[0], "%Y-%m-%d %H:%M")[:6])) iterate_time = first_row_time future_threading_groups = [] future_for_treading_data = Future.objects.get( code=filename.split('.')[0]) # future_for_treading_data.save() while iterator != len(data_list): try: begin_date = datetime.datetime(*(time.strptime( data_list[iterator].split(' ')[0], "%Y/%m/%d") )[:6]) except: begin_date = datetime.datetime(*(time.strptime( data_list[iterator].split(' ')[0], "%Y-%m-%d") )[:6]) time1_start = begin_date + datetime.timedelta( hours=8, minutes=55) time1_end = begin_date + datetime.timedelta(hours=11, minutes=35) time2_start = begin_date + datetime.timedelta( hours=13, minutes=25) time2_end = begin_date + datetime.timedelta(hours=15, minutes=5) time3_start = begin_date + datetime.timedelta( hours=20, minutes=55) time3_end = begin_date + datetime.timedelta(hours=23, minutes=35) if not (time1_start <= iterate_time <= time1_end or time2_start <= iterate_time <= time2_end or time3_start <= iterate_time <= time3_end): iterator += 1 if iterator != 0: iterate_time = time1_start while time1_start <= iterate_time <= time1_end or time2_start <= iterate_time <= time2_end or time3_start <= iterate_time <= time3_end: if iterator == len(data_list): break try: next_time = datetime.datetime(*(time.strptime( data_list[iterator].split(',')[0], "%Y/%m/%d %H:%M")[:6])) except: next_time = datetime.datetime(*(time.strptime( data_list[iterator].split(',')[0], "%Y-%m-%d %H:%M")[:6])) if not ((time1_start <= next_time <= time1_end) or (time2_start <= next_time <= time2_end) or (time3_start <= next_time <= time3_end)): iterator += 1 break if iterate_time == next_time: future = FutureTreadingData( time=iterate_time, open_price=data_list[iterator].split( ',')[1], max_price=data_list[iterator].split( ',')[2], min_price=data_list[iterator].split( ',')[3], close_price=data_list[iterator].split( ',')[4], future=future_for_treading_data) future_threading_groups.append(future) iterator += 1 else: future = copy.deepcopy( future_threading_groups[-1]) future.time = iterate_time future_threading_groups.append(future) if iterate_time == time1_end: iterate_time = time2_start elif iterate_time == time2_end: iterate_time = time3_start elif iterate_time == time3_end: break else: iterate_time += datetime.timedelta(minutes=1) FutureTreadingData.objects.bulk_create( future_threading_groups) else: with open(rootdir + "/" + filename, 'r') as f: data_list = f.read().split('\n') data_list = data_list[1:-3] iterator = 0 first_row_data = data_list[iterator].split(',') try: first_row_time = datetime.datetime(*(time.strptime( first_row_data[0], "%Y/%m/%d %H:%M")[:6])) except: first_row_time = datetime.datetime(*(time.strptime( first_row_data[0], "%Y-%m-%d %H:%M")[:6])) iterate_time = first_row_time option_threading_groups = [] future_for_option = Future(code=filename.split('-')[0]) future_for_option.save() option_for_treading_data = Option( code=filename.split('.')[0], asset=future_for_option) option_for_treading_data.save() while iterator < len(data_list): try: begin_date = datetime.datetime(*(time.strptime( data_list[iterator].split(' ')[0], "%Y/%m/%d") )[:6]) except: begin_date = datetime.datetime(*(time.strptime( data_list[iterator].split(' ')[0], "%Y-%m-%d") )[:6]) time1_start = begin_date + datetime.timedelta( hours=8, minutes=55) time1_end = begin_date + datetime.timedelta(hours=11, minutes=35) time2_start = begin_date + datetime.timedelta( hours=13, minutes=25) time2_end = begin_date + datetime.timedelta(hours=15, minutes=5) time3_start = begin_date + datetime.timedelta( hours=20, minutes=55) time3_end = begin_date + datetime.timedelta(hours=23, minutes=35) if not (time1_start <= iterate_time <= time1_end or time2_start <= iterate_time <= time2_end or time3_start <= iterate_time <= time3_end): iterator += 1 if iterator != 0: iterate_time = time1_start while (time1_start <= iterate_time <= time1_end) or ( time2_start <= iterate_time <= time2_end) or ( time3_start <= iterate_time <= time3_end): # print(iterator) if iterator == len(data_list): break # next_time = datetime.datetime.now() try: next_time = datetime.datetime(*(time.strptime( data_list[iterator].split(',')[0], "%Y/%m/%d %H:%M")[:6])) except: next_time = datetime.datetime(*(time.strptime( data_list[iterator].split(',')[0], "%Y-%m-%d %H:%M")[:6])) if not ((time1_start <= next_time <= time1_end) or (time2_start <= next_time <= time2_end) or (time3_start <= next_time <= time3_end)): iterator += 1 break if iterate_time == next_time: option = OptionTreadingData( time=iterate_time, open_price=data_list[iterator].split( ',')[1], max_price=data_list[iterator].split( ',')[2], min_price=data_list[iterator].split( ',')[3], close_price=data_list[iterator].split( ',')[4], option=option_for_treading_data, volume=float( data_list[iterator].split(',')[6])) option_threading_groups.append(option) iterator += 1 else: option = copy.deepcopy( option_threading_groups[-1]) option.time = iterate_time option_threading_groups.append(option) if iterate_time == time1_end: iterate_time = time2_start elif iterate_time == time2_end: iterate_time = time3_start elif iterate_time == time3_end: break else: iterate_time += datetime.timedelta(minutes=1) OptionTreadingData.objects.bulk_create( option_threading_groups)
def data_transfer(rootdir): for parent, dirnames, filenames in os.walk(rootdir): for filename in filenames: with open(rootdir + "/" + filename, 'r') as f: data_list = f.read().split('\n') data_list = data_list[1:-3] try: data_time_list = [ datetime.datetime(*( time.strptime(data.split(' ')[0], "%Y/%m/%d"))[:6]) for data in data_list ] except: data_time_list = [ datetime.datetime(*( time.strptime(data.split(' ')[0], "%Y-%m-%d"))[:6]) for data in data_list ] # data_time_reduce_list = [] # for data_time in data_time_list: # if data_time not in data_time_reduce_list: # data_time_reduce_list += [data_time] # # print(data_time_reduce_list) data_filted_list = [] iter_mark = 0 iter_date = data_time_list[0] while iter_date != data_time_list[-1] + datetime.timedelta( days=1): if iter_date == data_time_list[iter_mark]: if 0 <= iter_date.weekday() < 5: data_filted_list.append(data_list[iter_mark]) iter_mark += 1 if iter_mark == len(data_time_list) or data_time_list[ iter_mark - 1] != data_time_list[iter_mark]: iter_date += datetime.timedelta(days=1) continue else: if 0 <= iter_date.weekday() < 5: data_filted_list.append( iter_date.strftime("%Y/%m/%d ") + data_list[iter_mark - 1].split(' ')[1]) iter_date += datetime.timedelta(days=1) print(filename) if "-" not in filename: iterator = 0 first_row_data = data_filted_list[iterator].split(',') try: first_row_time = datetime.datetime(*(time.strptime( first_row_data[0], "%Y/%m/%d %H:%M")[:6])) except: first_row_time = datetime.datetime(*(time.strptime( first_row_data[0], "%Y-%m-%d %H:%M")[:6])) iterate_time = first_row_time future_threading_groups = [] future_for_treading_data = Future.objects.get( code=filename.split('.')[0]) # future_for_treading_data.save() while iterator != len(data_filted_list): try: begin_date = datetime.datetime(*(time.strptime( data_filted_list[iterator].split(' ')[0], "%Y/%m/%d"))[:6]) except: begin_date = datetime.datetime(*(time.strptime( data_filted_list[iterator].split(' ')[0], "%Y-%m-%d"))[:6]) time1_start = begin_date + datetime.timedelta(hours=8, minutes=55) time1_end = begin_date + datetime.timedelta(hours=11, minutes=35) time2_start = begin_date + datetime.timedelta(hours=13, minutes=25) time2_end = begin_date + datetime.timedelta(hours=15, minutes=5) time3_start = begin_date + datetime.timedelta(hours=20, minutes=55) time3_end = begin_date + datetime.timedelta(hours=23, minutes=35) # if not (time1_start <= iterate_time <= time1_end or time2_start <= iterate_time <= time2_end or time3_start <= iterate_time <= time3_end): # iterator += 1 if iterator != 0: iterate_time = time1_start while time1_start <= iterate_time <= time1_end or time2_start <= iterate_time <= time2_end or time3_start <= iterate_time <= time3_end: while iterator == len(data_filted_list): if iterate_time == time1_end: iterate_time = time2_start elif iterate_time == time2_end: iterate_time = time3_start elif iterate_time == time3_end: break iterate_time += datetime.timedelta(minutes=1) if iterate_time == time3_end and iterator == len( data_filted_list): break try: next_time = datetime.datetime(*(time.strptime( data_filted_list[iterator].split(',')[0], "%Y/%m/%d %H:%M")[:6])) except: next_time = datetime.datetime(*(time.strptime( data_filted_list[iterator].split(',')[0], "%Y-%m-%d %H:%M")[:6])) # if not ((time1_start <= next_time <= time1_end) # or (time2_start <= next_time <= time2_end) # or (time3_start <= next_time <= time3_end)): # iterator += 1 # break if iterate_time == next_time: future = FutureTreadingData( time=iterate_time, open_price=data_filted_list[iterator].split( ',')[1], max_price=data_filted_list[iterator].split( ',')[2], min_price=data_filted_list[iterator].split( ',')[3], close_price=data_filted_list[iterator].split( ',')[4], future=future_for_treading_data) future_threading_groups.append(future) iterator += 1 else: future = copy.deepcopy(future_threading_groups[-1]) future.time = iterate_time future_threading_groups.append(future) if iterate_time == time1_end: iterate_time = time2_start elif iterate_time == time2_end: iterate_time = time3_start elif iterate_time == time3_end: break elif iterate_time <= next_time: iterate_time += datetime.timedelta(minutes=1) elif iterate_time > next_time: iterator += 1 mid = (int)(len(future_threading_groups) / 2) FutureTreadingData.objects.bulk_create( future_threading_groups[:mid]) FutureTreadingData.objects.bulk_create( future_threading_groups[mid:]) else: iterator = 0 first_row_data = data_filted_list[iterator].split(',') try: first_row_time = datetime.datetime(*(time.strptime( first_row_data[0], "%Y/%m/%d %H:%M")[:6])) except: first_row_time = datetime.datetime(*(time.strptime( first_row_data[0], "%Y-%m-%d %H:%M")[:6])) iterate_time = first_row_time option_threading_groups = [] future_for_option = Future(code=filename.split('-')[0]) future_for_option.save() option_for_treading_data = Option(code=filename.split('.')[0], asset=future_for_option) option_for_treading_data.save() while iterator < len(data_filted_list): try: begin_date = datetime.datetime(*(time.strptime( data_filted_list[iterator].split(' ')[0], "%Y/%m/%d"))[:6]) except: begin_date = datetime.datetime(*(time.strptime( data_filted_list[iterator].split(' ')[0], "%Y-%m-%d"))[:6]) time1_start = begin_date + datetime.timedelta(hours=8, minutes=55) time1_end = begin_date + datetime.timedelta(hours=11, minutes=35) time2_start = begin_date + datetime.timedelta(hours=13, minutes=25) time2_end = begin_date + datetime.timedelta(hours=15, minutes=5) time3_start = begin_date + datetime.timedelta(hours=20, minutes=55) time3_end = begin_date + datetime.timedelta(hours=23, minutes=35) # if not (time1_start <= iterate_time <= time1_end or time2_start <= iterate_time <= time2_end or time3_start <= iterate_time <= time3_end): # iterator += 1 if iterator != 0: iterate_time = time1_start while (time1_start <= iterate_time <= time1_end) or ( time2_start <= iterate_time <= time2_end) or ( time3_start <= iterate_time <= time3_end): while iterator == len(data_filted_list): if iterate_time == time1_end: iterate_time = time2_start elif iterate_time == time2_end: iterate_time = time3_start elif iterate_time == time3_end: break iterate_time += datetime.timedelta(minutes=1) if iterate_time == time3_end and iterator == len( data_filted_list): break # next_time = datetime.datetime.now() try: next_time = datetime.datetime(*(time.strptime( data_filted_list[iterator].split(',')[0], "%Y/%m/%d %H:%M")[:6])) except: next_time = datetime.datetime(*(time.strptime( data_filted_list[iterator].split(',')[0], "%Y-%m-%d %H:%M")[:6])) # if not ((time1_start <= next_time <= time1_end) # or (time2_start <= next_time <= time2_end) # or (time3_start <= next_time <= time3_end)): # iterator += 1 # break if iterate_time == next_time: option = OptionTreadingData( time=iterate_time, open_price=data_filted_list[iterator].split( ',')[1], max_price=data_filted_list[iterator].split( ',')[2], min_price=data_filted_list[iterator].split( ',')[3], close_price=data_filted_list[iterator].split( ',')[4], option=option_for_treading_data, volume=float( data_filted_list[iterator].split(',')[6])) option_threading_groups.append(option) iterator += 1 else: option = copy.deepcopy(option_threading_groups[-1]) option.time = iterate_time option_threading_groups.append(option) if iterate_time == time1_end: iterate_time = time2_start elif iterate_time == time2_end: iterate_time = time3_start elif iterate_time == time3_end: break elif iterate_time <= next_time: iterate_time += datetime.timedelta(minutes=1) elif iterate_time > next_time: iterator += 1 OptionTreadingData.objects.bulk_create(option_threading_groups)