Download MetaTrader 5
To add comments, please log in or register
Order a robot or an indicator for MetaTrader. The Freelance service will help you!
Il_Messia
56
Il_Messia 2017.01.11 00:28 

Hi guys,

i can't make updating my pending orders on every price changing, moving following price until 3 sec before news relaese (yeah another news trading ea). I wrote an ordermodify but seems to work just once.

i saw many ea on youtube that create moving pending order until news relaese, but i can't do this on my own.

attach my code.

Hope in your help


p.s.

"AAAAA.." and "ordine modificato" return to me just once.


void SellPendingOrder41()
{
while(true)
  {
   int error=GetLastError();
   int Slip=Slippage*(int)PipValue;
   double price=NormalizeDouble(Bid,NDigits)-PriceGap*PipValue*Point;
   double SL=price+StopLoss*PipValue*Point;
   if(StopLoss==0) SL=0;
   double TP=price-TakeProfit*PipValue*Point;
   if(TakeProfit == 0) TP = 0;

   int ticket= OrderSend(Symbol(),OP_SELLSTOP,Lots,price,Slip,SL,TP,"NewsTime!!",MagicNumberSell,0,Red);
   if(ticket == -1)
      {
      Print("OrderSend() error - ",ErrorDescription(GetLastError()));
      Print("errore "+error+"") ;
      if(error==136) break;
      Sleep(1000);
      RefreshRates();
            Print("REFRESHHHHHHHHHH");
      break;
     }
     else
      {
      OrderSelect(ticket,SELECT_BY_TICKET);
      OrderPrint();
       UpdateSell();
      break;
       }
      }
     }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void UpdateSell()        
{
   int datetime800=(int)TimeCurrent();
   int hour0=TimeHour(datetime800);
   int minute0=TimeMinute(datetime800);
   int second0=TimeSeconds(datetime800);
   int secpart=StartSecond;
   double price=NormalizeDouble(Bid,NDigits)-PriceGap*PipValue*Point;
   double SL=price+StopLoss*PipValue*Point;
   if(StopLoss==0) SL=0;
   double TP=price-TakeProfit*PipValue*Point;
   if(TakeProfit == 0) TP = 0;
        
         if(hour0==StartHour && minute0==StartMinute && second0<=secpart)
          {

//---
   for(int i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumberSell && OrderType()==OP_SELLSTOP)
          
          {
           bool res=OrderModify(OrderTicket(),price,SL,TP,0,Blue);
           Print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
           if(res==1)
           Print("OrderModify() error - ", ErrorDescription (GetLastError()));
           else
           Print("Ordine modificato");
           checkshortstop();
          }
       }
     }
   }
  
  
Marco vd Heijden
Moderator
4229
Marco vd Heijden 2017.01.11 02:01  
double price=NormalizeDouble(Bid,NDigits)-PriceGap*PipValue*Point;

Do you want Bid minus Pricegap times Pipvalue times Point ?

Is that

(Bid minus Pricegap) times Pipvalue times Point ?

or is it

(Bid minus Pricegap times Pipvalue) times Point ?

or maybe

Bid minus (Pricegap times Pipvalue) times Point ?

or

Bid minus Pricegap times (Pipvalue times Point) ?

Obviously you need to separate it into smaller chunks before you Normalize the outcome.

Here's an example:

bool modify=OrderModify(OrderTicket(),0,MarketInfo(OrderSymbol(),MODE_BID)-trail*Point(),0,0,clrNONE);

Il_Messia
56
Il_Messia 2017.01.11 08:41  
Marco vd Heijden:
double price=NormalizeDouble(Bid,NDigits)-PriceGap*PipValue*Point;

Do you want Bid minus Pricegap times Pipvalue times Point ?

Is that

(Bid minus Pricegap) times Pipvalue times Point ?

or is it

(Bid minus Pricegap times Pipvalue) times Point ?

or maybe

Bid minus (Pricegap times Pipvalue) times Point ?

or

Bid minus Pricegap times (Pipvalue times Point) ?

Obviously you need to separate it into smaller chunks before you Normalize the outcome.

Here's an example:

bool modify=OrderModify(OrderTicket(),0,MarketInfo(OrderSymbol(),MODE_BID)-trail*Point(),0,0,clrNONE);

Why 0 in price? The price is your 'market info...' isn't it? 
Marco vd Heijden
Moderator
4229
Marco vd Heijden 2017.01.11 12:20  

Yes in the case of a pending order thats correct.

This one was to modify stoploss but the principle is the same.
Il_Messia
56
Il_Messia 2017.01.11 13:49  
So how create this moving orders? 
Marco vd Heijden
Moderator
4229
Marco vd Heijden 2017.01.11 14:04  
By modifying existing orders.
Il_Messia
56
Il_Messia 2017.01.11 14:21  
Like done before but doesn't work! I don't understand what's wrong  
whroeder1
13978
whroeder1 2017.01.11 15:13  
Il_Messia: I don't understand what's wrong
bool modify=OrderModify(OrderTicket(),0,MarketInfo(OrderSymbol(),MODE_BID)-trail*Point(),0,0,clrNONE);
if(res==1)
Print("OrderModify() error - ", ErrorDescription (GetLastError()));
  1. You are modifying the open price of an order to zero -- invalid price -- that is "what's wrong." Perhaps you want to use OrderOpenPrice()?
  2. This is why I say:
    You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
    If you read the documentation, you would know that 1 converts to true. You look to see if the modify succeeds and complain that it failed -- that is "what's wrong."
  3. You can't modify orders if the pending price is closer to market price than the max of StopLevel or FreezeLevel. Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial As news approaches, FreezeLevel will stop you from getting close and if the market gaps, you SL will not protect you.
/
To add comments, please log in or register