Questions from Beginners MQL5 MT5 MetaTrader 5 - page 828

 
Greetings. The question is more of a logical one.
The so-called anti-martingale system, with a limit on the number of increases. If you close on profit, the next lot is increased by a factor. If there is a loss, or if the number of profitable positions in a row exceeds the specified number, the lot is equal to the starting lot.
Once the specified number of profitable positions is exceeded, how do I start the cycle of increasing again?
int K_U_=5;
double  LotExponent=2.00;
//+------------------------------------------------------------------+
double llot()
  {
   double lt1=LotLastCloPos(Symbol(),m_magic);//лот последней закрытой позиции
   if(CountProfit(Symbol(),m_magic)>=1 && CountProfit(Symbol(),m_magic)<K_U_)
      LotSize=lt1*LotExponent;//
   else
      LotSize=Lots;
   return(LotSize);
  }


/

 
lil_lil:
Greetings. The question is more of a logical one.
The so-called anti-martingale system, with a limit on the number of increases. If you close on profit, the next lot is increased by a factor. If there is a loss, or if the number of profitable positions in a row exceeds the specified number, the lot is equal to the starting lot.
Once the specified number of profitable positions is exceeded, how do I start the cycle of increasing again?


/

Example inStop Loss Take Profit code: lot size is stored (overwritten, increased and reset to minimum) in the "ExtLot" variable, declared at global program level (in the header). OnTradeTransaction() catches trades of the "Market Exit" type and checks how the trade was closed - at Take Profit or Stop Loss:

         if(deal_entry==DEAL_ENTRY_OUT)
           {
            if(deal_reason==DEAL_REASON_SL)
               ExtLot*=2.0;
            else if(deal_reason==DEAL_REASON_TP)
               ExtLot=m_symbol.LotsMin();
           }

If at Stop Loss, we double the lot, if at Take Profit, we reset the lot size to its minimum value.


Note: in the Expert Advisor itself the calculation of Stop Loss and Take Profit is a little bit incorrect - but the code will be republished soon.

 
Vladimir Karputov:

Example inStop Loss Take Profit code: lot size is stored (overwritten, increased and reset to minimum) in "ExtLot" variable declared on global program level (in the header). OnTradeTransaction() catches trades of the "Market Exit" type and checks how the trade was closed - at Take Profit or Stop Loss:

If at Stop Loss, we double the lot, if at Take Profit, we reset the lot size to its minimum value.


Note: in the Expert Advisor is slightly incorrect calculation of Stop Loss and Take Profit - but soon republished code.

I calculate the number of profitable trades going in a row, if it exceeds 5, then I return to the initial lot, position 6 opens with the initial lot and position 7 should open with volume = lot 6 multiplied by the coefficient, etc. When there are 5 more profitable positions, go back to the initial lot.

Profitable positions are 20 in a row. Their lots should be 1, 2, 4, 8,16, 1, 2, 4, 8,16, 1, 2, 4, 8,16, 1, 2, 4, 8,16


 
lil_lil:

I calculate the number of profitable trades in a row, if it exceeds 5, then I return to the initial lot, position 6 opens with the initial lot and position 7 should open with volume = lot 6 multiplied by the coefficient, etc. When there are 5 more profitable positions, go back to the initial lot.

Profitable positions are 20 in a row. Their lots should be 1, 2, 4, 8,16, 1, 2, 4, 8,16, 1, 2, 4, 8,16, 1, 2, 4, 8,16


You need to use a static variable in the lot counting function. Here's how it works:

//+------------------------------------------------------------------+
//|                                                       Test_1.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart(void)
  {
   static int counter=0;
   for(int i=0;i<18;i++)
     {
      Print(counter);
      counter++;
      if(counter==5)
         counter=0;
     }
  }
//+------------------------------------------------------------------+

Result:

0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
Files:
Test_1.mq5  2 kb
 
Vladimir Karputov:

You need to use a static variable in the lot counting function. Here's how it works:

Result:

I reset according to your example, but I can't get the order of the lot increase

There are 20 profitable positions, in a row. Their lots should be 1, 2, 4, 8,16, 1, 2, 4, 8,16, 1, 2, 4, 8,16, 1, 2, 4, 8,16 but it should be1, 2, 4, 8,16, 1, 1, 1, 1, 1,............

What is wrong?

int K_U_=5;  double   Lots=1.0;
double  LotExponent=2.00;
//+------------------------------------------------------------------+
double llot()
  {
   double lt1=LotLastCloPos(Symbol(),m_magic);
   if(CountProfit(Symbol(),m_magic)>=1 && CountProfit(Symbol(),m_magic)<K_U_)
      LotSize=lt1*LotExponent;//
   else
      LotSize=Lots;
   return(LotSize);
  }
  
//+------------------------------------------------------------------

int CountProfit(const string Symb,const long MagicNumber=0)
  {
    int counter=0;
   ulong Ticket;

   if(HistorySelect(0,LONG_MAX))
      for(int i=HistoryDealsTotal()-1; i>=0; i--){
      
         if((bool)(Ticket=HistoryDealGetTicket(i)) && 
            (HistoryDealGetInteger(Ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT) &&
            (HistoryDealGetInteger(Ticket, DEAL_MAGIC) == MagicNumber) &&
            (HistoryDealGetString(Ticket, DEAL_SYMBOL) == Symb))
           {
            if(HistoryDealGetDouble(Ticket,DEAL_PROFIT)>0)
            counter++; else
               break;
           }
           }
   if(counter==K_U_)
      counter=0;
   return(counter);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double LotLastCloPos(const string Symb,const long MagicNumber=0)
  {
   ulong Ticket;
   double lot_=0;

   if(HistorySelect(0,LONG_MAX))
      for(int i=0; i<HistoryDealsTotal(); i++)
         if((bool)(Ticket=HistoryDealGetTicket(i)) && 
            (HistoryDealGetInteger(Ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT) &&
            (HistoryDealGetInteger(Ticket, DEAL_MAGIC) == MagicNumber) &&
            (HistoryDealGetString(Ticket, DEAL_SYMBOL) == Symb))
           {

            lot_=HistoryOrderGetDouble(Ticket,ORDER_VOLUME_INITIAL);

           }

   return(lot_);
  }
 
lil_lil:

I am zeroing according to your example, but I am not getting the order of the lot increase

There are 20 profitable positions, in a row. Their lots should be 1, 2, 4, 8,16, 1, 2, 4, 8,16, 1, 2, 4, 8,16, 1, 2, 4, 8,16 and it turns out so1, 2, 4, 8,16, 1, 1, 1, 1, 1,............

What's wrong?

So you still do not want to use OnTradeTransaction in a normal way? So, you prefer to request EXACTLY the entire trading history (since 1970)? Why?

 
Vladimir Karputov:

So you don't want to work properly with OnTradeTransaction? So you prefer to request ABSOLUTELY ALL trading history (since 1970)? Why???

Found a reason, brackets.

What do you mean I don't want to, what doesOnTradeTransaction have to dowith my question ;)

 

Now, I see, two posts before my question, an example of the calculation.

Thank you.

 
lil_lil:

Found the reason, brackets.

What do you mean I don't want to, what doesOnTradeTransaction have to dowith my question;)

Just asking for trading history

HistorySelect(0,LONG_MAX)

means requesting all, all, all of the history from that trading account since 1970. This is very sub-optimal:

  • what if there are thousands of trades in that account?
  • What if (God forbid) you make this request on every tick?

 

Please share the functions that work with the trading history.

I need to get information about the date of last trade opening, closing, volume, financial result and its type (buy or sell).

If you have any similar functions, please share them.

Reason: