1. Welcome back! Thank you for being a part of this Traders Community. Let's discuss and share :)
    Selamat datang kembali! Trimakasih telah menjadi bagian dari Komunitas Trader ini. Mari berdiskusi dan berbagi :)
    Dismiss Notice

Need help with semi-auto EA that will open another order if user's order profit is more than 10 pip

Discussion in 'Belajar MQL dari Nol' started by Merdeka, 18 Oct 2018.

  1. Merdeka

    Merdeka New Member

    Equity
    Credit
    Ref Point
    Hello everyone,

    I have a semi-auto EA which will open a new order if the user's manual order open profit >= e.g.10 pip, but the EA did not open the order even though my open order is already profiting +50 pips. Can anyone help and identify what is wrong with the code? Thanks in advance

    Below are some snippets:

    Code:
    void OnTick()
      {
            ....
       // ------- OPEN ORDER -------
       // 3. Check if LAST ORDER BY USER (same pair) OPEN PROFIT >= Open_Profit. Note: manual open order magic number = 0
       if(LastOrderOpenProfit(0) >= Open_Profit * myPoint)
         {
          ticket = GetLastOrder(0);   
          myAlert("print", "Last order by user is profittable. OpenProfit*mypoint:" + (Open_Profit * myPoint) +" .Attempting to copy open order ticket: " + ticket);
         
          if(OrderSelect(ticket, SELECT_BY_POS, MODE_TRADES))
            {
             if(OrderType() % 2 == 0) //buy
               {
                RefreshRates();               
                price = Ask;
                SL = StopLoss * myPoint; //Stop Loss = value in points (relative to price)
                TP = TakeProfit * myPoint; //Take Profit = value in points (relative to price) 
                   
                if(UseRiskMM) TradeSize = MM_Size(SL);
                   else TradeSize = LotSize;
                   
                if(IsTradeAllowed())
                  {
                   ticket = myOrderSend(OP_BUY, price, TradeSize, "");
                   if(ticket <= 0) return;
                  }
                else //not autotrading => only send alert
                  {
                   myAlert("order", "");
                  }
                     
                myOrderModifyRel(ticket, 0, TP);
                myOrderModifyRel(ticket, SL, 0);
               }
             else //sell
               {
                RefreshRates();               
                price = Bid;
                SL = StopLoss * myPoint; //Stop Loss = value in points (relative to price)
                TP = TakeProfit * myPoint; //Take Profit = value in points (relative to price)
                   
                if(UseRiskMM) TradeSize = MM_Size(SL);
                   else TradeSize = LotSize;
                   
                if(IsTradeAllowed())
                  {
                   ticket = myOrderSend(OP_SELL, price, TradeSize, "");
                   
                   if(ticket <= 0) return;
                  }
                else //not autotrading => only send alert
                  {
                   myAlert("order", "");
                  }
                     
                myOrderModifyRel(ticket, 0, TP);
                myOrderModifyRel(ticket, SL, 0);
               }
            }
         }
    
    .....
    }
    
    double LastOrderOpenProfit(int magic)
      {
       double profit = 0.0;
       datetime time = 0;
       
       myAlert("print", "In LastOrderOpenProfit() function...");
       
       for(int i=OrdersTotal()-1; i>=0; --i)
         {
          if(OrderSelect(i, SELECT_BY_POS))
            {
             if(OrderSymbol() == Symbol()
                && OrderMagicNumber() == magic
                && OrderOpenTime() > time )
               {
                time = OrderOpenTime();
                profit = OrderProfit();
               
                myAlert("print", "Checked selected order same symbol/magicnumber and opentime> previous. MagicNumber:" + magic + ". Ticket: " +OrderTicket());
               }
            }         
         }
         
       return profit;
      }
    
    int GetLastOrder(int magic)
      {
       double profit = 0.0;
       datetime time = 0;
       
       for(int i=OrdersTotal()-1; i>=0; --i)
         {
          if(OrderSelect(i, SELECT_BY_POS))
            {
             if(OrderSymbol() == Symbol()
                && OrderMagicNumber() == magic
                && OrderOpenTime() > time )
               {
                return OrderTicket();
               }
            }         
         }
       return -1;
      }
     

    Attached Files:

  2. Trancexe

    Trancexe Active Member Credit Hunter

    Equity
    Credit
    Ref Point
    you can not use order profit.. calculate distance from OrderOpenPrice with current price
     

Share This Page