//+------------------------------------------------------------------+ //| TralingStop.mq4 | //| Copyright © 2005, Cleon Adonis Santos | //| www.fw380.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2005, Cleon Adonis Santos" #property link "www.fw380.com/forex" #include extern bool ShowAlert = true; extern int BarsNum = 5; extern int PipDistance = 1; extern bool UseValidStopLoss = true; double LTS, STS, Spread; double myPoint; bool new_bar = false; datetime last_tick; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { myPoint = SetPoint(); //---- return(0); } int start() { int count = 0, err; double OSL; // OrderStopLoss // Avoid too many OrderModifies on same bar as high and low of past bars has not changed. if (Time[0] == last_tick) new_bar = false; else { new_bar = true; last_tick = Time[0]; } if (!new_bar) return(0); // ----- Spread calculation ----- Spread = MarketInfo(Symbol(), MODE_SPREAD) * myPoint; // ----- Stop Loss Calculation ----- LTS = NormalizeDouble(Low[Lowest(NULL, 0, MODE_LOW, BarsNum, 1)] - (PipDistance * myPoint), Digits); STS = NormalizeDouble(High[Highest(NULL, 0, MODE_HIGH, BarsNum, 1)] + Spread + (PipDistance * myPoint), Digits); if (UseValidStopLoss) { LTS = ValidStopLoss(OP_BUY, Bid, LTS); STS = ValidStopLoss(OP_SELL, Ask, STS); } Comment("LTS: ", LTS, " - STS: ", STS); if (OrdersTotal() != 0) { while (count < OrdersTotal()) { OrderSelect(count, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol()) { OSL = OrderStopLoss(); OSL = NormalizeDouble(OSL, Digits); // Sometimes broker returns current stoploss with extra digits if (OrderType() == OP_BUY && (LTS > OSL || OSL == 0)) { OrderModify(OrderTicket(), OrderOpenPrice(), LTS, OrderTakeProfit(), 0, Blue); err = GetLastError(); if (err > 0 && ShowAlert) Alert("OP_BUY Error ", err + ":" + ErrorDescription(err), " - ", LTS, " - ", OrderTicket()); } if (OrderType() == OP_SELL && (STS < OSL || OSL == 0)) { OrderModify(OrderTicket(), OrderOpenPrice(), STS, OrderTakeProfit(), 0, Red); err = GetLastError(); if (err > 0 && ShowAlert) Alert("OP_SELL Error: ", err + ":" + ErrorDescription(err), " - ", STS, " - ", OrderTicket()); } if (OrderType() == OP_BUYSTOP && (LTS > OSL || OSL == 0)) { OrderModify(OrderTicket(), OrderOpenPrice(), LTS, OrderTakeProfit(), 0, Blue); err = GetLastError(); if (err > 0 && ShowAlert) Alert("OP_BUYSTOP Error: ", err + ":" + ErrorDescription(err), " - ", LTS, " - ", OrderTicket()); } if (OrderType() == OP_SELLSTOP && (STS < OSL || OSL == 0)) { OrderModify(OrderTicket(), OrderOpenPrice(), STS, OrderTakeProfit(), 0, Red); err = GetLastError(); if (err > 0 && ShowAlert) Alert("OP_SELLSTOP Error: ", err + ":" + ErrorDescription(err), " - ", STS, " - ", OrderTicket()); } } count++; } } return(0); } double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } double ValidStopLoss(int type, double price, double SL) { double mySL; double minstop; minstop = MarketInfo(Symbol(),MODE_STOPLEVEL); if (Digits == 3 || Digits == 5) minstop = minstop / 10; mySL = SL; if (type == OP_BUY) { if((price - mySL) < minstop*myPoint) mySL = price - minstop*myPoint; } if (type == OP_SELL) { if((mySL-price) < minstop*myPoint) mySL = price + minstop*myPoint; } return(NormalizeDouble(mySL,MarketInfo(Symbol(), MODE_DIGITS))); }