Open candel EA + TS

MRT

Trader
Apr 29, 2022
4
0
6
33
Hello Forum!

I'm trying to code a very simple EA which opens a trade when the new / next candle occurs + TS .
Part of the code is working as I want and new trades are opening fine but TS simply doesn't work.
I was wondering if somebody would be so kind and help me fixing this code ?


Code:
#property strict
input double lot     = 0.07;  // lot
input int slippage   = 10;    // max slippage
input int      TP=1000; // Take Profiti
input int      SL=55; // Stop Loss
input int MagicNumber=100; // Magic Number
extern bool   ProfitTrailing = True;  
extern int    TrailingStop   = 20;     
extern int    TrailingStep   = 10;     
extern bool   UseSound       = True; 
extern string NameFileSound  = "expert.wav"; 

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
void start() {
  for (int i=0; iTrailingStop*pp) {
      if (OrderStopLoss()TrailingStop*pp) {
      if (OrderStopLoss()>pAsk+(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0) {
        ModifyStopLoss(pAsk+TrailingStop*pp);
        return;
      }
    }
  }
}

//+------------------------------------------------------------------+
//|                                                                  |
//|          :                                                       |
//|                        StopLoss                                  |
//+------------------------------------------------------------------+
void ModifyStopLoss(double ldStopLoss) {
  bool fm;

  fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
  if (fm && UseSound) PlaySound(NameFileSound);
}

datetime tlast;
int OnInit()
{
   tlast = INT_MAX;
   return INIT_SUCCEEDED;
}
void OnTick()
{
    if(Time[0] > tlast)
    {
        if(Open[1] >= Close[1])
            bool res = OrderSend(Symbol(),OP_SELL,lot,Bid,slippage,Bid+SL*Point,Bid-TP*Point,NULL,MagicNumber,0);
        else
        if(Open[1] <= Close[1])
            bool res = OrderSend(Symbol(),OP_SELL,lot,Bid,slippage,Bid+SL*Point,Bid-TP*Point,NULL,MagicNumber,0);

    }
    tlast = Time[0];
}
 

MRT

Trader
Apr 29, 2022
4
0
6
33
thx for noticing.
Now should be fine:

MQL4:
#property strict
input double lot     = 0.07;  // lot
input int slippage   = 10;    // max slippage
input int      TP=1000; // Take Profiti
input int      SL=55; // Stop Loss
input int MagicNumber=100; // Magic Number
extern bool   ProfitTrailing = True; 
extern int    TrailingStop   = 20;    
extern int    TrailingStep   = 10;    
extern bool   UseSound       = True;
extern string NameFileSound  = "expert.wav";
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
void start() {
  for (int i=0; i<OrdersTotal(); i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      TrailingPositions();
    }
  }
}
//+------------------------------------------------------------------+
//|sssssssssssssssssssssss                        |
//+------------------------------------------------------------------+
void TrailingPositions() {
  double pBid, pAsk, pp;
  pp = MarketInfo(OrderSymbol(), MODE_POINT);
  if (OrderType()==OP_BUY) {
    pBid = MarketInfo(OrderSymbol(), MODE_BID);
    if (!ProfitTrailing || (pBid-OrderOpenPrice())>TrailingStop*pp) {
      if (OrderStopLoss()<pBid-(TrailingStop+TrailingStep-1)*pp) {
        ModifyStopLoss(pBid-TrailingStop*pp);
        return;
      }
    }
  }
  if (OrderType()==OP_SELL) {
    pAsk = MarketInfo(OrderSymbol(), MODE_ASK);
    if (!ProfitTrailing || OrderOpenPrice()-pAsk>TrailingStop*pp) {
      if (OrderStopLoss()>pAsk+(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0) {
        ModifyStopLoss(pAsk+TrailingStop*pp);
        return;
      }
    }
  }
}
//+------------------------------------------------------------------+
//|                                                                  |
//|          :                                                       |
//|                        StopLoss                                  |
//+------------------------------------------------------------------+
void ModifyStopLoss(double ldStopLoss) {
  bool fm;
  fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
  if (fm && UseSound) PlaySound(NameFileSound);
}
datetime tlast;
int OnInit()
{
   tlast = INT_MAX;
   return INIT_SUCCEEDED;
}
void OnTick()
{
    if(Time[0] > tlast)
    {
        if(Open[1] >= Close[1])
            bool res = OrderSend(Symbol(),OP_SELL,lot,Bid,slippage,Bid+SL*Point,Bid-TP*Point,NULL,MagicNumber,0);
        else
        if(Open[1] <= Close[1])
            bool res = OrderSend(Symbol(),OP_SELL,lot,Bid,slippage,Bid+SL*Point,Bid-TP*Point,NULL,MagicNumber,0);
    }
    tlast = Time[0];
}
 
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
18,535
1,355
144
Odesa
www.earnforex.com
I don't think your start() function is launching.
Also, I'd use brackets inside the conditions to avoid ambiguity.
Also, your default settings mean that the trailing stop will only work when the position is in profit that is at least as large as a trailing stop.