如何读取配置和保存配置?

虽然TradeStation提供全局字典可以在不同APP、策略之间共享键值对,但是这些共享的属性在平台关闭后会被释放。一般而言,保存配置与读取配置的过程只不过是将配置信息在保存时写入到文件,在读取时是从该文件中读取配置信息。

示例1:使用文件保存配置信息

using guosen;
using elsystem.io;
using elsystem.collections;

vars:string ConfigPath("");
method void AnalysisTechnique_Initialized( elsystem.Object sender, elsystem.InitializedEventArgs args ) 
vars:int loop,Vector vec;
begin
    ConfigPath = elsystem.Environment.GetMyWorkDirectory()+"configure_test.txt";
    print("ConfigPath is: "+ ConfigPath);
    vec = ReadConfig(ConfigPath); //
    if(vec=null)then
    Begin
        print("Read configure error.");
    End
    Else
    Begin
        for loop = 0 to vec.Count-1
        Begin
            print("Config"+numtostr(loop+1,0)+":"+vec[loop].ToString());
        End;
    End;    
end;


method void AnalysisTechnique_UnInitialized( elsystem.Object sender, elsystem.UnInitializedEventArgs args ) 
begin
    SaveConfig(ConfigPath);
end;

//从path读取配置文件
Method vector ReadConfig(string path)
vars:StreamReader sr,string line,vector vec_config;
Begin
    vec_config = new Vector;
    Try
        sr = StreamReader.Create(path); //创建读入流
        line = sr.ReadLine(); //读取一行
        if(line.Trim().Equals(""))then
        Begin
            print("File content is empty");//读取的文本为空字符串
            return null;
        End
        Else
            vec_config = line.Split(","); //以逗号为分隔拆分
        Return vec_config;
    catch(elsystem.Exception ex)
        print("File read exception. Not found or locked.");
        Return null;
    End;
End;

//保存配置信息到path
Method void SaveConfig(string path)
vars:StreamWriter sw,string configure;
Begin
    configure = "I'm configure.," + elsystem.DateTime.Now.ToString(); //自定义的配置文件: config,DateTime
    Try
        sw = StreamWriter.Create(path);//创建写入流
        sw.Write(configure);
        if(sw<>null)then
        Begin
            sw.Close();//关闭写入流
        End;
    catch(elsystem.Exception ex)
        print("Configure save failed.");
    End;
End;
//打印语句输出栏结果示例
//2016/12/6 13:22:53 Config saved.
//ConfigPath is: C:\Program Files (x86)\TradeStation 9.5\MyWork\configure_test.txt
//Config1:I'm configure.
//Config2:2016/12/6 13:22:53
Copyright © 1998-2018 国信证券股份有限公司 版权所有发布时间: 2018-05-14 14:58:15

results matching ""

    No results matching ""