Discussion of article "Cross-Platform Expert Advisor: Stops" - page 2

 

I'm trying to change stop loss for main stop... How do I do that?

That's what I'm doing:

COrder *order=orders.At(i);

....

COrderStop *order_stop=order.MainStop();
double new_sl=...;
order_stop.StopLoss(new_sl);
order.MainStop(order_stop);
order.CheckStops();

I really broke my head trying to do that...

Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
Before you proceed to study the trade functions of the platform, you must have a clear understanding of the basic terms: order, deal and position...
 

It's not updating because of checking in COrderStopVirtualBase::Update

if(CheckPointer(m_objsl))
  {
   double sl_line=m_objsl.GetPrice();
   if(sl_line!=StopLoss())
      stoploss= sl_line;
  }

StopLoss() returns new value but sl_line still old...

 
Enrico Lambino:
Hi Shep,

Thank you for pointing these out. I have not noticed this until now. I will update the code.

Yes, I am aware of this. This is still due to the forward declarations. CStop requires instances of COrder and COrderStop in its methods. The same also when you try to compile CStop. All these classes that require each other should be compiled together. It would give errors if each is compiled on its own. For example:

This will not compile. You have to declare a forward declaration so the compiler would recognize the class member m_object2 within the object1 class:

This is ok if Object1 and Object2 are in the same file. If they are in two separate files, then you need to make a forward declaration for Object2 on the class file of Object1, and for Object1 on the class file Object 2.Then the compiler would return no errors if you compile either file. That is true, until you add methods to either of the two classes.

The current version of the compiler recognizes forward declarations, but not the listed methods of the class declared forwardly. That is why you do not receive error messages like "CStop - declaration without type", but you receive compiler error messages for the methods such as you the one you posted above. The compiler recognizes the class, but not the methods of the class that has been forwardly declared.


Hi Enrico,

Thanks for the quick response. You are quite right about the forward declarations.  I was worried that it might be a deeper problem than that.  It makes sense that the object using the forwardly declared class has no knowledge of that class's members.

I use a different model for my execution engine which uses the strategy pattern.  I have a few annoying problems with it, and was just taking a look at your library when I came across those issues.

Your work is really great, you have sound design and engineering skills and thanks for being kind enough to share.

Thanks, Shep.

 
Shephard Mukachi:

Hi Enrico,

Thanks for the quick response. You are quite right about the forward declarations.  I was worried that it might be a deeper problem than that.  It makes sense that the object using the forwardly declared class has no knowledge of that class's members.

I use a different model for my execution engine which uses the strategy pattern.  I have a few annoying problems with it, and was just taking a look at your library when I came across those issues.

Your work is really great, you have sound design and engineering skills and thanks for being kind enough to share.

Thanks, Shep.

Hi Shep,

You're welcome and thank you for letting me know. I recall I was also a bit puzzled when I ran into that issue with forward declarations.

Glad to hear that you have found the articles to be useful. I wish you all the best in your efforts in building your own EA engine.

Regards, Enrico

 
mbjen:

It's not updating because of checking in COrderStopVirtualBase::Update

StopLoss() returns new value but sl_line still old...

The purpose of COrderStopVirtualBase::Update is actually the opposite of what you intend. It is meant to adjust the sl/tp value when its own stop line is updated, usually from outside the EA (dragging on chart or directly altering the value on the object properties window). Use MoveStopLoss() and MoveTakeProfit() methods instead for virtual stops.

This is supposed to be for the next article, but if you are eager, you may want to take a look at the CheckTrailing() method of COrderStopBase. It modifies the order stop when eligible for trailing. The method applies to all three types:

bool COrderStopBase::CheckTrailing(void)
  {
   if(!CheckPointer(m_stop) || m_order.IsClosed() || m_order.IsSuspended() || 
      (m_stoploss_closed && m_takeprofit_closed))
      return false;
   double stoploss=0,takeprofit=0;
   string symbol=m_order.Symbol();
   ENUM_ORDER_TYPE type=m_order.OrderType();
   double price=m_order.Price();
   double sl = StopLoss();
   double tp = TakeProfit();
   if(!m_stoploss_closed)
      stoploss=m_stop.CheckTrailing(symbol,type,price,sl,TRAIL_TARGET_STOPLOSS);
   if(!m_takeprofit_closed)
      takeprofit=m_stop.CheckTrailing(symbol,type,price,tp,TRAIL_TARGET_TAKEPROFIT);
   if(!IsStopLossValid(stoploss))
      stoploss=0;
   if(!IsTakeProfitValid(takeprofit))
      takeprofit=0;
   return Modify(stoploss,takeprofit); //<---- this
  }

Alternatively, the CTrail class can also be used to alter sl/tp levels without having to retrieve an instance of an order stop (not just for trailing or breakeven).

 

I really enjoyed the article, it opened my eyes. Please, write a follow up article. I am really interested in how you would approach scalping with this tool.

Thanks a million for this and all your other work.