Indicator -> Automated Trade - Keeps firing

 

Hi Guys,

New to MQL and trying to automate my trades from an indicator that I have. I've been reading up on the various threads here and been able to get some code together but it keeps firing everybar. Would appreciate if someone could point me in the right direction in order for me to fix.

Code:

// Variable initialization
extern int magic=6203;
extern double lots = 0.01;
datetime newbar; 
double Step1, Step2, Step3, Histo1, Histo2, Histo3, Shift;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
   
//---
   return(INIT_SUCCEEDED);
  }


//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }


//+------------------------------------------------------------------+
//| Check to make sure enough funds for the trade                    |
//+------------------------------------------------------------------+
bool CheckMoneyForTrade(string symb, double lots,int type)
  {
   double free_margin=AccountFreeMarginCheck(symb,type, lots);
   //-- if there is not enough money
   if(free_margin<0)
     {
      string oper=(type==OP_BUY)? "Buy":"Sell";
      Print("Not enough money for ", oper," ",lots, " ", symb, " Error code=",GetLastError());
      return(false);
     }
   //--- checking successful
   return(true);
  }
  

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() // this starts the engine of the code.
{
   if(newbar==Time[0])return(0); // Was hoping that this would stop the firing on each bar but doesn't
   else newbar=Time[0];
   
   
   double ArrowUP=iCustom(Symbol(), 0, "AlertArrow", Step1, Step2, Step3, 0, 0, 0, 0, Shift);  
   double ArrowDN=iCustom(Symbol(), 0, "AlertArrow", Step1, Step2, Step3, 0, 0, 0, 1, Shift);  
   
   double HistoUP=iCustom(Symbol(), 0, "AlertValidated", Histo1, Histo2, Histo3, 0, 0, 0, 0, 0);  
   double HistoDN=iCustom(Symbol(), 0, "AlertValidated", Histo1, Histo2, Histo3, 0, 0, 0, 1, 0);  
   
   // Do we have an alert, has it been validated, and do we have enough money?
   if ((ArrowDN != EMPTY_VALUE ) && (HistoDN != EMPTY_VALUE ) && (CheckMoneyForTrade(Symbol(), lots, 1)))
   {
      Comment("Sell - ", TimeToStr(TimeCurrent(),TIME_SECONDS)); // Should only see this appear with the time that the arrow showed
   }
   
   if ((ArrowUP != EMPTY_VALUE ) && (HistoUP != EMPTY_VALUE ) && (CheckMoneyForTrade(Symbol(), lots, 0))) 
   {
      Comment("Buy - ", TimeToStr(TimeCurrent(),TIME_SECONDS));
   }
   
   return(0); // returns back to the start function of the program.
}

When I add this to my chart, it fires immediately (the correct direction) and adds the time when added to the chart. And then you see the time keep updating which indicates that if I had an orderopen instead of comments, that it would be opening an order each candle.

 
NOENEEL NAVEEN SHARMA:

Hi Guys,

New to MQL and trying to automate my trades from an indicator that I have. I've been reading up on the various threads here and been able to get some code together but it keeps firing everybar. Would appreciate if someone could point me in the right direction in order for me to fix.

Code:

When I add this to my chart, it fires immediately (the correct direction) and adds the time when added to the chart. And then you see the time keep updating which indicates that if I had an orderopen instead of comments, that it would be opening an order each candle.

Since you intend to execute once per bar, then the last parameter of your iCustoms should be 1, not 0 (and definitely not uninitialized Shift).

Also, your indicators' EMPTY_VALUE may not be EMPTY_VALUE... indicators can use any value, so you need to open your Data Window to verify.
 
Ok, looks like i need to do some more reading around icustom.
 

So, updated it and removed shift as well as it wasn't needed. But it's still trying to execute the order on every bar? I only want it to execute on the bar that shows the arrow.


Update section:

int start() // this starts the engine of the code.
{
   if(newbar==Time[0])return(0); // 
   else newbar=Time[0];
   
   
   double ArrowUP=iCustom(Symbol(), 0, "AlertArrow", Step1, Step2, Step3, 0, 0, 0, 0);  
   double ArrowDN=iCustom(Symbol(), 0, "AlertArrow", Step1, Step2, Step3, 0, 0, 0, 1);  
   
   double HistoUP=iCustom(Symbol(), 0, "AlertValidated", Histo1, Histo2, Histo3, 0, 0, 0, 0);  
   double HistoDN=iCustom(Symbol(), 0, "AlertValidated", Histo1, Histo2, Histo3, 0, 0, 0, 1);  
   
   if ((ArrowDN != EMPTY_VALUE ) && (HistoDN != EMPTY_VALUE ) && (CheckMoneyForTrade(Symbol(), lots, 1))) 
   {
      Comment("Sell - ", TimeToStr(TimeCurrent(),TIME_SECONDS));
   }
   
   if ((ArrowUP != EMPTY_VALUE ) && (HistoUP != EMPTY_VALUE ) && (CheckMoneyForTrade(Symbol(), lots, 0))) 
   {
      Comment("Buy - ", TimeToStr(TimeCurrent(),TIME_SECONDS));
   }
   
   return(0); // returns back to the start function of the program.
}
 
   double ArrowUP=iCustom(Symbol(), 0, "AlertArrow", Step1, Step2, Step3, 0, 0, 0, 0);  //Buffer 0, shift 0 ??
   double ArrowDN=iCustom(Symbol(), 0, "AlertArrow", Step1, Step2, Step3, 0, 0, 0, 1);  //Buffer 0, shift 1 ??  
   
   double HistoUP=iCustom(Symbol(), 0, "AlertValidated", Histo1, Histo2, Histo3, 0, 0, 0, 0);  //Buffer 0, shift 0 ??  
   double HistoDN=iCustom(Symbol(), 0, "AlertValidated", Histo1, Histo2, Histo3, 0, 0, 0, 1);  //Buffer 0, shift 1 ??  

