如何使用 Mathematica 建立回测程序?
How to build up a backtesting program with Mathematica?
给定一只股票 {price,vary price} 序列如下:
lstPrice={{4.66,-0.05},{4.69,0.03},{4.78,0.09},{4.78,0.},{4.81,0.03},{4.85,0.04},{4.78,-0.07},{5.1,0.32},{5.29,0.19},{5.19,-0.1},{5.28,0.09},{5.22,-0.06},{5.18,-0.04},{5.07,-0.11},{5.08,0.01},{5.09,0.01},{5.07,-0.02},{5.1,0.03},{5.05,-0.05},{5.05,0.},{5.13,0.08},{5.1,-0.03},{5.09,-0.01},{5.21,0.12},{5.24,0.03},{5.26,0.02},{5.35,0.09},{5.19,-0.16},{5.24,0.05},{5.09,-0.15},{5.18,0.09},{5.19,0.01},{5.18,-0.01},{5.13,-0.05},{5.15,0.02},{5.06,-0.09},{5.09,0.03},{5.08,-0.01},{5.01,-0.07},{4.99,-0.02},{4.99,0.},{4.94,-0.05},{4.98,0.04},{4.92,-0.06},{4.87,-0.05},{4.91,0.04},{4.91,0.},{4.92,0.01},{4.95,0.03},{4.9,-0.05},{4.93,0.03},{4.99,0.06},{5.04,0.05},{4.98,-0.06},{5.17,0.19},{5.07,-0.1},{5.08,0.01},{5.14,0.06},{5.17,0.03},{5.07,-0.1}}
和最大资本水平为$500000,最大头寸水平为100000,
当vary price为负时,买入p1%的资金水平,当vary price为正时,卖出p2%的头寸水平。以检查这个简单的策略最终是否成功。
如何建立一个 Mathematica 回测程序来测试这个想法,函数式编程最好,或者像下面的块。谢谢!
initCapital=500000;(*min to 0 *)
initPosition=0;(*max to 100000*)
p1=0.3;
p2=0.2;
BacktestDo[list_?ListQ]:=If[list[[2]]>0,Buy[p1],Sell[p2]](*Buy and Sell not implemented*)
BacktestDo/@listPrice
这假设 p1
和 p2
分别为 30% 和 20%。如果您的意思是 0.3% 和 0.2%,请将代码调整为 p1/100
和 p2/100
,如注释中所示。
listPrice = {
{4.66, -0.05}, {4.69, 0.03}, {4.78, 0.09}, {4.78, 0.},
{4.81, 0.03}, {4.85, 0.04}, {4.78, -0.07}, {5.1, 0.32},
{5.29, 0.19}, {5.19, -0.1}, {5.28, 0.09}, {5.22, -0.06},
{5.18, -0.04}, {5.07, -0.11}, {5.08, 0.01}, {5.09, 0.01},
{5.07, -0.02}, {5.1, 0.03}, {5.05, -0.05}, {5.05, 0.},
{5.13, 0.08}, {5.1, -0.03}, {5.09, -0.01}, {5.21, 0.12},
{5.24, 0.03}, {5.26, 0.02}, {5.35, 0.09}, {5.19, -0.16},
{5.24, 0.05}, {5.09, -0.15}, {5.18, 0.09}, {5.19, 0.01},
{5.18, -0.01}, {5.13, -0.05}, {5.15, 0.02}, {5.06, -0.09},
{5.09, 0.03}, {5.08, -0.01}, {5.01, -0.07}, {4.99, -0.02},
{4.99, 0.}, {4.94, -0.05}, {4.98, 0.04}, {4.92, -0.06},
{4.87, -0.05}, {4.91, 0.04}, {4.91, 0.}, {4.92, 0.01},
{4.95, 0.03}, {4.9, -0.05}, {4.93, 0.03}, {4.99, 0.06},
{5.04, 0.05}, {4.98, -0.06}, {5.17, 0.19}, {5.07, -0.1},
{5.08, 0.01}, {5.14, 0.06}, {5.17, 0.03}, {5.07, -0.1}};
initCapital = 500000;(* min to 0 *)
initPosition = 0;(* max to 100000 *)
p1 = 0.3;
p2 = 0.2;
capital = {initCapital};
position = {initPosition};
totalassets = {initCapital};
buy[p1_, price_] := Module[{value},
value = Last[capital] p1 (* or use p1/100 *);
(* check limits *)
If[Last[position] + value/price > 100000 ||
Last[capital] - value < 0,
(* skip transaction *)
AppendTo[position, Last[position]];
AppendTo[capital, Last[capital]];
AppendTo[totalassets, Last[totalassets]],
(* or make transaction *)
AppendTo[position, Last[position] + value/price];
AppendTo[capital, Last[capital] - value];
AppendTo[totalassets, price Last[position] + Last[capital]];
{"buy", p1, value, Last[capital]}]]
sell[p2_, price_] := Module[{quantity},
quantity = Last[position] p2 (* or use p2/100 *);
(* make transaction *)
AppendTo[position, Last[position] - quantity];
AppendTo[capital, Last[capital] + quantity*price];
AppendTo[totalassets, price Last[position] + Last[capital]];
{"sell", p2, quantity*price, Last[capital]}]
backtestDo[list_List] := If[list[[2]] < 0,
buy[p1, First[list]],
sell[p2, First[list]]
]
backtestDo /@ listPrice;
GraphicsColumn[Map[ListLinePlot[ToExpression[#],
DataRange -> Length[listPrice] + 1,
PlotLabel -> #,
ImagePadding -> {{40, 10}, {Automatic, Automatic}}] &,
{"capital", "position", "totalassets"}], ImageSize -> 400]
给定一只股票 {price,vary price} 序列如下:
lstPrice={{4.66,-0.05},{4.69,0.03},{4.78,0.09},{4.78,0.},{4.81,0.03},{4.85,0.04},{4.78,-0.07},{5.1,0.32},{5.29,0.19},{5.19,-0.1},{5.28,0.09},{5.22,-0.06},{5.18,-0.04},{5.07,-0.11},{5.08,0.01},{5.09,0.01},{5.07,-0.02},{5.1,0.03},{5.05,-0.05},{5.05,0.},{5.13,0.08},{5.1,-0.03},{5.09,-0.01},{5.21,0.12},{5.24,0.03},{5.26,0.02},{5.35,0.09},{5.19,-0.16},{5.24,0.05},{5.09,-0.15},{5.18,0.09},{5.19,0.01},{5.18,-0.01},{5.13,-0.05},{5.15,0.02},{5.06,-0.09},{5.09,0.03},{5.08,-0.01},{5.01,-0.07},{4.99,-0.02},{4.99,0.},{4.94,-0.05},{4.98,0.04},{4.92,-0.06},{4.87,-0.05},{4.91,0.04},{4.91,0.},{4.92,0.01},{4.95,0.03},{4.9,-0.05},{4.93,0.03},{4.99,0.06},{5.04,0.05},{4.98,-0.06},{5.17,0.19},{5.07,-0.1},{5.08,0.01},{5.14,0.06},{5.17,0.03},{5.07,-0.1}}
和最大资本水平为$500000,最大头寸水平为100000, 当vary price为负时,买入p1%的资金水平,当vary price为正时,卖出p2%的头寸水平。以检查这个简单的策略最终是否成功。 如何建立一个 Mathematica 回测程序来测试这个想法,函数式编程最好,或者像下面的块。谢谢!
initCapital=500000;(*min to 0 *)
initPosition=0;(*max to 100000*)
p1=0.3;
p2=0.2;
BacktestDo[list_?ListQ]:=If[list[[2]]>0,Buy[p1],Sell[p2]](*Buy and Sell not implemented*)
BacktestDo/@listPrice
这假设 p1
和 p2
分别为 30% 和 20%。如果您的意思是 0.3% 和 0.2%,请将代码调整为 p1/100
和 p2/100
,如注释中所示。
listPrice = {
{4.66, -0.05}, {4.69, 0.03}, {4.78, 0.09}, {4.78, 0.},
{4.81, 0.03}, {4.85, 0.04}, {4.78, -0.07}, {5.1, 0.32},
{5.29, 0.19}, {5.19, -0.1}, {5.28, 0.09}, {5.22, -0.06},
{5.18, -0.04}, {5.07, -0.11}, {5.08, 0.01}, {5.09, 0.01},
{5.07, -0.02}, {5.1, 0.03}, {5.05, -0.05}, {5.05, 0.},
{5.13, 0.08}, {5.1, -0.03}, {5.09, -0.01}, {5.21, 0.12},
{5.24, 0.03}, {5.26, 0.02}, {5.35, 0.09}, {5.19, -0.16},
{5.24, 0.05}, {5.09, -0.15}, {5.18, 0.09}, {5.19, 0.01},
{5.18, -0.01}, {5.13, -0.05}, {5.15, 0.02}, {5.06, -0.09},
{5.09, 0.03}, {5.08, -0.01}, {5.01, -0.07}, {4.99, -0.02},
{4.99, 0.}, {4.94, -0.05}, {4.98, 0.04}, {4.92, -0.06},
{4.87, -0.05}, {4.91, 0.04}, {4.91, 0.}, {4.92, 0.01},
{4.95, 0.03}, {4.9, -0.05}, {4.93, 0.03}, {4.99, 0.06},
{5.04, 0.05}, {4.98, -0.06}, {5.17, 0.19}, {5.07, -0.1},
{5.08, 0.01}, {5.14, 0.06}, {5.17, 0.03}, {5.07, -0.1}};
initCapital = 500000;(* min to 0 *)
initPosition = 0;(* max to 100000 *)
p1 = 0.3;
p2 = 0.2;
capital = {initCapital};
position = {initPosition};
totalassets = {initCapital};
buy[p1_, price_] := Module[{value},
value = Last[capital] p1 (* or use p1/100 *);
(* check limits *)
If[Last[position] + value/price > 100000 ||
Last[capital] - value < 0,
(* skip transaction *)
AppendTo[position, Last[position]];
AppendTo[capital, Last[capital]];
AppendTo[totalassets, Last[totalassets]],
(* or make transaction *)
AppendTo[position, Last[position] + value/price];
AppendTo[capital, Last[capital] - value];
AppendTo[totalassets, price Last[position] + Last[capital]];
{"buy", p1, value, Last[capital]}]]
sell[p2_, price_] := Module[{quantity},
quantity = Last[position] p2 (* or use p2/100 *);
(* make transaction *)
AppendTo[position, Last[position] - quantity];
AppendTo[capital, Last[capital] + quantity*price];
AppendTo[totalassets, price Last[position] + Last[capital]];
{"sell", p2, quantity*price, Last[capital]}]
backtestDo[list_List] := If[list[[2]] < 0,
buy[p1, First[list]],
sell[p2, First[list]]
]
backtestDo /@ listPrice;
GraphicsColumn[Map[ListLinePlot[ToExpression[#],
DataRange -> Length[listPrice] + 1,
PlotLabel -> #,
ImagePadding -> {{40, 10}, {Automatic, Automatic}}] &,
{"capital", "position", "totalassets"}], ImageSize -> 400]