Experts: TrailingTakeProfit

 

TrailingTakeProfit:

This non-trading Expert Advisor is designed for the simple trailing of take profit orders of open positions.


Author: Scriptor

 
//+------------------------------------------------------------------+
//| Modifies the position selected on the ticket |
//+------------------------------------------------------------------+
bool PositionModifyByTicket(const string symbol_name,const ulong magic_number,const ulong ticket,const double sl,const double tp)
  {
//--- check stopped
   if(IsStopped())
      return(false);
//--- clean
   ZeroMemory(g_request);
   ZeroMemory(g_result);
//--- setting request
   g_request.action  =TRADE_ACTION_SLTP;
   g_request.position=ticket;
   g_request.symbol  =symbol_name;
   g_request.magic   =magic_number;
   g_request.sl      =sl;
   g_request.tp      =tp;
//--- action and return the result
   return(OrderSend(g_request,g_result));
  }

Superfluous.

 
//+------------------------------------------------------------------+
//|| Trailing function|
//+------------------------------------------------------------------+
void Trailing(void)
  {
   int   total=PositionsTotal();
   for(int i=total; i>=0; i--)
     {
      ulong ticket=PositionGetTicket(i);
      if(ticket==0) continue;
      string symbol_name=PositionGetString(POSITION_SYMBOL);
      ulong  magic_number=(ulong)PositionGetInteger(POSITION_MAGIC);
      ENUM_POSITION_TYPE type=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
      if(InpPositionType>WRONG_VALUE && type!=(ENUM_POSITION_TYPE)InpPositionType) continue;

It is very expensive (especially string) to create dedicated variables before all continue checks.


Imagine a simple optimisation for 100,000 passes of 10,000,000 ticks. Then OnTick will be called one TRILLION times. Any extra action called a trillion times will take extra seconds/minutes/hours. Value your own and others' time.

 
fxsaber:

It is very expensive (especially string) to create dedicated variables before all continue checks.


Imagine a simple optimisation for 100,000 passes of 10,000,000 ticks. Then OnTick will be called one TRILLION times. Any extra action called a trillion times will take extra seconds/minutes/hours. Value your time and other people's time.

This is not a trading EA. Why optimise it? It's just a trawl. And a weird one at that.
 
Artyom Trishkin:
This is not a trading EA. Why optimise it? It's just a trawl. And a weird one at that.

It's a migrating Trailing function for training purposes.

 
fxsaber:

This is a migrating Trailing function for training purposes.

It is not sufficient to be migrating. Only in the context of this code.
 
Artyom Trishkin:
Insufficient for migrating. Only in the context of the code in question.

Migratory - when 90% of the code is borrowed.