Take profit and lot increment

 

Hello have this function working our perfectly but I want to do some modification on it. Currently when EA has successive number of losses for instance 2 and from the input variable the n_op(number of operation is 3),the EA will increase the lot of the third trade together with the tp by a certain formula as shown in my code. The EA will then reset back to normal lots as long as the trade is closed in profit either by hitting tp or opposite close signal. What I want now is the EA to only rset back to normal lots and tp after the trade has hit the tp and not close positively by opposite close. Kindly help: Below is my cod;

double LotOptimised(int type)
   {
      double Lot = lotto_inizio;
      double LastLot = 0;
      datetime last_time = 0;
      double profit = 0;
       
      for(int i = OrdersHistoryTotal() - 1;i >= 0; i--)
         {
             if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))break;
             if(OrderSymbol() != Symbol())continue;
             if(OrderMagicNumber() != magic_number)continue;
                  
             if(OrderOpenTime() > last_time)
               {
                    if(OrderProfit() < 0)LastLot = OrderLots();
                    else LastLot = 0;
                    profit = OrderProfit();
                    last_time = OrderCloseTime();
               }
        }
       double tickSize = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
       
       if(type == OP_BUY)g_tp = Ask + take_profit * tickSize;
       if(type == OP_SELL)g_tp = Bid - take_profit * tickSize;
       
       if(profit < 0 && LastLot > 0)
           {
                int losses = loss_count();
                
                if(n_op - 1 > 0 && losses >= n_op - 1)
                  {
                     double Y = (losses + 1) / n_op;
                     if(Y > 0)Lot = lotto_inizio + lotto_incr * Y;
                     
                     if(type == OP_BUY)g_tp = Ask + (take_profit * Point) * take_profit_molt * Y;
                     if(type == OP_SELL)g_tp = Bid - (take_profit * Point) * take_profit_molt * Y;
                  } 
           }
     
      
      
      double minLot  = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
      double maxLot  = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
      double LotStep = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);
      
      Lot = MathRound(Lot/LotStep) * LotStep;
      
      Lot = MathMin(MathMax(Lot,minLot),maxLot);
      
      return Lot;
   }
 
  1.        if(type == OP_BUY)g_tp = Ask + take_profit * tickSize;
           if(type == OP_SELL)g_tp = Bid - take_profit * tickSize;
    

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)


  2. Ernest Akoyi: What I want now is the EA to only rset back to normal lots and tp after the trade has hit the tp and not close positively by opposite close. Kindly help: Below is my cod;

    Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
              No free help (2017)

  3. How to trigger StopLoss or TakeProfit in Mql4 - Interest Rate - MQL4 and MetaTrader 4 - MQL4 programming forum (2023)

 
William Roeder #:
  1. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)


  2. Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
              No free help (2017)

  3. How to trigger StopLoss or TakeProfit in Mql4 - Interest Rate - MQL4 and MetaTrader 4 - MQL4 programming forum (2023)

Hello I think I have stated my problem and also shown the attempt of my work. The above code works perfectly but I only want it to reset the tp and lot upon hitting the tp and not by opposite signal function

 
Ernest Akoyi #:

Hello I think I have stated my problem and also shown the attempt of my work. The above code works perfectly but I only want it to reset the tp and lot upon hitting the tp and not by opposite signal function

check history, if the order_price > tp for buys (opposite for sells) then reset *tp* ( I think you meant to write lotsize)

 
Chioma Obunadike #:

check history, if the order_price > tp for buys (opposite for sells) then reset *tp* ( I think you meant to write lotsize)

Hello what I mean is that anytime EA closes in positive the lot and tp will be reset to their original values what I want now is the EA to only reset lot and tp upon hitting the desired tp and not when it closes positive that is by opposite close.

 
Ernest Akoyi #:

Hello what I mean is that anytime EA closes in positive the lot and tp will be reset to their original values what I want now is the EA to only reset lot and tp upon hitting the desired tp and not when it closes positive that is by opposite close.

I have told you what to do