for(int tstepb=0; tstepb>=0; tstepb++) //Why use a loop, especially a never-ending loop { if(Bid-OrderOpenPrice()>=(WhenToTrail*Point)+(TrailingStep*tstepb*Point)) if(OrderStopLoss()<Bid-(TrailingStep*Point)) { bool buytstep=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(TrailingStep*Point),OrderTakeProfit(),0,clrBlue); } }
You are stuck in a never-ending loop and there is no need for the loop at all
You could consider something like
double new_stop_loss; if(Bid-OrderOpenPrice()>=WhenToTrail*Point) { if(OrderOpenPrice()-OrderStopLoss()>Point) new_stop_loss=OrderOpenPrice(); else new_stop_loss=Bid-WhenToTrail*Point; if(new_stop_loss-OrderStopLoss()>=(TrailingStep*Point)) { bool buytstep=OrderModify(OrderTicket(),OrderOpenPrice(),new_stop_loss,OrderTakeProfit(),0,clrBlue); } }
That is for your buy orders, not compiled or tested.
Don't forget that for sell orders you will need to take into account orders that may not have an initial SL.
You are stuck in a never-ending loop and there is no need for the loop at all
You could consider something like
That is for your buy orders, not compiled or tested.
Don't forget that for sell orders you will need to take into account orders that may not have an initial SL.
You are stuck in a never-ending loop and there is no need for the loop at all
You could consider something like
That is for your buy orders, not compiled or tested.
Don't forget that for sell orders you will need to take into account orders that may not have an initial SL.
i tried rewriting for a sell trailing stop i wasn't successful double new_stop_loss; int WhenToTrail=100; int TrailingStep=20; if(Bid-OrderOpenPrice()>=WhenToTrail*Point) { if(OrderOpenPrice()-OrderStopLoss()>Point) new_stop_loss=OrderOpenPrice(); else new_stop_loss=Bid-WhenToTrail*Point; if(new_stop_loss-OrderStopLoss()>=(TrailingStep*Point)) { bool buytstep=OrderModify(OrderTicket(),OrderOpenPrice(),new_stop_loss,OrderTakeProfit(),0,clrBlue); } }
-
Please don't add text inside quoted text or CODE blocks, put it outside.
MQL4 forum editor problem - MQL4 programming forum (2015) -
if(Bid-OrderOpenPrice()>=WhenToTrail*Point)
You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.
-
Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
-
Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 -
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.)
Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
My GBPJPY shows average spread = 26 points, average maximum spread = 134.
My EURCHF shows average spread = 18 points, average maximum spread = 106.
(your broker will be similar).
Is it reasonable to have such a huge spreads (20 pip spreads) in EURCHF? - General - MQL5 programming forum (2022)
-
Simplify your code int WhenToTrail=100; int TrailingStep=20; double newSL=OrderClosePrice() + WhenToTrail*Point; // Ask + d double currSL=MathMax(OrderOpenPrice(), OrderStopLoss() -TrailingStep); // BE or better. if(currSL - newSL > _Point) { bool buytstep=OrderModify(OrderTicket(),OrderOpenPrice(),newSL,OrderTakeProfit(),0,clrBlue); }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hey guys,
I am trying to make a trailing step. Essentially like a trailing stop, but instead of moving the stop loss every point in profit, it only moves the stop loss after X amount in profit. Below is my code (tries to modify as soon as order enters and I get a constant OrderModify error 1):
Am I going about this incorrectly..? Is my logic incorrect..?
Any help is appreciated!