【问题标题】:How to build up a backtesting program with Mathematica?如何使用 Mathematica 建立回测程序?
【发布时间】:2017-04-02 03:13:50
【问题描述】:

给定一个股票 {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, 当变动价格为负时,买入资金水平的p1%,当变动价格为正时,卖出持仓水平的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

【问题讨论】:

  • 不清楚你在问什么,但我建议你看看FoldList
  • 谢谢! MapThread 也可以。

标签: functional-programming wolfram-mathematica stock trading


【解决方案1】:

这假设 p1p2 分别为 30% 和 20%。如果您的意思是 0.3% 和 0.2%,请将代码调整为 p1/100p2/100,如 cmets 所示。

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]

【讨论】:

  • 非常出色的工作!通过您的代码和绘图,我了解 trending 是市场的关键词。谢谢!
猜你喜欢
  • 2011-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
  • 2012-08-11
  • 1970-01-01
  • 2022-12-16
  • 2014-11-02
相关资源
最近更新 更多