private const int MAX_LOAD_OPTIONS_COUNT = 20; //最大讀取選擇權歷史資料個數 #endif #if __BACKTEST private static void DumpReport(AbstractOrderService orderService1, AbstractOrderService orderService2, List<double[]> twiCloses) { StringBuilder cBuilder = new StringBuilder(1024 * 1024); int iCount = orderService1.Positions.Count; if (iCount > 0) { PositionSeries cPositions1 = orderService1.Positions; PositionSeries cPositions2 = orderService2.Positions; string sSymbolId1 = orderService1.Bars.Request.Symbol; string sSymbolId2 = orderService2.Bars.Request.Symbol; for (int i = 0; i < iCount; i++) { IMarketPosition cPosition1 = cPositions1[i]; IMarketPosition cPosition2 = cPositions2[i]; List<ITrade> cTrades1 = cPosition1.ClosedTrades; foreach (ITrade cTrade in cTrades1) { ITradeOrder cOpenO = cTrade.EntryOrder; ITradeOrder cCloseO = cTrade.ExitOrder; cBuilder.Append(cOpenO.Ticket).Append(',').Append(twiCloses[int.Parse(cOpenO.Ticket)][0]).Append(',').Append(twiCloses[int.Parse(cOpenO.Ticket)][1]).Append(',').Append(twiCloses[int.Parse(cOpenO.Ticket)][2]).Append(',').Append(twiCloses[int.Parse(cOpenO.Ticket)][3]).Append(',').Append(sSymbolId1).Append(',').Append(cOpenO.Category).Append(',').Append(cOpenO.Action).Append(',').Append(cOpenO.Contracts).Append(',').Append(cOpenO.Price).Append(',').Append(string.Empty).Append(',').Append(cOpenO.Fee).Append(',').Append(cOpenO.Tax).Append(',').Append(cOpenO.Time.ToString("yyyy/MM/dd HH:mm(ddd)")).Append(',').AppendLine(cOpenO.Name); cBuilder.Append(cCloseO.Ticket).Append(',').Append(twiCloses[int.Parse(cCloseO.Ticket)][0]).Append(',').Append(twiCloses[int.Parse(cCloseO.Ticket)][1]).Append(',').Append(twiCloses[int.Parse(cCloseO.Ticket)][2]).Append(',').Append(twiCloses[int.Parse(cCloseO.Ticket)][3]).Append(',').Append(sSymbolId1).Append(',').Append(cCloseO.Category).Append(',').Append(cCloseO.Action).Append(',').Append(cCloseO.Contracts).Append(',').Append(cCloseO.Price).Append(',').Append(cTrade.Profit).Append(',').Append(cCloseO.Fee).Append(',').Append(cCloseO.Tax).Append(',').Append(cCloseO.Time.ToString("yyyy/MM/dd HH:mm(ddd)")).Append(',').AppendLine(cCloseO.Name); } List<ITrade> cTrades2 = cPosition2.ClosedTrades; foreach (ITrade cTrade in cTrades2) { ITradeOrder cOpenO = cTrade.EntryOrder; ITradeOrder cCloseO = cTrade.ExitOrder; cBuilder.Append(cOpenO.Ticket).Append(',').Append(twiCloses[int.Parse(cOpenO.Ticket)][0]).Append(',').Append(twiCloses[int.Parse(cOpenO.Ticket)][1]).Append(',').Append(twiCloses[int.Parse(cOpenO.Ticket)][2]).Append(',').Append(twiCloses[int.Parse(cOpenO.Ticket)][3]).Append(',').Append(sSymbolId2).Append(',').Append(cOpenO.Category).Append(',').Append(cOpenO.Action).Append(',').Append(cOpenO.Contracts).Append(',').Append(cOpenO.Price).Append(',').Append(string.Empty).Append(',').Append(cOpenO.Fee).Append(',').Append(cOpenO.Tax).Append(',').Append(cOpenO.Time.ToString("yyyy/MM/dd HH:mm(ddd)")).Append(',').AppendLine(cOpenO.Name); cBuilder.Append(cCloseO.Ticket).Append(',').Append(twiCloses[int.Parse(cCloseO.Ticket)][0]).Append(',').Append(twiCloses[int.Parse(cCloseO.Ticket)][1]).Append(',').Append(twiCloses[int.Parse(cCloseO.Ticket)][2]).Append(',').Append(twiCloses[int.Parse(cCloseO.Ticket)][3]).Append(',').Append(sSymbolId2).Append(',').Append(cCloseO.Category).Append(',').Append(cCloseO.Action).Append(',').Append(cCloseO.Contracts).Append(',').Append(cCloseO.Price).Append(',').Append(cTrade.Profit).Append(',').Append(cCloseO.Fee).Append(',').Append(cCloseO.Tax).Append(',').Append(cCloseO.Time.ToString("yyyy/MM/dd HH:mm(ddd)")).Append(',').AppendLine(cCloseO.Name); } } File.AppendAllText("report.csv", cBuilder.ToString(), Encoding.UTF8); } }
/// <summary> /// 建構子 /// </summary> /// <param name="service">AbstractOrderService 下單服務抽象類別</param> /// <param name="args">下單參數</param> internal OrderPriced(AbstractOrderService service, SOrderParameters args) { this.ID = service.GetOrderID(); Contracts cContract = args.Lots; this.Info = new Order(args.Name, args.Action, OrderCategory.Limit, (cContract.IsDefault) ? service.DefaultContracts : args.Lots, false, args.ExitTypeInfo); __cSender = service as IOrderSender; __cPositions = service.Positions; }
/// <summary> /// 建構子 /// </summary> /// <param name="service">AbstractOrderService 下單服務抽象類別</param> /// <param name="args">下單參數</param> /// <param name="openNextBar">是否開倉於下一根 Bars</param> internal OrderMarket(AbstractOrderService service, SOrderParameters args, bool openNextBar) { this.ID = service.GetOrderID(); Contracts cContract = args.Lots; this.Info = new Order(args.Name, args.Action, OrderCategory.Market, (cContract.IsDefault) ? service.DefaultContracts : args.Lots, openNextBar, args.ExitTypeInfo); __cSender = service as IOrderSender; __cPositions = service.Positions; }
internal override void CStudyInitialize() { if (__cProperty.OrderSource != null) { if (__cOrderService != null) { //如果之前已經建立下單服務元件 __cOrderService.Dispose(); //釋放下單服務元件 } string sOrderSource = __cProperty.OrderSource; __cOrderService = OrderManager.Manager.CreateOrderService(sOrderSource); __cOrderService.onResponse += OrderService_onResponse; __cOrderService.SetInstrument(this.Instruments[0]); __cOrderService.SetDefaultContracts(__cProperty.DefaultContracts); this.OrderCreator = __cOrderService as IOrderCreator; if (log.IsInfoEnabled) log.InfoFormat("[SignalObject.SetOrderSource] Set \"{0}\" and create...", sOrderSource); } 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(); //參數設定完成之後才初始化下單元件 } }
protected override void Create() { // 初始化下單物件,Contracts.UserSpecified 可指定規模,OrderExit.FromAll 可一次全平 多單 = OrderCreator.MarketThisBar(new SOrderParameters(Contracts.UserSpecified, EOrderAction.Buy)); 空單 = OrderCreator.MarketThisBar(new SOrderParameters(Contracts.UserSpecified, EOrderAction.SellShort)); 多沖 = OrderCreator.MarketThisBar(new SOrderParameters(Contracts.Default, EOrderAction.Sell, OrderExit.FromAll)); 空沖 = OrderCreator.MarketThisBar(new SOrderParameters(Contracts.Default, EOrderAction.BuyToCover, OrderExit.FromAll)); __cService = OrderCreator as AbstractOrderService; __cTimer = new Timer(1); __cTimer.AutoReset = false; __cTimer.Elapsed += Timer_onElapsed; //__cTimer.Start(); //bbb = BarsOfData(2); cBuilder.AppendLine("[F000]"); __cKDSum = new VariableSeries<double>(this); __cMACD = new MACD(this); __cMACD.FastPeriod = 5; __cMACD.SlowPeriod = 10; __cMACD.MACDPeriod = 10; __cKD = new KD(this); __cKD.Length = 5; }
private void CreateOrderService(Instrument buyBars, Instrument sellBars) { if(__cOrderService1 != null) { __cOrderService1.Dispose(); } #if __SIMULATE __cOrderService1 = OrderManager.Manager.CreateOrderService("Netwings.OrderService;Netwings.SimulateOrderService"); #else __cOrderService1 = OrderManager.Manager.CreateOrderService("Netwings.OrderService;Netwings.RealOrderService"); SetPipeNumber(InputAttribute.GetParameters(__cOrderService1), 1, 1); #endif __cOrderService1.onResponse += OrderService_onResponse; __cOrderService1.SetInstrument(buyBars); __cOrderService1.SetDefaultContracts(1); __cOrderService1.Initialize(); IOrderCreator cOrderCreator1 = __cOrderService1 as IOrderCreator; __cBOrderO = cOrderCreator1.MarketThisBar(new SOrderParameters(Contracts.UserSpecified, "CALL_BUY", EOrderAction.Buy, OrderExit.FromAll)); __cBOrderC = cOrderCreator1.MarketThisBar(new SOrderParameters(Contracts.Default, "CALL_CLOSE", EOrderAction.Sell, OrderExit.FromAll)); if (log.IsInfoEnabled) log.Info("[CreateOrderService] Set BUY \"Netwings.OrderService;Netwings.SimulateOrderService\" and create..."); if(__cOrderService2 != null) { __cOrderService2.Dispose(); } #if __SIMULATE __cOrderService2 = OrderManager.Manager.CreateOrderService("Netwings.OrderService;Netwings.SimulateOrderService"); #else __cOrderService2 = OrderManager.Manager.CreateOrderService("Netwings.OrderService;Netwings.RealOrderService"); SetPipeNumber(InputAttribute.GetParameters(__cOrderService2), 1, 2); #endif __cOrderService2.onResponse += OrderService_onResponse; __cOrderService2.SetInstrument(sellBars); __cOrderService2.SetDefaultContracts(1); __cOrderService2.Initialize(); IOrderCreator cOrderCreator2 = __cOrderService2 as IOrderCreator; __cSOrderO = cOrderCreator2.MarketThisBar(new SOrderParameters(Contracts.UserSpecified, "PUT_SELL", EOrderAction.SellShort, OrderExit.FromAll)); __cSOrderC = cOrderCreator2.MarketThisBar(new SOrderParameters(Contracts.Default, "PUT_CLOSE", EOrderAction.BuyToCover, OrderExit.FromAll)); if (log.IsInfoEnabled) log.Info("[CreateOrderService] Set SELL \"Netwings.OrderService;Netwings.SimulateOrderService\" and create..."); }
/// <summary> /// 設定下單服務元件(需要設定佣金參數, 所以必須傳出) /// </summary> /// <param name="service">下單服務元件</param> internal void SetOrderService(AbstractOrderService service) { __cOrderService = service; }