蟹老闆真不愧是google關鍵字高手
一出馬就在stackoverflow找到答案
以下是我寫在C#的R語言程式
string orcode = @"library(quantmod)
stock <- getSymbols('{0}.TW', auto.assign = FALSE,from='{1}')
ind <- apply(stock, 1, function(x) all(is.na(x)))
stock <- stock[!ind,]
ma5 <- runMean(stock[, 4], n = 5)
ma10 <- runMean(stock[, 4], n = 10)
ma20 <-runMean(stock[, 4], n = 20)
ma60 <- runMean(stock[, 4], n = 60)
price <- last(stock[,4])
c1 <- ifelse(last(ma5) > last(ma10) & last(ma10) > last(ma20) & last(ma20) > last(ma60), 1, 0)
c2 <- ifelse(c1 == 1 & last(ma5) > price & last(ma20) > price, 1, 0)";
取得個股5日線、10日線、20日線、60日線
當 ma5 > ma10 > ma20 > ma60 時回傳1,否則回傳0
回傳1的股票我要進一步作處理(強勢股處理)
REngine engine = REngine.GetInstance();
engine.Initialize();
string rcode = string.Format(orcode, s.Id, yearAgo);
var a = engine.Evaluate(rcode).AsCharacter().ToArray();
if (a[0] == "1")
{
var price = engine.Evaluate("price").AsCharacter().ToArray();
s.Price = float.Parse(price[0].ToString());
okList.Add(s);
}
ngine.Evaluate("dev.off");
主要是這段 var a = engine.Evaluate(rcode).AsCharacter().ToArray();
把回傳結果用陣列輸出,然後取得a[0]就可以得到R回傳值了
參考資料
https://stackoverflow.com/questions/26180506/c-sharp-not-connecting-to-r-using-rdotnet
沒有留言:
張貼留言