请教关于wealthlab5 Static Data Adapters 的问题

Discussion in 'Wealth-Lab Developer' started by 饕餮2008, Mar 5, 2010.

  1. 最近遇到了一个问题颇头疼。
    我用wealth-lab做股票的筛选,由于数据源是TXT的,每次光数据读入就花费很多时间,让人有些不耐烦。即使用了cache功能,速度也没见提高多少(而且第一次用的时候速度还会更慢),于是想编写一个专用的Static Data Adapters,但是在网上找了半天,没有这方面的API,不知道哪位前辈能指点一二,不胜感激。
     
  2. Creating Static Data Adapters for Wealth-Lab.NET
    Introduction Back to Development Guide

    This document explains how to create new static data adapters for Wealth-Lab.Net. A static data adapter allows Wealth-Lab to load, chart, and backtest historical bar/volume data. The static data adapter itself is a .Net class that derives from the StaticDataProvider base class, and resides in a .Net library assembly (dll).
    To build a Static Data Adapter, perform the following steps:
    • Create a Class Library project in Visual Studio that will contain one or more Static Data Adapters.
    • Add a reference to the WealthLab.dll assembly in your project's References section. This assembly contains classes in the WealthLab namespace.
    • Create one or more classes that derive from the StaticDataProvider base class, that resides in the WealthLab namespace.
    To test your Static Data Adapter, do the following:
    • Drop your Static Data Adapter Assembly (created above) into the Wealth-Lab Pro installation folder.
    • Execute Wealth-Lab Pro.
    • Select "New DataSet" from the "Data" menu.
    • Your adapter should now be visible in the list of Data Providers that users can select when creating a new DataSet.
    StaticDataProvider Base Class
    All static data adapters derive from the StaticDataProvider base class. This class provides a number of abstract methods that must be overridden in your adapter. It also contains a number of optional virtual methods that you can override to support additional functionality.
    StaticDataProvider also implements the IWizard interface, so you must implement the IWizard methods in your static data adapter class. The IWizard interface provides a way for your adapter to interact with the New DataSet Wizard, and provide user interface pages that the user can move through to define the properties of new DataSets created based on the data provider you are supporting.
    Initialization
    Your static data adapter class is initialized by Wealth-Lab after it is created. Wealth-Lab calls the Initialize method to allow you to perform initialization.
    public virtual void Initialize(IDataHost dataHost)
    In the Initialize method you should perform any one-time initialization that your adapter needs. Note that Wealth-Lab will create multiple instances of your adapter, and each instance will have its Initialize method called after it is created. The dataHost parameter contains an instance of an IDataHost interface (IDataHost documentation page) whose methods and properties you can use during the initialization. You can also access the IDataHost interface from any of your adapter's other methods by using the DataHost property that comes with StaticDataProvider.
    Important: If you override Initialize, be sure to call the base implementation in your overridden method.
    Descriptive Method Overrides
    Override the following methods/properties of StaticDataProvider to provide descriptive information about the data provider you are supporting.
    public abstract string FriendlyName
    Return the "name" of the data provider that you are supporting. This name appears in the New DataSet Wizard list of available data providers.
    public abstract string Description
    Return a string describing the data provider. This appears when the user clicks your data adapter in a list.
    public abstract Bitmap Glyph
    Return a 16x16 bitmap representing the data provider. Use Fuchsia as the transparent color of your bitmap.
    public virtual string URL
    Return a URL that points to a web page that describes the data provider. This property is optional, and you do not need to override this if you do not have an appropriate web page.
    Creating a New DataSet
    Wealth-Lab uses the concept of a "DataSet" to represent a set of historical data from a specific data provider. (Note: in the internal Wealth-Lab framework, the terms DataSet and DataSource are interchangeable.) Wealth-Lab Pro users use a New DataSet Wizard to select a data provider, and then click Next to advance to Wizard pages that are specific to each data provider. In these data-provider-specific pages the users specify whatever information they need in order to define the DataSet. Each data provider will require different information. Some providers might only require that the user enter a list of symbols, other providers might require that the user navigate to a folder on their computer.
    Your static data adapter must communicate with the Wizard through the methods of the IWizard interface. These methods are described below:
    UserControl WizardFirstPage()
    You should return an instance of a UserControl-derived class that contains the user interface for the first page of the data adapter's New DataSet Wizard. Your Wizard page UserControls should be 560 pixels wide. The height of the pages are not important, the Wizard will create scrollbars if the pages are too high.
    UserControl WizardNextPage(UserControl currentPage)
    You should return an instance of a UserControl-derived class that represents the next page of the New DataSet Wizard. This is the page that should come immediately after the "currentPage" that is provided as a parameter. If the currentPage is actually the last page, return null.
    If there are user data entry errors on the current Wizard page that should prevent the user from advancing to the next page, you should throw a WizardValidationException at this point.
    UserControl WizardPreviousPage(UserControl currentPage)
    Return the instance of a UserControl-derived class that represents the previous Wizard page, the page immediately prior to the one passed as the currentPage parameter. You can expect this method to only be called after the user has advanced through at least one page in your set of Wizard pages.
    Creating the DataSet
    After the user completed the New DataSet Wizard, Wealth-Lab will call the CreateDataSource method in your adapter. This is Wealth-Lab asking you to create an instance of a DataSource object (that represents a Wealth-Lab DataSet) describing the DataSet that the user defined in the Wizard pages above.
    public abstract DataSource CreateDataSource();
    The DataSource object represents what users of Wealth-Lab Pro know as a DataSet. You should create a new DataSource instance, and set the properties below based on the information that the user entered on your Wizard pages.
    Note!
    This public constructor for CreateDataSource is parameterless so that it can be serialized. However, you must pass this (your StaticDataProvider object) to CreateDataSource when creating the DataSource object.
    Example: DataSource ds = new DataSource(this);
    public BarScale Scale
    The data scale of the historical data contained in the DataSet. Possible values are:
    • BarScale.Daily
    • BarScale.Weekly
    • BarScale.Monthly
    • BarScale.Quarterly
    • BarScale.Yearly
    • BarScale.Minute
    • BarScale.Second
    • BarScale.Tick
    public int BarInterval
    For intraday scales, represents the bar interval of the data contained in the DataSet.
    public string DSString
    The "DSString" should contain all of the information that you require to re-construct the DataSet internally in your adapter. This might include a directory, a list of symbols delimited by commas, or any other data entry elements that the user entered into your Wizard pages.
    public virtual string SuggestedDataSourceName
    Here you can optionally assign a suggested name for the new DataSet. The New DataSet Wizard will pre-populate the name entry field with this value, but will allow the user to change the suggested name.
    Returning the Symbols
    Wealth-Lab will periodically ask your adapter to return the list of symbols that are contained in a DataSet. You should use the DSString to determine the symbols that are contained in the DataSet, and return this information to Wealth-Lab.
    public abstract void PopulateSymbols(DataSource ds, List<string> symbols)
    Populate the symbols contained in the specified DataSource object (DataSet) in the List<string> provided in the symbols parameter.
    Returning Historical Data
    Your adapter's main job is returning historical bar/volume data from the data provider that you are supporting. Historical data is returned by the RequestData method, in the form of a new Bars object that you create. Use the Bars.Add method to add new bars of data, including open, high, low, close and volume values.
    When you are handling the RequestData method, you can at this time support Wealth-Lab's "data update on demand" feature, if your data provider makes this possible. Use the DataHost property that is contained in the StaticDataProvider base class, and examine its OnDemandUpdateEnabled property. If true, you can at this time check to see if the symbol requested requires a data update, and perform this update on the fly before returning the updated data to Wealth-Lab.
    public abstract Bars RequestData(DataSource ds, string symbol, DateTime startDate, DateTime endDate, int maxBars, bool includePartialBar)
    Wealth-Lab calls RequestData when it wants historical data from your adapter. You will return the historical data as a new instance of a Bars object. Before adding actual bars to the Bars instance that you create, you can optionally set its SecurityName and SecurityType properties to describe the symbol that was requested in more detail.
    The following parameters provide more information about the data being requested:
    DataSource ds
    This is the DataSource object that represents a Wealth-Lab DataSet. This object was originally created by your adapter during the CreateDataSource call above. It contains the DSString property that should allow you to access all of the information that you need to load the data from the historical provider you are supporting. It also contains the Scale and BarInterval properties that tell you what data scale that you should return.
    string symbol
    This is the symbol being requested.
    DateTime startDate
    The start date of the data range being requested. If there is no explicit start date, this parameter will contain DateTime.MinValue.
    DateTime endDate
    The end date of the data range being requested. If there is no explicit end date, this parameter will contain DateTime.MaxValue.
    int maxBars
    The maximum number of bars that should be returned, starting at the most recent bar and working backward. A value of zero indicates that all data should be returned. If startDate and/or endDate are specified, maxBars will always contain zero.
    bool includePartialBar
    If this parameter is true, and the market is currently open, you should include the current, partial, bar of data that represents the current realtime bar (if available). If this parameter is false you should make sure to exclude the partial bar if it is available.
    public virtual void CheckConnectionWithServer()
    Wealth-Lab calls this method in certain cases before it attempts to download data. You should implement this method by making a simple, quick, call to the back end server to check connectivity. Let any exceptions that occur just bubble through. For example, request a single quote or one bar of data from the back end server for symbol QQQQ or some other common symbol.
    Supporting Different Scales
    If your adapter has the capability of dynamically returning ad-hoc symbol data for specific data scales, you can use the method below to indicate this fact to Wealth-Lab. This allows Wealth-Lab to keep these data scale buttons enabled on the Wealth-Lab Pro toolbar even if the DataSet that is currently selected cannot be compressed down to that scale. For example, if your adapter can return ad-hoc minute-based data and the user is on a Daily DataSet, the Minute scale button will remain enabled. If pressed, Wealth-Lab will produce a new DataSource object on the fly that contains the desired scale, and pass this to you in the RequestData method. Note that this DataSource object will not have anything in its DSString property. This means that your adapter must not be dependant on the DSString in order to obtain and return historical data.
    public abstract bool SupportsDynamicUpdate(BarScale scale);
    Return true if your adapter supports dynamic update as described above for the specified data scale.
    Updating Data
    The following methods provide hooks into different data and DataSet update capabilities in Wealth-Lab. You can indicate whether your adapter supports these features, and implement them in the methods below.
    public virtual bool CanModifySymbols
    Return true if your adapter allows for the symbols defined in a DataSet to be changed, either in the Wealth-Lab Pro Data Manager, or through various right click options.
    public virtual string ModifySymbols(DataSource ds, List<string> symbols)
    Wealth-Lab will call this method in your adapter after the user has modified the symbols in a DataSet. You are passed the DataSource object instance that represents the DataSet, and the new list of symbols that it should contain. You should re-build the DSString for the DataSet and return this as the return value.
    public virtual bool SupportsDataSourceUpdate
    Return true if your adapter supports updating of a complete DataSet from the Data Manager.
    public virtual void UpdateDataSource(DataSource ds, IDataUpdateMessage dataUpdateMsg)
    Wealth-Lab calls this method in your adapter when the users updates a DataSet based on your adapter in the Data Manager. You are passed the DataSource object that represents the DataSet that should be updated, and an instance of an IDataUpdateMessage interface (see below) that you should use to pass information about the update back to Wealth-Lab.
    public virtual bool SupportsProviderUpdate
    Return true if your adapter supports the "Provider update" action in the Wealth-Lab Pro Data Manager. The Provider update action is intended to update all of the data maintained by your adapter at once.
    public virtual void UpdateProvider(IDataUpdateMessage dataUpdateMsg, List<DataSource> dataSources, bool updateNonDSSymbols, bool deleteNonDSSymbols)
    Wealth-Lab calls this method in your adapter when the user has selected to perform a "Provider update" and your static data adapter was included in this update. Here you should update all of the data maintained by your adapter. Use the IDataUpdateMessage instance (see below) to communicate the update status back to Wealth-Lab as it proceeds. The dataSources parameter contains a list of DataSource objects that represent the DataSets that should be included in the update. If the updateNonDSSymbols parameter is true, you should also update other symbols that were created by your adapter, even if they do not exist in any DataSets. Conversely, if deleteNonDSSymbols is true, you should delete any symbols that your adapter created that do not currently exist in any DataSets.
    public virtual void CancelUpdate()
    Wealth-Lab will call this method in your adapter to signal that the user has selected to cancel any data update operations that are in progress.
    public virtual bool CanDeleteSymbolDataFile
    Return true if your provider has the concept of "deleting" the historical data for a particular symbol.
    public virtual void DeleteSymbolDataFile(DataSource ds, string symbol)
    Delete the historical data file for the specified symbol, in the scale that is represented in the DataSource.Scale and DataSource.BarInterval properties.
    public virtual bool CanEditSymbolDataFile
    Return true if your provider can save changes to a Bars object that have been introduced in a place such as the Wealth-Lab Pro Data Manager tool. Currently the Data Manager can apply stock split adjustments, but future versions might introduce the ability to directly edit data on a bar by bar basis.
    public virtual void SaveEditedSymbolDataFile(DataSource ds, Bars bars)
    Save the Bars object that is passed here, it is the result of an edit that occurred in a tool such as the Wealth-Lab Pro Data Manager.
    Requesting Bars objects that contain updated data only
    This facility is used by the Strategy Monitor tool in Wealth-Lab Pro, which requests only updated data for symbols and adds this data the the Bars objects it maintains internally, without saving these changes to disk. This allows the SM tool to operate more efficiently instead of constantly reading and writing large amounts of data to disk. The ramification here is that only providers that support this facility will be able to be used in the Strategy Monitor.
    public virtual bool CanRequestUpdates
    Return true to indicate that the provider supports the facility.
    IDataUpdateMessage interface
    Instances of this interface are passed by Wealth-Lab during the UpdateDataSource and UpdateProvider methods. They are intended to allow you to communicate the state of the update back to Wealth-Lab.
    void DisplayUpdateMessage(string message);
    Pass a string back to Wealth-Lab indicating that a certain symbol was updated, or some other action occurred during the update process.
    void ReportUpdateProgress(int progressPercent);
    Report the overall progress of the update back to Wealth-Lab.
    BarDataStore Class
    If your static data adapter needs to download data and maintain its own local history of bar/volume data, you should consider using the BarDataStore class, which provide a built-in mechanism to achieve this functionality. BarDataStore automatically maintains a file-based historical database using sub-folders for data scale, and further sub-folders based on the first letter of the symbol. Important BarDataStore methods and properties are described below:
    public BarDataStore(IDataHost dataHost, StaticDataProvider provider)
    The constructor creates a new BarDataStore instance. It uses the IDataHost.BaseDataFolder as its root folder, then creates a subfolder to store the historical data based on the class name of the StaticDataProvider instance supplied in the provider parameter (pass this).
    public void SaveBarsObject(Bars bars)
    Saves the specified Bars object in the data store.
    public bool ContainsSymbol(string symbol, BarScale scale, int barInterval)
    Returns whether the specified symbol with the specified scale currently exists in the data store.
    public void LoadBarsObject(Bars bars, DateTime startDate, DateTime endDate, int maxBars)
    Populates the Bars object passed as a parameter with the data contained in the data store, using the start/end date ranges and maximum number of bars parameters.
    public void LoadBarsObject(Bars bars)
    Loads all of the data for the specified Bars object from the data store. This is useful when data needs to be updated, and you must load all data, apply an update, and then call SaveBarsObject to save the updated historical data.
    public override DateTime SymbolLastUpdated(string symbol, BarScale scale, int barInterval)
    Returns the date/time that the data store was last updated for the specified symbol and scale. Uses special logic to return the last date/time actually contained in the Bars object if this data is not current as of the most recent market close. Use this method to determine if a symbol needs to be updated "on demand".
     
  3. 很多api 以前官方曾公布过,后来一些原因又撤掉了。现在又公布了一些api,那个 wealthlab5 Static Data Adapters api 估计也会很快公布吧。

    有一个方法,你只要把数据源是TXT放到一个文件夹,然后用wealth-lab导入,以后你只要更新那个文件夹里的TXT就可以了,你可以用程序把TXT复制到那个文件夹,这样能实现和Static Data Adapters 一样的功能了。
     
  4. 不知楼主说的“慢”是2秒?还是5秒?,版主的方法估计输出在3秒左右(全部品种)。对股票来说不慢了。
     
  5. 非常感谢各位的指点。
    我的操作是这样的,我是针对股票的30分钟K线做些运算,期间需要将30分钟周期切换到日线周期和周线周期,每个股票我本来想弄几年的30分钟K线数据,但是发现那样慢的实在受不了,现在就弄了1年的30分钟数据,1600多只股票,还有一些能交易的基金等,总数也不少了(在目录中这些TXT的文本有几百兆),我为了验证我的一些猜想,我会基本上一个时间段一个时间的段的筛选,每次都要等(我的机器是P4 2.0的,机器是老了些)。
    本人知道C#语言你无法指望其能和编译语言一样快,但是已经比以前的解释型Pascal速度快不少了(在进行参数优化时快了几倍),目前比较大量的时间是花在I/O上了,尤其是进行数据转换的时候,文本数据转换到二进制数据较耗费时间,真正的运算实际上时间花的不多,所以想搞一个自己的Data Adapter,直接读取二进制数据,这样理论上会快不少
    再一次感谢大家的指点
     
  6. 这位兄台,您是如何做的能不能指点一二?要是有简单的方法我也不想自己编程序了,谢谢
     
  7. 忘记说了,我的数据是从飞狐里导出的,飞狐只能导出5分钟的数据,所以数据量特别庞大,也许编个小程序自己将5分钟数据合并成30分钟数据也是一种办法,呵呵
     
  8. 把股票的个数限制一下,比如深沪300成分股,导入所有股票未必有意义!
     
  9. 到官方主页上看, 接口是coming soon的. 网络上能找到WLD4.2的接口, 但是WLD5不能用
    哪位有WLD5的, 共享出来造福大伙吧