//+------------------------------------------------------------------+ //| Bogie-Fractal_TrailingStop.mq4 | //| Copyright © 2009, Bogie Enterprises | //| | //+------------------------------------------------------------------+ #property copyright "Copyright © 2009, Bogie Enterprises" //-----Inputs ------ Copy these to input section. extern bool UseFractalTrailingStop = true; extern int MagicNumber = 12345; // May already exist in your EA. double Fractal_Lower, Fractal_Upper; //----------------- //----------------- int init() { return(0); } //----------------- //----------------- int deinit() { return(0); } //---------------- //--------------- int start() { //------------- Copy this FractalTrailingStop code to the int start() section of your EA if(UseFractalTrailingStop) { if(_OrdersTotal() > 0) { Fractal_Lower = NormalizeDouble(iFractals(NULL,0,2,2),Digits); Fractal_Upper = NormalizeDouble(iFractals(NULL,0,1,2),Digits); for(int nCnt = 0; nCnt < _OrdersTotal(); nCnt++) { OrderSelect(nCnt, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if(OrderType()==OP_BUY) BuyModify(); if(OrderType()==OP_SELL) SellModify(); } } } } //---------- End of FractalTrailingStop code ------------------ //--------- End of Start() --------------------- return(0); } //-------------- //--------- Add all of this remaining code after the int start() section of EA //----------------- Modify Long Position ---------------------- void BuyModify () { if(Fractal_Lower>0 && Fractal_Lower > OrderStopLoss()) { if(IsTradeContextBusy()) { Print("Trade context is busy! The expert cannot modify Buy position!"); return(-1); } OrderModify(OrderTicket(), OrderOpenPrice(), Fractal_Lower, OrderTakeProfit(), 0, Aqua); } return(0); } //---------------- Modify Short Position ------------------------ void SellModify () { if(Fractal_Upper>0 && Fractal_Upper < OrderStopLoss()) { if(IsTradeContextBusy()) { Print("Trade context is busy! The expert cannot modify Sell position!"); return(-1); } OrderModify(OrderTicket(), OrderOpenPrice(), Fractal_Upper, OrderTakeProfit(), 0, Aqua); } return(0); } //---------------- Current Open Positions ------------------------ int _OrdersTotal() { int _total=0; for (int i=0; i