Need help with EA script

 

Hi everyone,

I'm new at scripting and I'm having a hard time with an EA that i'm trying to build.

What I want the EA to do is close partial order (75%) when it reaches 10 pips or so in profit and at the same time move my stop loss to break even(or pip in profit from entry price). The issue i'm having is that it's not doing both at the same time. I understand how to write each script individually but not together. Another problem i'm also having is that the partial close keeps closing 75% until i have 0.01 lot left. I only want it to close 75% once and the remaining lots to be close manually. 

I would greatly appreciate any help. If you need any clarifications on something that I wrote, please ask.

Thanks in advance!

void OnTick()
  {
     // if we have no open orders
     if (OrdersTotal()==0)
         {
            //open a test position
            int buyticket = OrderSend(_Symbol,OP_BUY,0.10,Ask,3,Bid-1000*_Point,Ask+20000*_Point,NULL,0,0,Green);
         }  
      //close 75% and move stop loss to break even
      PartialProfitBE();
  }
//+------------------------------------------------------------------+
void PartialProfitBE()
{
   //go through all orders
   
   for (int b= OrdersTotal()-1; b>=0; b--)
 
   { 
      //if we have an open position
      if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      
      //if the order symbol belongs to the chart
      if (OrderSymbol() == Symbol())
      
      //if we have a buy position
      if (OrderType()==OP_BUY)
      
         {
            //if the stop loss is below the open price
            if (OrderStopLoss()<OrderOpenPrice())
            
            //if the Bid price is 10 pips above the open price
            if (Bid > OrderOpenPrice()+ 100 * _Point)
            
            //close 75% of order
            OrderClose(OrderTicket(), (OrderLots()*0.75), Bid,3);
            
            //move the stop loss to 1 points above the open price
            OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice()+ 10 * _Point), OrderTakeProfit(),0,clrNONE);
                 
         }
      else if (OrderType()==OP_SELL) //if we have a sell postion
         {
            //if the stop loss is above the open price
            if (OrderStopLoss()> OrderOpenPrice())
            
            //if the Ask price is 10 pips below the open price
            if (Ask < OrderOpenPrice()-100 * _Point)
            
            //close 75% of order
            OrderClose(OrderTicket(), (OrderLots()*0.75),Ask,3);
            
           // move the stop loss to 1 points below the open price
            OrderModify(OrderTicket(), OrderOpenPrice(), (OrderOpenPrice()- (10*_Point)), OrderTakeProfit(),0,clrNONE);
         }
    } 
}
 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.
 
Keith Watford:
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.
sorry, new here. this was my first post and i realized it after i posted it. Thanks for moving it. don't even know how to do that lol
 
            OrderClose(OrderTicket(), (OrderLots()*0.75), Bid,3);
  1. You can't just use OrderLots()×75% because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot.

  2. You also must check if you have already done it, to avoid repeated closing. Alternatives:

    • Move SL to Break Even+1 before the partial close. That way you know that you already did it.

    • Set a flag in persistent storage (files, global variables w/flush)

    • Open two orders initially, and close one (manually or by TP.)

 
William Roeder:
  1. You can't just use OrderLots()×75% because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot.

  2. You also must check if you have already done it, to avoid repeated closing. Alternatives:

    • Move SL to Break Even+1 before the partial close. That way you know that you already did it.

    • Set a flag in persistent storage (files, global variables w/flush)

    • Open two orders initially, and close one (manually or by TP.)

In regards to the partial close lot size, would a closure of 50% be considered a multiple of LotStep? would I just have to change it from 75% to 50%? When I did a backtest of it with the 75%, it worked but it also bought back the amount that it closed. I'm not sure if it's because of the for loop. You also mentioned to consider moving the SL first before the partial close. Would I just have to switch their placement or would I also have to do something with the way the script is written.  sorry for my ignorance. As stated above, i'm pretty new at this. I'm not sure if my request was stated clearly above but this is what I want the EA to do:

* I manually enter a trade with 0.20 lots

* Pair moves in profit by 10 pips/100 points

*EA moves SL to BE and at the same time closes 50% of lot (0.10 in this case) and then stops working 

* I control the remaining lots (0.10).

*When a new trade is made, the process starts all over again.


are you able to update my code with what's missing. I would really appreciate it!