1

eine Aktie Gegeben {Preis variieren Preis} Sequenz wie folgt aus:Wie baut man ein Backtesting-Programm mit Mathematica auf?

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}} 

und max Kapitalniveau auf $ 500000, max Positionsebene bis 100000, Wenn Preis variieren ist negativ, kaufen p1 Prozent der Hauptstadt Ebene, wenn variieren Preis postive, verkaufen p2 Prozent der Positionsebene. um diese einfache Strategie zu überprüfen, ob bis zum Ende erfolgreich. Wie baut man ein Mathematica Backtesting Programm auf, um die Idee zu testen, Functional Programming am besten, oder wie Balgblock. Thansk!

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 
+0

seine nicht klar, was Sie fragen, aber ich schlage vor, Sie schauen auf 'FoldList' – agentp

+0

Dank! 'MapThread' könnte auch tun. – Jerry

+0

Ich habe einen neueren Backtesting-Block geschrieben, der sich auf @Chris Degnens Antwort bezieht. Sehen Sie mehr auf github. https://github.com/liuhuashan/StocktaSolva. – Jerry

Antwort

0

Dies setzt voraus, p1 & p2 30% und 20% jeweils. Wenn Sie 0,3% und 0,2% meinen, passen Sie den Code wie in den Kommentaren angegeben an p1/100 und p2/100 an.

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] 

enter image description here

+0

Sehr schöne Arbeit! über Ihren Code und Zeichnungen verstehe ich _trending_ ist das Keyword zu vermarkten. Vielen Dank! – Jerry