Open candel EA + TS

1
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: Select all


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

}


Who is online

Users browsing this forum: No registered users and 16 guests