Hello guys,
I just spent some time trying to code this simple EA which should just modify a PENDING limit order within a fixed amount of pips whenever the price is moving away from it. Basically if you bid 1.0900 limit for EURUSD @ 1.0910 with a fixed 10 pip gap and the price moves up one pip to 1.0911, then your limit moves to 1.0901. Very simple.
I tried to use the OrderOpenPrice() and OrderModify() functions but I am obviously missing something. If a coder could take a quick look at the script I would really appreciate the help. If I'm totally wrong then please let me know. Thanks!
void OnTick()
{
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS))
{
// only handle trades on the current symbol
if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_BUYLIMIT)
{
// move limit upward if price moves up
if(NormalizeDouble(Ask - TrailDistance*Point, Digits) > NormalizeDouble(OrderOpenPrice(), Digits))
{
if(OrderModify(OrderTicket(),NormalizeDouble(Ask-TrailDistance*Point),OrderStopLoss(),OrderTakeProfit(), Digits),0))
Print("Modified limit for order ", OrderTicket(), " on ", OrderSymbol());
else
Print("Error modifying limit for order ", OrderTicket(), " on ", OrderSymbol());
}
}
if(OrderType() == OP_SELLLIMIT)
{
// move limit downward if price moves down
if(NormalizeDouble(Bid + TrailDistance*Point, Digits) < NormalizeDouble(OrderOpenPrice(), Digits))
{
if(OrderModify(OrderTicket(),NormalizeDouble(Bid+TrailDistance*Point),OrderStopLoss(),OrderTakeProfit(), Digits),0))
Print("Modified limit for order ", OrderTicket(), " on ", OrderSymbol());
else
Print("Error modifying limit for order ", OrderTicket(), " on ", OrderSymbol());
}
}
}
}
}
}
I just spent some time trying to code this simple EA which should just modify a PENDING limit order within a fixed amount of pips whenever the price is moving away from it. Basically if you bid 1.0900 limit for EURUSD @ 1.0910 with a fixed 10 pip gap and the price moves up one pip to 1.0911, then your limit moves to 1.0901. Very simple.
I tried to use the OrderOpenPrice() and OrderModify() functions but I am obviously missing something. If a coder could take a quick look at the script I would really appreciate the help. If I'm totally wrong then please let me know. Thanks!
void OnTick()
{
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS))
{
// only handle trades on the current symbol
if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_BUYLIMIT)
{
// move limit upward if price moves up
if(NormalizeDouble(Ask - TrailDistance*Point, Digits) > NormalizeDouble(OrderOpenPrice(), Digits))
{
if(OrderModify(OrderTicket(),NormalizeDouble(Ask-TrailDistance*Point),OrderStopLoss(),OrderTakeProfit(), Digits),0))
Print("Modified limit for order ", OrderTicket(), " on ", OrderSymbol());
else
Print("Error modifying limit for order ", OrderTicket(), " on ", OrderSymbol());
}
}
if(OrderType() == OP_SELLLIMIT)
{
// move limit downward if price moves down
if(NormalizeDouble(Bid + TrailDistance*Point, Digits) < NormalizeDouble(OrderOpenPrice(), Digits))
{
if(OrderModify(OrderTicket(),NormalizeDouble(Bid+TrailDistance*Point),OrderStopLoss(),OrderTakeProfit(), Digits),0))
Print("Modified limit for order ", OrderTicket(), " on ", OrderSymbol());
else
Print("Error modifying limit for order ", OrderTicket(), " on ", OrderSymbol());
}
}
}
}
}
}