Download MetaTrader 5
To add comments, please log in or register
All articles are available from MetaTrader 5. Contribute your own one!
Erik Janson Vieira Coelho
8
Erik Janson Vieira Coelho 2016.03.08 20:23 
Hello
Initially desculpelo for bad English.
I am with following problem. 
I write this code:
extern int MagicNumber=10001;
extern double Lots =0.1;
extern double StopLoss=60;
extern double TakeProfit=30;
extern int Slippage=3;

int operacao=0;
int ThisBarTrade=0;
double resetlots=Lots;
int resultado=0;

int start()
{

//+---------------------Exibe informações no gráfico -----------------------+

ObjectCreate("ObjSeparador", OBJ_LABEL, 0, 0, 0);
ObjectSetText("ObjSeparador","+----------------------------+",6, "Verdana", White);
ObjectSet("ObjSeparador", OBJPROP_CORNER, 1);
ObjectSet("ObjSeparador", OBJPROP_XDISTANCE, 10);
ObjectSet("ObjSeparador", OBJPROP_YDISTANCE, 10);

ObjectCreate("ObjEA", OBJ_LABEL, 0, 0, 0);
ObjectSetText("ObjEA","Heading dos Mineiros",6, "Verdana", White);
ObjectSet("ObjEA", OBJPROP_CORNER, 1);
ObjectSet("ObjEA", OBJPROP_XDISTANCE, 10);
ObjectSet("ObjEA", OBJPROP_YDISTANCE, 20);

ObjectCreate("ObjNome", OBJ_LABEL, 0, 0, 0);
ObjectSetText("ObjNome",AccountName(),6, "Verdana", White);
ObjectSet("ObjNome", OBJPROP_CORNER, 1);
ObjectSet("ObjNome", OBJPROP_XDISTANCE, 10);
ObjectSet("ObjNome", OBJPROP_YDISTANCE, 30);   

ObjectCreate("ObjNumero", OBJ_LABEL, 0, 0, 0);
ObjectSetText("ObjNumero",AccountNumber(),6, "Verdana", White);
ObjectSet("ObjNumero", OBJPROP_CORNER, 1);
ObjectSet("ObjNumero", OBJPROP_XDISTANCE, 10);
ObjectSet("ObjNumero", OBJPROP_YDISTANCE, 40);

ObjectCreate("ObjSeparador2", OBJ_LABEL, 0, 0, 0);
ObjectSetText("ObjSeparador2","+----------------------------+",6, "Verdana", White);
ObjectSet("ObjSeparador2", OBJPROP_CORNER, 1);
ObjectSet("ObjSeparador2", OBJPROP_XDISTANCE, 10);
ObjectSet("ObjSeparador2", OBJPROP_YDISTANCE, 50);

ObjectCreate("ObjBalace", OBJ_LABEL, 0, 0, 0);
ObjectSetText("ObjBalace","Balaco: " + AccountBalance(),6, "Verdana", White);
ObjectSet("ObjBalace", OBJPROP_CORNER, 1);
ObjectSet("ObjBalace", OBJPROP_XDISTANCE, 10);
ObjectSet("ObjBalace", OBJPROP_YDISTANCE, 60);

ObjectCreate("ObjAsk", OBJ_LABEL, 0, 0, 0);
ObjectSetText("ObjAsk","Ask: " + Ask,6, "Verdana", White);
ObjectSet("ObjAsk", OBJPROP_CORNER, 1);
ObjectSet("ObjAsk", OBJPROP_XDISTANCE, 10);
ObjectSet("ObjAsk", OBJPROP_YDISTANCE, 70);

ObjectCreate("ObjBid", OBJ_LABEL, 0, 0, 0);
ObjectSetText("ObjBid","Bid: " + Bid,6, "Verdana", White);
ObjectSet("ObjBid", OBJPROP_CORNER, 1);
ObjectSet("ObjBid", OBJPROP_XDISTANCE, 10);
ObjectSet("ObjBid", OBJPROP_YDISTANCE, 80);

ObjectCreate("Objmar", OBJ_LABEL, 0, 0, 1);
ObjectSetText("Objmar","Equity: " + AccountEquity(),6, "Verdana", White);
ObjectSet("Objmar", OBJPROP_CORNER, 1);
ObjectSet("Objmar", OBJPROP_XDISTANCE, 10);
ObjectSet("Objmar", OBJPROP_YDISTANCE, 90);

ObjectCreate("Objfreemar", OBJ_LABEL, 0, 0, 1);
ObjectSetText("Objfreemar","Margem Livre: " + AccountFreeMargin(),6, "Verdana", White);
ObjectSet("Objfreemar", OBJPROP_CORNER, 1);
ObjectSet("Objfreemar", OBJPROP_XDISTANCE, 10);
ObjectSet("Objfreemar", OBJPROP_YDISTANCE, 100);

ObjectCreate("ObjProfit", OBJ_LABEL, 0, 0, 1);
ObjectSetText("ObjProfit","Lucro: " + AccountProfit(),6, "Verdana", White);
ObjectSet("ObjProfit", OBJPROP_CORNER, 1);
ObjectSet("ObjProfit", OBJPROP_XDISTANCE, 10);
ObjectSet("ObjProfit", OBJPROP_YDISTANCE, 110);

ObjectCreate("Objpar", OBJ_LABEL, 0, 0, 1);
ObjectSetText("Objpar","Par atual: " + Symbol(),6, "Verdana", White);
ObjectSet("Objpar", OBJPROP_CORNER, 1);
ObjectSet("Objpar", OBJPROP_XDISTANCE, 10);
ObjectSet("Objpar", OBJPROP_YDISTANCE, 120);

ObjectCreate("Objorderpar", OBJ_LABEL, 0, 0, 1);
ObjectSetText("Objorderpar","Ordens no par atual: " + TotalOrdersCount(),6, "Verdana", White);
ObjectSet("Objorderpar", OBJPROP_CORNER, 1);
ObjectSet("Objorderpar", OBJPROP_XDISTANCE, 10);
ObjectSet("Objorderpar", OBJPROP_YDISTANCE, 130);

ObjectCreate("Objtotalordens", OBJ_LABEL, 0, 0, 1);
ObjectSetText("Objtotalordens","Total de Ordens Abertas: " + OrdersTotal(),6, "Verdana", White);
ObjectSet("Objtotalordens", OBJPROP_CORNER, 1);
ObjectSet("Objtotalordens", OBJPROP_XDISTANCE, 10);
ObjectSet("Objtotalordens", OBJPROP_YDISTANCE, 140);

ObjectCreate("Objprecoabertura", OBJ_LABEL, 0, 0, 1);
ObjectSetText("Objprecoabertura","Preço de Abertura da última ordem: " + resultado,6, "Verdana", White);
ObjectSet("Objprecoabertura", OBJPROP_CORNER, 1);
ObjectSet("Objprecoabertura", OBJPROP_XDISTANCE, 10);
ObjectSet("Objprecoabertura", OBJPROP_YDISTANCE, 150);

ObjectCreate("ObjSeparador3", OBJ_LABEL, 0, 0, 0);
ObjectSetText("ObjSeparador3","+----------------------------+",6, "Verdana", White);
ObjectSet("ObjSeparador3", OBJPROP_CORNER, 1);
ObjectSet("ObjSeparador3", OBJPROP_XDISTANCE, 10);
ObjectSet("ObjSeparador3", OBJPROP_YDISTANCE, 160);

//+------------------------------------------------------------------+

	// ------------ verifica permissões =----------------------

	//int    AllowedAccountNo = 11217861; // StrToInteger(/* 123456 */ char [52]+char[57]+char[49]+char[53]+char[51]);
	//  if (AccountNumber() != AllowedAccountNo) 
	//  {
	//    Print("Você não tem autorização para utilizar este EA. Favor entrar em contato com o email: ejvcoelho@gmail.com!");
	//    int ret=MessageBox("Você não tem autorização para utilizar este EA. 	//Favor entrar em contato com o email: ejvcoelho@gmail.com!", "Aviso de Licença", MB_OK);
	//    return(1);
	//  }



 if((TotalOrdersCount()==0) && (ThisBarTrade!=Bars)) 
  {
     	operacao=0;
     	Lots=resetlots;

	   if(Open[1]<Close[1]) // regra de abertura de compras
	   {
	   resultado=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,(Ask-(StopLoss/10000)),(Ask+(TakeProfit/10000)),"Edge",MagicNumber,0,Blue);
	   operacao=1;
	   ThisBarTrade=Bars;
	   }
	
	   if(Open[1]>Close[1]) // regra de abertura de vendas
	   {
      resultado=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,(Bid+(StopLoss/10000)),(Bid-(TakeProfit/10000)),"MINERADOR",MagicNumber,0,Red);
	   operacao=2;
	   ThisBarTrade=Bars;
	   }
	  

   }
