internal void TickCheck() { if (TunnelStatus == enTunnelStatus.Running || TunnelStatus == enTunnelStatus.PendingOrders) { //when it's a trap, and out of the validity period if (tmBot.config.OpenCycleMethod == enOpenCycleMethod.Trap && tmPositions.Count() == 0 && tmBot.config.Trap.Validity != TimeSpan.Zero && tmBot.TimeInUtc >= CurrentSessionDate.ActualStartDate.Add(tmBot.config.Trap.Validity).ToUniversalTime()) { tmBot.Print("Validity period is over now at: {0:dd MMM yyyy HH:mm:ss}, closing all and repeating stuff.", CurrentSessionDate.ActualStartDate.Add(tmBot.config.Trap.Validity)); EndTunnel(); } //when using SmartBucket if (TunnelStatus == enTunnelStatus.Running && UsesAnySmartBucket && IsSmartBucketInCycle && JustPassedCenterTunnel) { RetreatTunnel(); } if (TunnelStatus == enTunnelStatus.Running && OnTarget) { EndTunnel(); } } else if (TunnelStatus == enTunnelStatus.Inactive) { if (tmBot.config.OpenCycleMethod == enOpenCycleMethod.Trap && ((UseSessionDates && IsWithinCurrentSessionTime) || (!UseSessionDates && IsTrapTime))) { TunnelStatus = enTunnelStatus.PendingActivation; SessionNumber += 1; //executing for Trap method var HalfHeight = Math.Abs(TunnelHeight / 2); TunnelCeiling = tmBot.ShiftPriceInPips(tmBot.Symbol.Ask, HalfHeight); tmBot.Print("Tunnel Ceiling set at {0}", TunnelCeiling.ToString()); TunnelFloor = tmBot.ShiftPriceInPips(tmBot.Symbol.Bid, -HalfHeight); tmBot.Print("Tunnel Floor set at {0}", TunnelFloor.ToString()); TunnelCenter = TunnelCeiling.FindCenterAgainst(TunnelFloor, tmBot.Symbol.Digits); tmBot.Print("Tunnel Center set at {0}", TunnelCenter.ToString()); TunnelChartObjects.DrawMainLines(tmBot, TunnelFloor, TunnelCeiling, TunnelCenter); TunnelChartObjects.DrawMainLinesHistory(tmBot, TunnelFloor, TunnelCeiling, TunnelCenter, TMLabel + PositionSessionNumber); TunnelChartObjects.DrawDirection(tmBot, Direction); LastVolume = tmBot.LotToVolume(StartingLotSize); tmBot.PlaceStopOrderAsync(TradeType.Buy, tmBot.Symbol.Name, LastVolume, TunnelCeiling, TMLabel + GetPositionIndexNumber() + PositionSessionNumber, TrapStarted); tmBot.PlaceStopOrderAsync(TradeType.Sell, tmBot.Symbol.Name, LastVolume, TunnelFloor, TMLabel + GetPositionIndexNumber() + PositionSessionNumber, TrapStarted); } else if (UseSessionDates && IsWithinCurrentSessionTime && tmBot.config.OpenCycleMethod != enOpenCycleMethod.Trap) { TunnelStatus = enTunnelStatus.PendingActivation; SessionNumber += 1; LastVolume = tmBot.LotToVolume(StartingLotSize); tmBot.ExecuteMarketOrderAsync(CurrentSessionDate.SessionTradeType, GetSymbolName(CurrentSessionDate), LastVolume, TMLabel + GetPositionIndexNumber() + PositionSessionNumber, InitialPositionOpened); } } }
private void InitialPositionOpened(TradeResult result) { if (result.IsSuccessful) { if ((tmBot.config.SessionDates.Enabled && CurrentSessionDate.SessionTradeType == TradeType.Buy) || tmBot.config.OpenCycleMethod == enOpenCycleMethod.AlwaysBuy || (tmBot.config.OpenCycleMethod == enOpenCycleMethod.RememberLastProfitable && LastTradeType == TradeType.Buy)) { TunnelCeiling = result.Position.EntryPrice; tmBot.Print("Tunnel Ceiling set at {0}", TunnelCeiling.ToString()); TunnelFloor = tmBot.ShiftPriceInPips(TunnelCeiling, -TunnelHeight); tmBot.Print("Tunnel Floor set at {0}", TunnelFloor.ToString()); TunnelCenter = TunnelCeiling.FindCenterAgainst(TunnelFloor, tmBot.Symbol.Digits); tmBot.Print("Tunnel Center set at {0}", TunnelCenter.ToString()); TunnelChartObjects.DrawMainLines(tmBot, TunnelFloor, TunnelCeiling, TunnelCenter); TunnelChartObjects.DrawDirection(tmBot, Direction); LastVolume = tmBot.Symbol.NormalizeVolumeInUnits(result.Position.VolumeInUnits * tmBot.config.LotMultiplier); tmBot.PlaceStopOrderAsync(TradeType.Sell, tmBot.config.SessionDates.Enabled ? GetSymbolName(CurrentSessionDate) : tmBot.Symbol.Name, LastVolume, TunnelFloor, TMLabel + GetPositionIndexNumber() + PositionSessionNumber, AnotherOrderPlaced); } else if ((tmBot.config.SessionDates.Enabled && CurrentSessionDate.SessionTradeType == TradeType.Sell) || tmBot.config.OpenCycleMethod == enOpenCycleMethod.AlwaysSell || (tmBot.config.OpenCycleMethod == enOpenCycleMethod.RememberLastProfitable && LastTradeType == TradeType.Sell)) { TunnelFloor = result.Position.EntryPrice; tmBot.Print("Tunnel Floor set at {0}", TunnelFloor.ToString()); TunnelCeiling = tmBot.ShiftPriceInPips(TunnelFloor, TunnelHeight); tmBot.Print("Tunnel Ceiling set at {0}", TunnelCeiling.ToString()); TunnelCenter = TunnelCeiling.FindCenterAgainst(TunnelFloor, tmBot.Symbol.Digits); tmBot.Print("Tunnel Center set at {0}", TunnelCenter.ToString()); TunnelChartObjects.DrawMainLines(tmBot, TunnelFloor, TunnelCeiling, TunnelCenter); TunnelChartObjects.DrawDirection(tmBot, Direction); LastVolume = tmBot.Symbol.NormalizeVolumeInUnits(result.Position.VolumeInUnits * tmBot.config.LotMultiplier); tmBot.PlaceStopOrderAsync(TradeType.Buy, tmBot.config.SessionDates.Enabled ? GetSymbolName(CurrentSessionDate) : tmBot.Symbol.Name, LastVolume, TunnelCeiling, TMLabel + GetPositionIndexNumber() + PositionSessionNumber, AnotherOrderPlaced); } Statistics.SetMaxBounce(tmPositions.Count() + 1, tmBot.Time); } else { tmBot.Print("FAILED: Order failed to place at InitialPositionOpened, reason: '" + (result.Error.HasValue ? result.Error.Value.ToString() : "no valid error") + "'."); } }
internal void Initialize() { tmBot.Print("»»»»»»»» Initializing..."); TunnelStatus = enTunnelStatus.Inactive; LastVolume = tmBot.LotToVolume(StartingLotSize); if (tmBot.config != null) { if (!tmBot.config.SessionDates.Enabled) // if SessionDates is not enabled { TunnelStatus = enTunnelStatus.PendingActivation; if (tmBot.config.OpenCycleMethod == enOpenCycleMethod.AlwaysBuy || (tmBot.config.OpenCycleMethod == enOpenCycleMethod.RememberLastProfitable && LastTradeType == TradeType.Buy)) { tmBot.ExecuteMarketOrderAsync(TradeType.Buy, tmBot.Symbol.Name, LastVolume, TMLabel + GetPositionIndexNumber() + PositionSessionNumber, InitialPositionOpened); } else if (tmBot.config.OpenCycleMethod == enOpenCycleMethod.AlwaysSell || (tmBot.config.OpenCycleMethod == enOpenCycleMethod.RememberLastProfitable && LastTradeType == TradeType.Sell)) { tmBot.ExecuteMarketOrderAsync(TradeType.Sell, tmBot.Symbol.Name, LastVolume, TMLabel + GetPositionIndexNumber() + PositionSessionNumber, InitialPositionOpened); } else if (tmBot.config.OpenCycleMethod == enOpenCycleMethod.Trap) { if (tmPositions.Count() == 0 && IsTrapTime) { int HalfHeight = Math.Abs(TunnelHeight / 2); TunnelCeiling = tmBot.ShiftPriceInPips(tmBot.Symbol.Ask, HalfHeight); tmBot.Print("Tunnel Ceiling set at {0}", TunnelCeiling.ToString()); TunnelFloor = tmBot.ShiftPriceInPips(tmBot.Symbol.Bid, -HalfHeight); tmBot.Print("Tunnel Floor set at {0}", TunnelFloor.ToString()); TunnelCenter = TunnelCeiling.FindCenterAgainst(TunnelFloor, tmBot.Symbol.Digits); tmBot.Print("Tunnel Center set at {0}", TunnelCenter.ToString()); TunnelChartObjects.DrawMainLines(tmBot, TunnelFloor, TunnelCeiling, TunnelCenter); TunnelChartObjects.DrawMainLinesHistory(tmBot, TunnelFloor, TunnelCeiling, TunnelCenter, TMLabel + PositionSessionNumber); TunnelChartObjects.DrawDirection(tmBot, Direction); tmBot.PlaceStopOrderAsync(TradeType.Buy, tmBot.Symbol.Name, LastVolume, TunnelCeiling, TMLabel + GetPositionIndexNumber() + PositionSessionNumber, TrapStarted); tmBot.PlaceStopOrderAsync(TradeType.Sell, tmBot.Symbol.Name, LastVolume, TunnelFloor, TMLabel + GetPositionIndexNumber() + PositionSessionNumber, TrapStarted); } else { tmBot.Print("BollingerBandsDistance: {0}", BollingerBandsDistance); TunnelStatus = enTunnelStatus.Inactive; } } else { tmBot.Print("Open Cycle Method: '{0}' is not implemented", tmBot.config.OpenCycleMethod.ToString()); TunnelStatus = enTunnelStatus.Inactive; } } else { // if SessionDates is enabled ParsedSessionDates = GetParsedSessionDates(); CurrentSessionDate = UseSessionDates ? ParsedSessionDates.First() : null; if (ParsedSessionDates.Count > 0) { tmBot.Print("Session Dates found {0} records. The next is at: {1:dd MMM yyyy HH:mm:ss}.", ParsedSessionDates.Count, CurrentSessionDate.ActualStartDate); } else { tmBot.Print("No more Session Dates."); } } } else { throw new InvalidOperationException("Configuration is not properly loaded."); } }