Your iCustom calls cannot be correct.

 

Ok, perhaps it's my interpretation of what the indicator writer gave me (I don't have the source code)

All i have from them is:

if(!TestOP){
       ArrowUP=iCustom(Symbol(),0,AlertArrow,Step1,Step2,Step3,0,0,0,0,Shift);  
       ArrowDN=iCustom(Symbol(),0,AlertArrow,Step1,Step2,Step3,0,0,0,1,Shift);  

       HistoUP=iCustom(Symbol(),0,AlertValidated,Histo1,Histo2,Histo3,0,0,0,0,0);  
       HistoDN=iCustom(Symbol(),0,AlertValidated,Histo1,Histo2,Histo3,0,0,0,1,0);  
      } 

These are the indicator inputs if that helps


 

I meant this:

   double ArrowUP=iCustom(Symbol(), 0, "AlertArrow", Step1, Step2, Step3, 0, 0, 0, 0, 1);  
   double ArrowDN=iCustom(Symbol(), 0, "AlertArrow", Step1, Step2, Step3, 0, 0, 0, 1, 1);  
   
   double HistoUP=iCustom(Symbol(), 0, "AlertValidated", Histo1, Histo2, Histo3, 0, 0, 0, 0, 1);  
   double HistoDN=iCustom(Symbol(), 0, "AlertValidated", Histo1, Histo2, Histo3, 0, 0, 0, 1, 1);  

And verify whether these indicators return EMTPY_VALUE...

 
Aaaah far out, I think they sent me the wrong buffer code, doesn't match ugh.
 
NOENEEL NAVEEN SHARMA:

Ok, perhaps it's my interpretation of what the indicator writer gave me (I don't have the source code)

All i have from them is:

These are the indicator inputs if that helps

I don't see how your 4 iCustom() lines matches the inputs of the indicators.

Perhaps you can try out default values, by simplifying your iCustom() to these:

   double ArrowUP=iCustom(Symbol(), 0, "AlertArrow", 0, 1);  
   double ArrowDN=iCustom(Symbol(), 0, "AlertArrow", 1, 1);  
   
   double HistoUP=iCustom(Symbol(), 0, "AlertValidated", 0, 1);  
   double HistoDN=iCustom(Symbol(), 0, "AlertValidated", 1, 1);  

See what happens first...

 

Yeah i think they sent me the wrong buffer code to use. But looks like they say to use shift value of 0? 

Ok still not working and i have tried the following combinations:

   double ArrowUP=iCustom(Symbol(),0,"AlertArrow", Ma1, Ma2, Ma3, Ma4, 0, 0, 0, 0, 0);  
   double ArrowDN=iCustom(Symbol(),0,"AlertArrow", Ma1, Ma2, Ma3, Ma4, 0, 0, 0, 1, 0);  
   
   double HistoUP=iCustom(Symbol(),0,"AlertValidated", Histogram, Method_Type, 0, 0, 0, 0, 0);  
   double HistoDN=iCustom(Symbol(),0,"AlertValidated", Histogram, Method_Type, 0, 0, 0, 1, 0);  
   double ArrowUP=iCustom(Symbol(),0,"AlertArrow", Ma1, Ma2, Ma3, Ma4, 0, 0, 0, 0, 1);  
   double ArrowDN=iCustom(Symbol(),0,"AlertArrow", Ma1, Ma2, Ma3, Ma4, 0, 0, 0, 1, 1);  
   
   double HistoUP=iCustom(Symbol(),0,"AlertValidated", Histogram, Method_Type, 0, 0, 0, 0, 1);  
   double HistoDN=iCustom(Symbol(),0,"AlertValidated", Histogram, Method_Type, 0, 0, 0, 1, 1); 
   double ArrowUP=iCustom(Symbol(),0,"AlertArrow", 0, 1);  
   double ArrowDN=iCustom(Symbol(),0,"AlertArrow", 1, 1);  
   
   double HistoUP=iCustom(Symbol(),0,"AlertValidated", 0, 1);  
   double HistoDN=iCustom(Symbol(),0,"AlertValidated", 1, 1);  
 
NOENEEL NAVEEN SHARMA:

Yeah i think they sent me the wrong buffer code to use. But looks like they say to use shift value of 0? 

Ok still not working and i have tried the following combinations:

Look at my earlier post - check return values, verify if they are EMTPY_VALUE. Also, follow what they said and use 0 IF the arrows appear at bar 0.

Reason: