Beispiel #1
0
        internal void ModifyTrade(Trade mTrade)
        {
            GetMtrades();
            int AtradeId = -1;

            Atrades.TryGetValue(mTrade.Id, out AtradeId);
            if (AtradeId > 0)
            {
                int total = OrdersTotal();
                if (OrderSelect(AtradeId, SELECT_BY_TICKET))
                {
                    LogPrint("ModifyTrade: Atrade found Id=" + AtradeId);
                    var dt  = (DateTime)OrderCloseTime();
                    var com = OrderCommission();
                    var prc = OrderOpenPrice();
                    if (dt == null || dt.Year < 1980)  // order still open
                    {
                        if (com <= 0)
                        {
                            prc = mTrade.OpenPrice;  // order still Pending, update OpenPrice
                        }
                        else
                        {
                            LogPrint(" ModifyTrade: Cannot modify OpenPrice because mTrade is Filled !! mTrade.Id =" + mTrade.Id);
                        }
                        var      price = NormalizeDouble(prc, Digits);
                        var      sl    = NormalizeDouble(mTrade.StopLoss, Digits);
                        var      tp    = NormalizeDouble(mTrade.TakeProfit, Digits);
                        datetime exp   = null;
                        if (mTrade.ExpirationDate != null)
                        {
                            if (mTrade.ExpirationDate.Value.Year > 1980)
                            {
                                exp = (datetime)mTrade.ExpirationDate;
                            }
                        }
                        if (!OrderModify(AtradeId, price, sl, tp, exp))
                        {
                            var err = GetLastError();
                            LogPrint("CloseTrade: OrderModify failed. Id=" + AtradeId + " MT4.Id=" + mTrade.Id + " reason=" + err);
                            LogPrint("CloseTrade: OrderModify failed. price=" + price + " sl=" + sl + " tp=" + tp + " exp=" + exp.ToString());
                            if (err == 3)
                            {
                                LogPrint("Id=" + AtradeId + " price=" + price + " sl=" + sl + " tp=" + tp + " exp =" + exp.ToString());
                            }
                        }
                        else
                        {
                            LogPrint("CloseTrade: OrderModify successful. Id=" + AtradeId + " MT4.Id=" + mTrade.Id);
                            LogPrint("Id=" + AtradeId + " price=" + price + " sl=" + sl + " tp=" + tp + " exp =" + exp.ToString());
                        }
                    }
                    else
                    {
                        LogPrint("CloseTrade: Atrade already closed. Id=" + AtradeId + " MT4.Id=" + mTrade.Id);
                    }
                }
                else
                {
                    LogPrint("ModifyTrade: !! Atrade not found by OrderSelect. Atrades.Count=" + Atrades.Count + " mId = " + mTrade.Id + " AId=" + AtradeId);
                    Atrades.Remove(mTrade.Id);
                }
            }
            else
            {
                LogPrint("ModifyTrade: !! Atrade not found in Atrades. Atrades.Count=" + Atrades.Count + " mId=" + mTrade.Id);
            }
        }