def get_signal(signal, aum, balance, EndDate, close_dict): signal['symbol'] = signal.index symbol_lst = signal.symbol.tolist() porfolio = Future() main_contract_dict = porfolio.get_main_symbol(product=symbol_lst, date=EndDate) main_contract = pd.DataFrame(main_contract_dict).T[['main_contract']] contract_lst = main_contract.main_contract.tolist() ExchangeID_dict = porfolio.get_ExchangeID(contract_lst=contract_lst) ExchangeInstID_dict = porfolio.get_ExchangeInstID( contract_lst=contract_lst) VolumeMultiple_dict = porfolio.get_VolumeMultiple(contract_lst) main_contract['symbol'] = main_contract.index signal_dict = {} for symbol in symbol_lst: main_contract = main_contract_dict[symbol]['main_contract'] trading_code = ExchangeID_dict[main_contract][ 'ExchangeID'] + '.' + ExchangeInstID_dict[main_contract][ 'ExchangeInstID'] signal_dict[symbol] = { 'symbol': symbol, 'trading_code': trading_code, 'weight': signal.loc[symbol]['weight'], 'last_price': close_dict[symbol], 'VolumeMultiple': VolumeMultiple_dict[main_contract]['VolumeMultiple'] } trading_info = pd.DataFrame(signal_dict).T trading_info['position'] = aum * balance / len(symbol_lst) * trading_info[ 'weight'] / trading_info['last_price'] / trading_info['VolumeMultiple'] # trading_info['position'] = trading_info['position'].apply(lambda x: int(np.around(x, 0))) print(trading_info) return trading_info
for idx, row in df.iterrows(): info_txt = info_txt + '净盈亏: ' + str(np.around(row.net_profit, 4)) + '; 多头盈亏: ' +\ str(np.around(row.long, 4)) + ' 空头盈亏: ' + \ str(np.around(row.short, 4)) + ';' print(info_txt) tkinter.messagebox.showinfo('提示', info_txt) if __name__ == '__main__': # 创建api实例,设置web_gui=True生成图形化界面 api = TqApi(web_gui=True) product_lst = ['SC', 'PP'] hold_code_lst = ['sc2009', 'pp2009'] info_lst = ['ExchangeID', 'VolumeMultiple'] porfolio = Future() # tq_code_lst = [exchageID[i.upper()]['ExchangeID'] + '.' + i for i in hold_code_lst] # print(tq_code_lst) # 订阅 cu2002 合约的10秒线 long_code_lst = ['sc2009'] short_code_lst = ['pp2009'] long_cost_lst = [306.02] short_cost_lst = [6962.27] long_volume = [1] short_volume = [-1] info_lst = ['ExchangeID', 'VolumeMultiple'] long_product_lst = [i[:-4] for i in long_code_lst] short_product_lst = [j[:-4] for j in short_code_lst] symbol_lst = copy.deepcopy(long_product_lst) symbol_lst.extend(short_product_lst)
result = [[]] for x in items: result.extend([subset + [x] for subset in result]) return result if __name__ == "__main__": level_lst = [i for i in range(1, 6)] level_lst = [5] date_lst = [('2015-01-01', '2020-01-01'), ('2015-01-01', '2020-07-01')] # date_lst = [('2015-01-01', '2020-01-01')] method = 'sum' # 单利:sum;复利:muti fee = np.float(0.00015) fold_ini_path = 'e://Strategy//MT4//' # fold_ini_path = 'G://缠论//回测报告//' porfolio = Future() mode = '蓝线笔_蓝线反转确认_蓝线反转平仓_200627' code_lst = [ 'ap', 'ag', 'al', 'cf', 'cu', 'fu', 'i', 'j', 'ni', 'pb', 'pp', 'rb', 'sc', 'tf', 'v', 'zc', 'zn', 'c', 'if', 'sf', 'p', 'hc', 'au', 'jm', 'sm', 'ru', 'bu', 'oi', 'sr', 'ta', 'm', 'ma' ] # 所有品种32个 # code_lst = ['ma', 'ta', 'c', 'bu', 'sf', 'v', 'sm', 'hc', 'rb', 'pp', 'p', 'zc', 'ag', 'al', 'i', # 'pb', 'ap', 'zn'] # 保证金<10000的品种18个 # code_lst = ['ma', 'ta', 'c', 'bu', 'sf', 'v', 'sm', 'hc', 'rb', 'pp', 'p'] # 保证金<5000的品种11个 ret = {} ret['symbol'] = [] ret['tm'] = [] ret['start_time'] = [] ret['end_time'] = []
# 求集合的所有子集 result = [[]] for x in items: result.extend([subset + [x] for subset in result]) return result if __name__ == "__main__": fold_ini_path = 'e://Strategy//MT4//' # fold_ini_path = 'G://缠论//回测报告//' level = 1 date_lst = [('2020-01-01', '2020-07-01'), ('2015-01-01', '2017-01-01'), ('2017-01-01', '2020-01-01')] # date_lst = [('2017-01-01', '2020-07-01')] fee = np.float(0.00015) porfolio = Future() period_ini_lst = [15, 30, 60, 240, 1440] period_ini_lst = [5, 15, 30, 60, 240, 1440] period_lst_all = PowerSetsRecursive(period_ini_lst) period_lst_all = [i for i in period_lst_all if len(i) == 1] # period_lst_all = [[5, 15, 30, 60, 240, 1440]] print(period_lst_all) code_lst = ['ap', 'ag', 'al', 'cf', 'cu', 'fu', 'i', 'j', 'ni', 'pb', 'pp', 'rb', 'sc', 'tf', 'v', 'zc', 'zn', 'c', 'if', 'sf', 'p', 'hc', 'au', 'jm', 'sm', 'ru', 'bu', 'oi', 'sr', 'ta', 'm', 'ma'] # 所有品种32个 # code_lst_5 = ['ap', 'j', 'i', 'fu', 'sm', 'if', 'v', 'zn', 'pp', 'ni', 'pb'] # 所有5分钟夏普>0 # code_lst_15 = ['ap', 'ag', 'al', 'cf', 'cu', 'fu', 'i', 'j', 'ni', 'pb', 'pp', 'sc', 'v', 'zc', 'zn', 'c', # 'if', 'sf', 'hc', 'au', 'sm', 'sr', 'ta'] # 所有15分钟夏普>0 # code_lst_30 = ['ap', 'al', 'fu', 'i', 'j', 'pp', 'rb', 'sc', 'tf', 'v', 'zc', 'zn', # 'if', 'sf', 'sm', 'ta'] # 所有30分钟夏普>0
code_lst_5.extend(code_lst_1440) period_dict = {} for symbol in symbol_lst: period_dict[symbol.upper()] = len([i for i in code_lst_5 if i == symbol]) init_aum = 100000 * len(symbol_lst) symbol_lst = [i.upper() for i in symbol_lst] calen, next_tradeday, EndDate, StartDate, hq_last_date = get_date(calen, today) print(today) # close_dict = {} # for index_code in symbol_lst: # code = code_dic[index_code] # index_hq = stock_price(code, hq_last_date, hq_last_date, 'daily') # close_dict[index_code] = index_hq.close.tolist()[-1] # print(close_dict) porfolio = Future() main_contract_dict = porfolio.get_main_symbol(product=symbol_lst, date=hq_last_date) main_contract = pd.DataFrame(main_contract_dict).T[['main_contract']] print(main_contract) PriceTick_dict = porfolio.get_PriceTick(symbol_lst) contract_lst = main_contract.main_contract.tolist() ExchangeID_dict = porfolio.get_ExchangeID(contract_lst=contract_lst) ExchangeInstID_dict = porfolio.get_ExchangeInstID(contract_lst=contract_lst) VolumeMultiple_dict = porfolio.get_VolumeMultiple(contract_lst) LongMarginRatio_dict = porfolio.get_LongMarginRatio(contract_lst) ShortMarginRatio_dict = porfolio.get_ShortMarginRatio(contract_lst) main_contract['symbol'] = main_contract.index print(main_contract) signal_dict = {} for symbol in symbol_lst:
datetime.timedelta(days=1), end_date='2030-01-01')[0] if datetime.datetime.now().hour >= 18: calen.append(next_tradeday) EndDate = calen[-1] StartDate = calen[0] hq_last_date = calen[-2] return calen, next_tradeday, EndDate, StartDate, str(hq_last_date)[:10] if __name__ == "__main__": path = 'G:/trading/trading_report/' account = '21900576' ip = '127.0.0.1:1114' bars = 5 porfolio = Future() today = datetime.date.today() calen = get_trade_days(count=bars) calen = list(calen) calen, next_tradeday, EndDate, StartDate, hq_last_date = get_date( calen, today) hq_last_date = hq_last_date[:4] + hq_last_date[5:7] + hq_last_date[8:] today = datetime.date.strftime(today, '%Y%m%d') api = JzStrategy('ZhangFang', ip) ### 查询 fund_df = api.req_fund() print(fund_df) fund_df = fund_df.loc[:, [ '交易日', '当前保证金总额', '期货结算准备金', '上次结算准备金', '平仓盈亏', '持仓盈亏', '投资者帐号' ]]
if __name__ == '__main__': symbol_lst = [ 'C', 'CS', 'A', 'B', 'M', 'RM', 'Y', 'P', 'OI', 'L', 'V', 'PP', 'TA', 'RU', 'BU', 'MA', 'SC', 'FU', 'AL', 'ZN', 'CU', 'PB', 'NI', 'SN', 'J', 'JM', 'I', 'RB', 'HC', 'ZC', 'SF', 'SM', 'FG', 'IF', 'IH', 'IC', 'T', 'TF', 'AG', 'AU', 'JD', 'AP', 'CJ', 'CF', 'SR' ] symbol_lst = ['RU', 'C', 'P', 'AU', 'CU', 'ZN', 'SC', 'Y', 'CF', 'I', 'AG'] date = datetime.date.today() bars = 5 calen = get_trade_days(count=bars) calen = list(calen) calen, next_tradeday, EndDate, StartDate, hq_last_date = get_date( calen, date) porfolio = Future() print(EndDate) df = porfolio.get_main_symbol(product=symbol_lst, date=EndDate) df = pd.DataFrame(df).T print(df) df.to_csv('G:/trading/main_contract.csv') # code = 'Y8888.XDCE' # sday = '2010-01-01' # eday = '2020-06-02' # temp = get_price(code, start_date=sday, end_date=eday, frequency='1m', fields=None, skip_paused=True, fq='pre', # count=None)[['open', 'high', 'low', 'close', 'volume']] # print(temp) # # temp['date_time'] = temp.index # # temp['date_time'] = temp['date_time'].apply(lambda x: str(x) + str(' 00:00')) # # temp = temp.set_index(['date_time'])
balance = 10 bars = 5 calen = get_trade_days(count=bars) today = datetime.date.today() calen = list(calen) if today in calen: calen, next_tradeday, EndDate, StartDate, hq_last_date = get_date( calen, today) # EndDate = today signal = pd.read_csv(signal_path + 'temp//ymjh_' + hq_last_date + '.csv', index_col=0) print(signal) signal['symbol'] = signal.index symbol_lst = signal.symbol.tolist() porfolio = Future() main_contract_dict = porfolio.get_main_symbol(product=symbol_lst, date=EndDate) main_contract = pd.DataFrame(main_contract_dict).T[['main_contract']] print(main_contract) contract_lst = main_contract.main_contract.tolist() ExchangeID_dict = porfolio.get_ExchangeID(contract_lst=contract_lst) ExchangeInstID_dict = porfolio.get_ExchangeInstID( contract_lst=contract_lst) VolumeMultiple_dict = porfolio.get_VolumeMultiple(contract_lst) main_contract['symbol'] = main_contract.index print(main_contract) signal_dict = {} for symbol in symbol_lst: main_contract = main_contract_dict[symbol]['main_contract']
'C', 'CS', 'A', 'B', 'M', 'RM', 'Y', 'P', 'OI', 'L', 'V', 'PP', 'TA', 'RU', 'BU', 'MA', 'SC', 'FU', 'AL', 'ZN', 'CU', 'PB', 'NI', 'SN', 'J', 'JM', 'I', 'RB', 'HC', 'ZC', 'SF', 'SM', 'FG', 'IF', 'IH', 'IC', 'T', 'TF', 'AG', 'AU', 'JD', 'AP', 'CJ', 'CF', 'SR' ] info_lst = [ 'ExchangeID', 'VolumeMultiple', 'MaxMarketOrderVolume', 'MaxLimitOrderVolume' ] # 'VolumeMultiple', contract_lst = [ 'RU2009', 'FG2009', 'AU2012', 'SR2009', 'P2009', 'C2009', 'ZN2008', 'CU2008', 'M2009', 'SC2012', 'Y2009', 'OI2009', 'AG2012', 'SC2008', 'JD2009', 'AP2010', 'IF2007', 'IH2007', 'IC2007', 'SA2009' ] # contract_lst = ['RU'] date = datetime.date.today() porfolio = Future() df = porfolio.get_contract_info(contract_lst=contract_lst, info_lst=info_lst) df = pd.DataFrame(df).T print(df) df.to_csv('G:/trading/exchageID.csv') # 获取持仓限制 limit_position = porfolio.get_limit_position(contract_lst) limit_position_ret = {} limit_position_ret['limit_position'] = limit_position limit_position = pd.DataFrame(limit_position_ret) print(limit_position) contract_lst = ['RU'] df = porfolio.get_VolumeMultiple(contract_lst) print(df)