/-------------------------------------------------------------------------------------------
// Set Trailing Stop//-------------------------------------------------------------------------------------------string Symb=Symbol(); // Symbol//------------------------------------------------------------------------------- 2 --for(i=1; i<=OrdersTotal(); i++) // Cycle searching in orders
{
if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
{ // Analysis of orders:int Tipo=OrderType(); // Order typeif(OrderSymbol()!=Symb||Tipo>1)continue;// The order is not "ours"
SL=OrderStopLoss(); // SL of the selected order//---------------------------------------------------------------------- 3 --while(true) // Modification cycle
{
//double TS=Trail_Stop; // Initial valuedouble TS=NormalizeDouble(Trail_Stop*StopMultd,Digits); //cambie TS por este valor normalizadoint Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL); //Min. distanceif (TS < Min_Dist) // If less than allowed
TS=Min_Dist; // New value of TS//------------------------------------------------------------------- 4 --bool Modify=false; // Not to be modifiedswitch(Tipo) // By order type
{
case0 : // Order Buyif (NormalizeDouble(SL,Digits)<
NormalizeDouble(Bid-TS*dblPoints,Digits)) // If it is lower than we want
{
SL=Bid-TS*dblPoints; // then modify itstring Text=" Buy "; // Text for Buy
Modify=true; // To be modified
}
break; // Exit 'switch'case1 : // Order Sellif (NormalizeDouble(SL,Digits)>
NormalizeDouble(Ask+TS*dblPoints,Digits) // If it is higher than we want
|| NormalizeDouble(SL,Digits)==0) //or equal to zero
{
SL=Ask+TS*dblPoints; // then modify it
Text=" Sell "; // Text for Sell
Modify=true; // To be modified
}
} // End of 'switch'if (Modify==false) // If it is not modifiedbreak; // Exit 'while'//------------------------------------------------------------------- 5 --
TP =OrderTakeProfit(); // TP of the selected orderdouble Price =OrderOpenPrice(); // Price of the selected orderint Ticket=OrderTicket(); // Ticket of the selected orderAlert ("Modification to",Text,"#",Ticket," - Awaiting response..");
bool Ans=OrderModify(Ticket,Price,SL,TP,0);//Modify it!//------------------------------------------------------------------- 6 --if (Ans==true) // Got it! :)
{
Alert ("Order ",Text,Ticket," is modified:)");
break; // From modification cycle.
}
Please edit your post.
For large amounts of code, attach it.
EDIT your original post not leave it and add another.
mql4newbie: When I modify my stop loss of an open order, I get this weird Error number 0 with the error description "no error". My result is a FALSE and I cannot understand why.
You get this when you look at the error number (GetLastError() or _LastError) after a call that successfully completes. Don't look at it unless, e.g. OrderModify returns false.
BarrowBoy: One usual cause is where you OrderModify (for example to move a trailing stop) but actually send unchanged values
//--------------------------------------------------------------------------------- // function Trailing Stop//---------------------------------------------------------------------------------bool Trailing_Stop(int Stop,int Multd)
{
string Symb=Symbol(); // Symbolint Tipo=OrderType(); // Order typeint i;
for(i=1; i<=OrdersTotal(); i++) // Cycle searching in orders
{
if (OrderSelect(i-1,SELECT_BY_POS)==true) // Select if the next is available
{ // Analysis of orders:if(OrderSymbol()!=Symb||Tipo>1)continue;// The order is not "ours"double SL=OrderStopLoss(); // SL of the selected orderwhile(true) // Modification cycle
{
double TS=Stop*Multd; // Valor del Trail Stop x 10int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL); //Min. distanceif (TS < Min_Dist) // If less than allowed
TS=Min_Dist; // New value of TSbool Modify=false; // start with a Not to be modified instructionswitch(Tipo) // Evaluate By order type
{
case0 : // Order Buy
SL=NormalizeDouble(Bid-TS*Point,Digits); // calculate new SLif(OrderStopLoss()<SL) // If Order Stop Loss is lower than what we want
{
string Text=" Buy "; // Text for Buy
Modify=true; // To be modified
}
break; // Exit 'switch'case1 : // Order Sell
SL=NormalizeDouble(Ask+TS*Point,Digits); // then modify itif(OrderStopLoss()>SL) // If Order Stop LOss is higher than what we want
{
Text=" Sell "; // Text for Sell
Modify=true; // To be modified
}
} // End of 'switch'if(Modify==false)break; // If it is not modified exit while Alert ("Modification to",Text,"#",OrderTicket()," - Awaiting response..");
bool Ans=OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0); //Modify it!if (Ans==true) // Got it! :)
{
Alert ("Order ",Text,OrderTicket()," is modified:)");
break; // From modification cycle.
}
break; // From modification cycle
} // End of modification cycle while (true)
} // End of order analysis
} // End of order searchreturn(Ans);
}
Hi guys
I have created this Function to perform the Trailing Stop feature, it is very helpful
and it can be used on any EA, it works very well
since I copied most of this code from somene else and I am very pleased with it, I just wanted to publish the function I created and maybe help someone else with it too
important notes:
bool Trailing_Stop(int Stop,int Multd)
the function uses just two parameters:
Stop: is the size of the trailing stop in pips, for example Stop=20 pips
Multd: factor to be used in case your broker is a 5 digit broker instead of a 4 digit broker. If this is the case Multd=10. If you use a 4 digit broker use Multd=1.
I have created this Function to perform the Trailing Stop feature, it is very helpful
and it can be used on any EA, it works very well
since I copied most of this code from somene else and I am very pleased with it, I just wanted to publish the function I created and maybe help someone else with it too
important notes:
the function uses just two parameters:
Stop: is the size of the trailing stop in pips, for example Stop=20 pips
Multd: factor to be used in case your broker is a 5 digit broker instead of a 4 digit broker. If this is the case Multd=10. If you use a 4 digit broker use Multd=1.
I have the same problem. It actually works, trailing the price correctly, but I's like to know what's wrong with the code below. Pips_Factor is my name for _Point, and this is for a Buy; Sell is below and same thing happens, works with Error 0
This_TS = NormalizeDouble(MathMin(Bid-(Trailing_Stop*Pips_Factor),MarketInfo(Symbol(),MODE_STOPLEVEL)),Digits); // calculated TS must not pass STOPLEVEL; use the min for a buy
if ( OrderStopLoss() < This_TS ) // if the calculated TS is closer to the price then the current one
This_TS = NormalizeDouble(MathMax(Ask+(Trailing_Stop*Pips_Factor),MarketInfo(Symbol(),MODE_STOPLEVEL)),Digits); // calculated TS must not pass STOPLEVEL; use the max for a sell
if ( OrderStopLoss() > This_TS ) // if the calculated TS is closer to the price then the current one
Now it should look clearer.
thanks in advance for your help
Play video
For large amounts of code, attach it.
Hi guys
I have created this Function to perform the Trailing Stop feature, it is very helpful
and it can be used on any EA, it works very well
since I copied most of this code from somene else and I am very pleased with it, I just wanted to publish the function I created and maybe help someone else with it too
important notes:
the function uses just two parameters:
Stop: is the size of the trailing stop in pips, for example Stop=20 pips
Multd: factor to be used in case your broker is a 5 digit broker instead of a 4 digit broker. If this is the case Multd=10. If you use a 4 digit broker use Multd=1.
Hi guys
I have created this Function to perform the Trailing Stop feature, it is very helpful
and it can be used on any EA, it works very well
since I copied most of this code from somene else and I am very pleased with it, I just wanted to publish the function I created and maybe help someone else with it too
important notes:
the function uses just two parameters:
Stop: is the size of the trailing stop in pips, for example Stop=20 pips
Multd: factor to be used in case your broker is a 5 digit broker instead of a 4 digit broker. If this is the case Multd=10. If you use a 4 digit broker use Multd=1.
how i can solve it my friend
the expert is working good
or not
thanks my friend
I have the same problem. It actually works, trailing the price correctly, but I's like to know what's wrong with the code below. Pips_Factor is my name for _Point, and this is for a Buy; Sell is below and same thing happens, works with Error 0
This_TS = NormalizeDouble(MathMin(Bid-(Trailing_Stop*Pips_Factor),MarketInfo(Symbol(),MODE_STOPLEVEL)),Digits); // calculated TS must not pass STOPLEVEL; use the min for a buy
if ( OrderStopLoss() < This_TS ) // if the calculated TS is closer to the price then the current one
retval = OrderModify(OrderTicket(),OrderOpenPrice(),This_TS,OrderTakeProfit(),0,Blue);
for Sell:
This_TS = NormalizeDouble(MathMax(Ask+(Trailing_Stop*Pips_Factor),MarketInfo(Symbol(),MODE_STOPLEVEL)),Digits); // calculated TS must not pass STOPLEVEL; use the max for a sell
if ( OrderStopLoss() > This_TS ) // if the calculated TS is closer to the price then the current one
retval = OrderModify(OrderTicket(),OrderOpenPrice(),This_TS,OrderTakeProfit(),0,Blue);