Increment take profit and Lot size - page 2

 
William Roeder #:

Open or pending.

MT4:

  1. Do not assume history has only closed orders.
              OrderType() == 6, 7 in the history pool? - MQL4 programming forum #4 and #5 (2017)

  2. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

  3. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

    Broker History
    FXCM
    Commission - «TICKET»
    Rollover - «TICKET»

    >R/O - 1,000 EUR/USD @0.52

    #«ticket»  N/A
    OANDA
    Balance update
    Financing (Swap: One entry for all open orders.)

I am yet to find solution to my problem could somebody help me here is the full code I have written, your assistance will be appreciated.

//+------------------------------------------------------------------+
//|                                                 Ml_Fiverr_V1.mq4 |
//|                                        Copyright 2023,Dev Ernest |
//|                                          ernestakoyi56@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023,Dev Ernest"
#property link      "ernestakoyi56@gmail.com"
#property version   "1.00"
#property strict


extern int magic_number=1111;
extern int ticks_apertura=100;
extern float lotto_inizio=0.1;
extern float lotto_incr=0.02;
extern int take_profit=100;
extern float take_profit_molt=1.5;
extern int n_op=4;
extern int stop_loss=1000;
//extern int periodo_media=10;

extern int                   ma_period         = 10; //Period
extern int                   ma_shift          = 0; //Shift
extern ENUM_MA_METHOD        ma_method         = MODE_SMA; //MA method
extern ENUM_APPLIED_PRICE    ma_price          = PRICE_CLOSE; //Apply to
bool starting=true;
int verso_aperture=0;
int posCounter=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   closeAllMarketPositions();
   posCounter=0;
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
  // double mm=iMA(Symbol(),Period(),periodo_media,0,0,0,0);
  double mm = iMA(Symbol(),0,ma_period,ma_shift,ma_method, ma_price,1);
   if(starting)
   {
      
      //definisce orientamento iniziale rispetto media
      closeAllMarketPositions();
      if(Bid < mm)
         verso_aperture = 1;
      else
         verso_aperture = -1;
      if(verso_aperture!=0)
         starting=false;
   }
   
   openIfNeeded();
   closeForTP();
   UpdateLotAndTP();
}

void openIfNeeded()
{
 // double mm=iMA(Symbol(),Period(),periodo_media,0,0,0,0);
  double mm = iMA(Symbol(),0,ma_period,ma_shift,ma_method, ma_price,1);
  if(verso_aperture>0)
  {
    if(Bid < mm - ticks_apertura*Point)
    {
      if(countLongOpenOrders()==0)
      {
         closeAllMarketPositions();
         
         posCounter++;
         int opened=OrderSend(Symbol(),OP_BUY,calcLotto(),Ask,50,Ask-stop_loss*Point,0,""+posCounter,magic_number);
         if(opened>0)
         {

            verso_aperture=-1;
         }
         else
            posCounter--;
      }
    }
  
  }
  else if(verso_aperture<0)
  {
    if(Bid > mm + ticks_apertura * Point)
    {
      if(countShortOpenOrders()==0)
      {
         closeAllMarketPositions();
        
         posCounter++;
         int opened=OrderSend(Symbol(),OP_SELL,calcLotto(),Bid,50,Bid+stop_loss*Point,0,""+posCounter,magic_number);
         if(opened>0)
         {
            
            verso_aperture=1;
         }
         else
            posCounter--;
      }
    }
  }
 
}

void closeForTP()
{
   for(int i=0;i<OrdersTotal();i++)
   {
      if(OrderSelect(i,SELECT_BY_POS))
      {
         if(OrderMagicNumber()==magic_number)
         {
            if(OrderType()==OP_BUY)
            {
               
               if(Bid-OrderOpenPrice()>=take_profit*Point)
                  if(OrderClose(OrderTicket(),OrderLots(),Bid,40*Point))
                  {
                     verso_aperture=-1;
                     i--;
                     posCounter=0;
                  }
               
            }
            else if(OrderType()==OP_SELL)
            {
               
               if(OrderOpenPrice()-Ask>=take_profit*Point)
                 if(OrderClose(OrderTicket(),OrderLots(),Ask,40*Point))
                  {
                     verso_aperture=1;
                     i--;
                     posCounter=0;
                  }
            }
         }
      }
   }
}

