[Archive!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Couldn't go anywhere without you - 2. - page 13

 
Hi! Could you please tell me how to make a trailing stop or Breakeven in an EA, which works well separately from the EA, but can't be combined with one?
 
paukas:

Got it, thank you. How else can I write this condition? I need it to close everything only if there is a profit.
 
todem:
Hi! Could you please tell me how to make a trailing stop or Breakeven in an EA, which works well separately from the EA, but can't be combined with one?
//-------------------------------------------------------------------+
extern bool   Trailing       = True;
extern int    MinProfit      = 55;
extern int    TrailingStop   = 5;
extern int    TrailingStep   = 1;
//-------------------------------------------------------------------+
extern bool   LossKiller     = True;
extern int    MinProfitB     = 21;
extern int    KillLossLevel  = 16;
//-------------------------------------------------------------------+
int start()
  {
//----
   if(LossKiller) KillLoss();
   if(Trailing) TrailPositions();
//+------------------------------------------------------------------+
//|-----------------------// Trail positions //----------------------+
//+------------------------------------------------------------------+
void TrailPositions()
{
  int Orders = OrdersTotal();
  for (int i=0; i<Orders; i++)
  {
    if (!(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))) continue;
    if (OrderSymbol() != Symbol()) continue;
    {    
      if (OrderType() == OP_BUY && OrderMagicNumber() == Magic)  
      {
        if (Bid-OrderOpenPrice() > MinProfit*Point) 
        {
          if (OrderStopLoss() < Bid-(TrailingStop+TrailingStep-1)*Point) 
          {
          OrderModify(OrderTicket(), OrderOpenPrice(), Bid-TrailingStop*Point,OrderTakeProfit(), 0, Gold);           
          }
        }
      }   
      if (OrderType() == OP_SELL && OrderMagicNumber() == Magic)  
      {
        if (OrderOpenPrice()-Ask > MinProfit*Point) 
        {
          if (OrderStopLoss() > Ask+(TrailingStop+TrailingStep-1)*Point) 
          {
          OrderModify(OrderTicket(), OrderOpenPrice(), Ask+TrailingStop*Point,OrderTakeProfit(), 0, Gold);           
          }
        }
      }   
    }   
  }  
}
//+------------------------------------------------------------------+
//|--------------------------// KillLoss //--------------------------+
//+------------------------------------------------------------------+
void KillLoss()
{
  int Orders = OrdersTotal();
  for (int i=0; i<Orders; i++)
  {
    if(!(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))) continue;
    if(OrderSymbol() != Symbol()) continue;
    {    
      if(OrderType()==OP_BUY && (OrderStopLoss()<OrderOpenPrice() || OrderStopLoss()==0) && OrderMagicNumber()==Magic)
      {
        if(Bid-OrderOpenPrice() > MinProfitB*Point) 
        {
          if(OrderStopLoss() < Bid-(KillLossLevel-1)*Point) 
          {
          OrderModify(OrderTicket(), OrderOpenPrice(),OrderOpenPrice()+KillLossLevel*Point,OrderTakeProfit(), 0, Gold);          
          }
        }
      }        
      if(OrderType()==OP_SELL && (OrderStopLoss()>OrderOpenPrice() || OrderStopLoss()==0) && OrderMagicNumber()==Magic)
      {
        if(OrderOpenPrice()-Ask > MinProfitB*Point) 
        {
          if(OrderStopLoss() > Ask+(KillLossLevel-1)*Point) 
          {
          OrderModify(OrderTicket(), OrderOpenPrice(),OrderOpenPrice()-KillLossLevel*Point,OrderTakeProfit(), 0, Gold);          
          }
        }
      }   
    }   
  }  
}
//+------------------------------------------------------------------+
 
rustein:
Help fix, not closing all open positions...

Thank you.


Which ones won't close?

If they are losing, it's fine

 
VINAR:


Which ones won't close?

If they are unprofitable, that's fine

Only one is profitable :) Why ? Thanks.

I searched on the forum. But I don't understand...

 
Doberman101:

Greetings.

Help me write an EA.

Respectfully.

People should rush in with suggestions - each with their own... They haven't even decided on the basis of which TS you need an advisor... And why they didn't go to the "joba"... :))

If you need help writing code (or, better yet, understanding it), you're welcome, but if... then... to the job ... :)))

 
artmedia70:

People should rush out with offers - each with his or her own... They haven't even decided on the basis of which TC an adviser is needed... And why they didn't go to the "jobe"... :))

If you need help writing code (or, better yet, understanding it), you're welcome, but if... then... to the job ... :)))

Artem, all bring up?

Only the questioner needs something else.

The councillor is not plum and right now.

 
rustein:

Only one is profitable :) Why ? Thanks.

I searched the forum. But didn't figure it out...


AccountProfit() returns profit value for current account in base currency.

When the first profitable one is closed, it seems that AccountProfit() becomes less than 0.

You can write, for example, like this:

void CloseAllOrders()
{
  if(AccountProfit() > 0)
    for(int i=OrdersTotal()-1;i>=0;i--)
    {
      RefreshRates();
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
        if(OrderType()== OP_BUY) 
           OrderClose (OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),10,CLR_NONE);
        if(OrderType()== OP_SELL)
           OrderClose (OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),10,CLR_NONE);
      }
    }  
    return(0);
}
 
artmedia70:

People should rush out with offers - each with his or her own... They haven't even decided on the basis of which TC an adviser is needed... And why they didn't go to the "jobe"... :))

If you need help writing code (or, better yet, understanding it), you're welcome, but if ... then... to the job ... :)))


Why such a negative response? A grown man... and no one has taught you respect in life?

That's the end of the despute. I treat elders with respect, however "rotten" they may be.

 
VINAR:

Artem, are you bringing everything up?

Only the questioner needs something else.

The councillor is not plum and right now.


I asked for help writing code...

Where did you read that I need " EA is not draining and right now"?