if (_Ticket>0) //if order is open { if(!OrderSelect(1,SELECT_BY_POS,MODE_TRADES)) // << trailing stop only works, when Error {Print("Order selection error = ",GetLastError());
_Ticket what value does it have ??
why do you select 1 inside
OrderSelect(1,SELECT_BY_POS,MODE_TRADES)
without doing an orderloop. What is according to you happening at
if(!OrderSelect(1,SELECT_BY_POS,MODE_TRADES))
can you check also the OrderTicket() if you select 1 ??
OrderSelect(1,SELECT_BY_POS
This selects the second opened order (any EA/manual trades, any pair, any magic number.) If you only have one (or zero) opened this will fail.
If you already have the order data saved why do you need to select it again? Maybe to check if the order has closed (TP/SL) before modifying? Why not call your function (OneOrderInit) or select by ticket not position.
OneOrderInit should return a boolean (either if found an order or it doesn't,) or it should set _Ticket to zero.
_Ticket what value does it have ??
why do you select 1 inside
without doing an orderloop. What is according to you happening at
if(!OrderSelect(1,SELECT_BY_POS,MODE_TRADES))
can you check also the OrderTicket() if you select 1 ??
This selects the second opened order (any EA/manual trades, any pair, any magic number.) If you only have one (or zero) opened this will fail.
If you already have the order data saved why do you need to select it again? Maybe to check if the order has closed (TP/SL) before modifying? Why not call your function (OneOrderInit) or select by ticket not position.
OneOrderInit should return a boolean (either if found an order or it doesn't,) or it should set _Ticket to zero.
The thing is: I don't know why I need to select it again... simply when I don't do it, the OrderModify will not work which means it doesn't change the stoploss.
I want my trailing stop to work without a second OrderSelect. Maybe it hast to do something with zeroizing the _Ticket every time the OneOrderInit() function gets called? I tried everything from not zeroizing _Ticket to putting loops before and after thie _Ticket > 0 condition.. nothing worked. Also it is maybe important to mention that my EA should only place one order at a time, that is why I need the (Ticket > 0) { ... } else { ...}
_Ticket has the value 1 when initialised.
if you test it with strategytester the first trade might be opend with orderticketnumber 1 but that will be only the first trade of your EA
if you place this EA yours on demo account you won't find here the value 1
1 could be replaced with any number greater than 1 but not 0. When I choose 0 inside the OrderSelect the trailing stop does not work. The OrderTicket() is 0 if I select 1.
The problem is you don't understand the code in this line
OrderSelect(1,SELECT_BY_POS,MODE_TRADES)
that 1 in this line is not an orderticketnumber because the open trades are SELECT_BY_POS
if there is only one trade open then that trade has position 0 if you SELECT_BY_POS
See also the comment of WHRoeder
this what i can suggest for your trailing problems
//+------------------------------------------------------------------+ #property version "1.00" #property strict bool ProfitTrailing = true; extern int TrailingStop = 20; //Pips int TrailingStep = 1; //Pips bool UseSound = true; string NameFileSound = "expert.wav"; double pBid, pAsk, pp; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- Trail(); } //+------------------------------------------------------------------+ //========================================= void Trail() //========================================== { for(int i=0; i<OrdersTotal(); i++) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { TrailingPositions(); } } } //+------------------------------------------------------------------+ //| Ñîïðîâîæäåíèå ïîçèöèè ïðîñòûì òðàëîì | //+------------------------------------------------------------------+ void TrailingPositions() { pp = MarketInfo(OrderSymbol(), MODE_POINT); if(OrderType()==OP_BUY && OrderSymbol()==Symbol()) { pBid = MarketInfo(OrderSymbol(), MODE_BID); if(!ProfitTrailing || (pBid-OrderOpenPrice())>TrailingStop*pp) { if(OrderStopLoss()<pBid-(TrailingStop+TrailingStep-1)*pp) { ModifyStopLoss(pBid-TrailingStop*pp); } } } if(OrderType()==OP_SELL && OrderSymbol()==Symbol()) { pAsk = MarketInfo(OrderSymbol(), MODE_ASK); if(!ProfitTrailing || OrderOpenPrice()-pAsk>TrailingStop*pp) { if(OrderStopLoss()>pAsk+(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0) { ModifyStopLoss(pAsk+TrailingStop*pp); } } } } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ void ModifyStopLoss(double ldStopLoss) { bool fm; fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE); if(fm && UseSound) PlaySound(NameFileSound); } //+------------------------------------------------------------------+
- 2021.02.26
- www.mql5.com
EDIT your original post, please do not just post the code correctly in a new post.
Please do not bring up a topic that is nearly 7 years old for no good reason.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello this is my first post. I've been passively reading these forums for 3 month now.... :) But this problem I just can't fix alone and it drives me insane ... ^^
My Expert Advisor should set Trailingstops according to calculated Fibonachi levels. However it seems that the trailing stop is only working, when the OrderSelect after "if(Ticket>0){" produces an error. I find that very confusing, because when the OrderSelect is working the conditions for setting Trailingstops are just ignored or whatever and without this OrderSelect the trailing stop isn't working either.... And second, the trailing stops for the sell orders never work.
I would be happy if some expert is looking into this and can give me any advice... :)
Here is the code, first the included OrderInitiation file and after that the main logic from my EA: