Newbie trying to code Ichimoku Kinko Hyo indicator into an EA. Help me please.

To add comments, please log in or register
Rustyy117
19
Rustyy117  

Hi,

Firstly I'd just like to preface this by saying i'm new to coding in general so please be gentle if I've made any obvious mistakes (which I'm sure I have).

Ok, so I'm trying to create an EA that buys and sells based off the Ichimoku Kinko Hyo indicator.

It's a super simple EA, I've left out loads of stuff to try and keep it clean, here's the bulk of it:

void OnTick()

  {

//---

   int Orders;

   

   Orders = OrdersTotal();

      if(Orders<1)ichimoku();

 }     

void ichimoku() 

      {



   double Tenkansen = iIchimoku(NULL,0,9,26,52,MODE_TENKANSEN,1);

   double Kijunsen = iIchimoku(NULL,0,9,26,52,MODE_KIJUNSEN,1);

   double Senkouspana = iIchimoku(NULL,0,9,26,52,MODE_SENKOUSPANA,1);

   double Senkouspanb = iIchimoku(NULL,0,9,26,52,MODE_SENKOUSPANB,1);

   double Chikouspan = iIchimoku(NULL,0,9,26,52,MODE_CHIKOUSPAN,1);

   

   

   if(Open[1]<Chikouspan)OrderEntry();

   

      }      

      

void OrderEntry()

   {

   OrderSend(Symbol(),OP_BUY,Lotsize,Ask,3,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),NULL,MagicNumber,0,Green);

   }     


I've also attached the entire EA to this post.

Ok, so all I'm trying to say here is if the Open of candle 1 is less than (below) Chikouspan (the green line of the indicator) then run the OrderEntry function which places a Buy order.

When I run this EA nothing happens, it doesn't place any trades (tested with 5 years of data), looking back through the chart it's obvious that it should have.

However if I replace "Chikouspan" with say, "Kijunsen" then it will place trades.


So my question is why will it not place trades based on Chikouspan?


Thanks in advance for your help.

Rustyy117

Files:
ichimoku.mq4 3 kb
Juvenille Emperor Limited
Moderator
10220
Eleni Anna Branou  

Please use the </> button to post your code.


William Roeder
20210
William Roeder  
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. int Orders = OrdersTotal();
    if(Orders<1)ichimoku()
    Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  3.   if(Open[1]<Chikouspan)OrderEntry();
    This condition will be true every tick and you will open a new order every tick. Search new bar, or act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - Strategy Tester - Expert Advisors and Automated Trading - MQL5 programming forum

  4. OrderSend(Symbol(),OP_BUY,Lotsize,Ask,3,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),NULL,MagicNumber,0,Green);
    Check your return codes for errors, report them and you would know why.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  5. Rustyy117: So my question is why will it not place trades based on Chikouspan?
    Use the debugger or print out your variables, including _LastError and find out why.

Rustyy117
19
Rustyy117  
whroeder1:
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  3. This condition will be true every tick and you will open a new order every tick. Search new bar, or act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - Strategy Tester - Expert Advisors and Automated Trading - MQL5 programming forum

  4. Check your return codes for errors, report them and you would know why.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  5. Use the debugger or print out your variables, including _LastError and find out why.

1. Thanks, have edited my post accordingly.

2.Not quite sure I understand, there is a Magic Number in the EA, if I wanted to use it on other charts then I could just manually change it, no?

3.Surely it wouldn't open a new order every tick because the function ichimoku is only executed if OrdersTotal =<1, so if there's already an order open then it wouldn't open a second. When I test the EA with 

if(Open[1]<Kijunsen)OrderEntry();

it does open/close trades, but only 1 at a time as expected.

Ok yeah, I will use LastError and see whats going on, well try too.


Thanks for your help so far.

William Roeder
20210
William Roeder  
Rustyy117: 2.Not quite sure I understand, there is a Magic Number in the EA, if I wanted to use it on other charts then I could just manually change it, no?
You aren't using it. Read the link provided in #2.2
Rustyy117
19
Rustyy117  
whroeder1:
You aren't using it. Read the link provided in #2.2

Oh ok, I've changed MagicNumber to OrderMagicNumber, the value is the same at 1. Does there need to be an OrderSelect function to check that the magic number is correct?

I added some code to see what the last error was but none were displayed:

if(Open[1]<Chikouspan)OrderEntry();

   else Comment( "The Last Error was:",GetLastError());

In the Journal it says TestGenerator: 760 Generating errors, logged 100 first error records

The vast majority are "TestGenerator: unmatched data error (Volume limit 966 exceeded) Another is TestGenerator: unmatched data error (low value 1.08667 at 2016.01.27 13:30 is not reached from the least timeframe, low price 1.08704 mismatches)

I do get a warning after compiling the EA, "return value of 'OrderSend' should be checked" Not sure why though.

So over my head here :/


Rustyy117
19
Rustyy117  

Quick update here, I've changed the code alittle, not really sure why it wouldn't trade with Chikouspan but would with Kijunsen before, but now it does work with Chikouspan.

