ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 通过神经网络进行交易 > 来源:https://uqer.io/community/share/55b8acbaf9f06c91fa18c5ce ```py start = '2014-01-01' # 回测起始时间 end = '2015-05-25' # 回测结束时间 benchmark = 'HS300' # 策略参考标准 universe = set_universe('HS300') # 证券池,支持股票和基金 capital_base = 1000000 # 起始资金 freq = 'd' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测 refresh_rate = 1 # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟 import pybrain as brain from pybrain.tools.shortcuts import buildNetwork from pybrain.tools.customxml import NetworkReader HISTORY = 10 # 通过前十日数据预测 fnn = buildNetwork(HISTORY, 15, 7, 1) # 初始化神经网络 def initialize(account): # 初始化虚拟账户状态 fnn = NetworkReader.readFrom('net.csv') def handle_data(account): # 每个交易日的买入卖出指令 hist = account.get_attribute_history('closePrice', 10) bucket = [] for s in account.universe: sample = hist[s] possibility = fnn.activate(sample) bucket.append((possibility, s)) if possibility < 0 and s in account.valid_secpos: order_to(s, 0) bucket = sorted(bucket, key=lambda x: x[0], reverse=True) print bucket[0][0] if bucket[0][0] < 0: raise Exception('Network Error') for s in bucket[:10]: if s[0] > 0.5 and s[1] not in account.valid_secpos: order(s[1], 10000 * s[0] * 80000) ``` ![](https://box.kancloud.cn/2016-07-30_579cbdad267af.jpg) ```py [ 1.44446298] [ 1.57722526] [ 1.44509945] [ 1.44829344] [ 1.48584942] [ 1.60968867] [ 1.61088618] [ 1.43639898] [ 1.43767639] [ 1.43911414] [ 1.43768517] [ 1.43585662] [ 1.43720968] [ 1.43317016] [ 1.43301566] [ 1.42953404] [ 1.42678559] [ 1.43098489] [ 1.42855878] [ 1.42709837] [ 1.42726163] [ 1.42585347] [ 1.42973957] [ 1.42980622] [ 1.43599317] [ 1.44286782] [ 1.57580564] [ 1.59120978] [ 1.6130606] [ 1.59582232] [ 1.5850841] [ 1.61084701] [ 1.59595849] [ 1.52961191] [ 1.50583099] [ 1.46038687] [ 1.44689328] [ 1.5432668] [ 1.55312445] [ 1.44337678] [ 1.44056972] [ 1.50173311] [ 1.59748366] [ 1.4267731] [ 1.47709901] [ 1.62105239] [ 1.60780394] [ 1.53541989] [ 1.60721757] [ 1.58754631] [ 1.5909996] [ 1.60486746] [ 1.48532045] [ 1.56199286] [ 1.42685994] [ 1.42218871] [ 1.42513733] [ 1.42560821] [ 1.42627889] [ 1.42422753] [ 1.42382572] [ 1.42222283] [ 1.41752142] [ 1.41257471] [ 1.41516891] [ 1.41390184] [ 1.58426403] [ 1.53824457] [ 1.45517987] [ 1.500387] [ 1.48309551] [ 1.51026016] [ 1.52573794] [ 1.53639431] [ 1.35975534] [ 1.3949126] [ 1.41854269] [ 1.5371124] [ 1.5318818] [ 1.61626035] [ 1.46463971] [ 1.35377736] [ 1.3781526] [ 1.36485304] [ 1.35738739] [ 1.35879235] [ 1.35848317] [ 1.35674074] [ 1.35842602] [ 1.35549472] [ 1.40440556] [ 1.35685947] [ 1.35700859] [ 1.44201184] [ 1.43235995] [ 1.37015535] [ 1.35396728] [ 1.35545512] [ 1.35623892] [ 1.39545221] [ 1.35725555] [ 1.52999178] [ 1.52399418] [ 1.39365249] [ 1.36779515] [ 1.35482391] [ 1.40293755] [ 1.37213596] [ 1.35738371] [ 1.35808458] [ 1.35662849] [ 1.35528448] [ 1.35510845] [ 1.35379783] [ 1.35430934] [ 1.35312843] [ 1.35581243] [ 1.36879701] [ 1.41158962] [ 1.44027263] [ 1.44380821] [ 1.48272708] [ 1.51507127] [ 1.46605994] [ 1.61084145] [ 1.58922279] [ 1.46771218] [ 1.40289457] [ 1.34716878] [ 1.35043834] [ 1.35590544] [ 1.37653415] [ 1.34764272] [ 1.34831244] [ 1.34689904] [ 1.34150245] [ 1.33927252] [ 1.33978952] [ 1.3470568] [ 1.34433552] [ 1.34484056] [ 1.34160806] [ 1.3407761] [ 1.3424078] [ 1.3433431] [ 1.34328446] [ 1.33992925] [ 1.34388204] [ 1.34802088] [ 1.3453579] [ 1.3428265] [ 1.34329775] [ 1.34191156] [ 1.34611248] [ 1.37349663] [ 1.34815805] [ 1.34014992] [ 1.34521152] [ 1.34456372] [ 1.34089661] [ 1.34023757] [ 1.3410812] [ 1.33807578] [ 1.33572014] [ 1.34433535] [ 1.33505861] [ 1.33827504] [ 1.33755043] [ 1.38559783] [ 1.35527351] [ 1.33053597] [ 1.33701674] [ 1.33273647] [ 1.33668717] [ 1.33941937] [ 1.34060378] [ 1.3372182] [ 1.61340736] [ 1.59055412] [ 1.33505241] [ 1.60308339] [ 1.51156137] [ 1.35797843] [ 1.34580909] [ 1.48117895] [ 1.44494812] [ 1.35293003] [ 1.35665647] [ 1.37410369] [ 1.35666235] [ 1.33729064] [ 1.45931719] [ 1.55375605] [ 1.48339986] [ 1.35060715] [ 1.36146995] [ 1.34245541] [ 1.35342592] [ 1.35796042] [ 1.37098111] [ 1.34045319] [ 1.42147708] [ 1.365122] [ 1.4076879] [ 1.39762825] [ 1.34262013] [ 1.38706403] [ 1.33523713] [ 1.33186205] [ 1.33077059] [ 1.3324637] [ 1.33112122] [ 1.32952302] [ 1.33383435] [ 1.32954544] [ 1.33443469] [ 1.33090967] [ 1.33522262] [ 1.33175321] [ 1.49987289] [ 1.51376666] [ 1.4208718] [ 1.49241705] [ 1.36766608] [ 1.36990194] [ 1.33322159] [ 1.34836793] [ 1.34669257] [ 1.36690579] [ 1.37890552] [ 1.59037649] [ 1.60582728] [ 1.61743431] [ 1.62123338] [ 1.61336502] [ 1.60121318] [ 1.62107838] [ 1.41357384] [ 1.61966948] [ 1.51775743] [ 1.33704794] [ 1.37279934] [ 1.34484306] [ 1.3705884] [ 1.41262748] [ 1.44408315] [ 1.52046936] [ 1.38814136] [ 1.38882472] [ 1.35596408] [ 1.52776999] [ 1.55767315] [ 1.33500518] [ 1.33840795] [ 1.34727997] [ 1.43367698] [ 1.35595655] [ 1.34698186] [ 1.59583696] [ 1.374913] [ 1.60214431] [ 1.53554784] [ 1.49221176] [ 1.59822169] [ 1.35287993] [ 1.34985064] [ 1.34512204] [ 1.33554636] [ 1.33612458] [ 1.32905663] [ 1.32990288] [ 1.36225504] [ 1.59836396] [ 1.32984726] [ 1.33153792] [ 1.39786779] [ 1.3416728] [ 1.3547156] [ 1.3417874] [ 1.33787953] [ 1.42237594] [ 1.32939148] [ 1.34560785] [ 1.33542025] [ 1.32921129] [ 1.32924703] [ 1.32956219] [ 1.32953676] [ 1.32962066] [ 1.33064464] [ 1.32916515] [ 1.32946366] [ 1.33199463] [ 1.32940815] [ 1.33035788] [ 1.33158764] [ 1.33103393] [ 1.3312874] [ 1.32907548] [ 1.33131474] [ 1.33113065] [ 1.33056411] [ 1.54542979] [ 1.43053565] [ 1.44441014] [ 1.55239121] [ 1.37602661] [ 1.62125583] [ 1.36640902] [ 1.56636469] [ 1.33713086] [ 1.33348418] [ 1.33584004] [ 1.35366715] [ 1.39788942] [ 1.41189411] [ 1.57317611] [ 1.40385926] [ 1.61962342] [ 1.55777659] [ 1.5813632] [ 1.52487439] [ 1.44917861] [ 1.35809968] [ 1.35031112] [ 1.34328138] [ 1.3453355] [ 1.36096032] [ 1.34087397] ```