Problems with Trade errors!

 

@Ahmet Metin Yilmaz@Keith Watford@Sergey Gerasimov@Vladimir Kazennov@William Roeder@nicholish enHello guys,

I have been struggling to fix this problem in my code below because it keeps opening multiple buy positions instead of printing failed to buy in the journal!

//Create an instance of CTrade
#include <Trade\Trade.mqh>

CTrade trade;
CPositionInfo m_position;


void OnTick()
     {//
  
double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
MqlRates PriceInfo[];

ArraySetAsSeries(PriceInfo,true);

int Data=CopyRates(_Symbol,_Period,0,6,PriceInfo);


//Buy when condition true
if(PriceInfo[1].close>PriceInfo[1].open && PositionsTotal()==0)
 {//
trade.Buy(
0.2,//how much
NULL,//current symbol
Ask,//buy prices
0,//stop loss
0,//take profit 
NULL//comment
);

 }//
 
 
//INCASE BUY FAILED...
if(!trade.Buy(
0.2,//how much
NULL,//current symbol
Ask,//buy prices
0,//stop loss
0,//take profit 
NULL//comment
))
{//

//PRINT SOMETHING 
Print("Request to open BUY has failed. Return code=",trade.ResultRetcode(), ".Code Description:",trade.ResultRetcodeDescription());


}//
   
       }//Ontick

What should be done to handle failed trade errors using Ctrade?

My Apologies since iam not well familiar with mql5!... and thanks for your time cheers 

 
Ifadatty999:

@Ahmet Metin Yilmaz@Keith Watford@Sergey Gerasimov@Vladimir Kazennov@William Roeder@nicholish enHello guys,

I have been struggling to fix this problem in my code below because it keeps opening multiple buy positions instead of printing failed to buy in the journal!

What should be done to handle failed trade errors using Ctrade?

My Apologies since iam not well familiar with mql5!... and thanks for your time cheers 

Hi, What you can do is save the value of 

trade.Buy

in a variable .

bool buy =false;

So, declare the variable before you open the trade, when you open the trade just make it 

buy = trade.Buy(
0.2,//how much
NULL,//current symbol
Ask,//buy prices
0,//stop loss
0,//take profit 
NULL//comment
);

And when you want to check if you opened a trade, check if buy is true or false

 
@William Roeder@Vladimir Stashkevich@nicholish en@Keith Watford@Ahmet Metin Yilmaz@Stanislav IvanovStanislav Ivanov:

Hi, What you can do is save the value of 

in a variable .

So, declare the variable before you open the trade, when you open the trade just make it 

And when you want to check if you opened a trade, check if buy is true or false

Hi , Thanks for your reply ... I did what you said but failed it stills open multiple buy position s and all I need is just reporting buy error has failed!


//Create an instance of CTrade
#include <Trade\Trade.mqh>

CTrade trade;
CPositionInfo m_position;

input double LotSize=0.2;

bool buy=true;


void OnTick()
     {//
  
double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   


Buy=trade.Buy(
0.2,//how much
NULL,//current symbol
Ask,//buy prices
0,//stop loss
0,//take profit 
NULL//comment
);



//Buy when condition true
if(PositionsTotal()==0)
 {//
trade.Buy(
0.2,//how much
NULL,//current symbol
Ask,//buy prices
0,//stop loss
0,//take profit 
NULL//comment
);

 }//
 
 
//INCASE BUY FAILED...
if(buy==false)
{//

//PRINT SOMETHING 
Print("Request to open BUY has failed. Return code=",trade.ResultRetcode(), ".Code Description:",trade.ResultRetcodeDescription());


}//
   
       }//Ontick