This commit is contained in:
guanjihuan 2024-03-14 22:09:18 +08:00
parent 836f7aa9f8
commit 96283e9ae1
4 changed files with 159 additions and 0 deletions

View File

@ -0,0 +1,14 @@
import guan
stock_symbols_60, stock_symbols_00, stock_symbols_30, stock_symbols_68, \
stock_symbols_8_4,stock_symbols_others = guan.stock_symbols_classification()
print(len(stock_symbols_60))
print(len(stock_symbols_00))
print()
file_list_60 = guan.get_all_filenames_in_directory(directory='./all_to_2024_03_07/stock_data_60', \
file_format=None, show_root_path=0, sort=1, include_subdirectory=1)
print(len(file_list_60))
file_list_00 = guan.get_all_filenames_in_directory(directory='./all_to_2024_03_07/stock_data_00', \
file_format=None, show_root_path=0, sort=1, include_subdirectory=1)
print(len(file_list_00))

View File

@ -0,0 +1,38 @@
import guan
import numpy as np
import datetime
file_list_60 = guan.get_all_filenames_in_directory(directory='./all_to_2024_03_07/stock_data_60', \
file_format=None, show_root_path=0, sort=1, include_subdirectory=1)
file_list_00 = guan.get_all_filenames_in_directory(directory='./all_to_2024_03_07/stock_data_00', \
file_format=None, show_root_path=0, sort=1, include_subdirectory=1)
len_stock_60 = len(file_list_60)
guan.make_directory('./2018_01_01_to_2024_03_07/')
guan.make_directory('./2018_01_01_to_2024_03_07/stock_data_60/')
for i00 in range(len_stock_60):
stock_data = guan.load_data('./all_to_2024_03_07/stock_data_60/'+file_list_60[i00], file_format='')
num_data = len(stock_data)
new_stock_data = []
for i0 in range(num_data):
if stock_data[i0, 0]>datetime.date(2018, 1, 1):
new_stock_data.append(list(stock_data[i0, :]))
new_stock_data = np.array(new_stock_data)
guan.dump_data(data=new_stock_data, filename='./2018_01_01_to_2024_03_07/stock_data_60/'+file_list_60[i00], file_format='')
print('数据处理结束1')
len_stock_00 = len(file_list_00)
guan.make_directory('./2018_01_01_to_2024_03_07/stock_data_00/')
for i00 in range(len_stock_00):
stock_data = guan.load_data('./all_to_2024_03_07/stock_data_00/'+file_list_00[i00], file_format='')
num_data = len(stock_data)
new_stock_data = []
for i0 in range(num_data):
if stock_data[i0, 0]>datetime.date(2018, 1, 1):
new_stock_data.append(list(stock_data[i0, :]))
new_stock_data = np.array(new_stock_data)
guan.dump_data(data=new_stock_data, filename='./2018_01_01_to_2024_03_07/stock_data_00/'+file_list_00[i00], file_format='')
print('数据处理结束2')

View File

@ -0,0 +1,28 @@
import guan
stock_symbols_60, stock_symbols_00, stock_symbols_30, stock_symbols_68, \
stock_symbols_8_4,stock_symbols_others = guan.stock_symbols_classification()
import os
guan.make_directory('./all_to_2024_03_07')
guan.make_directory('./all_to_2024_03_07/stock_data_60')
for stock_symbol in stock_symbols_60:
try:
if not os.path.exists('./all_to_2024_03_07/stock_data_60/'+stock_symbol+'.txt'):
title, stock_data = guan.history_data_of_one_stock(symbol=stock_symbol, \
period='daily', start_date="19000101", end_date='21000101')
guan.dump_data(data=stock_data, filename='./all_to_2024_03_07/stock_data_60/'+stock_symbol, file_format='.txt')
except:
pass
print('60下载完成')
guan.make_directory('./all_to_2024_03_07/stock_data_00')
for stock_symbol in stock_symbols_00:
try:
if not os.path.exists('./all_to_2024_03_07/stock_data_00/'+stock_symbol+'.txt'):
title, stock_data = guan.history_data_of_one_stock(symbol=stock_symbol, \
period='daily', start_date="19000101", end_date='21000101')
guan.dump_data(data=stock_data, filename='./all_to_2024_03_07/stock_data_00/'+stock_symbol, file_format='.txt')
except:
pass
print('00下载完成')