I think perhaps part of the issue before is that I had my OrderSend as a Void which doesn't return anything, OrderSend should be an int.

Here's the new code:

//+------------------------------------------------------------------+
//|                                                     ichimoku.mq4 |
//|                                                        Rustyy117 |
//|                                                   www.google.com |
//+------------------------------------------------------------------+
#property copyright "Rustyy117"
#property link      "www.google.com"
#property version   "1.00"
#property strict

extern int TakeProfit=300;
extern int StopLoss=100;

extern double Lotsize=0.1;
extern int OrderMagicNumber=01;

double pips;
int OrderEntry;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE); //Adjusts pip size based on symbol choosen.
   if (ticksize == 0.00001 || Point == 0.001)
   pips = ticksize*10;
   else pips = ticksize;
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   int Orders;
   
   Orders = OrdersTotal();
      if(Orders<1)ichimoku();
 }     
void ichimoku()
   {
   double Tenkansen = iIchimoku(NULL,0,9,26,52,MODE_TENKANSEN,1);
   double Kijunsen = iIchimoku(NULL,0,9,26,52,MODE_KIJUNSEN,1);
   double Senkouspana = iIchimoku(NULL,0,9,26,52,MODE_SENKOUSPANA,1);
   double Senkouspanb = iIchimoku(NULL,0,9,26,52,MODE_SENKOUSPANB,1);
   double Chikouspan = iIchimoku(NULL,0,9,26,52,MODE_CHIKOUSPAN,1);
   
 
   
   if(Open[1]<Chikouspan)OrderEntry;

   OrderEntry = OrderSend(Symbol(),OP_BUY,Lotsize,Ask,3,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),NULL,OrderMagicNumber,0,Green);
   if(OrderEntry<0) 
     { 
      Print("OrderSend failed with error #",GetLastError()); 
     } 
   else 
      Print("OrderSend placed successfully"); 
   
     
}   

I still get a warning about if(Open[1]<Chikouspan)OrderEntry; the warning is "expression has no effect"

I said before that it now works with Chikouspan, well it does place trades now, however it'll immediately enter a new trade after closing the previous one, even if the open price of candle 1 isn't less than Chikouspan(!)

@whroeder, you said earlier that the 'if' statement would always be true, could you explain why?

Thanks for the help, much appreciated.

Rustyy117
19
Rustyy117  

Hello again,

Thought I would rewrite some of the code, try and remove the warning after compiling and add some code that tells the EA when there's a new candle and what to do after

//+------------------------------------------------------------------+
#property strict

extern int TakeProfit = 300;
extern int StopLoss = 100;
extern double LotSize = 0.1;

extern int OrderMagicNumber = 02;

double pips;




//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE); //Adjusts pip size based on symbol choosen.
   if (ticksize == 0.00001 || Point == 0.001)
   pips = ticksize*10;
   else pips = ticksize;
   
      return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int start()
{
   static datetime time = Time[0];
   
   if(time != Time[0])Orders();
      {
       time = Time[0];
      } 
      return(0);
}
   int Orders()
   {
   int total = OrdersTotal();
   if(total <1)iChimoku();
   
   return(0);
   }
  
   
   void iChimoku()
   {
   double   Tenkansen = iIchimoku(NULL,0,9,26,52,MODE_TENKANSEN,1);
   double   Kijunsen = iIchimoku(NULL,0,9,26,52,MODE_KIJUNSEN,1);
   double   Senkouspana = iIchimoku(NULL,0,9,26,52,MODE_SENKOUSPANA,1);
   double   Senkouspanb = iIchimoku(NULL,0,9,26,52,MODE_SENKOUSPANB,1);
   double   Chikouspan = iIchimoku(NULL,0,9,26,52,MODE_CHIKOUSPAN,1);
   
   if(Open[1]<Kijunsen)OrderEntry();
   }

int OrderEntry()
   {
    int ticket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,10,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),NULL,0,0,Green);
      if(ticket<0)  
         
         Print("OrderSend failed with error #",GetLastError());
         
      else
         Print("OrderSend Placed Successfully");
         
         return(ticket);
          
   }

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

Once again, this works with Kijunsen, but not Chikouspan, with Chikouspan I get mismatch errors and volume errors, I thought it might be a problem with my data, so tried downloading the lastest, also tried uninstalling and reinstalling MT4 from my broker but still get the same errors.

Not asking for anyone to code for me, just a nudge in the right direction would make my day, please help.

Thanks.

DAP
28
DAP  
Rustyy117:

Hello again,

Thought I would rewrite some of the code, try and remove the warning after compiling and add some code that tells the EA when there's a new candle and what to do after

Once again, this works with Kijunsen, but not Chikouspan, with Chikouspan I get mismatch errors and volume errors, I thought it might be a problem with my data, so tried downloading the lastest, also tried uninstalling and reinstalling MT4 from my broker but still get the same errors.

Not asking for anyone to code for me, just a nudge in the right direction would make my day, please help.

Thanks.



I don't know if you ever figured this out, but ChikouSpan is nothing more than price. You don't have to use the indicator to get it. ChikouSpan = Close[1];  

To add comments, please log in or register