前回、ある時間地点における回答配列を作成しましたので、今回は回答配列と遺伝子の配列を比較し、ある程度以上一致している場合は、売り買いのモーションを起こすようにしていこうと思います。
遺伝子配列と回答配列の一致度をどのように見るかですが、両者とも0と1の配列では、同じ序列のものをかけ合わせると、0と1は0、1と1は1となり区別できるのですが、
0と0も0になってしまい、不一致の場合と区別できません。
なので配列を1と-1に変えようと思います。
こうすることで、一致する1と1は1、-1と-1も1、不一致である1と-1は-1となります。
さらに各項目に重みを付け、なるべく近い日取りのものほど売買に影響をあたえるようにします。
この重み付けは前から順に18,16,14,12,10,10,8,6,4,2とします。
1日前と2日前のみが一致する場合は、18+16で合計点が34点になります。
この合計点がある一定の値を超えると売り買いのモーションを起こします。
では実際のコードですが、
これで評価関数が完成しました。
次回はこの関数を使って遺伝的アルゴリズムを回していこうと思います。
import sys from dataload_class import vstr2date from dataload_class import dataload def predata_init(): code = 1305 data = dataload(code) data = data.loadfile() if data != 0: t = vstr2date(data) # print data[dataload.hajimene] return [t, data[dataload.hajimene]] def negative2zero(num): if num<=0: num = 0 return num def main(): stockPrice = [] #株価 date = [] #日付 popGrope =[] date,stockPrice = predata_init() # print date,stockPrice popGrope = [[[-1, 1, -1, -1, 1, -1, -1, 1, -1, 1], [1, -1, -1, 1, 1, -1, 1, 1, 1, 1]], [[1, 1, 1, 1, 1, -1, -1, 1, 1, 1], [1, 1, -1, 1, 1, 1, 1, 1, -1, -1]], [[1, 1, 1, -1, -1, -1, -1, -1, 1, 1], [-1, 1, 1, 1, -1, 1, -1, -1, 1, 1]], [[-1, 1, 1, 1, -1, -1, 1, 1, -1, -1], [-1, -1, -1, 1, 1, -1, 1, -1, -1, -1]], [[-1, -1, 1, -1, 1, -1, 1, 1, -1, 1], [1, 1, -1, -1, 1, 1, -1, 1, -1, -1]], [[-1, 1, -1, -1, 1, 1, 1, 1, 1, 1], [-1, 1, -1, 1, -1, 1, 1, 1, -1, 1]], [[1, 1, 1, 1, 1, 1, -1, 1, -1, -1], [1, -1, 1, 1, -1, 1, -1, -1, -1, -1]], [[1, -1, -1, -1, 1, -1, -1, -1, 1, -1], [1, -1, -1, 1, -1, -1, 1, -1, 1, 1]], [[-1, 1, 1, 1, -1, 1, -1, -1, 1, 1], [1, -1, -1, -1, -1, 1, -1, 1, 1, 1]], [[1, -1, 1, -1, -1, -1, 1, -1, -1, 1], [-1, -1, 1, 1, 1, 1, 1, -1, -1, 1]]] popnum = 0 for popnum in range(10): pop = popGrope[popnum] kaiGean = pop[0] uriGean = pop[1] principal = 1000000 #元金(¥100万) holding = 0 #持ち株量 #評価ループ lenStockprice = len(stockPrice) # print lenStockprice fluctuationArry =[] #現在時間の評価配列を作る today = lenStockprice - 100 agoArry =[0,1,2,3,5,8,13,21,35,56,89] n=0 m=0 print "--------------------------------------------------" print date[today] for j in range(300): todayPrice = stockPrice[today] # print date[today] # print todayPrice for i in range(10): n = agoArry[i] m = agoArry[i+1] # print n,m ndayagoPrice = stockPrice[today + n] #n日前の株価 mdayagoPrice = stockPrice[today + m] #m日前の株価 changeValue =ndayagoPrice - mdayagoPrice # print mdayagoPrice, date[today + m] if changeValue<=0: fluctuationArry.append(-1)#小さかったら評価配列に-1追加 else: fluctuationArry.append(1) # print fluctuationArry #ある地点での評価に使う配列完成 #評価配列と遺伝子の一致度を見る kaiPoint =(negative2zero(fluctuationArry[0]*kaiGean[0])*18)+\ (negative2zero(fluctuationArry[1]*kaiGean[1])*16)+\ (negative2zero(fluctuationArry[2]*kaiGean[2])*14)+\ (negative2zero(fluctuationArry[3]*kaiGean[3])*12)+\ (negative2zero(fluctuationArry[4]*kaiGean[4])*10)+\ (negative2zero(fluctuationArry[5]*kaiGean[5])*10)+\ (negative2zero(fluctuationArry[6]*kaiGean[6])*8)+\ (negative2zero(fluctuationArry[7]*kaiGean[7])*6)+\ (negative2zero(fluctuationArry[8]*kaiGean[8])*4)+\ (negative2zero(fluctuationArry[9]*kaiGean[9])*2) # print kaiPoint if kaiPoint>=40: # print principal,holding,todayPrice holding = holding+ principal /todayPrice principal = principal -(principal /todayPrice)*todayPrice # print principal,holding,todayPrice uriPoint =(negative2zero(fluctuationArry[0]*uriGean[0])*18)+\ (negative2zero(fluctuationArry[1]*uriGean[1])*16)+\ (negative2zero(fluctuationArry[2]*uriGean[2])*14)+\ (negative2zero(fluctuationArry[3]*uriGean[3])*12)+\ (negative2zero(fluctuationArry[4]*uriGean[4])*10)+\ (negative2zero(fluctuationArry[5]*uriGean[5])*10)+\ (negative2zero(fluctuationArry[6]*uriGean[6])*8)+\ (negative2zero(fluctuationArry[7]*uriGean[7])*6)+\ (negative2zero(fluctuationArry[8]*uriGean[8])*4)+\ (negative2zero(fluctuationArry[9]*uriGean[9])*2) if uriPoint>=40: # print "a",principal,holding principal = principal +holding*todayPrice holding = holding - holding # print principal, holding # print kaiPoint,uriPoint,principal,holding fluctuationArry = [] today = today -1 #日付を進める #評価ループ print principal + holding* todayPrice print date[today] if __name__ == '__main__': main()
これで評価関数が完成しました。
次回はこの関数を使って遺伝的アルゴリズムを回していこうと思います。
0 件のコメント:
コメントを投稿