/// <summary> /// Stops connection to MT /// </summary> private void Disconnect() { nullPing = true; pingAttempt = 0; if (Data.IsConnected && Configs.PlaySounds) { Data.SoundDisconnect.Play(); } Data.IsConnected = false; StopTrade(); JournalMessage jmsg = new JournalMessage(JournalIcons.Blocked, DateTime.Now, Language.T("Not Connected")); AppendJournalMessage(jmsg); Data.Bid = 0; Data.Ask = 0; Data.SetCurrentAccount(DateTime.MinValue, 0, 0, 0, 0); bool poschanged = Data.SetCurrentPosition(0, -1, 0, 0, DateTime.MinValue, 0, 0, 0, ""); ShowCurrentPosition(poschanged); SetEquityInfoText(string.Format("{0} {1}", 0, Data.AccountCurrency)); UpdateBalanceChart(Data.BalanceData, Data.BalanceDataPoints); SetTradeStrip(); SetConnMarketText(Language.T("Not Connected")); SetLblConnectionText(Language.T("Not Connected")); SetConnIcon(0); SetTickInfoText(""); SetLblSymbolText(""); SetFormText(); }
/// <summary> /// Pings the server in order to check the connection. /// </summary> void TimerPing_Tick(object sender, EventArgs e) { if (DateTime.Now < tickLocalTime.AddSeconds(1)) { return; // The last tick was soon enough. } lock (lockerTickPing) { MT4Bridge.PingInfo ping = bridge.GetPingInfo(); if (ping == null && !nullPing) { // Wrong ping. pingAttempt++; if ((pingAttempt == 1 || pingAttempt % 10 == 0) && JournalShowSystemMessages) { JournalMessage jmsgsys = new JournalMessage(JournalIcons.System, DateTime.Now, Language.T("Unsuccessful ping") + " No " + pingAttempt + "."); AppendJournalMessage(jmsgsys); } if (pingAttempt == 30) { JournalMessage jmsgsys = new JournalMessage(JournalIcons.Warning, DateTime.Now, Language.T("There is no connection with MetaTrader.")); AppendJournalMessage(jmsgsys); if (Configs.PlaySounds) { Data.SoundError.Play(); } } if (pingAttempt < 60) { SetConnIcon(pingAttempt < 30 ? 3 : 4); return; } Disconnect(); } else if (ping != null) { // Successful ping. nullPing = false; bool bUpdateData = false; if (!Data.IsConnected || IsChartChangeged(ping.Symbol, (DataPeriods)(int)ping.Period)) { // Disconnected or chart change. pingAttempt = 0; if (JournalShowSystemMessages) { JournalMessage jmsgsys = new JournalMessage(JournalIcons.System, DateTime.Now, ping.Symbol + " " + ping.Period.ToString() + " " + Language.T("Successful ping.")); AppendJournalMessage(jmsgsys); } StopTrade(); if (!UpdateDataFeedInfo(ping.Time, ping.Symbol, (DataPeriods)(int)ping.Period)) { return; } Data.Bid = ping.Bid; Data.Ask = ping.Ask; Data.InstrProperties.Spread = ping.Spread; Data.InstrProperties.TickValue = ping.TickValue; Data.IsConnected = true; bUpdateData = true; SetFormText(); SetConnIcon(1); SetTradeStrip(); if (Configs.PlaySounds) { Data.SoundConnect.Play(); } MT4Bridge.TerminalInfo te = bridge.GetTerminalInfo(); string connection = Language.T("Connected to a MetaTrader terminal."); if (te != null) { connection = string.Format(Language.T("Connected to") + " {0} " + Language.T("by") + " {1}", te.TerminalName, te.TerminalCompany); Data.ExpertVersion = te.ExpertVersion; Data.LibraryVersion = te.LibraryVersion; Data.TerminalName = te.TerminalName; } SetLblConnectionText(connection); string market = string.Format("{0} {1}", ping.Symbol, ping.Period); SetConnMarketText(market); JournalMessage jmsg = new JournalMessage(JournalIcons.OK, DateTime.Now, market + " " + connection); AppendJournalMessage(jmsg); // Check for reconnection. if (symbolReconnect == Data.Symbol && periodReconnect == Data.Period && accountReconnect == Data.AccountNumber) { StartTrade(); // Restart trade. } } else if (pingAttempt > 0 && JournalShowSystemMessages) { // After a wrong ping. pingAttempt = 0; JournalMessage jmsgsys = new JournalMessage(JournalIcons.System, DateTime.Now, ping.Symbol + " " + ping.Period.ToString() + " " + Language.T("Successful ping.")); AppendJournalMessage(jmsgsys); } bool isNewPrice = Math.Abs(Data.Bid - ping.Bid) > Data.InstrProperties.Point / 2; DateTime dtPingServerTime = tickServerTime.Add(DateTime.Now - tickLocalTime); string sBid = ping.Bid.ToString(Data.FF); string sAsk = ping.Ask.ToString(Data.FF); SetLblBidAskText(sBid + " / " + sAsk); Data.Bid = ping.Bid; Data.Ask = ping.Ask; Data.InstrProperties.Spread = ping.Spread; Data.InstrProperties.TickValue = ping.TickValue; Data.ServerTime = ping.Time; bool isAccChanged = Data.SetCurrentAccount(ping.Time, ping.AccountBalance, ping.AccountEquity, ping.AccountProfit, ping.AccountFreeMargin); bool isPosChanged = Data.SetCurrentPosition(ping.PositionTicket, ping.PositionType, ping.PositionLots, ping.PositionOpenPrice, ping.PositionOpenTime, ping.PositionStopLoss, ping.PositionTakeProfit, ping.PositionProfit, ping.PositionComment); SetDataAndCalculate(ping.Symbol, ping.Period, dtPingServerTime, isNewPrice, bUpdateData); SetEquityInfoText(string.Format("{0:F2} {1}", ping.AccountEquity, Data.AccountCurrency)); ShowCurrentPosition(isPosChanged); if (isAccChanged) { JournalMessage jmsg = new JournalMessage(JournalIcons.Currency, DateTime.Now, string.Format(Language.T("Account Balance") + " {0:F2}, " + Language.T("Equity") + " {1:F2}, " + Language.T("Profit") + ", {2:F2}, " + Language.T("Free Margin") + " {3:F2}", ping.AccountBalance, ping.AccountEquity, ping.AccountProfit, ping.AccountFreeMargin)); AppendJournalMessage(jmsg); } if (Data.IsBalanceDataChganged) { UpdateBalanceChart(Data.BalanceData, Data.BalanceDataPoints); } SetTickInfoText(string.Format("{0} {1} / {2}", ping.Time.ToString("HH:mm:ss"), sBid, sAsk)); SetConnIcon(1); // Sends OrderModify on SL/TP errors if (IsWrongStopsExecution()) { ResendWrongStops(); } } } return; }
/// <summary> /// Sets the instrument's properties after connecting; /// </summary> bool UpdateDataFeedInfo(DateTime time, string symbol, DataPeriods period) { lock (lockerDataFeed) { Data.ResetBidAsk(); Data.ResetAccountStats(); Data.ResetPositionStats(); Data.ResetBarStats(); Data.ResetTicks(); // Reads market info from the chart MT4Bridge.MarketInfo marketInfo = bridge.GetMarketInfoAll(symbol); if (marketInfo == null) { if (JournalShowSystemMessages) { JournalMessage jmsgsys = new JournalMessage(JournalIcons.System, DateTime.Now, symbol + " " + (MT4Bridge.PeriodType)(int) period + " " + Language.T("Cannot update market info.")); AppendJournalMessage(jmsgsys); } return(false); } // Sets instrument properties Data.Period = period; Data.InstrProperties.Symbol = symbol; Data.InstrProperties.LotSize = (int)marketInfo.ModeLotSize; Data.InstrProperties.MinLot = marketInfo.ModeMinLot; Data.InstrProperties.MaxLot = marketInfo.ModeMaxLot; Data.InstrProperties.LotStep = marketInfo.ModeLotStep; Data.InstrProperties.Digits = (int)marketInfo.ModeDigits; Data.InstrProperties.Spread = marketInfo.ModeSpread; Data.InstrProperties.SwapLong = marketInfo.ModeSwapLong; Data.InstrProperties.SwapShort = marketInfo.ModeSwapShort; Data.InstrProperties.TickValue = marketInfo.ModeTickValue; Data.InstrProperties.StopLevel = marketInfo.ModeStopLevel; Data.InstrProperties.MarginRequired = marketInfo.ModeMarginRequired; SetNumUpDownLots(marketInfo.ModeMinLot, marketInfo.ModeLotStep, marketInfo.ModeMaxLot); // Sets Market Info string[] values = new string[] { symbol, Data.DataPeriodToString(period), marketInfo.ModeLotSize.ToString(), marketInfo.ModePoint.ToString("F" + marketInfo.ModeDigits.ToString()), marketInfo.ModeSpread.ToString(), marketInfo.ModeSwapLong.ToString(), marketInfo.ModeSwapShort.ToString() }; UpdateStatusPageMarketInfo(values); MT4Bridge.Bars bars = bridge.GetBars(symbol, (MT4Bridge.PeriodType)(int) period); if (bars == null) { if (JournalShowSystemMessages) { Data.SoundError.Play(); JournalMessage jmsgsys = new JournalMessage(JournalIcons.System, DateTime.Now, symbol + " " + (MT4Bridge.PeriodType)(int) period + " " + Language.T("Cannot receive bars!")); AppendJournalMessage(jmsgsys); } return(false); } if (bars.Count < MaxBarsCount((int)period)) { if (JournalShowSystemMessages) { Data.SoundError.Play(); JournalMessage jmsg = new JournalMessage(JournalIcons.Error, DateTime.Now, symbol + " " + (MT4Bridge.PeriodType)(int) period + " " + Language.T("Cannot receive enough bars!")); AppendJournalMessage(jmsg); } return(false); } if (JournalShowSystemMessages) { JournalMessage jmsgsys = new JournalMessage(JournalIcons.System, DateTime.Now, symbol + " " + (MT4Bridge.PeriodType)(int) period + " " + Language.T("Market data updated, bars downloaded.")); AppendJournalMessage(jmsgsys); } // Account Information. MT4Bridge.AccountInfo account = bridge.GetAccountInfo(); if (account == null) { if (JournalShowSystemMessages) { Data.SoundError.Play(); JournalMessage jmsg = new JournalMessage(JournalIcons.Error, DateTime.Now, symbol + " " + (MT4Bridge.PeriodType)(int) period + " " + Language.T("Cannot receive account information!")); AppendJournalMessage(jmsg); } return(false); } if (JournalShowSystemMessages) { JournalMessage jmsgsys = new JournalMessage(JournalIcons.System, DateTime.Now, symbol + " " + (MT4Bridge.PeriodType)(int) period + " " + Language.T("Account information received.")); AppendJournalMessage(jmsgsys); } Data.AccountName = account.Name; Data.IsDemoAccount = account.IsDemo; Data.AccountCurrency = account.Currency; Data.SetCurrentAccount(time, account.Balance, account.Equity, account.Profit, account.FreeMargin); UpdateBalanceChart(Data.BalanceData, Data.BalanceDataPoints); SetTradeStrip(); SetLblSymbolText(symbol); } return(true); }
/// <summary> /// Bridge OnTick /// </summary> void Bridge_OnTick(object source, MT4Bridge.TickEventArgs tea) { lock (lockerTickPing) { if (pingAttempt > 0 && JournalShowSystemMessages) { JournalMessage jmsgsys = new JournalMessage(JournalIcons.System, DateTime.Now, tea.Symbol + " " + tea.Period.ToString() + " " + Language.T("Tick received after an unsuccessful ping.")); AppendJournalMessage(jmsgsys); } pingAttempt = 0; if (!Data.IsConnected) { return; } tickLocalTime = DateTime.Now; tickServerTime = tea.Time; if (IsChartChangeged(tea.Symbol, (DataPeriods)(int)tea.Period)) { StopTrade(); Data.IsConnected = false; SetFormText(); if (Configs.PlaySounds) { Data.SoundDisconnect.Play(); } JournalMessage jmsg = new JournalMessage(JournalIcons.Warning, DateTime.Now, tea.Symbol + " " + tea.Period.ToString() + " " + Language.T("Tick received from a different chart!")); AppendJournalMessage(jmsg); return; } bool bNewPrice = Math.Abs(Data.Bid - tea.Bid) > Data.InstrProperties.Point / 2; Data.Bid = tea.Bid; Data.Ask = tea.Ask; Data.InstrProperties.Spread = tea.Spread; Data.InstrProperties.TickValue = tea.TickValue; Data.ServerTime = tea.Time; Data.SetTick(tea.Bid); bool isAccChanged = Data.SetCurrentAccount(tea.Time, tea.AccountBalance, tea.AccountEquity, tea.AccountProfit, tea.AccountFreeMargin); bool isPosChanged = Data.SetCurrentPosition(tea.PositionTicket, tea.PositionType, tea.PositionLots, tea.PositionOpenPrice, tea.PositionOpenTime, tea.PositionStopLoss, tea.PositionTakeProfit, tea.PositionProfit, tea.PositionComment); bool updateData = true; SetDataAndCalculate(tea.Symbol, tea.Period, tea.Time, bNewPrice, updateData); string bidText = tea.Bid.ToString(Data.FF); string askText = tea.Ask.ToString(Data.FF); SetLblBidAskText(bidText + " / " + askText); // Tick data label if (JournalShowTicks) { string tickInfo = string.Format("{0} {1} {2} {3} / {4}", tea.Symbol, tea.Period, tea.Time.ToString("HH:mm:ss"), bidText, askText); JournalMessage jmsg = new JournalMessage(JournalIcons.Globe, DateTime.Now, tickInfo); AppendJournalMessage(jmsg); } UpdateTickChart(Data.InstrProperties.Point, Data.ListTicks.ToArray()); SetEquityInfoText(string.Format("{0:F2} {1}", tea.AccountEquity, Data.AccountCurrency)); ShowCurrentPosition(isPosChanged); if (isAccChanged) { JournalMessage jmsg = new JournalMessage(JournalIcons.Currency, DateTime.Now, string.Format(Language.T("Account Balance") + " {0:F2}, " + Language.T("Equity") + " {1:F2}, " + Language.T("Profit") + ", {2:F2}, " + Language.T("Free Margin") + " {3:F2}", tea.AccountBalance, tea.AccountEquity, tea.AccountProfit, tea.AccountFreeMargin)); AppendJournalMessage(jmsg); } if (Data.IsBalanceDataChganged) { UpdateBalanceChart(Data.BalanceData, Data.BalanceDataPoints); } SetTickInfoText(string.Format("{0} {1} / {2}", tea.Time.ToString("HH:mm:ss"), bidText, askText)); SetConnIcon(2); // Sends OrderModify on SL/TP errors if (IsWrongStopsExecution()) { ResendWrongStops(); } } return; }