/////////////////////////////////////////////////////////////////////////////////////////////
// Trailing stop losses
void TrailOrders(int trailing_stop, int magic_number)
{
//Iterate orders
int num_orders = OrdersTotal();
for(int trade = 0; trade < num_orders; trade++)
{
//Select an order
if (!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
{
continue;
}
if (OrderMagicNumber() != magic_number)
{
continue;
}
if (OrderType()==OP_BUY)
{
if (Bid-OrderOpenPrice() > Point*trailing_stop)
{
if (OrderStopLoss() < Bid-Point*trailing_stop)
{
if (!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*trailing_stop,OrderTakeProfit(),0,Green))
{
Print("TrailOrders, OrderModify failed. Err#:", ErrorDescription(GetLastError()));
}
}
}
}
if (OrderType()==OP_SELL)
{
if (OrderOpenPrice()-Ask > Point*trailing_stop)
{
if (OrderStopLoss() > Ask+Point*trailing_stop)
{
if (!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*trailing_stop,OrderTakeProfit(),0,Red))
{
Print("TrailOrders, OrderModify failed. Err#:", ErrorDescription(GetLastError()));
}
}
}
}
}
}
just use e-trailing
the default of 8 is when trail is moved to break even(no matter what the initial s/l is set)
the default of 2 is how many pips it trails after being moved to break even
change those 2 settings to your liking.
I use this EA on every trade I make. Usually I trail to b/e at 25/30 pips and trail every 5 pips there after.
you can set initial s/l at 100 and target of 200. or what ever you want, the broker never knows what point your ea will trail at.
Hi ...
I use this code in the most of my EA's to protect profit is the account profit reach a specific $ but not pips.
The code is not recomended to add to EA's which are attached to plateform with other EA's because it has no MAGIC No.
You can add MAGIC No. easily if you like.
extern int MaxProfitPerPair = 7; //in Dollars
...
...
...
if(AccountProfit()>= MaxProfitPerPair * OrdersTotal())
CloseAllPositions();
...
...
...
void CloseAllPositions(){
int total = OrdersTotal();
for(int i=total-1;i>=0;i--){
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;
switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;
//Close opened short positions
case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
}
if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}
return(0);
}
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Can anyone here code this ....
I would like to protect profits after a certian number of pips is reached
So once "x" number of pips is reached it starts a trailing stop.
None of my attempts have been successfull.
Also is there anyway to fake a takeprofit and stoploss if your broker only alows it to be more than 10?
I was thinking about putting some rules to the OrderClose ()....
Thanks,