Modifying Orders Question

 

I'm trying to modify buy and sell orders, but having a problem. When I'm trying to modify a buy, my code is modifying the buy, not only by the code that I have for modifying buys, but also by the code that I have for modifying a sell. Which causes my TP to decrease rather than to increase. Not sure of what the problem is. Any help would be greatly appreciated!

Thanks!

// ------------------------------------------------------------------------------ Initial MODIFY BUY -------------

if (OrderSelect(0, SELECT_BY_POS)==True)

{

if( Total > 0 && OrderType() == 0 && Profit > 1.0 && B == 0)

{

B = 1;

NewProfit = (Bid+0.00020);

Alert("NEW Profit = ",DoubleToStr(NewProfit,Digits));

while(Modify == false)

{

RefreshRates();

Alert ("Attempt to Initial Modifiy BUY ",Ticket,". Awaiting response..");

Modify = OrderModify(Ticket,OrderOpenPrice(),0,NewProfit,0);

Tt = GetLastError();

if(Cnt > 2)

{Cnt = 0; return(0);}

if (Tt > 0)

{Modify = false; Alert("Failed to Initial Modify BUY Order: ",Ticket," Error # ",Tt," Attempting to Modify Again! Bid = ",DoubleToStr(Bid,Digits)," PastBid = ",DoubleToStr(PastBid,Digits)," ",textB," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," B = ",B," "," Order Type = ",OrderType()); Sleep (5999); RefreshRates(); Cnt = Cnt + 1;

Alert (GetLastError());}

}

if (GetLastError() == 0)

{Alert(" BUY Order Initial Modified # ",Ticket," Bid = ",DoubleToStr(Bid,Digits)," PastBid = ",DoubleToStr(PastBid,Digits)," ",textB," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," B = ",B," "," Order Type = ",OrderType());}

} }

// ------------------------------------------------------------------------------ MODIFY BUY -------------

if (OrderSelect(0, SELECT_BY_POS)==True)

{

if( Total > 0 && OrderType() == 0 && B == 1 && Bid > PastBid && Profit > 0)

{

NewProfit = (Bid+0.00005);

if(TakeProfit < NewProfit)

{

Alert("NEW Profit = ",DoubleToStr(NewProfit,Digits));

while(Modify == false)

{

RefreshRates();

Alert ("Attempt to ReModifiy BUY ",Ticket,". Awaiting response..");

Modify = OrderModify(Ticket,OrderOpenPrice(),0,NewProfit,0);

Tt = GetLastError();

if(Cnt > 2)

{Cnt = 0; return(0);}

if (Tt > 0)

{Modify = false; Alert("Failed to ReModify BUY Order: ",Ticket," Error # ",Tt," Attempting to Modify Again! Bid = ",DoubleToStr(Bid,Digits)," PastBid = ",DoubleToStr(PastBid,Digits)," ",textB," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," B = ",B," "," Order Type = ",OrderType()); Sleep (5999); RefreshRates(); Cnt = Cnt + 1;

Alert (GetLastError());}

}

if (GetLastError() == 0)

{Alert(" BUY Order ReModified # ",Ticket," Bid = ",DoubleToStr(Bid,Digits)," PastBid = ",DoubleToStr(PastBid,Digits)," ",textB," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," B = ",B," "," Order Type = ",OrderType());}

} } }

// ------------------------------------------------------------------------------ Initial MODIFY SELL -------------

if (OrderSelect(0, SELECT_BY_POS)==True)

{

if (Total > 0 && OrderType() == 1 && Profit > 1.00 && S == 0)

{ S = 1;

NewProfit = (Ask-0.00020);

Alert("NEW Profit = ",DoubleToStr(NewProfit,Digits));

while(Modify == false)

{

RefreshRates();

Alert ("Attempt to Initial Modifiy ",Ticket,". Awaiting response..");

Modify = OrderModify(Ticket,OrderOpenPrice(),0,NewProfit,0);

Tt = GetLastError();

if(Cnt > 2)

{Cnt = 0; return(0);}

if (Tt > 0)

{Modify = false; Alert("Failed to Initial Modify Order: ",Ticket," Error # ",Tt," Attempting to Modify Again! Ask = ",DoubleToStr(Ask,Digits)," PastAsk = ",DoubleToStr(PastAsk,Digits)," ",textC," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," S = ",S," "," Order Type = ",OrderType()); Sleep (5999); RefreshRates(); Cnt = Cnt + 1;

Alert (GetLastError());}

}

if (GetLastError() == 0)

{Alert(" SELL Order Initial Modified # ",Ticket," Ask = ",DoubleToStr(Ask,Digits)," PastAsk = ",DoubleToStr(PastAsk,Digits)," ",textC," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," S = ",S," "," Order Type = ",OrderType());}

}

}

// ------------------------------------------------------------------------------ MODIFY SELL -------------

if (OrderSelect(0, SELECT_BY_POS)==True)

{

if (Total > 0 && OrderType() == 1 && S == 1 && Ask 0);

{

NewProfit = (Ask-0.00005);

if(TakeProfit > NewProfit)

{

Alert("NEW Profit = ",DoubleToStr(NewProfit,Digits));

while(Modify == false)

{

RefreshRates();

Alert ("Attempt to ReModifiy ",Ticket,". Awaiting response..");

Modify = OrderModify(Ticket,OrderOpenPrice(),0,NewProfit,0);

Tt = GetLastError();

if(Cnt > 2)

{Cnt = 0; return(0);}

if (Tt > 0)

{Modify = false; Alert("Failed to ReModify Order: ",Ticket," Error # ",Tt," Attempting to Modify Again! Ask = ",DoubleToStr(Ask,Digits)," PastAsk = ",DoubleToStr(PastAsk,Digits)," ",textC," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," S = ",S," "," Order Type = ",OrderType()); Sleep (5999); RefreshRates(); Cnt = Cnt + 1;

Alert (GetLastError());}

}

if (GetLastError() == 0)

{Alert(" SELL Order ReModified # ",Ticket," Ask = ",DoubleToStr(Ask,Digits)," PastAsk = ",DoubleToStr(PastAsk,Digits)," ",textC," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," S = ",S," "," Order Type = ",OrderType());}

}

} }

 
Yellowbeard:
I'm trying to modify buy and sell orders, but having a problem. When I'm trying to modify a buy, my code is modifying the buy, not only by the code that I have for modifying buys, but also by the code that I have for modifying a sell. Which causes my TP to decrease rather than to increase. Not sure of what the problem is. Any help would be greatly appreciated!

Thanks!

// ------------------------------------------------------------------------------ Initial MODIFY BUY -------------

if (OrderSelect(0, SELECT_BY_POS)==True)

{

if( Total > 0 && OrderType() == 0 && Profit > 1.0 && B == 0)

{

B = 1;

NewProfit = (Bid+0.00020);

Alert("NEW Profit = ",DoubleToStr(NewProfit,Digits));

while(Modify == false)

{

RefreshRates();

Alert ("Attempt to Initial Modifiy BUY ",Ticket,". Awaiting response..");

Modify = OrderModify(Ticket,OrderOpenPrice(),0,NewProfit,0);

Tt = GetLastError();

if(Cnt > 2)

{Cnt = 0; return(0);}

if (Tt > 0)

{Modify = false; Alert("Failed to Initial Modify BUY Order: ",Ticket," Error # ",Tt," Attempting to Modify Again! Bid = ",DoubleToStr(Bid,Digits)," PastBid = ",DoubleToStr(PastBid,Digits)," ",textB," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," B = ",B," "," Order Type = ",OrderType()); Sleep (5999); RefreshRates(); Cnt = Cnt + 1;

Alert (GetLastError());}

}

if (GetLastError() == 0)

{Alert(" BUY Order Initial Modified # ",Ticket," Bid = ",DoubleToStr(Bid,Digits)," PastBid = ",DoubleToStr(PastBid,Digits)," ",textB," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," B = ",B," "," Order Type = ",OrderType());}

} }

// ------------------------------------------------------------------------------ MODIFY BUY -------------

if (OrderSelect(0, SELECT_BY_POS)==True)

{

if( Total > 0 && OrderType() == 0 && B == 1 && Bid > PastBid && Profit > 0)

{

NewProfit = (Bid+0.00005);

if(TakeProfit < NewProfit)

{

Alert("NEW Profit = ",DoubleToStr(NewProfit,Digits));

while(Modify == false)

{

RefreshRates();

Alert ("Attempt to ReModifiy BUY ",Ticket,". Awaiting response..");

Modify = OrderModify(Ticket,OrderOpenPrice(),0,NewProfit,0);

Tt = GetLastError();

if(Cnt > 2)

{Cnt = 0; return(0);}

if (Tt > 0)

{Modify = false; Alert("Failed to ReModify BUY Order: ",Ticket," Error # ",Tt," Attempting to Modify Again! Bid = ",DoubleToStr(Bid,Digits)," PastBid = ",DoubleToStr(PastBid,Digits)," ",textB," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," B = ",B," "," Order Type = ",OrderType()); Sleep (5999); RefreshRates(); Cnt = Cnt + 1;

Alert (GetLastError());}

}

if (GetLastError() == 0)

{Alert(" BUY Order ReModified # ",Ticket," Bid = ",DoubleToStr(Bid,Digits)," PastBid = ",DoubleToStr(PastBid,Digits)," ",textB," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," B = ",B," "," Order Type = ",OrderType());}

} } }

// ------------------------------------------------------------------------------ Initial MODIFY SELL -------------

if (OrderSelect(0, SELECT_BY_POS)==True)

{

if (Total > 0 && OrderType() == 1 && Profit > 1.00 && S == 0)

{ S = 1;

NewProfit = (Ask-0.00020);

Alert("NEW Profit = ",DoubleToStr(NewProfit,Digits));

while(Modify == false)

{

RefreshRates();

Alert ("Attempt to Initial Modifiy ",Ticket,". Awaiting response..");

Modify = OrderModify(Ticket,OrderOpenPrice(),0,NewProfit,0);

Tt = GetLastError();

if(Cnt > 2)

{Cnt = 0; return(0);}

if (Tt > 0)

{Modify = false; Alert("Failed to Initial Modify Order: ",Ticket," Error # ",Tt," Attempting to Modify Again! Ask = ",DoubleToStr(Ask,Digits)," PastAsk = ",DoubleToStr(PastAsk,Digits)," ",textC," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," S = ",S," "," Order Type = ",OrderType()); Sleep (5999); RefreshRates(); Cnt = Cnt + 1;

Alert (GetLastError());}

}

if (GetLastError() == 0)

{Alert(" SELL Order Initial Modified # ",Ticket," Ask = ",DoubleToStr(Ask,Digits)," PastAsk = ",DoubleToStr(PastAsk,Digits)," ",textC," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," S = ",S," "," Order Type = ",OrderType());}

}

}

// ------------------------------------------------------------------------------ MODIFY SELL -------------

if (OrderSelect(0, SELECT_BY_POS)==True)

{

if (Total > 0 && OrderType() == 1 && S == 1 && Ask 0);

{

NewProfit = (Ask-0.00005);

if(TakeProfit > NewProfit)

{

Alert("NEW Profit = ",DoubleToStr(NewProfit,Digits));

while(Modify == false)

{

RefreshRates();

Alert ("Attempt to ReModifiy ",Ticket,". Awaiting response..");

Modify = OrderModify(Ticket,OrderOpenPrice(),0,NewProfit,0);

Tt = GetLastError();

if(Cnt > 2)

{Cnt = 0; return(0);}

if (Tt > 0)

{Modify = false; Alert("Failed to ReModify Order: ",Ticket," Error # ",Tt," Attempting to Modify Again! Ask = ",DoubleToStr(Ask,Digits)," PastAsk = ",DoubleToStr(PastAsk,Digits)," ",textC," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," S = ",S," "," Order Type = ",OrderType()); Sleep (5999); RefreshRates(); Cnt = Cnt + 1;

Alert (GetLastError());}

}

if (GetLastError() == 0)

{Alert(" SELL Order ReModified # ",Ticket," Ask = ",DoubleToStr(Ask,Digits)," PastAsk = ",DoubleToStr(PastAsk,Digits)," ",textC," OOP = ",DoubleToStr(OrderOpenPrice(),Digits)," TP: ",DoubleToStr(NewProfit, Digits)," Profit = ",Profit," S = ",S," "," Order Type = ",OrderType());}

}

} }

Since that is a partial code, and we can not have any idea of the rest of the code (which can be a cause of error too), why don't you simply add one "return(0);" at the end of the buy order processing code (I am assuming that the buy order processing code was entered correctly)?

 

My problem is within the remodification of the orders. It seems as though OrderType is ignored. The code was not to remodify on a negative profit either. See my log results below:

11:40:00 Custom Test M83 USDCHF,M5: open #42911948 buy 0.10 USDCHF at 0.93883 ok

614668 12:24:21 Custom Test M83 USDCHF,M5: Alert: Attempt to Initial Modifiy BUY 42911948. Awaiting response..

614675 12:24:21 Custom Test M83 USDCHF,M5: modify #42911948 buy 0.10 USDCHF at 0.93883 sl: 0.00000 tp: 0.93915 ok

614676 12:24:21 Custom Test M83 USDCHF,M5: Alert: BUY Order Initial Modified # 42911948 Bid = 0.93892 PastBid = 0.93883 UP OOP = 0.93883 TP: 0.93915 Profit = 1.28 B = 1 0

614678 12:24:21 Custom Test M83 USDCHF,M5: Alert: BUY Order ReModified # 42911948 Bid = 0.93892 PastBid = 0.93883 UP OOP = 0.93883 TP: 0.93897 Profit = 1.28 B = 1 0

614697 12:24:21 Custom Test M83 USDCHF,M5: Alert: Attempt to ReModifiy 42911948. Awaiting response..

614698 12:24:21 Custom Test M83 USDCHF,M5: modify #42911948 buy 0.10 USDCHF at 0.93883 sl: 0.00000 tp: 0.93899 ok

614699 12:24:22 Custom Test M83 USDCHF,M5: Alert: SELL Order ReModified # 42911948 Ask = 0.93904 PastAsk = 0.93894 UP OOP = 0.93883 TP: 0.93899 Profit = 0.96 S = 0 0

614724 12:24:22 Custom Test M83 USDCHF,M5: Alert: Attempt to ReModifiy 42911948. Awaiting response..

614725 12:24:22 Custom Test M83 USDCHF,M5: modify #42911948 buy 0.10 USDCHF at 0.93883 sl: 0.00000 tp: 0.93897 ok

614726 12:24:22 Custom Test M83 USDCHF,M5: Alert: SELL Order ReModified # 42911948 Ask = 0.93902 PastAsk = 0.93904 DOWN OOP = 0.93883 TP: 0.93897 Profit = 0.53 S = 0 0

614803 12:24:22 Custom Test M83 USDCHF,M5: Alert: Attempt to ReModifiy 42911948. Awaiting response..

614805 12:24:22 Custom Test M83 USDCHF,M5: modify #42911948 buy 0.10 USDCHF at 0.93883 sl: 0.00000 tp: 0.93896 ok

614806 12:24:23 Custom Test M83 USDCHF,M5: Alert: SELL Order ReModified # 42911948 Ask = 0.93901 PastAsk = 0.93903 DOWN OOP = 0.93883 TP: 0.93896 Profit = 0.53 S = 0 0

614825 12:24:23 Custom Test M83 USDCHF,M5: Alert: Attempt to ReModifiy 42911948. Awaiting response..

614832 12:24:23 Custom Test M83 USDCHF,M5: modify #42911948 buy 0.10 USDCHF at 0.93883 sl: 0.00000 tp: 0.93894 ok

614833 12:24:23 Custom Test M83 USDCHF,M5: Alert: SELL Order ReModified # 42911948 Ask = 0.93899 PastAsk = 0.93903 DOWN OOP = 0.93883 TP: 0.93894 Profit = 0.32 S = 0 0

614881 12:24:23 Custom Test M83 USDCHF,M5: Alert: Attempt to ReModifiy 42911948. Awaiting response..

614882 12:24:23 Custom Test M83 USDCHF,M5: modify #42911948 buy 0.10 USDCHF at 0.93883 sl: 0.00000 tp: 0.93892 ok

614883 12:24:23 Custom Test M83 USDCHF,M5: Alert: SELL Order ReModified # 42911948 Ask = 0.93894 PastAsk = 0.93903 DOWN OOP = 0.93883 TP: 0.93892 Profit = 0 S = 0 0

614902 12:24:23 Custom Test M83 USDCHF,M5: Alert: Attempt to ReModifiy 42911948. Awaiting response..

614903 12:24:23 Custom Test M83 USDCHF,M5: modify #42911948 buy 0.10 USDCHF at 0.93883 sl: 0.00000 tp: 0.93889 ok

614904 12:24:24 Custom Test M83 USDCHF,M5: Alert: SELL Order ReModified # 42911948 Ask = 0.93894 PastAsk = 0.93897 DOWN OOP = 0.93883 TP: 0.93889 Profit = -0.32 S = 0 0

614952 12:24:24 Custom Test M83 USDCHF,M5: Alert: Attempt to ReModifiy 42911948. Awaiting response..

614953 12:24:24 Custom Test M83 USDCHF,M5: modify #42911948 buy 0.10 USDCHF at 0.93883 sl: 0.00000 tp: 0.93888 ok

614954 12:24:24 Custom Test M83 USDCHF,M5: Alert: SELL Order ReModified # 42911948 Ask = 0.93885 PastAsk = 0.93897 DOWN OOP = 0.93883 TP: 0.93888 Profit = -0.43 S = 0 0

615002 12:24:24 Custom Test M83 USDCHF,M5: Alert: Attempt to ReModifiy 42911948. Awaiting response..

615003 12:24:24 Custom Test M83 USDCHF,M5: modify #42911948 buy 0.10 USDCHF at 0.93883 sl: 0.00000 tp: 0.93887 ok

615004 12:24:25 Custom Test M83 USDCHF,M5: Alert: SELL Order ReModified # 42911948 Ask = 0.93894 PastAsk = 0.93897 DOWN OOP = 0.93883 TP: 0.93887 Profit = -0.53 S = 0 0

Reason: