Thread: How to code?
View Single Post
  #17 (permalink)  
Old 02-04-2006, 11:08 AM
sunwest's Avatar
sunwest sunwest is offline
Member
 
Join Date: Jan 2006
Location: London
Posts: 93
sunwest is on a distinguished road
Hello,

I am not sure if this could be integrated into your EA, but I put a separate fonction that you can put at the end of the code and call during the main loop.

Make sure you have a global variable "Magic" that you are using when placing order:

----
int Magic;
-----

Also you need to set the number of pips in profit "ProfitModifySL" before you would like to set your stopLoss to the actual opening price:

----
extern double ProfitModifySL=15; // After being in 15 pips Profit Stoploss is adjusted to the opening price of the order
-----

//+------------------------------------------------------------------+
//| Scan through Order and if in profit by PrmSL Modify SL |
//+------------------------------------------------------------------+
void fModifySLWhenInProfit()
{
for (int i=0; i<OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if (OrderType()==OP_BUY && Bid-OrderOpenPrice()>=ProfitModifySL*Point)
fModifyStopLoss(OrderOpenPrice());

if (OrderType()==OP_SELL && OrderOpenPrice()-Ask>=ProfitModifySL*Point)
fModifyStopLoss(OrderOpenPrice());
}
}
}
}
//+------------------------------------------------------------------+
//| Modify Stop Loss |
//+------------------------------------------------------------------+
void fModifyStopLoss(double tStopLoss)
{
bool result = OrderModify(OrderTicket(),OrderOpenPrice(),tStopLo ss,OrderTakeProfit(),0,NULL);
}

Otherwise I attached to the post a simple EA that does it.
Hope this help

Cheers
Attached Files
File Type: mq4 Sample-v1.mq4 (3.3 KB, 80 views)
Reply With Quote