我的目标是用Mathematica编写一个算法,搜索当前价格低于或高于平均值2标准差的股票。我从昨天开始学习这个程序,但从那时起我就一直在网上寻找帮助。我有代码,但在这过程中我会出错。有人能帮我吗?下面是我的当前代码
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]
发布于 2011-06-07 17:26:21
非常勇敢地跳入深海,但我建议你先学一些基本知识。你说你一直在“搜索互联网上的帮助”,但你有没有尝试过Mathematica的车载文档中心?它有数千页的帮助,只有一个按键的距离。
不管怎么说,关于您的代码,以下几点建议:
SellSignal = [MeanPrice]-[StandardDeviation$]*2
及其后面的一行包含函数调用括号,没有相应的函数名。你可能不打算把这些括号放在那里,False
部件在If[CurrentPrice>SellSignal,"Sell",False]
中和下一行是不必要的,可以在这里删除DatePlus
函数,该函数考虑到闰年等问题。一些非常基本的代码可以帮助您入门:
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的基本介绍性文章(例如快速入门和核心语言概述)。
发布于 2011-06-07 18:47:53
在这里,您的程序正在运行:
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}]
但请注意,我只是修改了最小值,使它在不使用成语的情况下运行。--无论如何,这并不是一个好的程序。我这么做只是为了让你玩一玩,学习一些构造。
哈哈!
https://stackoverflow.com/questions/6268943
复制相似问题