我在MATLAB中有一个函数,它有两个参数1)交易日期2)OHLC和给定股票的交易量并返回一些技术指标,写成
function [MACD, ADO, CHAIK, STOCH] = test(date, pricevolume)
tsObject = fints(date, pricevolume, {'Open','High','Low','Close','Volume'});
MACD = fts2mat(macd(tsObject,'Close'));
ADO = fts2mat(adosc(tsObject));
CHAIK = fts2mat(chaikosc(tsObject));
STOCH = fts2mat(stochosc(tsObject));
end
使用部署工具我创建了一个.net程序集,并在我的F#项目的引用中添加了它以及MWArray。在F#项目中,我有另一个名为stockInfo的函数,该函数获取股票ID并返回股票的价格,交易量等。
在F#中使用MATLAB函数我写了
open MathWorks.MATLAB.NET.Arrays
open MathWorks.MATLAB.NET.Utility
open test
//Creates instance of class created by matlab
let x = new Class1()
//returns price for a stock
let price = stockInfo(1)
let mon = price.Monthly
//creates sequence of dates
let dates = mon |> Seq.map(fun x-> x.Date)
//creates sequence of OHLC and Volume
let pv = mon|> Seq.map(fun x-> x.Open, x.High, x.Low, x.Close, x.Volume)
//I have to call matlab function
let y = x.test(4, dates, pv)
但是最后一行的测试函数期望MWArray成为它的第二和第三个参数。
如何将日期和pv序列转换为MWArray?
答案 0 :(得分:2)
一种方法是避免使用F#Seq并遵循C#方式,在这种情况下我必须以这种方式编写
let rows = mon.Count()
let dates = new MWCellArray(rows)
let pvs: double[,] = Array2D.zeroCreate rows 5
for i in 0 .. rows-1 do
pvs.[i,0] <- mon.[i].Open
pvs.[i,1] <- mon.[i].High
pvs.[i,2] <- mon.[i].Low
pvs.[i,3] <- mon.[i].Close
pvs.[i,4] <- double mon.[i].Volume
dates.[[|i+1|]] <- new MWCharArray( mon.[i].Date.Date.ToString())
let pv = new MWNumericArray(pvs)
let y = x.test(4,dates,pv)
我将结果发送到y
。