internal void EndTunnel() { if (TunnelStatus == enTunnelStatus.Running) { foreach (var tp in tmBot.Positions.OrderByDescending(x => x.NetProfit)) { tmBot.ClosePosition(tp); } foreach (var to in tmBot.PendingOrders) { tmBot.CancelPendingOrder(to); } TunnelChartObjects.RemoveAll(tmBot); } NetLossBucket = 0; LastVolume = tmBot.LotToVolume(tmBot.config.StartingLotSize); if (TunnelStatus != enTunnelStatus.Inactive) { TunnelStatus = enTunnelStatus.Inactive; } }
internal void EndTunnel() { if (TunnelStatus != enTunnelStatus.Inactive) { //Positions closing if (tmPositions.Count().IsZero()) { tmBot.Print("No position to close."); if (NetLossBucket.IsNegative()) { tmBot.Print("Net Loss Bucket: {0:#,##0.00} will be brought over to the next session date.", NetLossBucket); } } else { var closingPosIDs = new List <int>(); //to feed stuff so that the code below can only execute after all been closed closingPosIDs.AddRange(PositionsToClose.Select(x => x.Id)); foreach (var tp in PositionsToClose) { //tmBot.ClosePositionAsync(tp, AfterEndTunnelPosition); tmBot.ClosePositionAsync(tp, poscloseResult => { if (poscloseResult.IsSuccessful) { closingPosIDs.RemoveAll(x => x == poscloseResult.Position.Id); ActualProfit += poscloseResult.Position.NetProfit; tmBot.Print("Temporary Actual Profit: {0:#,##0.00}.", ActualProfit); if (closingPosIDs.Count.IsZero()) //when there's no more position on the line { tmBot.Print("No more positions, Final Actual Profit: {0:#,##0.00}.", ActualProfit); if (UsesAnySmartBucket) { if (IsSmartBucketAcrossCycle && ActualProfit.IsNegative()) { NetLossBucket = ActualProfit; tmBot.Print("Net Loss Bucket: {0:#,##0.00} will be brought over to the next session date.", NetLossBucket); } else { NetLossBucket = 0; tmBot.Print("Net Loss Bucket resets to zero (0)."); } } ActualProfit = 0; //reset the Actual Profit } } }); //tmBot.ClosePosition(tp); } } //Pending Orders closing if (tmBot.PendingOrders.Count.IsZero()) { tmBot.Print("No pending order to close."); //in case no pending orders, things must be reset first. tmBot.Print("Removing chart objects, set the initial volume back, and set tunnel to inactive, then re-initialize."); TunnelChartObjects.RemoveAll(tmBot); LastVolume = tmBot.LotToVolume(StartingLotSize); if (TunnelStatus != enTunnelStatus.Inactive) { TunnelStatus = enTunnelStatus.Inactive; } Initialize(); } else { var closingPendOrderIDs = new List <int>(); //to feed stuff so that the code below can only execute after all been closed closingPendOrderIDs.AddRange(tmBot.PendingOrders.Select(x => x.Id)); foreach (var to in tmBot.PendingOrders) { //tmBot.CancelPendingOrderAsync(to, AfterEndTunnelPendingOrder); tmBot.CancelPendingOrderAsync(to, orderCloseResult => { if (orderCloseResult.IsSuccessful) { closingPendOrderIDs.RemoveAll(x => x == orderCloseResult.PendingOrder.Id); if (closingPendOrderIDs.Count.IsZero()) //when there's no more position on the line { tmBot.Print("Removing chart objects, set the initial volume back, and set tunnel to inactive, then re-initialize."); TunnelChartObjects.RemoveAll(tmBot); LastVolume = tmBot.LotToVolume(StartingLotSize); if (TunnelStatus != enTunnelStatus.Inactive) { TunnelStatus = enTunnelStatus.Inactive; } Initialize(); } } }); //tmBot.CancelPendingOrder(to); } } } }