Discussion of article "Using limit orders instead of Take Profit without changing the EA's original code" - page 2

 
Dmitriy Gizlyk #:

Hi, you must add * before paprameter

Hi Dmitry,


Something is still not working, the same three lines gives the following errors:

- parameter passed as reference, variable expected LimitTakeProfit.mqh 54 57

- redefinition; different type modifiers LimitTakeProfit.mqh 54 35


Thank you for answering!

 

Hi Dmitry,

I know this article was published a while ago, but I wanted to thank you for it! It is very useful, your explanation is really clear, and the class works like a charm. Thank you!

I believe I found a minor bug in the posted code. Halfway through function SetTakeProfits, we find this line:

switch((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE))

This is inside a loop through all TP. The problem is that later in the loop, function CheckLimitOrder is called, who in turn calls function CheckOrderInHistory, and there another Position may be selected. This means that in the next iteration of the loop, the "switch" in the code above may be different.

To solve this, I believe that position_type should be stored in a variable, before the loop begins. Thus:

bool CLimitTakeProfit::SetTakeProfits(ulong position_ticket, double new_tp=0)

// (...)

   double position_volume=PositionGetDouble(POSITION_VOLUME);

//********** ADD THE NEXT LINE
   ENUM_POSITION_TYPE position_type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
//****************************

   double closed=0;
   double closed_perc=0;
   double fix_closed_per=0;
//---
   for(int i=0;i<total;i++)
     {
      tp_request.comment="TP"+IntegerToString(i)+"_"+IntegerToString(position_ticket);
      if(i_TakeProfit.At(i)<tp_int && d_TakeProfit.At(i)>0)
        {
         if(closed>=100 || fix_closed_per>=100)
            break;
//---
         double lot=position_volume*MathMin(d_TakeProfit.At(i),100-closed_perc)/(100-fix_closed_per);
         lot=MathMin(position_volume-closed,lot);
         lot=c_Symbol.LotsMin()+MathMax(0,NormalizeDouble((lot-c_Symbol.LotsMin())/c_Symbol.LotsStep(),0)*c_Symbol.LotsStep());
         lot=NormalizeDouble(lot,2);
         tp_request.volume=lot;

//********** MODIFY THE NEXT LINE
         //switch((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE))
         switch(position_type)
//*******************************
           {
            case POSITION_TYPE_BUY:
              tp_request.type=ORDER_TYPE_SELL_LIMIT;
              tp_request.price=c_Symbol.NormalizePrice(open_price+i_TakeProfit.At(i)*c_Symbol.Point());

// (...)

Hope this makes sense. Again, thanks for an excellent job!

 

Hello everyone!

I'm having a [Invalid expiration] problem, anyone knows how to fix it?

 
Marcel Cardoso #:

Hello everyone!

I'm having a [Invalid expiration] problem, anyone knows how to fix it?

I have the same problem, did you manage to solve it?

Reason: