2015年05月08日
Rubyではじめるシステムトレード8
”Rubyではじめるシステムトレード”のサンプルスクリプトをPan Active Market Databaseの株価を利用して、トレードのシミュレーションを行うことを目指している。
ここまでで、動作確認用のサンプルスクリプトが問題なく動くようになった。最後にシミュレーター本体"simulate.rb"を実行する。シミュレーションの設定ファイルはRubyのスクリプトとして用意される仕様になっている。書籍で解説されているのは移動平均乖離率を利用したトレードで、設定ファイルは"estrangement.rb"で、Pan Active Market Databaseから株価を取得するように以下の太字のように記述する。
"estrangement.rb"
Simulation.setting "estrangement", "0.0.1" do
trading_system do
entry EstrangementEntry, span: 20, rate: 5
exit StopOutExit
exit EstrangementExit, span: 20, rate: 3
stop AverageTrueRangeStop, span: 20, ratio: 1
filter MovingAverageDirectionFilter, span: 40
end
from "2005/06/20"
to "2015/05/01"
data_loader PanDatabaseToStock, data_dir: "data",
stock_list: "./data/tosho_list.txt"
record_dir "result"
record_every_stock true
end
特にPanDatabaseToStockで株価を扱うときは、サンプルスクリプト内の"market_section: "東証1部""の項目を削除しなくてはいけない。シミュレーションの実行毎に結果を保存するフォルダを用意するのは面倒だが、バージョン番号を”0.0.1”などと指定することで、バージョン番号に応じた新しいフォルダが作成される仕組みになっており大変便利。
そして"simulate.rb"を設定ファイルを指定して以下のように実行する。
$ ruby bin/simulation.rb setting/estrangement.rb
1銘柄だけのシミュレーションを行うときは、証券コードを指定して、
$ ruby bin/simulation.rb setting/estrangement.rb 1001
などとする。
証券コードが次々と表示されていき、計算は25分程度で終了する。そして全取引のまとめ”_stats.csv”と各証券銘柄の取引結果"_stats_for_each_stock.csv"が各証券銘柄の取引履歴”証券コード.csv”ファイルと共に、resultフォルダ以下に自動的に作成される。
"_stats.csv"
総損益,勝ち数,負け数,分け数,勝率,平均損益,PF,総R倍数,平均R倍数,総損益率,平均損益率,平均期間
1158395500.0,83499,94306,2561,0.4629420178969429,6422.471530110996,1.2210031536952026,14993.294925766015,0.08312705790318582,69061.88261362449,0.38289856521530935,4.124729716243638
"_stats_for_each_stock.csv"
コード,総損益,勝ち数,負け数,分け数,勝率,平均損益,PF,総R倍数,平均R倍数,総損益率,平均損益率,平均期間
641,1500,1,1,0,0.5,750.0,3.5,2.5,1.25,5.28080167255425,2.640400836277125,2.0
1001,-100300,8,10,0,0.4444444444444444,-5572.222222222223,0.7176238738738738,-1.9033385747429017,-0.10574103193016121,-2.795630513321206,-0.15531280629562255,3.611111111111111
1002,-5700.0,6,7,0,0.46153846153846156,-438.46153846153845,0.7164179104477612,-2.132984967475883,-0.16407576672891408,-3.624682490150885,-0.27882173001160654,4.153846153846154
1301,13600.0,16,10,0,0.6153846153846154,523.0769230769231,3.029850746268657,12.887687312687316,0.4956802812572044,52.45106692872076,2.017348728027722,3.730769230769231
1305,-1300,7,7,1,0.4666666666666667,-86.66666666666667,0.9362745098039216,0.8626722834898157,0.057511485565987716,0.3769308474350742,0.02512872316233828,4.4
1306,8800,6,5,0,0.5454545454545454,800.0,1.7521367521367521,3.5806101585196415,0.3255100144108765,7.536231212718562,0.6851119284289602,4.909090909090909
1308,6200,7,5,1,0.5384615384615384,476.9230769230769,1.4428571428571428,1.7273119835988107,0.1328701525845239,5.296953503322498,0.40745796179403826,4.384615384615385
1309,1267000,17,10,1,0.6071428571428571,45250.0,2.191909689557855,13.259806339866424,0.4735645121380866,50.43860396524438,1.8013787130444423,4.535714285714286
1310,4900,6,8,1,0.4,326.6666666666667,1.3888888888888888,0.634211993193686,0.04228079954624574,4.908410659166213,0.32722737727774753,4.066666666666666
1311,2500.0,10,12,0,0.45454545454545453,113.63636363636364,1.1396648044692737,1.0303491814866166,0.04683405370393712,0.8679429764590654,0.03945195347541206,3.9545454545454546
1312,-40000,4,7,0,0.36363636363636365,-3636.3636363636365,0.8148148148148148,0.33187218068656543,0.030170198244233223,-0.5045378096797617,-0.04586707360725106,4.181818181818182
1313,-5100,14,14,0,0.5,-182.14285714285714,0.9314516129032258,-0.9650050867744064,-0.03446446738480023,-3.9954731770827667,-0.14269547061009882,3.6785714285714284
1314,19200,8,4,0,0.6666666666666666,1600.0,2.422222222222222,5.94264513026753,0.49522042752229417,25.40178475834549,2.1168153965287906,4.833333333333333
1316,-18200,1,12,0,0.07692307692307693,-1400.0,0.08542713567839195,-9.626117414997594,-0.7404705703844303,-23.372069590075803,-1.7978515069289078,3.923076923076923
1317,-15400,6,10,0,0.375,-962.5,0.624390243902439,3.40959647911679,0.21309977994479937,-6.257450225745265,-0.39109063910907904,4.3125
1318,-7700,5,8,0,0.38461538461538464,-592.3076923076923,0.6607929515418502,0.5095915452924209,0.03919934963787853,-4.941934075657878,-0.380148775050606,4.846153846153846
1319,-7500.0,8,19,0,0.2962962962962963,-277.77777777777777,0.3951612903225806,-6.754853479853482,-0.25017975851309193,-31.252810953670135,-1.1575115168025976,2.6666666666666665
1320,13000,9,6,0,0.6,866.6666666666666,1.0555555555555556,-0.38794319006693634,-0.025862879337795755,6.695760537470772,0.4463840358313848,4.066666666666666
1321,-14000.0,7,7,0,0.5,-1000.0,0.940677966101695,-1.1734195355719315,-0.08381568111228081,4.338163997030426,0.30986885693074473,4.285714285714286
1322,13300,10,11,1,0.45454545454545453,604.5454545454545,1.0888443553774214,1.2576197578972792,0.05716453444987633,5.166575594032352,0.23484434518328873,3.772727272727273
1323,9500,12,10,0,0.5454545454545454,431.8181818181818,2.217948717948718,5.717460317460318,0.2598845598845599,40.46200480929459,1.8391820367861178,4.090909090909091
1324,5000,16,14,0,0.5333333333333333,166.66666666666666,1.8620689655172413,9.61818181818182,0.32060606060606067,55.191423193329825,1.8397141064443274,4.166666666666667
1325,7200.0,14,10,1,0.56,288.0,2.0,7.798496240601503,0.31193984962406013,35.2954808379909,1.411819233519636,3.64
1326,50000,5,4,0,0.5555555555555556,5555.555555555556,1.3875968992248062,2.306836225113155,0.2563151361236839,8.888206968476673,0.9875785520529637,3.888888888888889
1327,71500,9,7,0,0.5625,4468.75,1.8313953488372092,5.476436834840725,0.3422773021775453,16.631144905082127,1.039446556567633,3.6875
1328,23500,5,4,0,0.5555555555555556,2611.1111111111113,1.681159420289855,3.4101465225075667,0.3789051691675074,5.9935148832698175,0.6659460981410908,4.0
1329,-74000.0,7,7,0,0.5,-5285.714285714285,0.7347670250896058,-0.49334941900071094,-0.035239244214336496,-1.9490350662498237,-0.13921679044641597,4.357142857142857
1330,-105000,6,8,0,0.42857142857142855,-7500.0,0.6276595744680851,-3.02343673618687,-0.2159597668704907,-4.732708024485519,-0.33805057317753706,3.9285714285714284
1331,-100,2,3,0,0.4,-20.0,0.9411764705882353,-0.37142857142857144,-0.07428571428571429,-1.0569997933075834,-0.21139995866151667,3.8
1332,-20400,16,30,0,0.34782608695652173,-443.4782608695652,0.5255813953488372,-13.465148957509637,-0.2927206295110791,-48.37360690345826,-1.0516001500751795,3.5652173913043477
1334,-4200.0,20,24,1,0.4444444444444444,-93.33333333333333,0.7835051546391752,-2.3178178847296493,-0.05150706410510332,-12.943845296109627,-0.2876410065802139,4.066666666666666
1341,23500,1,1,0,0.5,11750.0,3.238095238095238,2.238095238095238,1.119047619047619,5.462529274004685,2.7312646370023423,2.5
1342,-2500.0,0,3,0,0.0,-833.3333333333334,0.0,-3.0972222222222223,-1.0324074074074074,-8.952895607598016,-2.9842985358660052,4.666666666666667
1343,11100,4,3,0,0.5714285714285714,1585.7142857142858,2.2065217391304346,3.3829788568919,0.48328269384170003,13.07048687957986,1.8672124113685515,5.571428571428571
1344,2900,7,6,0,0.5384615384615384,223.07692307692307,1.3372093023255813,3.0666666666666664,0.2358974358974359,4.572486431439351,0.3517297254953347,5.3076923076923075
1345,-900,3,4,0,0.42857142857142855,-128.57142857142858,0.926829268292683,-0.02520853130609213,-0.0036012187580131616,0.9842882181882335,0.14061260259831906,4.0
1346,-145000,2,4,0,0.3333333333333333,-24166.666666666668,0.15204678362573099,-3.2366401105896907,-0.5394400184316152,-10.120862432816255,-1.6868104054693758,4.166666666666667
1347,52600,6,3,0,0.6666666666666666,5844.444444444444,4.005714285714285,6.717390742573354,0.7463767491748171,13.360013051167872,1.484445894574208,4.555555555555555
1348,-5700.0,2,5,0,0.2857142857142857,-814.2857142857143,0.5327868852459017,-0.8572856428928392,-0.12246937755611988,-5.27112434572508,-0.7530177636750114,5.0
なんとかシミュレーターが動くようになった!これで取引ルールの評価に進むことができる。
ここまでで、動作確認用のサンプルスクリプトが問題なく動くようになった。最後にシミュレーター本体"simulate.rb"を実行する。シミュレーションの設定ファイルはRubyのスクリプトとして用意される仕様になっている。書籍で解説されているのは移動平均乖離率を利用したトレードで、設定ファイルは"estrangement.rb"で、Pan Active Market Databaseから株価を取得するように以下の太字のように記述する。
"estrangement.rb"
Simulation.setting "estrangement", "0.0.1" do
trading_system do
entry EstrangementEntry, span: 20, rate: 5
exit StopOutExit
exit EstrangementExit, span: 20, rate: 3
stop AverageTrueRangeStop, span: 20, ratio: 1
filter MovingAverageDirectionFilter, span: 40
end
from "2005/06/20"
to "2015/05/01"
data_loader PanDatabaseToStock, data_dir: "data",
stock_list: "./data/tosho_list.txt"
record_dir "result"
record_every_stock true
end
特にPanDatabaseToStockで株価を扱うときは、サンプルスクリプト内の"market_section: "東証1部""の項目を削除しなくてはいけない。シミュレーションの実行毎に結果を保存するフォルダを用意するのは面倒だが、バージョン番号を”0.0.1”などと指定することで、バージョン番号に応じた新しいフォルダが作成される仕組みになっており大変便利。
そして"simulate.rb"を設定ファイルを指定して以下のように実行する。
$ ruby bin/simulation.rb setting/estrangement.rb
1銘柄だけのシミュレーションを行うときは、証券コードを指定して、
$ ruby bin/simulation.rb setting/estrangement.rb 1001
などとする。
証券コードが次々と表示されていき、計算は25分程度で終了する。そして全取引のまとめ”_stats.csv”と各証券銘柄の取引結果"_stats_for_each_stock.csv"が各証券銘柄の取引履歴”証券コード.csv”ファイルと共に、resultフォルダ以下に自動的に作成される。
"_stats.csv"
総損益,勝ち数,負け数,分け数,勝率,平均損益,PF,総R倍数,平均R倍数,総損益率,平均損益率,平均期間
1158395500.0,83499,94306,2561,0.4629420178969429,6422.471530110996,1.2210031536952026,14993.294925766015,0.08312705790318582,69061.88261362449,0.38289856521530935,4.124729716243638
"_stats_for_each_stock.csv"
コード,総損益,勝ち数,負け数,分け数,勝率,平均損益,PF,総R倍数,平均R倍数,総損益率,平均損益率,平均期間
641,1500,1,1,0,0.5,750.0,3.5,2.5,1.25,5.28080167255425,2.640400836277125,2.0
1001,-100300,8,10,0,0.4444444444444444,-5572.222222222223,0.7176238738738738,-1.9033385747429017,-0.10574103193016121,-2.795630513321206,-0.15531280629562255,3.611111111111111
1002,-5700.0,6,7,0,0.46153846153846156,-438.46153846153845,0.7164179104477612,-2.132984967475883,-0.16407576672891408,-3.624682490150885,-0.27882173001160654,4.153846153846154
1301,13600.0,16,10,0,0.6153846153846154,523.0769230769231,3.029850746268657,12.887687312687316,0.4956802812572044,52.45106692872076,2.017348728027722,3.730769230769231
1305,-1300,7,7,1,0.4666666666666667,-86.66666666666667,0.9362745098039216,0.8626722834898157,0.057511485565987716,0.3769308474350742,0.02512872316233828,4.4
1306,8800,6,5,0,0.5454545454545454,800.0,1.7521367521367521,3.5806101585196415,0.3255100144108765,7.536231212718562,0.6851119284289602,4.909090909090909
1308,6200,7,5,1,0.5384615384615384,476.9230769230769,1.4428571428571428,1.7273119835988107,0.1328701525845239,5.296953503322498,0.40745796179403826,4.384615384615385
1309,1267000,17,10,1,0.6071428571428571,45250.0,2.191909689557855,13.259806339866424,0.4735645121380866,50.43860396524438,1.8013787130444423,4.535714285714286
1310,4900,6,8,1,0.4,326.6666666666667,1.3888888888888888,0.634211993193686,0.04228079954624574,4.908410659166213,0.32722737727774753,4.066666666666666
1311,2500.0,10,12,0,0.45454545454545453,113.63636363636364,1.1396648044692737,1.0303491814866166,0.04683405370393712,0.8679429764590654,0.03945195347541206,3.9545454545454546
1312,-40000,4,7,0,0.36363636363636365,-3636.3636363636365,0.8148148148148148,0.33187218068656543,0.030170198244233223,-0.5045378096797617,-0.04586707360725106,4.181818181818182
1313,-5100,14,14,0,0.5,-182.14285714285714,0.9314516129032258,-0.9650050867744064,-0.03446446738480023,-3.9954731770827667,-0.14269547061009882,3.6785714285714284
1314,19200,8,4,0,0.6666666666666666,1600.0,2.422222222222222,5.94264513026753,0.49522042752229417,25.40178475834549,2.1168153965287906,4.833333333333333
1316,-18200,1,12,0,0.07692307692307693,-1400.0,0.08542713567839195,-9.626117414997594,-0.7404705703844303,-23.372069590075803,-1.7978515069289078,3.923076923076923
1317,-15400,6,10,0,0.375,-962.5,0.624390243902439,3.40959647911679,0.21309977994479937,-6.257450225745265,-0.39109063910907904,4.3125
1318,-7700,5,8,0,0.38461538461538464,-592.3076923076923,0.6607929515418502,0.5095915452924209,0.03919934963787853,-4.941934075657878,-0.380148775050606,4.846153846153846
1319,-7500.0,8,19,0,0.2962962962962963,-277.77777777777777,0.3951612903225806,-6.754853479853482,-0.25017975851309193,-31.252810953670135,-1.1575115168025976,2.6666666666666665
1320,13000,9,6,0,0.6,866.6666666666666,1.0555555555555556,-0.38794319006693634,-0.025862879337795755,6.695760537470772,0.4463840358313848,4.066666666666666
1321,-14000.0,7,7,0,0.5,-1000.0,0.940677966101695,-1.1734195355719315,-0.08381568111228081,4.338163997030426,0.30986885693074473,4.285714285714286
1322,13300,10,11,1,0.45454545454545453,604.5454545454545,1.0888443553774214,1.2576197578972792,0.05716453444987633,5.166575594032352,0.23484434518328873,3.772727272727273
1323,9500,12,10,0,0.5454545454545454,431.8181818181818,2.217948717948718,5.717460317460318,0.2598845598845599,40.46200480929459,1.8391820367861178,4.090909090909091
1324,5000,16,14,0,0.5333333333333333,166.66666666666666,1.8620689655172413,9.61818181818182,0.32060606060606067,55.191423193329825,1.8397141064443274,4.166666666666667
1325,7200.0,14,10,1,0.56,288.0,2.0,7.798496240601503,0.31193984962406013,35.2954808379909,1.411819233519636,3.64
1326,50000,5,4,0,0.5555555555555556,5555.555555555556,1.3875968992248062,2.306836225113155,0.2563151361236839,8.888206968476673,0.9875785520529637,3.888888888888889
1327,71500,9,7,0,0.5625,4468.75,1.8313953488372092,5.476436834840725,0.3422773021775453,16.631144905082127,1.039446556567633,3.6875
1328,23500,5,4,0,0.5555555555555556,2611.1111111111113,1.681159420289855,3.4101465225075667,0.3789051691675074,5.9935148832698175,0.6659460981410908,4.0
1329,-74000.0,7,7,0,0.5,-5285.714285714285,0.7347670250896058,-0.49334941900071094,-0.035239244214336496,-1.9490350662498237,-0.13921679044641597,4.357142857142857
1330,-105000,6,8,0,0.42857142857142855,-7500.0,0.6276595744680851,-3.02343673618687,-0.2159597668704907,-4.732708024485519,-0.33805057317753706,3.9285714285714284
1331,-100,2,3,0,0.4,-20.0,0.9411764705882353,-0.37142857142857144,-0.07428571428571429,-1.0569997933075834,-0.21139995866151667,3.8
1332,-20400,16,30,0,0.34782608695652173,-443.4782608695652,0.5255813953488372,-13.465148957509637,-0.2927206295110791,-48.37360690345826,-1.0516001500751795,3.5652173913043477
1334,-4200.0,20,24,1,0.4444444444444444,-93.33333333333333,0.7835051546391752,-2.3178178847296493,-0.05150706410510332,-12.943845296109627,-0.2876410065802139,4.066666666666666
1341,23500,1,1,0,0.5,11750.0,3.238095238095238,2.238095238095238,1.119047619047619,5.462529274004685,2.7312646370023423,2.5
1342,-2500.0,0,3,0,0.0,-833.3333333333334,0.0,-3.0972222222222223,-1.0324074074074074,-8.952895607598016,-2.9842985358660052,4.666666666666667
1343,11100,4,3,0,0.5714285714285714,1585.7142857142858,2.2065217391304346,3.3829788568919,0.48328269384170003,13.07048687957986,1.8672124113685515,5.571428571428571
1344,2900,7,6,0,0.5384615384615384,223.07692307692307,1.3372093023255813,3.0666666666666664,0.2358974358974359,4.572486431439351,0.3517297254953347,5.3076923076923075
1345,-900,3,4,0,0.42857142857142855,-128.57142857142858,0.926829268292683,-0.02520853130609213,-0.0036012187580131616,0.9842882181882335,0.14061260259831906,4.0
1346,-145000,2,4,0,0.3333333333333333,-24166.666666666668,0.15204678362573099,-3.2366401105896907,-0.5394400184316152,-10.120862432816255,-1.6868104054693758,4.166666666666667
1347,52600,6,3,0,0.6666666666666666,5844.444444444444,4.005714285714285,6.717390742573354,0.7463767491748171,13.360013051167872,1.484445894574208,4.555555555555555
1348,-5700.0,2,5,0,0.2857142857142857,-814.2857142857143,0.5327868852459017,-0.8572856428928392,-0.12246937755611988,-5.27112434572508,-0.7530177636750114,5.0
なんとかシミュレーターが動くようになった!これで取引ルールの評価に進むことができる。
【このカテゴリーの最新記事】
-
no image
この記事へのコメント
コメントを書く
この記事へのトラックバックURL
https://fanblogs.jp/tb/3634146
※ブログオーナーが承認したトラックバックのみ表示されます。
この記事へのトラックバック