- You can't use any Trade Functions - MQL4 Reference unless you select a order first. Everything after that select failure is nonsense.
- You would have known this had you checked your return codes
What are Function
return values ? How do I use them ? - MQL4 forum and Common Errors
in MQL4 Programs and How to Avoid Them - MQL4 Articles
- You can't use any Trade Functions - MQL4 Reference unless you select a order first.
- You would have known this had you checked your return codes
What are Function
return values ? How do I use them ? - MQL4 forum and Common Errors
in MQL4 Programs and How to Avoid Them - MQL4 Articles
//EXECUTE SELL CONDITION
//************************************************************
{
if( sell_condition_1 && sell_condition_2 && sell_condition_3 && sell_condition_4 && sell_condition_5 && sell_condition_6 && sell_condition_7)
{
double ShortStopLoss = Bid + (StopLoss * Point*10);
double ShortTakeProfit = Bid - (TakeProfit * Point*10);
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
ShortTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,ShortStopLoss,ShortTakeProfit,"Sell Order",MagicNumber,0,Red);
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
}
}
//************************************************************
for(cnt=0;cnt<total;cnt++)
{
if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
continue;
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
//--- long position is opened
if(OrderType()==OP_BUY)
{
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
if(!OrderModify(LongTicket,OrderOpenPrice(),Bid-(Point*TrailingStop),OrderTakeProfit(),0,Green))
Print("OrderModify error ",GetLastError());
return;
}
}
}
}
else // go to short position
{
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
if(!OrderModify(ShortTicket,OrderOpenPrice(),Ask+(Point*TrailingStop),OrderTakeProfit(),0,Red))
Print("OrderModify error ",GetLastError());
return;
}
}
}
}
}
}
-
In the presence of multiple orders (one EA multiple charts, multiple EA's,
manual trading)
- You must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
- and check OrderSelect. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
- You must RefreshRates after sleep and between multiple server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead.
- Print out your variables, and find out why.
Don't duplicate code if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
if(!OrderModify(ShortTicket, OrderOpenPrice(), Ask+(Point*TrailingStop), OrderTakeProfit(), 0, Red))Simplify double tsl = Ask + Point*TrailingStop;
if(tsl < MathMin(OrderOpenPrice(), OrderStopLoss() ) || OrderStopLoss()==0){
if(!OrderModify(ShortTicket, OrderOpenPrice(), tsl, OrderTakeProfit(), 0, Red))
First of all you should make your code more efficient
This condition
should be checked first as the whole block of code is irrelevant if it <=0
Where is total assigned a value?
Why are you looping through the orders when you are trying to modify a specific order. OrderOpenPrice() etc are the values for the selected ticket, not necessarily LongTicket's
First of all you should make your code more efficient
This condition
should be checked first as the whole block of code is irrelevant if it <=0
Where is total assigned a value?
Why are you looping through the orders when you are trying to modify a specific order. OrderOpenPrice() etc are the values for the selected ticket, not necessarily LongTicket's
for(cnt=0 ; cnt < OrdersTotal(); cnt++)
{
if( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false )
continue;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(OrderType()==OP_BUY)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
double tslL = Bid - (Point*TrailingStop*10);
if(tslL > MathMin(OrderOpenPrice(), OrderStopLoss() ) || OrderStopLoss()==0){
if(!OrderModify(OrderTicket(), OrderOpenPrice(), tslL, OrderTakeProfit(), 0, Red))
Print("OrderModify error ",GetLastError());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
else if (OrderType() == OP_SELL)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
{
double tslS = Ask + (Point*TrailingStop*10);
if(tslS < MathMin(OrderOpenPrice(), OrderStopLoss() ) || OrderStopLoss()==0){
if(!OrderModify(OrderTicket(), OrderOpenPrice(), tslS, OrderTakeProfit(), 0, Red))
Print("OrderModify error ",GetLastError());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
- if(tslL > MathMin(OrderOpenPrice(), OrderStopLoss() ) || OrderStopLoss()==0){This will trail continuously not just above break even.
- Print out your variables, and find out why.
- if(tslL > MathMin(OrderOpenPrice(), OrderStopLoss() ) || OrderStopLoss()==0){This will trail continuously not just above break even.
- Print out your variables, and find out why.
Thanks for the help whroeder, I'm still not getting my code to work though. I've made some more changes and tried to print every variable.
It seems that the Bid/Ask and OpenPrice difference is always 10.. which is the spread that I use to backtest. So this means that it is not looping and I have tried shifting this all over my code and it still won't loop.
I have changed my code again. All I want it to do is once the price moves past my TrailingStop level, then the StopLoss should move to where I opened the order originally. Could anyone please help me figure out why this is not working?
//EXECUTE SELL CONDITION
//************************************************************
{
if( sell_condition_1 && sell_condition_2 && sell_condition_3 && sell_condition_4 && sell_condition_5 && sell_condition_6 && sell_condition_7)
{
double ShortStopLoss = Bid + (StopLoss * Point*10);
double ShortTakeProfit = Bid - (TakeProfit * Point*10);
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
ShortTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,ShortStopLoss,ShortTakeProfit,"Sell Order",MagicNumber,0,Red);
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
OrderSelect(ShortTicket,SELECT_BY_TICKET);
RefreshRates();
if((OrderOpenPrice()-Ask)>TrailingStop)
{
bool res=OrderModify(ShortTicket,OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);
if(!res)
Print("Error in OrderModify. Error code=",GetLastError());
else
Print("Order modified successfully.");
}
}
C0mput3r: I'm still not getting my code to work though. I've made some more changes and tried to print every variable. I have tried shifting this all over my code and it still won't loop. I have changed my code again. All I want it to do is once the price moves past my TrailingStop level, then the StopLoss should move to where I opened the order originally. |
|
|
I removed the prints to make the code shorter to attach on here for you guys, otherwise I feel it would've been too cluttered.
Sorry if my previous comment wasn't clear, I couldn't get the code to engage the Trailing stop loss with a loop so I removed the loop and that is the code (or portion of) that I copied for you.
I figured out what the original issue was though, for some reason it did not pick up OrderOpenPrice() as anything other than 0.0001, even if I assigned an Int variable to it. I changed Int to Double and then it finally worked.
The whole goal of the code that I am trying to add is for the stoploss to move to breakeven(OrderOpenPrice), as soon as the price has moved 50% of the distance between the entry point and my TP.
I have managed to get the code to engage the ModifyOrder, although now once TP is reached it just doubles the distance of the TP for my next trade but the distance of my next trade's SL stays the same.
I am attaching my entire code for you. If you could give me any advice on this last issue, I would be extremely grateful.
Kind regards,
Anton
I have deleted your new topic.
If you can't resolve your problems here, starting a new topic will not bring any different results.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi Guys,
I'm trying to add to my code that once price has reached exactly halfway between my entry point and TP, then SL will move to where the entry position was, to add some downside protection on my code.
I looked around to find a basic trending SL for a starting point for me to work from but I am getting ModifyOrder Error 4051 & 4108 the whole time. Does anyone know where I might be missing something?
Thanks in advance!
//EXECUTE SELL CONDITION
//************************************************************
{
if( sell_condition_1 && sell_condition_2 && sell_condition_3 && sell_condition_4 && sell_condition_5 && sell_condition_6 && sell_condition_7)
{
double ShortStopLoss = Bid + (StopLoss * Point*10);
double ShortTakeProfit = Bid - (TakeProfit * Point*10);
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
ShortTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,ShortStopLoss,ShortTakeProfit,"Sell Order",MagicNumber,0,Red);
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
}
}
//************************************************************
OrderSelect(OrderTicket(), SELECT_BY_TICKET);
if (OrderType() == OP_BUY)
{
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop*10)
{
if(OrderStopLoss()<Bid-Point*TrailingStop*10)
{
if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(Point*TrailingStop*10),OrderTakeProfit(),0,Green))
Print("OrderModify error ",GetLastError());
return;
}
}
}
else
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop*10))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop*10)) || (OrderStopLoss()==0))
{
if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(Point*TrailingStop*10),OrderTakeProfit(),0,Red))
Print("OrderModify error ",GetLastError());
return;
}
}
}
}