Trailing Stop
Hi All,
Any suggestions and/or criticism for why my trailing stop isn't working?
Hope you can help!
What does it do i have a few TS ea's may do the trick
Let me know
Hi All,
Any suggestions and/or criticism for why my trailing stop isn't working?
Hope you can help!
what do you mean by "isn't working"?
double a = OrderModify(OrderTicket(), OrderOpenPrice(), Bid - 30 *_Point, 0, 0, Green); double b = OrderModify(OrderTicket(), OrderOpenPrice(), Ask + 30 * _Point, 0, 0, Red);
OrderModify is bool type.
- docs.mql4.com
Hi All,
Any suggestions and/or criticism for why my trailing stop isn't working?
Hope you can help!
Here is it working now
//+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- tStop(Symbol(),100,0); } //+------------------------------------------------------------------+ void tStop(string symb,int stop, int MN)// Symbol + stop in pips + magic number { double bsl=NormalizeDouble(MarketInfo(symb,MODE_BID)-stop*MarketInfo(symb,MODE_POINT),MarketInfo(symb,MODE_DIGITS)); double ssl=NormalizeDouble(MarketInfo(symb,MODE_ASK)+stop*MarketInfo(symb,MODE_POINT),MarketInfo(symb,MODE_DIGITS)); for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()==MN) if(OrderSymbol()==symb) if(OrderType()==OP_BUY && (OrderStopLoss()<bsl || OrderStopLoss()==0)) if(OrderModify(OrderTicket(),OrderOpenPrice(),bsl,OrderTakeProfit(),0,clrNONE)) { Print(symb+" Buy's Stop Trailled to "+(string)bsl); }else{ Print(symb+" Buy's Stop Trail ERROR"); } if(OrderType()==OP_SELL && (OrderStopLoss()>ssl || OrderStopLoss()==0)) if(OrderModify(OrderTicket(),OrderOpenPrice(),ssl,OrderTakeProfit(),0,clrNONE)) { Print(symb+" Sell's Stop Trailled to "+(string)ssl); }else{ Print(symb+" Sell's Stop Trail ERROR"); } } } //+------------------------------------------------------------------+
void TrailingStopTrail(int type, double TS, double step, bool aboveBE) //set Stop Loss to "TS" if price is going your way with "step" { int total = OrdersTotal(); TS = NormalizeDouble(TS, Digits()); step = NormalizeDouble(step, Digits()); for(int i = total-1; i >= 0; i--) { while(IsTradeContextBusy()) Sleep(100); if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if(OrderMagicNumber() != MagicNumber || OrderSymbol() != Symbol() || OrderType() != type) continue; RefreshRates(); if(type == OP_BUY && (!aboveBE || Ask > OrderOpenPrice() + TS) && (NormalizeDouble(OrderStopLoss(), Digits()) <= 0 || Ask > OrderStopLoss() + TS + step)) myOrderModify(OrderTicket(), Ask - TS, 0); else if(type == OP_SELL && (!aboveBE || Bid < OrderOpenPrice() - TS) && (NormalizeDouble(OrderStopLoss(), Digits()) <= 0 || Bid < OrderStopLoss() - TS - step)) myOrderModify(OrderTicket(), Bid + TS, 0); } } //Ontick TrailingStopTrail(OP_BUY, 25 * Point(), 50 * Point(), true); //Trailing Stop = trail TrailingStopTrail(OP_SELL, 25 * Point(), 50 * Point(), true); //Trailing Stop = trail
TS = NormalizeDouble(TS, Digits()); step = NormalizeDouble(step, Digits()); : myOrderModify(OrderTicket(), Ask - TS, 0);
- Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't
use it. It's use is always wrong
- SL/TP (stops) need to be normalized to tick size (not Point.) (On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 and MetaTrader 4 - MQL4 programming forum) and abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 and MetaTrader 4 - MQL4 programming forum
- Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on Metals. So do it right: Trailing Bar Entry EA - MQL4 and MetaTrader 4 - MQL4 programming forum or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 and MetaTrader 4 - MQL4 programming forum
- Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
-
You buy at the Ask and sell at the Bid.
- Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
-
Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at
a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3 - The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)
while(IsTradeContextBusy()) Sleep(100);
After Sleep and between server calls the market will have changed. You must update your variables. To use any pre-defined variables and series arrays you must call RefreshRates
RefreshRates - Timeseries and Indicators Access - MQL4 Reference
Hi All,
Any suggestions and/or criticism for why my trailing stop isn't working?
Hope you can help!