简体   繁体   English

如何将F#Seq转换为MWArray

[英]How to Convert F# Seq to MWArray

I have a function in MATLAB, which takes two argument 1) trade dates 2) OHLC and Volume of a given stock and returns some technical indicators, written as 我在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

Using deploy tool I have created a .net assembly and added it as well as MWArray in references of my F# project. 使用部署工具我创建了一个.net程序集,并在我的F#项目的引用中添加了它以及MWArray。 In F# Project I have another function named stockInfo which takes stock id and returns price,volume, etc. for a stock. 在F#项目中,我有另一个名为stockInfo的函数,该函数获取股票ID并返回股票的价格,交易量等。

To use the MATLAB function in F# I have written 要在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)

but the test function in last line expects MWArray as its 2nd and 3rd argument. 但是最后一行的测试函数期望MWArray成为它的第二和第三个参数。

How to convert sequences of dates and pv to MWArray? 如何将日期和光伏序列转换为MWArray?

One way is to avoid F# Seq and follow C# way, in which case I have to write in this way 一种方法是避免使用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)

and I get my results in y . 我让我的结果y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM