掘金社区

TA-Lib的C#使用示例Pinned highlighted

逍遥游 发表在掘金2 2016-01-06 01:59:21

掘金2
3395
0
0

源自国外网站,内容很简单,就不翻译了:

	How do I get started with TA-LIB on Visual C# .NET?		
		
	
	TA-LIB is the most widely used library for creating trading applications.

1.Download ta-lib zip file from http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-msvc.zip. Extract abd save it to a folder eg., C:Downloads

2.If you do not have Visual C++ express, download it from http://www.microsoft.com/express/Downloads/#2010-Visual-CPP

3.After installing Visual C++ 2010, launch Visual C++ express. Choose menu
File->Open Project. Select project file TA-Lib.sln in
C:Downloads a-lib-0.4.0-msvc a-libdotnetsrc. (assuming you
extracted the zip file in C:Downloads).
Note that it will ask you to convert the project from Visual C++ 2005 to 2010 version. Confirm yes.

4.After loading the project, choose Debug->Build Solution

After build is complete, note that TA-Lib-Core.dll is built in
C:Downloads a-lib-0.4.0-msvc a-libdotnetsrcDebug. Your dll is now
ready to be used in C# application

6.If you do not have Visual C# express, download it from http://www.microsoft.com/express/Downloads/#2010-Visual-CS

7.After installing it, launch Visual C# express

8.Choose menu File->new Project. Select node Visual C# in the "Project types" window. Select "WPF Application" in the "Visual Studio Installed templates" window. Enter a project name (eg.,talib_test), and select Ok.

9.In the solution explorer, right-click on references. Choose menu "Add reference"

  1. In the "Add reference" dialog, select tab "Browse"

  2. Navigate to C:Downloads a-lib-0.4.0-msvc a-libdotnetsrcRelease, and select TA-Lib-Core.dll

12.You can now start using the TA lib core functions by importing namespace
TicTacTec.TA.Library.Core. For an example or demo, please see http://www.quantcode.com/modules/mydownloads/singlefile.php?cid=15&lid=546 最后这个链接是示例

其中调用MACD代码的示例如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using TicTacTec.TA.Library;

namespace talib_test
{
    public class Macdres
    {
        public int Index { get; set; }
        public double Macd { get; set; }
        public double Signal { get; set; }
        public double MacdHistogram { get; set; }
    }

    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {

        public static List<string[]> parseCSV(string path)
        {
            List<string[]> parsedData = new List<string[]>();

            try
            {
                using (StreamReader readFile = new StreamReader(path))
                {
                    string line;
                    string[] row;

                    while ((line = readFile.ReadLine()) != null)
                    {
                        row = line.Split(&#39;,&#39;);
                        parsedData.Add(row);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return parsedData;
        }


        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int i;
            string curDir=Directory.GetCurrentDirectory();
            string fileName=curDir+@&#34;msft.csv&#34;;
            List<string[]> parsedData = parseCSV(fileName);
            int nCount = parsedData.Count;
            double[] closingPricesArr = new double[nCount - 1];
            for (i = 0; i &lt; nCount - 1; i++)
                closingPricesArr[i] = Convert.ToDouble(parsedData[i + 1][6]);

            int startIdx = 100;    // 起始值,注意从0开始会有空
            int endIdx = 1000;                             
            int optInFastPeriod = 12;   // 参数
            int optInSlowPeriod = 26;
            int optInSignalPeriod = 9;                  
            double[] inReal = closingPricesArr;    // 收盘价

            int outBegIdx;
            int outNBElement;

            double[] outMACD = new double[endIdx - startIdx+1];       // 定义输出数据的存放, 通达信界面上的DIF
            double[] outMACDSignal = new double[endIdx - startIdx + 1];   // 通达信界面上的DEA
            double[] outMACDHist = new double[endIdx - startIdx + 1];      //  通达信界面上的macd

            ///  inReal就是输入的bar的close数组, 
            Core.RetCode res = Core.Macd(startIdx, endIdx, inReal, optInFastPeriod, optInSlowPeriod, optInSignalPeriod, out  outBegIdx, out  outNBElement, outMACD, outMACDSignal, outMACDHist);
            List<macdres> resarr = new List<macdres>(endIdx - startIdx + 1);
            int counter=0;
            for (i = startIdx; i &lt; endIdx - startIdx + 1; i++)
            {
                Macdres macdres = new Macdres();
                macdres.Index = i;
                macdres.Macd = outMACD[counter];
                macdres.Signal = outMACDSignal[counter];
                macdres.MacdHistogram = outMACDHist[counter];
                resarr.Add(macdres);
                counter++;
            }
            listView1.ItemsSource = resarr;
        }
    }
}

输出数据对应图示:

</macdres></macdres></string[]></string[]></string[]></string[]>

暂无评论

Looks like your connection to 掘金量化社区 - 量化交易者的策略交流学习社区 was lost, please wait while we try to reconnect.