/// <summary> /// 交易服務建立員 /// </summary> /// <param name="instrument">商品資訊介面</param> /// <param name="args">交易服務組件設定參數</param> /// <param name="orderSource">交易服務來源名稱[預設:null](format: 交易組件名稱;交易服務類別名稱, null=使用預設交易服務來源類別名稱)</param> /// <param name="autoExecuteOnWork">是否由系統自動執行 OnWork 方法(預設=true, 如果為 false 則須由使用者自行呼叫 OnWork 方法)</param> protected void CreateTrader(IInstrument instrument, object args, string orderSource = null, bool autoExecuteOnWork = true) { bool bExist = false; string sSymbolId = instrument.Request.Symbol; lock (__cTradeServices) { bExist = __cTradeServices.ContainsKey(sSymbolId); } if (bExist) { //如果交易服務已經存在 SelectTrader(sSymbolId); //直接選擇交易服務 return; } int iDataStream = 1; if (__cTradeServices.Count > 0) { //如果為 0 則表示尚未建立任何一個下單交易服務(如果 > 0 則將建立的 instrument 加入至 instruments 陣列內, 加入陣列的資料才會跟著 Bars 資料同步 Move Current 位置) iDataStream = AddDataStream(instrument); } orderSource = (orderSource == null) ? __cProperty.OrderSource : orderSource; AbstractOrderService cOrderService = OrderManager.Manager.CreateOrderService(orderSource); cOrderService.onResponse += OrderService_onResponse; cOrderService.SetInstrument(instrument as Instrument, iDataStream); cOrderService.SetDefaultContracts(__cProperty.DefaultContracts); cOrderService.AutoExecuteOnWork = autoExecuteOnWork; //設定傭金或手續費 AbstractOrderService cBaseService = null; if (__cTradeServices.TryGetValue(this.Bars.Request.Symbol, out cBaseService)) { cOrderService.SetCommissions(cBaseService.Commissions); } if (args != null) { var cParameters = InputAttribute.GetParameters(cOrderService); if (cParameters.Count > 0) { var cProperies = args.GetType().GetProperties(); Dictionary<string, object> cArgs = new Dictionary<string, object>(cProperies.Length); foreach (var cProperty in cProperies) { cArgs.Add(cProperty.Name, cProperty.GetValue(args, null)); } foreach (var cParameter in cParameters) { object oValue = null; if (cArgs.TryGetValue(cParameter.Name, out oValue)) { cParameter.SetValue(oValue); } } } cOrderService.Initialize(); //初始化交易服務 } lock (__cTradeServices) { __cTradeServices.Add(sSymbolId, cOrderService); } if (log.IsInfoEnabled) log.InfoFormat("[SignalObject.CreateTrader] Set \"{0}\" and create... symbolId={1}", orderSource, sSymbolId); }
internal override void CStudyInitialize() { if (__cProperty.OrderSource != null) { IInstrument cBars0 = this.Bars; CreateTrader(cBars0, null); //建立交易服務元件 SelectTrader(cBars0.Request.Symbol); //選擇作用中的交易服務元件 } List<InputAttribute> cParameters = new List<InputAttribute>(); cParameters.AddRange(InputAttribute.GetParameters(this)); if (__cOrderService != null) { cParameters.AddRange(InputAttribute.GetParameters(__cOrderService)); } ScriptParametersEvent cScriptParametersEvent = new ScriptParametersEvent(); cScriptParametersEvent.SetOrderService(__cOrderService); cScriptParametersEvent.SetScriptParameters(cParameters); OnScriptParameters(cScriptParametersEvent); if (__cOrderService != null) { __cOrderService.Initialize(); //參數設定完成之後才初始化下單元件 } }