Statistical Stock Scanner in Mathematica
my goal is to write an algorithm in Mathematica that will search for stocks whose current price is trading below or above 2 Standard Deviations of the Mean. I literally started to learn the program yesterday but I have been scouring the internet ever since for help. I have the code, but I am getting errors along the way. can someone 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]
Very brave to jump straight in the deep waters, but I'd suggest trying to learn the basics first. You say you've been "scouring the internet for help" but did you tried Mathematica's on-board documentation center? It has thousands pages of help, just one key press away.
Anyway as to your code, a few tips:
- Don't end a variable with a $. Though not wrong in principle, they are used for system variables
- The line
SellSignal = [MeanPrice]-[StandardDeviation$]*2
, and the one following it contain function call brackets without corresponding function names. You probably don't intend to have these brackets there - The
,False
part inIf[CurrentPrice>SellSignal,"Sell",False]
and the next line is unnecessary and can be deleted here - The earlier date calculation can be better done with the dedicated
DatePlus
function, which takes things like leap years etc. into account - You probably don't want to see all the output of all the lines. You may suppress output using ';' (which also acts to separate compound statements)
- An asterisk for multiplication is unnecessary. A space will do, just as in math. a*b, a b, a 2, 2 a and 2a (without a space) are all correct multiplications.
- The data you receive from some of the calls include both prices and dates. You are also trying to average the dates and find their standard deviation.
- Though it is allowed to start a variable with an uppercase letter you better avoid doing that in order to prevent using Mathematica's own keywords (which all start with an uppercase letter).
- I don't think your buy and sell signals are very smart. You might think of selling when prices are historically high, but you're doing it when they are above the historic low watermark.
- Same for buying. Also, when current price is between your two signals the program provides conflicting advice.
- You need a construction to repeat the calculations for every NYSE member
Some very very basic code to get you started:
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}
]
Please note that Stackoverflow is intended for people who have faithfully tried to do their best to do some research in the problems they encounter. I have the feeling that you don't really meet this criterion. My urgent request is: read some basic introductory text about Mathematica (for instance Getting started and Core Language Overview).
Here you have your program running:
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}]
But please note I modified just the minimum to get it running without using idioms. This is not, by any standard, a good program. I did it just to let you play a little with it and learn some constructs.
HTH!
精彩评论