Open candel EA + TS

 

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; 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];

}
Extract profit down to the last pip
Extract profit down to the last pip
  • www.mql5.com
The article describes an attempt to combine theory with practice in the algorithmic trading field. Most of discussions concerning the creation of Trading Systems is connected with the use of historic bars and various indicators applied thereon. This is the most well covered field and thus we will not consider it. Bars represent a very artificial entity; therefore we will work with something closer to proto-data, namely the price ticks.
 
Robert Stan:

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]

Which part of your code is failing?

Is the order not being modified or is the TrailingStop function never reaching a true condition?

Set a debug breakpoint, or use Print statements if using strategy tester, to find out where exactly the point of failure is.
 
Alexander Martinez #:

Which part of your code is failing?

Is the order not being modified or is the TrailingStop function never reaching a true condition?

Set a debug breakpoint, or use Print statements if using strategy tester, to find out where exactly the point of failure is.

order is not being modified by TS.