Esempio n. 1
0
        /// <summary>
        ///     Adds a message to the journal.
        /// </summary>
        protected void AppendJournalMessage(JournalMessage message)
        {
            messages.Add(message);
            if (messages.Count > Configs.JournalLength)
            {
                messages.RemoveRange(0, messages.Count - Configs.JournalLength);
            }

            UpdateJournal(messages);
        }
Esempio n. 2
0
        /// <summary>
        ///     Stops trade and shows a message.
        /// </summary>
        private void AfterStrategyOpening()
        {
            StopTrade();

            string message = Language.T("Strategy") + " \"" + Data.Strategy.StrategyName + "\" " +
                             Language.T("loaded successfully.");
            var jmsg = new JournalMessage(JournalIcons.Information, DateTime.Now, message);

            AppendJournalMessage(jmsg);
            Log(message);
        }
Esempio n. 3
0
        /// <summary>
        ///     Manual operation execution.
        /// </summary>
        public override void BtnOperationClick(object sender, EventArgs e)
        {
            if (!Data.IsConnected)
            {
                if (Configs.PlaySounds)
                {
                    Data.SoundError.Play();
                }
                return;
            }

            var btn = (Button)sender;

            switch (btn.Name)
            {
            case "btnBuy":
            {
                const OrderType type     = OrderType.Buy;
                string          symbol   = Data.Symbol;
                double          lots     = NormalizeEntrySize(OperationLots);
                double          price    = Data.Ask;
                int             slippage = Configs.AutoSlippage
                                           ? (int)Data.InstrProperties.Spread * 3
                                           : Configs.SlippageEntry;

                int stopLossPips;
                if (OperationStopLoss > 0 && OperationTrailingStop > 0)
                {
                    stopLossPips = Math.Min(OperationStopLoss, OperationTrailingStop);
                }
                else
                {
                    stopLossPips = Math.Max(OperationStopLoss, OperationTrailingStop);
                }

                double stoploss   = stopLossPips > 0 ? Data.Bid - Data.InstrProperties.Point * stopLossPips : 0;
                double takeprofit = OperationTakeProfit > 0
                                                ? Data.Bid + Data.InstrProperties.Point * OperationTakeProfit
                                                : 0;

                if (Configs.PlaySounds)
                {
                    Data.SoundOrderSent.Play();
                }

                string message = string.Format(symbol + " " + Data.PeriodMtStr + " " +
                                               Language.T("An entry order sent") + ": " +
                                               Language.T("Buy") + " {0} " +
                                               LotOrLots(lots) + " " +
                                               Language.T("at") + " {1}, " +
                                               Language.T("Stop Loss") + " {2}, " +
                                               Language.T("Take Profit") + " {3}", lots,
                                               price.ToString(Data.Ff), stoploss.ToString(Data.Ff),
                                               takeprofit.ToString(Data.Ff));
                var jmsg = new JournalMessage(JournalIcons.OrderBuy, DateTime.Now, message);
                AppendJournalMessage(jmsg);
                Log(message);

                string parameters = "TS1=" + OperationTrailingStop + ";BRE=" + OperationBreakEven;

                int response = bridge.OrderSend(symbol, type, lots, price, slippage, stopLossPips,
                                                OperationTakeProfit, parameters);

                if (response >= 0)
                {
                    Data.AddBarStats(OperationType.Buy, lots, price);
                    Data.WrongStopLoss   = 0;
                    Data.WrongTakeProf   = 0;
                    Data.WrongStopsRetry = 0;
                }
                else
                {
                    // Error in operation execution.
                    ReportOperationError();
                    Data.WrongStopLoss = stopLossPips;
                    Data.WrongTakeProf = OperationTakeProfit;
                }
            }
            break;

            case "btnSell":
            {
                const OrderType type     = OrderType.Sell;
                string          symbol   = Data.Symbol;
                double          lots     = NormalizeEntrySize(OperationLots);
                double          price    = Data.Bid;
                int             slippage = Configs.AutoSlippage
                                           ? (int)Data.InstrProperties.Spread * 3
                                           : Configs.SlippageEntry;

                int stopLossPips;
                if (OperationStopLoss > 0 && OperationTrailingStop > 0)
                {
                    stopLossPips = Math.Min(OperationStopLoss, OperationTrailingStop);
                }
                else
                {
                    stopLossPips = Math.Max(OperationStopLoss, OperationTrailingStop);
                }

                double stoploss   = stopLossPips > 0 ? Data.Ask + Data.InstrProperties.Point * stopLossPips : 0;
                double takeprofit = OperationTakeProfit > 0
                                                ? Data.Ask - Data.InstrProperties.Point * OperationTakeProfit
                                                : 0;

                if (Configs.PlaySounds)
                {
                    Data.SoundOrderSent.Play();
                }

                string message = string.Format(symbol + " " + Data.PeriodMtStr + " " +
                                               Language.T("An entry order sent") + ": " +
                                               Language.T("Sell") + " {0} " +
                                               LotOrLots(lots) + " " +
                                               Language.T("at") + " {1}, " +
                                               Language.T("Stop Loss") + " {2}, " +
                                               Language.T("Take Profit") + " {3}", lots,
                                               price.ToString(Data.Ff), stoploss.ToString(Data.Ff),
                                               takeprofit.ToString(Data.Ff));
                var jmsg = new JournalMessage(JournalIcons.OrderSell, DateTime.Now, message);
                AppendJournalMessage(jmsg);
                Log(message);

                string parameters = "TS1=" + OperationTrailingStop + ";BRE=" + OperationBreakEven;

                int response = bridge.OrderSend(symbol, type, lots, price, slippage, stopLossPips,
                                                OperationTakeProfit, parameters);

                if (response >= 0)
                {
                    Data.AddBarStats(OperationType.Sell, lots, price);
                    Data.WrongStopLoss   = 0;
                    Data.WrongTakeProf   = 0;
                    Data.WrongStopsRetry = 0;
                }
                else
                {
                    // Error in operation execution.
                    ReportOperationError();
                    Data.WrongStopLoss = stopLossPips;
                    Data.WrongTakeProf = OperationTakeProfit;
                }
            }
            break;

            case "btnClose":
            {
                string symbol   = Data.Symbol;
                double lots     = NormalizeEntrySize(Data.PositionLots);
                double price    = Data.PositionDirection == PosDirection.Long ? Data.Bid : Data.Ask;
                int    slippage = Configs.AutoSlippage ? (int)Data.InstrProperties.Spread * 6 : Configs.SlippageExit;
                int    ticket   = Data.PositionTicket;

                if (ticket == 0)
                {
                    // No position.
                    if (Configs.PlaySounds)
                    {
                        Data.SoundError.Play();
                    }
                    return;
                }

                if (Configs.PlaySounds)
                {
                    Data.SoundOrderSent.Play();
                }

                string message = string.Format(symbol + " " + Data.PeriodMtStr + " " +
                                               Language.T("An exit order sent") + ": " +
                                               Language.T("Close") + " {0} " +
                                               LotOrLots(lots) + " " +
                                               Language.T("at") + " {1}",
                                               lots, price.ToString(Data.Ff));
                var jmsg = new JournalMessage(JournalIcons.OrderClose, DateTime.Now, message);
                AppendJournalMessage(jmsg);
                Log(message);

                bool responseOk = bridge.OrderClose(ticket, lots, price, slippage);

                if (responseOk)
                {
                    Data.AddBarStats(OperationType.Close, lots, price);
                }
                else
                {
                    ReportOperationError();
                }

                Data.WrongStopLoss   = 0;
                Data.WrongTakeProf   = 0;
                Data.WrongStopsRetry = 0;
            }
            break;

            case "btnModify":
            {
                string symbol = Data.Symbol;
                double lots   = NormalizeEntrySize(Data.PositionLots);
                double price  = Data.PositionDirection == PosDirection.Long ? Data.Bid : Data.Ask;
                int    ticket = Data.PositionTicket;
                double sign   = Data.PositionDirection == PosDirection.Long ? 1 : -1;

                if (ticket == 0)
                {
                    // No position.
                    if (Configs.PlaySounds)
                    {
                        Data.SoundError.Play();
                    }
                    return;
                }

                if (Configs.PlaySounds)
                {
                    Data.SoundOrderSent.Play();
                }

                int stopLossPips;
                if (OperationStopLoss > 0 && OperationTrailingStop > 0)
                {
                    stopLossPips = Math.Min(OperationStopLoss, OperationTrailingStop);
                }
                else
                {
                    stopLossPips = Math.Max(OperationStopLoss, OperationTrailingStop);
                }

                double stoploss   = stopLossPips > 0 ? price - sign * Data.InstrProperties.Point * stopLossPips : 0;
                double takeprofit = OperationTakeProfit > 0
                                                ? price + sign * Data.InstrProperties.Point * OperationTakeProfit
                                                : 0;

                string message = string.Format(symbol + " " + Data.PeriodMtStr + " " +
                                               Language.T("A modify order sent") + ": " +
                                               Language.T("Stop Loss") + " {0}, " +
                                               Language.T("Take Profit") + " {1}",
                                               stoploss.ToString(Data.Ff),
                                               takeprofit.ToString(Data.Ff));
                var jmsg = new JournalMessage(JournalIcons.Recalculate, DateTime.Now, message);
                AppendJournalMessage(jmsg);
                Log(message);

                string parameters = "TS1=" + OperationTrailingStop + ";BRE=" + OperationBreakEven;

                bool responseOk = bridge.OrderModify(ticket, price, stopLossPips, OperationTakeProfit,
                                                     parameters);

                if (responseOk)
                {
                    Data.AddBarStats(OperationType.Modify, lots, price);
                    Data.WrongStopLoss   = 0;
                    Data.WrongTakeProf   = 0;
                    Data.WrongStopsRetry = 0;
                }
                else
                {
                    ReportOperationError();
                    Data.WrongStopLoss = stopLossPips;
                    Data.WrongTakeProf = OperationTakeProfit;
                }
            }
            break;
            }
        }