View File

@ -0,0 +1,79 @@
import guan
file_list_60 = guan.get_all_filenames_in_directory(directory='./2018_01_01_to_2024_03_07/stock_data_60', \
file_format=None, show_root_path=0, sort=1, include_subdirectory=1)
file_list_00 = guan.get_all_filenames_in_directory(directory='./2018_01_01_to_2024_03_07/stock_data_00', \
file_format=None, show_root_path=0, sort=1, include_subdirectory=1)
# print(len(file_list_60))
# print(len(file_list_00))
# print()
# 回测策略:当跌到成本时的 ratio 倍时翻倍投入补仓。这里只对00开头的股票进行回测
buy_limit = 100
for buy_limit in [100000, 10, 4]: # 限制补仓次数
print('限制补仓次数:', buy_limit, '\n')
for ratio in [0.95, 0.9, 0.85, 0.8, 0.75, 0.7, 0.65]:
win_count = 0
lose_count = 0
win_count_relative = 0
lose_count_relative = 0
win_count_0 = 0
lose_count_0 = 0
print('跌到成本的某个倍数补仓:', ratio)
invest_money_array = []
current_money_array = []
buy_times_array = []
profit_array = []
profit_array_0 = []
for i00 in range(len(file_list_00)):
stock_data = guan.load_data('./2018_01_01_to_2024_03_07/stock_data_00/'+file_list_00[i00], file_format='')
stock_data = stock_data[::-1]
if len(stock_data) > 0:
invest_money = 1000 # 初始投入资金
current_money = 1000 # 当前所有资金
buy_price = stock_data[0, 2] # 买入的价格(第一天买入)
cost_price = stock_data[0, 2] # 当前持有的成本价(第一天买入)
cost_num = invest_money/cost_price # 当前持有的股数(为了简化,这里不做取整处理)
len_date = len(stock_data[:, 2])
buy_times = 0
for i0 in range(len_date):
if stock_data[i0, 2]/cost_price < ratio and buy_times < buy_limit: # 当满足策略且补仓次数小于buy_limit时补仓
current_price = stock_data[i0, 2] # 当前的市场价
current_money = invest_money + (invest_money/cost_price)*current_price # 补仓后的所有资金
buy_price = stock_data[i0, 2] # 买入价为当前市场价
cost_num = (invest_money/cost_price)+(invest_money/buy_price) # 更新当前持有的股数
cost_price = 2*invest_money/cost_num # 更新当前持有的成本价
invest_money = invest_money*2 # 更新总投入资金
buy_times += 1
current_price = stock_data[i0, 2] # 当前的市场价
current_money = cost_num*current_price # 当前所有资金
buy_times_array.append(buy_times)
invest_money_array.append(invest_money)
current_money_array.append(current_money)
profit_array.append(current_money/invest_money)
profit_array_0.append(current_price/stock_data[0, 2])
if current_money > invest_money:
win_count += 1
else:
lose_count += 1
if current_money/invest_money > current_price/stock_data[0, 2]:
win_count_relative += 1
else:
lose_count_relative += 1
if current_price > stock_data[0, 2]:
win_count_0 += 1
else:
lose_count_0 += 1
print('策略的平均加仓次数:', sum(buy_times_array)/len(buy_times_array))
print('策略的平均投入资金:', sum(invest_money_array)/len(invest_money_array))
print('策略的平均最终所有资金:', sum(invest_money_array)/len(invest_money_array))
print('策略的平均利润率(包含本金):', sum(profit_array)/len(profit_array))
print('完成不操作的市场平均利润率(包含本金):', sum(profit_array_0)/len(profit_array_0))
print('策略赢的次数:', win_count)
print('策略输的次数;', lose_count)
print('策略相对于市场赢的次数:', win_count_relative)
print('策略相对于市场输的次数;', lose_count_relative)
print('完全不操作市场赢的次数:', win_count_0)
print('完全不操作市场输的次数;', lose_count_0)
print()
print('-----')
print()