double calcLotto()
{

   return lotto_inizio;
 
}

int countLongOpenOrders()
{
   int orders=0;
   for(int i=0;i<OrdersTotal();i++)
   {
      if(OrderSelect(i,SELECT_BY_POS))
      {
         if(OrderMagicNumber()==magic_number && OrderType()==OP_BUY)
            orders++;
      }
   }
   return orders;
}

int countShortOpenOrders()
{
   int orders=0;
   for(int i=0;i<OrdersTotal();i++)
   {
      if(OrderSelect(i,SELECT_BY_POS))
      {
         if(OrderMagicNumber()==magic_number && OrderType()==OP_SELL)
            orders++;
      }
   }
   return orders;
}

void closeAllMarketPositions()
{
   for(int i=0;i<OrdersTotal();i++)
   {
      if(OrderSelect(i,SELECT_BY_POS))
      {
         if(OrderMagicNumber()==magic_number)
         {
            if(OrderType()==OP_BUY)
            {
               if(OrderClose(OrderTicket(),OrderLots(),Bid,40*Point))
                  i--;
               
            }
            else if(OrderType()==OP_SELL)
            {
               if(OrderClose(OrderTicket(),OrderLots(),Ask,40*Point))
                  i--;
            }
         }
      }
   }
   
}
//+------------------------------------------------------------------+
void UpdateLotAndTP(int successiveLosses)
  {
     double y = (successiveLosses + 1) / n_op;
     
     if(y >= 1 && (int)y == y)
       {
          double newLot = lotto_inizio + lotto_incr * y;
          int newTP = (take_profit * Point) + (take_profit_molt * take_profit) * y;
          
           lotto_inizio = newLot;
           take_profit = newTP;
       }
  }
//+------------------------------------------------------------------+
int  countSuccessiveLosses()
  {
      int consecutiveLosses = 0;
      int totalOrders = OrdersHistoryTotal();
      
      for(int i = totalOrders - 1; i >= 0; i--)
         {
             if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))continue;
             if(OrderSymbol() != Symbol())continue;
             
             if(OrderProfit() < 0)
               {
                  consecutiveLosses++;
               }
             else
               {
                  break;
               }  
         }
      return consecutiveLosses;   
  }
 
William Roeder #:
Do not assume history is ordered by date, it's not.

But in MQL4, new orders will be added to the end of the history (unlike MQL5). This can be used.

Pseudocode:

void CHistOrdersCacheBase::onTick(void)
  {
   int total = OrdersHistoryTotal();
   for(int i = m_prevTickTotal; i < total; i++)
     {
      //...
     }
   m_prevTickTotal = total;
  }
 
Vladislav Boyko #:
unlike MQL5

This is a sore subject for me😄

Forum on trading, automated trading systems and testing trading strategies

MT4 End Of Life

Vladislav Boyko, 2023.09.20 18:02

Regarding the possibility of conversion...

Below is an example of a problem whose solution is free (in terms of performance) for MT4 and obscenely expensive for MT5

(I translated as best I could using google translate)

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

Ошибки, баги, вопросы

fxsaber, 2023.06.01 22:20

Please comment on how to solve a simple problem in MT5.

  • When any pending order is deleted, the EA must report the ticket of that order.
  • From the OnTick function, without using OnTrade-functions.
  • The calculations don't have to be cumbersome. This is important because the history can contain hundreds of thousands of pending orders.

Here is an example of a solution for MT4, we need an analogue for MT5.

void DeleteOrders()
{
  static int PrevTotal = OrdersHistoryTotal();
  
  const int Total = OrdersHistoryTotal();
  
  while (PrevTotal < Total)
    if (OrderSelect(PrevTotal++, SELECT_BY_POS, MODE_HISTORY) && (OrderType() > OP_SELL))
      Alert(OrderTicket());
}

For me personally, this is the only reason why I don't want to switch to MT5.

Working with order history is very important for developing reliable advisors. MT5 developers have made working with order history prohibitively expensive. The terminal developers are ignoring repeated requests to comment on this.

(If any of the moderators consider this to be off-topic, just delete my posts)