// ----------------------------- compensações --------------------------------    

if(TotalOrdersCount()>0)

{

	if((operacao==1)&&(Ask<(OrderOpenPrice()-((StopLoss/10000)/2)))&&(OrderSymbol()==Symbol())) // compensação de perdas na compra
	{	
		
		Lots=(Lots*3);
		resultado=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,(Bid+(StopLoss/10000)),(Bid-(TakeProfit/10000)),"MINERADOR",MagicNumber,0,Red);
		operacao=2;
		
	}
	
	
	   
	   if((operacao==2)&&(Bid>(OrderOpenPrice()+((StopLoss/10000)/2)))&&(OrderSymbol()==Symbol())) // compensação de perdas na venda
	   {
	
		   Lots=(Lots*3);
		   resultado=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,(Ask-(StopLoss/10000)),(Ask+(TakeProfit/10000)),"Edge",MagicNumber,0,Blue);
		   operacao=1;

	   }
	  
}	
     
   return(0);
 }


// --------- conta número de ordens ativas -------------------

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderSymbol() == Symbol() && OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);

}


It occurs that when EA is running on more than one chart.
the OrderOpenPrice assumes the value of the opening of the last order , even if it is different chart .

Can anyone tell me how to EA interpret the data within a single chart ?
Thanks
Keith Watford
Moderator
8007
Keith Watford 2016.03.08 20:38  
Select the required order before using
OrderOpenPrice()
whroeder1
13644
whroeder1 2016.03.09 11:59  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. if((TotalOrdersCount()==0) && (ThisBarTrade!=Bars)) 
    Bars is unreliable (a refresh/reconnect can change number of bars on chart) volume is unreliable (miss ticks) Always use time. New candle - MQL4 forum
  3. resultado=OrderSend( Symbol(), OP_BUY, Lots, Ask, Slippage, Ask-(StopLoss/10000), Ask+(TakeProfit/10000), "Edge", MagicNumber, 0, Blue);
    Don't hard code numbers. "/10000" is the same as "*Point" except on JPY and non-currencies.
  4. You buy at the Ask, so your stops are relative to the Bid.
  5. Check your return codes (OrderSend and OrderSelect) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  6. OrderOpenPrice assumes the value of the opening of the last order , even if it is different chart .
    Mt4 doesn't assume anything. Your TotalOrdersCount() last selected an order with the highest position.
    if(TotalOrdersCount()>0){
     if((operacao==1)&&(Ask<(OrderOpenPrice()-((StopLoss/10000)/2)))&&(OrderSymbol()==Symbol())) // compensação de perdas na compra
    Like GumRai said, you can't use any Trade Functions - MQL4 Reference until you select an order.
/
To add comments, please log in or register