Help Needed: Take Profit Not Moving Correctly on Live Account with Martingale Strategy

Vondereich

Trader
Aug 25, 2024
14
0
6
37
can someone help me why when during live account trade, my takeprofit is not moving correctly.. While using the demo account the takeprofit move flawlessly.. Here my code related to the system trade, by the way this using martingale, when more layer are open, the take profit consistently static to the set value of the user, let say if the broker spread is 15, the take profit is 5, when layering, the take profit will keep opening the static tp. which is it stuck at 5, means when hit, the profit is negative.. means loss..

You can DM me for the full source code, but i think this is enough

Part 1

Code:
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
double AskPrice(string symbol = "")
  {
   if(symbol == "")
      symbol = Symbol();
   return (MarketInfo(symbol, MODE_ASK));
  }
 
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
double BidPrice(string symbol = "")
  {
   if(symbol == "")
      symbol = Symbol();
   return (MarketInfo(symbol, MODE_BID));
  }
 
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
double StopLevel(string symbol = "")
  {
   if(symbol == "")
      symbol = Symbol();
   return (MarketInfo(symbol, MODE_STOPLEVEL));
  }
 
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
string OrderCmd(int ordertype)
  {
   string label;

   switch(ordertype)
     {
      case 0:
         label = "Buy";
         break;
      case 1:
         label = "Sell";
         break;
      case 2:
         label = "Buy Limit";
         break;
      case 3:
         label = "Sell Limit";
         break;
      case 4:
         label = "Buy Stop";
         break;
      case 5:
         label = "Sell Stop";
         break;
     }

   return (label);
  }
 
//+------------------------------------------------------------------+
//|Order Entry
//+------------------------------------------------------------------+

