Statistical Stock Scanner at Mathematica

My goal is to write an algorithm in Mathematica that will look for stocks whose current price is trading below or above 2 standard deviations from the average. I literally started learning the program yesterday, but I have tried the Internet since then for help. I have some code, but I am getting errors along the way. Can anybody help me? Below is my current code

Today = Date[]
StartDate = Today-{0,3,0,0,0,0}
NYSEMem = FinancialData["NYSE","Members"]
CurrentPrice = FinancialData[NYSEMem,"Price"]
HistoricalPrice = FinancialData[NYSEMem,{{StartDate},{Today}}]
StandardDeviation$ = StandardDeviation[HistoricalPrice]
MeanPrice = Mean[HistoricalData]
SellSignal = [MeanPrice]-[StandardDeviation$]*2
BuySignal = [MeanPrice]+[StandardDeviation$]*2
If[CurrentPrice>SellSignal,"Sell",False]
If[CurrenPrice<BuySignal,"Buy",False]
+3
source share
2 answers

It’s very bold to jump right into the deep waters, but I would suggest first to learn the basics. You say that you "cleaned the Internet for help", but you tried the built-in Mathematica documentation center? It has thousands of help pages, just one keystroke.

, , :

  • $. ,
  • SellSignal = [MeanPrice]-[StandardDeviation$]*2, . , ,
  • ,False If[CurrentPrice>SellSignal,"Sell",False], .
  • DatePlus, , ..
  • , . , ';' ( )
  • . , . a * b, a b, a 2, 2 a 2a ( ) .
  • , , , . .
  • , , Mathematica ( ).
  • , . , , , .
  • . , , .
  • , NYSE.

- , :

StartDate = DatePlus[Date[], {-3, "Month"}];
NYSEMem = Select[FinancialData["NYSE", "Members"], (\[Not] StringMatchQ[#, ___ ~~ 
       "^" ~~ ___] &)]; (* Throw away indices *)
Do[
 currentPrice = Check[FinancialData[stock, "Price"], $Failed];
 historicalPrice = 
  Check[FinancialData[stock, {StartDate, Date[]}], $Failed];
 If[currentPrice == $Failed || historicalPrice == $Failed || 
   currentPrice == Missing["NotAvailable"] || 
   historicalPrice == Missing["NotAvailable"], 
  Continue[]]; (* Shamefully inadequate error handling *)
 standardDeviationPrice = StandardDeviation[historicalPrice[[All, 2]]];
 meanPrice = Mean[historicalPrice[[All, 2]]]; 
            (* Mean of the second column of the data matrix *)
 sellSignal = meanPrice + 2 standardDeviationPrice; 
             (* swapped + and - in these two lines, plug your own method here *)
 buySignal = meanPrice - 2 standardDeviationPrice;
 Print[stock, ": ", 
  If[currentPrice > sellSignal, "Sell", 
   If[currentPrice < buySignal, "Buy", "Neutral"]]];
 , {stock, NYSEMem}
 ]

, Stackoverflow , , , . , . : Mathematica ( ).

+4

:

Today              = Date[];
StartDate          = Today - {0, 3, 0, 0, 0, 0};
NYSEMem            = FinancialData["NYSE", "Members"];
NYSEMem            = NYSEMem[[1000 ;; 1001]];
CurrentPrice       = FinancialData[#, "Price"] & /@ NYSEMem;
HistoricalPrice    = FinancialData[#, {StartDate, Today}] & /@ NYSEMem;
StandardDeviation$ = StandardDeviation[#[[All, 2]]] & /@ HistoricalPrice;
MeanPrice          = Mean[#[[All, 2]]] & /@ HistoricalPrice;
SellSignal         = MeanPrice - StandardDeviation$*2
BuySignal          = MeanPrice + StandardDeviation$*2
Do[
   If[CurrentPrice[[i]] > SellSignal[[i]], Print["Sell ", NYSEMem[[i]]]];
   If[CurrentPrice[[i]] < BuySignal[[i]],  Print["Buy ",  NYSEMem[[i]]]],
 {i, 2}]

, , , , . . , .

!

+3

All Articles