private static void SaveUserCode(UserCodeSet item) { FileStream fs = new FileStream(LASTCODEINFODIR + item.FileName, FileMode.OpenOrCreate); BinaryFormatter formatter = new BinaryFormatter(); if (item.IsZhuli) { Util.Log("序列化主力合约到文件" + item.FileName); } formatter.Serialize(fs, _DicUserCodeSetCodeInfo[item.Id]); fs.Close(); }
/// <summary> /// 添加一个合约组 /// </summary> /// <param name="name"></param> public static void Add(string name) { ObservableCollection <UserCodeSet> userCodeSetList = GetUserCodeSetList(); UserCodeSet userCodeSet = new UserCodeSet(); userCodeSet.Name = name; userCodeSet.Id = Guid.NewGuid().ToString(); userCodeSet.IsDefault = false; userCodeSet.FileName = Guid.NewGuid() + ".dat"; userCodeSet.IsArbitrage = false; userCodeSetList.Add(userCodeSet); _DicUserCodeSetCodeInfo.Add(userCodeSet.Id, new ObservableCollection <Contract>()); }
/// <summary> /// 添加主力合约板块,代码暂不添加 /// </summary> private static void AddZhuliCodeTemp(ObservableCollection <UserCodeSet> userCodeSets) { if (_IsZlRequested == true) { return; } //如果已经存在主力合约板块了,则返回 string guid = System.Guid.NewGuid().ToString(); UserCodeSet userCodeSet = GetZhuliCodeUserCodeSet(); if (userCodeSet != null) { guid = userCodeSet.Id; } else { userCodeSet = new UserCodeSet(); userCodeSet.IsArbitrage = false; //非套利合约组 userCodeSet.IsDefault = true; //是默认的,不可被删除 userCodeSet.IsZhuli = true; //是主力合约 userCodeSet.Id = guid; userCodeSet.Name = "主力合约"; userCodeSet.FileName = "ZhuliCode.dat"; ObservableCollection <Contract> codeInfoList = new ObservableCollection <Contract>(); _DicUserCodeSetCodeInfo.Add(guid, codeInfoList); //SaveUserCode(userCodeSet); _UserCodeSetList.Insert(2, userCodeSet); //此时应该将userCodeSetList写回到文件中 CommonUtil.SaveObjectToFile(_UserCodeSetList, ConfigFilePath); } //开一个线程请求主力合约 Thread t = new Thread(ZhuliCodeThread); t.IsBackground = true; t.Start(guid); }
/// <summary> /// 从配置文件中读取合约组信息 /// </summary> /// <returns></returns> public static ObservableCollection <UserCodeSet> GetUserCodeSetList() { if (_UserCodeSetList == null) { if (File.Exists(ConfigFilePath)) { try { //先从配置文件中读取合约组信息 _UserCodeSetList = (ObservableCollection <UserCodeSet>)(CommonUtil.RecoverObjectFromFile(typeof(ObservableCollection <UserCodeSet>), ConfigFilePath)); } catch (System.Exception ex) { Util.Log(ex.ToString()); } } if (_UserCodeSetList == null) { _UserCodeSetList = new ObservableCollection <UserCodeSet>(); UserCodeSet defaultSet = new UserCodeSet(); defaultSet.Name = "普通行情"; defaultSet.Id = Guid.NewGuid().ToString(); defaultSet.IsDefault = true; defaultSet.FileName = LASTCODEINFOPATH; defaultSet.IsArbitrage = false; _UserCodeSetList.Add(defaultSet); UserCodeSet GroupSet = new UserCodeSet(); GroupSet.Name = "组合行情"; GroupSet.Id = Guid.NewGuid().ToString(); GroupSet.IsDefault = true; GroupSet.FileName = LASTGROUPCODEINFOPATH; GroupSet.IsArbitrage = true; _UserCodeSetList.Add(GroupSet); } //再读取各合约组的合约情况 foreach (var item in _UserCodeSetList) { if (File.Exists(LASTCODEINFODIR + item.FileName)) { ObservableCollection <Contract> userCodes = new ObservableCollection <Contract>(); try { FileStream fs2 = new FileStream(LASTCODEINFODIR + item.FileName, FileMode.OpenOrCreate); BinaryFormatter formatter = new BinaryFormatter(); ObservableCollection <Contract> temp = (ObservableCollection <Contract>)formatter.Deserialize(fs2); foreach (Contract codeInfo in temp) { if (codeInfo != null && !CodeSetManager.IsOutOfDate(codeInfo.Code)) { userCodes.Add(codeInfo); } } fs2.Close(); } catch (System.Exception ex) { Util.Log_Error("exception: " + ex.Message); Util.Log_Error("exception: " + ex.StackTrace); } //要对userCodes进行处理,保证无重复的内容。 Dictionary <Contract, Boolean> codeDict = new Dictionary <Contract, bool>(); for (int i = 0; i < userCodes.Count; i++) { if (userCodes[i] != null) { if (codeDict.ContainsKey(userCodes[i])) { userCodes.RemoveAt(i); i -= 1; } else { codeDict.Add(userCodes[i], true); } } } while (userCodes.Count > USER_CODE_SET_MAX_CODES && item.IsZhuli == false) { userCodes.RemoveAt(userCodes.Count - 1); } if (item.IsZhuli) { Util.Log("添加UserCodes到主力合约: userCodes的个数为" + userCodes.Count.ToString()); foreach (Contract codeInfo in userCodes) { if (codeInfo != null) { //Util.Log("--CodeInfo为:" + codeInfo.ToString()); } else { //Util.Log("--CodeInfo为null"); } } } _DicUserCodeSetCodeInfo.Add(item.Id, userCodes); } else { _DicUserCodeSetCodeInfo.Add(item.Id, new ObservableCollection <Contract>()); } } } //AddZhuliCodeTemp(userCodeSetList); return(_UserCodeSetList); }