int Order(int ordertype, string comment)
{
   int    ticket;
   double lot     = SetupLot(ordertype);
   double price   = 0;
   double sl      = 0;
   double tp      = 0;

   if (ordertype == OP_BUY)
   {
      price = AskPrice();

      if (StopLoss > 0)
         sl = NormalizeDouble(price - (StopLoss * POINT), DIGIT);
      if (TakeProfit > 0)
         tp = NormalizeDouble(price + (TakeProfit * POINT), DIGIT);
   }

   if (ordertype == OP_SELL)
   {
      price = BidPrice();

      if (StopLoss > 0)
         sl = NormalizeDouble(price + (StopLoss * POINT), DIGIT);
      if (TakeProfit > 0)
         tp = NormalizeDouble(price - (TakeProfit * POINT), DIGIT);
   }

   ticket = OrderSend(Symbol(), ordertype, lot, price, SlipPage, sl, tp, comment, MagicNumber, 0);
   if (ticket == -1)
      ShowError("Order " + OrderCmd(ordertype));

   return ticket;
}

 
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void CloseOrder(int ordertype = -1)
  {
   for(int i = OrdersTotal()-1; i >= 0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
           {
            if(ordertype == -1)
              {
               if(OrderType() == OP_BUY)
                 {
                  if(!OrderClose(OrderTicket(),OrderLots(),BidPrice(OrderSymbol()),SlipPage,Blue))
                     ShowError("Close " + OrderCmd(OrderType()));
                 }
               else
                  if(OrderType() == OP_SELL)
                    {
                     if(!OrderClose(OrderTicket(),OrderLots(),AskPrice(OrderSymbol()),SlipPage,Red))
                        ShowError("Close " + OrderCmd(OrderType()));
                    }
                  else
                    {
                     if(!OrderDelete(OrderTicket()))
                        ShowError("Delete Pending Order " + OrderCmd(OrderType()));
                    }
              }
            else
              {
               if(OrderType() == ordertype)
                 {
                  if(ordertype == OP_BUY)
                    {
                     if(!OrderClose(OrderTicket(),OrderLots(),BidPrice(OrderSymbol()),SlipPage,Blue))
                        ShowError("Close " + OrderCmd(OrderType()));
                    }
                  else
                     if(ordertype == OP_SELL)
                       {
                        if(!OrderClose(OrderTicket(),OrderLots(),AskPrice(OrderSymbol()),SlipPage,Red))
                           ShowError("Close " + OrderCmd(OrderType()));
                       }
                     else
                       {
                        if(!OrderDelete(OrderTicket()))
                           ShowError("Delete Pending Order " + OrderCmd(OrderType()));
                       }
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
double TotalProfit()
  {
   if(TotalOrder() > 0)
     {
      profit = 0;

      for(int i = 0; i < OrdersTotal(); i++)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
               profit += OrderProfit()+OrderSwap()+OrderCommission();
           }
        }
     }

   return(profit);
  }
 
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void ShowError(string label)
  {
   string Error;
   int    error = GetLastError();

   Error        = StringConcatenate("Terminal: ",TerminalName(),"\n",
                                    label," error ",error,"\n",
                                    ErrorDescription(error));
   if(error > 2)
     {
      if(IsTesting())
         Comment(Error);
      else
         Alert(Error);
     }
  }

//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void AutoDigit()
  {
   POINT = MarketInfo(Symbol(),MODE_POINT);
   DIGIT = (int)MarketInfo(Symbol(),MODE_DIGITS);

   if(DIGIT == 3 || DIGIT == 5)
     {
      PT              = 10;
      SlipPage       *= 10;
      StopLoss       *= 10;
      TakeProfit     *= 10;
      TrailStartPip  *= 10;
      TrailStepPip   *= 10;
      PipStep        *= 10;
     }
  }
Post automatically merged:

Part2
Code:
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void last_price_buy()
  {
   int i;
   for(i=0; i0)
     {
      openbuylots = 0;
      openselllots = 0;    //  set the variables to zero
      for(int i=0; i OrderOpenTime())
              {
               EarliestOrder = OrderOpenTime();
               ftpb = OrderTakeProfit();
              }
           }
        }
     }
// Returns 0 if no matching orders
  }
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void FTPS()
  {
   ftps = 0;
   datetime EarliestOrder = D'2099/12/31';

   for(int i = 0; i < OrdersTotal(); i++)
     {
      if(OrderSelect(i, SELECT_BY_POS))
        {
         if(OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
           {
            if(EarliestOrder > OrderOpenTime())
              {
               EarliestOrder = OrderOpenTime();
               ftps = OrderTakeProfit();
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void FSLB()
  {
   fslb = 0;
   datetime EarliestOrder = D'2099/12/31';

   for(int i = 0; i < OrdersTotal(); i++)
     {
      if(OrderSelect(i, SELECT_BY_POS))
        {
         if(OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
           {
            if(EarliestOrder > OrderOpenTime())
              {
               EarliestOrder = OrderOpenTime();
               fslb = OrderStopLoss();
              }
           }
        }
     }
// Returns 0 if no matching orders
  }
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void FSLS()
  {
   fsls = 0;
   datetime EarliestOrder = D'2099/12/31';

   for(int i = 0; i < OrdersTotal(); i++)
     {
      if(OrderSelect(i, SELECT_BY_POS))
        {
         if(OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
           {
            if(EarliestOrder > OrderOpenTime())
              {
               EarliestOrder = OrderOpenTime();
               fsls = OrderStopLoss();
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void Manage_Layer()
  {
   last_price_buy();
   last_price_sell();
   last_lot_buy();
   last_lot_sell();

   int ticket = 0;

   double opencandle = iOpen(Symbol(),Period(),0);
   datetime candleTime   = iTime(Symbol(),Period(),0);

   int TTL_Buy=TotalOrder(OP_BUY);
   int TTL_Sell=TotalOrder(OP_SELL);
 
//===============================================================================================================
// Trend Order Buy
//===============================================================================================================
   if(TTL_Buy>0 && ((TTL_Buy+TTL_Sell) < MaxOrder))
     {

      nextbuy = NormalizeDouble(low_price_buy()-(PipStep*POINT),DIGIT);
      slbuy = 0;

      lotbuy  = SetupLot(OP_BUY);

      buy_state = 1;

      // layer by candle close
      if(Layer_Type==1 && last_bar_buy() == 0)
         buy_state = 0;
      if(buy_state == 1 && opencandle <= nextbuy && Layer_Type==1)
        {
         ticket = OrderSend(Symbol(),OP_BUY,lotbuy,AskPrice(),SlipPage,0,0,NamaEA +" " + (string)(TTL_Buy+1),MagicNumber,0,clrBlue);
        }

      // layer by grid
      if(buy_state == 1 && AskPrice() <= nextbuy && Layer_Type==0)
        {
         ticket = OrderSend(Symbol(),OP_BUY,lotbuy,AskPrice(),SlipPage,0,0,NamaEA +" " + (string)(TTL_Buy+1),MagicNumber,0,clrBlue);
        }
     }

   if((Hedge_Mode)&&(TTL_Sell==0)&&(TTL_Buy>=(Layer_Per_Group*(Hedge_Group_Start-1))) && ((TTL_Buy+TTL_Sell) < MaxOrder))
     {
      nextsell = NormalizeDouble(low_price_buy()-(PipStep*POINT),DIGIT);
      slsell = 0;
      lotsell  = SetupLot(OP_SELL);
      sell_state = 1;

      if(Layer_Type==1 && last_bar_sell() == 0)
         sell_state = 0;
      if(sell_state == 1 && opencandle >= nextsell && Layer_Type==1)
        {
         ticket = OrderSend(Symbol(),OP_SELL,Hedge_LotSize,BidPrice(),SlipPage,0,0,NamaEA +" Hedging 1st",MagicNumber,0,clrRed);

        }

      if(sell_state == 1 && BidPrice() >= nextsell && Layer_Type==0)
        {
         ticket = OrderSend(Symbol(),OP_SELL,Hedge_LotSize,BidPrice(),SlipPage,0,0,NamaEA +" Hedging 1st",MagicNumber,0,clrRed);

        }
     }

//===============================================================================================================
// Trend Order Sell
//===============================================================================================================
   if(TTL_Sell>0 && ((TTL_Buy+TTL_Sell) < MaxOrder))
     {

      nextsell = NormalizeDouble(high_price_sell()+(PipStep*POINT),DIGIT);
      slsell = 0;
      lotsell  = SetupLot(OP_SELL);

      sell_state = 1;

      if(Layer_Type==1 && last_bar_sell() == 0)
         sell_state = 0;
      if(sell_state == 1 && opencandle >= nextsell && Layer_Type==1)
        {
         ticket = OrderSend(Symbol(),OP_SELL,lotsell,BidPrice(),SlipPage,0,0,NamaEA +" " + (string)(TTL_Sell+1),MagicNumber,0,clrRed);

        }

      if(sell_state == 1 && BidPrice() >= nextsell && Layer_Type==0)
        {
         ticket = OrderSend(Symbol(),OP_SELL,lotsell,BidPrice(),SlipPage,0,0,NamaEA +" " + (string)(TTL_Sell+1),MagicNumber,0,clrRed);

        }
     }

   if((Hedge_Mode)&& (TTL_Buy==0) && (TTL_Sell>=(Layer_Per_Group*(Hedge_Group_Start-1)))&&(TTL_Sell>0) && ((TTL_Buy+TTL_Sell) < MaxOrder))
     {
      nextbuy = NormalizeDouble(high_price_sell()+(PipStep*POINT),DIGIT);
      slbuy = 0;

      lotbuy  = SetupLot(OP_BUY);
      buy_state = 1;

      if(Layer_Type==1 && last_bar_buy() == 0)
         buy_state = 0;
      if(buy_state == 1 && opencandle <= nextbuy && Layer_Type==1)
        {
         ticket = OrderSend(Symbol(),OP_BUY,Hedge_LotSize,AskPrice(),SlipPage,0,0,NamaEA +" Hedging 1st",MagicNumber,0,clrBlue);

        }

      if(buy_state == 1 && AskPrice() <= nextbuy && Layer_Type==0)
        {
         ticket = OrderSend(Symbol(),OP_BUY,Hedge_LotSize,AskPrice(),SlipPage,0,0,NamaEA +" Hedging 1st",MagicNumber,0,clrBlue);

        }
     }

  }
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void TargetProfit()
  {
   if(TotalOrder() > 0)
     {
      profit = 0;

      for(int i = 0; i < OrdersTotal(); i++)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
              {
               profit = profit+(OrderProfit()+OrderSwap()+OrderCommission());
              }
           }
        }

      if(TotalOrder() > 0)
         targetmoney = TakeMoney;

      if(TakeMoney > 0)
        {
         if(profit >= targetmoney)
            CloseAll = true;
           {
            if(CloseAll)
              {
               while(TotalOrder() > 0)
                 {
                  CloseOrder();
                 }
               Print("Close by CutProfit");
              }
           }
        }
     }
   if(TotalOrder() == 0)
     {
      CloseAll = false;

     }
  }

//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void TargetProfitPercent()
  {
   if(TotalOrder() > 0)
     {
      profit = 0;

      for(int i = 0; i < OrdersTotal(); i++)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
              {
               profit = profit+(OrderProfit()+OrderSwap()+OrderCommission());
              }
           }
        }

      if(TotalOrder() > 0)
         targetmoney = TakeMoney;

      if(TakeMoney > 0)
        {
         if(profit >= targetmoney*0.01*AccountBalance())
            CloseAll = true;
           {
            if(CloseAll)
              {
               while(TotalOrder() > 0)
                 {
                  CloseOrder();
                 }
               Print("Close by CutProfit");
              }
           }
        }
     }
   if(TotalOrder() == 0)
     {
      CloseAll = false;

     }
  }
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
void TargetLoss()
  {
   if(TotalOrder() > 0)
     {
      if(CutLossMoney > 0)
        {
         double target = -CutLossMoney;
         if(TotalProfit() <= target)
            CutAll = true;
        }

      if(CutAll)
        {
         CloseOrder();
         Print("Close by CutLossMoney");
        }
     }

   if(TotalOrder() == 0)
     {
      CutAll = false;

     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void TargetLossPercent()
  {
   if(TotalOrder() > 0)
     {
      if(CutLossMoneyPercent > 0)
        {
         double target = -CutLossMoneyPercent;
         if(TotalProfit() <= target*0.01*AccountBalance())
            CutAll = true;
        }

      if(CutAll)
        {
         CloseOrder();
         Print("Close by CutLossMoney");
        }
     }

   if(TotalOrder() == 0)
     {
      CutAll = false;

     }
  }
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
double BEP_buy()
  {
   int i;
   double need = 0;
   double lot_total_size = 0;
   last_price_buy();
   LotsTotal();
   double lot_used;
   if(Lots_By_Risk==0)
      lot_used = Lots_Manual;
   else
      lot_used = AccountBalance()*Lots_By_Risk/10000;

   for(i=0; i= 1)
     {
      tpbuy = NormalizeDouble(lpb+((BEP_buy()+TakeProfit)*POINT),DIGIT);
     }

   for(i=0; i= 1)
     {
      tpsell = NormalizeDouble(lps-((BEP_sell()+TakeProfit)*POINT),DIGIT);
     }

   for(i=0; i 0)
     {
      for(int i = OrdersTotal()-1; i >= 0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
            if(OrderSymbol() == Symbol())
              {
               if(OrderType() == orderType)
                  totalLots += OrderLots();
              }
           }
        }
     }

   return(totalLots);
  }
 

Vondereich

Trader
Aug 25, 2024
14
0
6
37
not sure is this related to spread or something related.. but when run on demo it just run fine.. because my ea has the ability to calculate the take profit to make profit, but using this broker cause me negative tp.. weird.. need to use trailing with larger take profit to run it
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,075
1,477
144
Odesa
www.earnforex.com
As you can see your code has been inserted incorrectly. It's best to attach the entire .mq4 if you expect someone to help you with this:
1725041067388.png
Showing some screenshots of the problem you are encountering would also help.
 

Vondereich

Trader
Aug 25, 2024
14
0
6
37
As you can see your code has been inserted incorrectly. It's best to attach the entire .mq4 if you expect someone to help you with this:
View attachment 29265
Showing some screenshots of the problem you are encountering would also help.
Sure, why not.. here my full source code.. really need a help :D thanks
 
Last edited by a moderator:

Vondereich

Trader
Aug 25, 2024
14
0
6
37
also some give the answer of +- the spread in order send, but using this keep the takeprofit moving in every second if the spread keep changing.. afraid been flagged by the broker..
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,075
1,477
144
Odesa
www.earnforex.com
Could you please post some screenshots demonstrating the problem? What you see going wrong and what you'd expect to see?
The TP calculation seem to depend on the account balance - perhaps, it's changing in your live account?
 

Vondereich

Trader
Aug 25, 2024
14
0
6
37
Could you please post some screenshots demonstrating the problem? What you see going wrong and what you'd expect to see?
The TP calculation seem to depend on the account balance - perhaps, it's changing in your live account?
In FBS broker. The take profit seems static. While in demo broker in run flawless. Means the tp is moving but in negative direction. When layering the ea create a new tp but base on the tp we set. Which is not right since the martingale when new entry is open. New tp with profit also created.

But in FBS the tp move too little. .00001 at for every layer.
Post automatically merged:

Because of that. I have to use trailing but it cost me take time to close all position
 

Vondereich

Trader
Aug 25, 2024
14
0
6
37
so today you can test the live market, and see the takeprofit.. when you set to let say 5, when layering, you will see the tp still go to 5 even with martingale..
 

Vondereich

Trader
Aug 25, 2024
14
0
6
37
For me, it uses 15 on the second layer:
View attachment 29319
It was 10 for the first layer, but was modified together with the second trade to the same level.
Normally if put 5 on tp. The second layer or 3rd and so on is more than 5.
Post automatically merged:

But using demo account is just fine. The tp will move normally. Real problem when run using live account like FBS broker.
Post automatically merged:

And especially running on cent account
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,075
1,477
144
Odesa
www.earnforex.com
Two potential issues I see here:

1. Is your FBS MT4 at Build 1421? If yes, then it's definitely the related MathCeil() problem.

2. If not, then I'd guess it's your last_price_buy() and last_price_sell() functions. They might not be necessarily finding the latest order. To make sure they are built similarly to your FTPB() and FTPS() - with actual time checks.
 

Vondereich

Trader
Aug 25, 2024
14
0
6
37
Two potential issues I see here:

1. Is your FBS MT4 at Build 1421? If yes, then it's definitely the related MathCeil() problem.

2. If not, then I'd guess it's your last_price_buy() and last_price_sell() functions. They might not be necessarily finding the latest order. To make sure they are built similarly to your FTPB() and FTPS() - with actual time checks.
yes., in my vps right now using 1421.. so how to solve it then ? can you pin point the problem
 

Vondereich

Trader
Aug 25, 2024
14
0
6
37
i asked the chatgpt.. is this code correct ?? replacing the mathceil to normalize digit ??


Code:
//+------------------------------------------------------------------+
//| Calculate Break-Even Point for Buy Orders                       |
//+------------------------------------------------------------------+
double BEP_buy()
{
    int i;
    double need = 0;
    double lot_used;
    
    last_price_buy();
    LotsTotal();
    
    if(Lots_By_Risk == 0)
        lot_used = Lots_Manual;
    else
        lot_used = AccountBalance() * Lots_By_Risk / 10000;

    for(i = 0; i < OrdersTotal(); i++)
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if(OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
            {
                double size_lot = OrderLots() / lot_used;
                need += (((OrderOpenPrice() - lpb) / POINT) * size_lot);
            }
        }
    }
    double bep = need / (openbuylots / lot_used);
    return(NormalizeDouble(bep, DIGIT)); // Use NormalizeDouble for precision
}

//+------------------------------------------------------------------+
//| Calculate Break-Even Point for Sell Orders                      |
//+------------------------------------------------------------------+
double BEP_sell()
{
    int i;
    double need = 0;
    double lot_used;

    last_price_sell();
    LotsTotal();

    if(Lots_By_Risk == 0)
        lot_used = Lots_Manual;
    else
        lot_used = AccountBalance() * Lots_By_Risk / 10000;

    for(i = 0; i < OrdersTotal(); i++)
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if(OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
            {
                double size_lot = OrderLots() / lot_used;
                need += (((lps - OrderOpenPrice()) / POINT) * size_lot);
            }
        }
    }
    double bep = need / (openselllots / lot_used);
    return(NormalizeDouble(bep, DIGIT)); // Use NormalizeDouble for precision
}
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,075
1,477
144
Odesa
www.earnforex.com
yes., in my vps right now using 1421.. so how to solve it then ? can you pin point the problem
It's a known bug in 1421. A potential workaround is to switch the CPU used by your VPS if there is such an option. But I'd rather downgrade to 1420.
i asked the chatgpt.. is this code correct ?? replacing the mathceil to normalize digit ??


Code:
//+------------------------------------------------------------------+
//| Calculate Break-Even Point for Buy Orders                       |
//+------------------------------------------------------------------+
double BEP_buy()
{
    int i;
    double need = 0;
    double lot_used;
   
    last_price_buy();
    LotsTotal();
   
    if(Lots_By_Risk == 0)
        lot_used = Lots_Manual;
    else
        lot_used = AccountBalance() * Lots_By_Risk / 10000;

    for(i = 0; i < OrdersTotal(); i++)
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if(OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
            {
                double size_lot = OrderLots() / lot_used;
                need += (((OrderOpenPrice() - lpb) / POINT) * size_lot);
            }
        }
    }
    double bep = need / (openbuylots / lot_used);
    return(NormalizeDouble(bep, DIGIT)); // Use NormalizeDouble for precision
}

//+------------------------------------------------------------------+
//| Calculate Break-Even Point for Sell Orders                      |
//+------------------------------------------------------------------+
double BEP_sell()
{
    int i;
    double need = 0;
    double lot_used;

    last_price_sell();
    LotsTotal();

    if(Lots_By_Risk == 0)
        lot_used = Lots_Manual;
    else
        lot_used = AccountBalance() * Lots_By_Risk / 10000;

    for(i = 0; i < OrdersTotal(); i++)
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if(OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
            {
                double size_lot = OrderLots() / lot_used;
                need += (((lps - OrderOpenPrice()) / POINT) * size_lot);
            }
        }
    }
    double bep = need / (openselllots / lot_used);
    return(NormalizeDouble(bep, DIGIT)); // Use NormalizeDouble for precision
}
It doesn't look correct to me, but yet again, your problem is most likely caused purely by the build 1421, not by my second point.
 

Vondereich

Trader
Aug 25, 2024
14
0
6
37
It's a known bug in 1421. A potential workaround is to switch the CPU used by your VPS if there is such an option. But I'd rather downgrade to 1420.

It doesn't look correct to me, but yet again, your problem is most likely caused purely by the build 1421, not by my second point.
so what is the solution ??
 

Vondereich

Trader
Aug 25, 2024
14
0
6
37
When I tried to update the latest build to my demo version, I encountered the same problem as with the real account. I identified that the issue came from the latest build, 1421. When you mentioned that MathCeil was causing the problem, I changed the code to use NormalizeDouble. It seems to be working now. Haha.
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,075
1,477
144
Odesa
www.earnforex.com
When I tried to update the latest build to my demo version, I encountered the same problem as with the real account. I identified that the issue came from the latest build, 1421. When you mentioned that MathCeil was causing the problem, I changed the code to use NormalizeDouble. It seems to be working now. Haha.
You have to understand that NormalizeDouble will produce different results than MathCeil on some inputs. Make sure such results will match your goals.
 

Vondereich

Trader
Aug 25, 2024
14
0
6
37
You have to understand that NormalizeDouble will produce different results than MathCeil on some inputs. Make sure such results will match your goals.
Yes, I know. It’s related to the modified Take Profit issue here. There are only two pieces of code involving MathCeil, and they are in the BEP code. you can check the full code in the mql4 code above.. and right now tested, seems working normally
 
Last edited: