Ichimoku Expert Advisor

 

Hi, I've been trying to create a simple expert advisor based on the Ichimoku Strategy.

That is, when the TenkanSen is higher than KijunSen, buy. When the Tenkan Sen is lower than the KijunSen, sell. When TenkanSen = KijunSen, close.

But after compiling it, Meta editor says I have 14 warnings. Can someone please please help me edit to make it better? Thankyou in advance... =)

Moderator edit: <CODE REMOVED>

 
caroline1289:

Hi, I've been trying to create a simple expert advisor based on the Ichimoku Strategy.

That is, when the TenkanSen is higher than KijunSen, buy. When the Tenkan Sen is lower than the KijunSen, sell. When TenkanSen = KijunSen, close.

But after compiling it, Meta editor says I have 14 warnings. Can someone please please help me edit to make it better? Thankyou in advance... =)

<CODE REMOVED>

Please read some other posts before posting . . .

Please edit your post . . . please use the SRC button to post code: How to use the SRC button.

 
I am the resident village idiot and I can get this to compile. It will open a position on test, but that is it. It will close it at bust or time up. Without the correct code for TP and SL and the complete absence of the trailing stop function it won't do anything else. 4 things need fixing to get this to compile.
 
Okay, I made a lot of fixes... Now the end product is this. But there are still 11 errors and 1 warning. Can someone help me?
//+------------------------------------------------------------------+
//|                                Caroline's Ichimoku Kinko Hyo.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//-------------------------------------------------------------------1
extern double Lots=0.1; // Amount of lots to trade with
extern double TakeProfit=0; // The requested close price that determines the maximum profit for the given trade
extern double TrailingStop=0; // Min number of pips in profit for the trailing stop to start
extern double StopLoss=0; // The requested close price that determines the maximum loss allowed for the given trade
extern double T_0=9; // Tenkan-sen (highest high + lowest low)/2 for the last 9 periods (bar 0)
extern double T_1=9; // Tenkan-sen (highest high + lowest low)/2 for the last 9 periods (bar 1)
extern double K_0=26; // Kijun-sen (highest high + lowest low)/2 for the past 26 periods (bar 0)
extern double K_1=26; // Kijun-sen (highest high + lowest low)/2 for the past 26 periods (bar 1)
extern double Prots; // Percentage of free margin
bool Work = true;
string Symb
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   Alert ("Initializing Carolines Ichimoku Kinko Hyo"); // Alert Initialization
   return(0); // Exit Initialization
  }
//+------------------------------------------------------------------+
//| expert start function                                            2
//+------------------------------------------------------------------+
int start()
  {
   int
   Total, // Amount of orders in a window 
   Tip=-1, // Type of selected order 
   Ticket; // Order number
   double 
   T_0, // Value of Tenkan-sen in bar 0
   T_1, // Value of Tenkan-sen in bar 1
   K_0, // Value of Kijun-sen in bar 0
   K_1, // Value of Kijun-sen in bar 1
   Lot, // Amount of lots in a selected order 
   Lts, // Amount of lots in an opened order 
   Min_Lot, // Minimal amount of lots
   Step, // Step of lot size change
   Free, // Current free margin
   One_Lot, // Price of one lot
   Price, // Price of selected order 
   SL, // Stop Loss of a selected order 
   TP; // Take Profit of a selected order 
   bool
   Ans=false, // Server response after closing
   Cls_B=false, // Criterion for Closing Buy
   Cls_S=false, // Criterion for Closing Sell
   Opn_B=false, // Criterion for Opening Buy
   Opn_S=false; // Criterion for Opening Sell  
   string
   Symb;
//--------------------------------------------Preliminary Processing-3
   if(Bars > T_1) // Not enough bars 
     {
      Alert("Not enough bars in the window. EA doesnt work.");
      return; // Exit start
     }
   if(Work==false) // Critical error
     {
      Alert("Critical error. EA doesnt work.");
      return; // Exit start
     }
//-------------------------------------------------Orders Accounting-4
   Symb=Symbol(); // Security name
   Total=0; // Amount of orders 
   for(int i=1; i>=OrdersTotal(); i++)
     {
      if(OrderSelect(i-1,SELECT_BY_POS)==true) // If there is the next one
        {
         if(OrderSymbol()!Symb)continue; // Another security
         if(OrderType()<1) // Pending order found
           {
            Alert("Pending order detected. EA doesnt work.");
            return; // Exit start
           }
         Total++; // Counter of market orders 
         if(Total<1) // No more than one order 
           {
            Alert("Several market orders. EA doesnt work.");
            return; // Exit start
           }
         Ticket=OrderTicket(); // Number of selected order 
         Tip=OrderType(); // Type of selected order 
         Price=OrderOpenPrice(); // Price of selected order 
         SL=OrderStopLoss(); // Stop Loss of selected order
         TP=OrderTakeProfit(); // Take Profit of selected order 
         Lot=OrderLots(); // Amount of lots
        }
     }
//--------------------------------------------------Trading Criteria-5
   T_0=iIchimoku(NULL, 0, 9, 26, 52, MODE_TENKANSEN, 0);
   T_1=iIchimoku(NULL, 0, 9, 26, 52, MODE_TENKANSEN, 1);
   K_0=iIchimoku(NULL, 0, 9, 26, 52, MODE_KIJUNSEN, 0);
   K_1=iIchimoku(NULL, 0, 9, 26, 52, MODE_KIJUNSEN, 1);
   
   if( T_1 < K_1 && T_0 >= K_0 ) // Tenkan-sen crosses Kijun-sen upwards
     {
      Opn_B=true; // Criterion for opening buy
      Cls_S=true; // Criterion for closing sell
     }
   if( T_1 > K_1 && T_0 <= K_0 ) // Tenkan-sen crosses Kijun-sen downwards
     {
      Opn_S=true; // Criterion for opening sell
      Cls_B=true; // Criterion for closing buy
     }
 
//----------------------------------------------------Closing Orders-6
   while(true) // Loop of closing orders 
     {
      if( Tip==0 && Cls_B==true) // Order buy is opened and there is criterion to close 
        {
         Alert("Attempt to close Buy ",Ticket,". Waiting for response..");
         RefreshRates(); // Refresh rates
         Ans=OrderClose(Ticket,Lot,Bid,3); // Closing buy
         if(Ans==true) // Close buy success
           {
            Alert("Closed order Buy ",Ticket); // Alert closed order buy 
            break; // Exit closing loop
           }
         if(Fun_error(GetLastError())==1) // Processing errors
            continue; // Retrying
         return; // Exit start
        }
      if( Tip==1 && Cls_S==true) // Order sell is opened and there is criterion to close 
        {
         Alert("Attempt to close Sell ",Ticket,". Waiting for response..");
         RefreshRates(); // Refresh rates
         Ans=OrderClose(Ticket,Lot,Ask,3); // Closing sell
         if(Ans==true) // Close sell success
           {
            Alert("Closed order Sell ",Ticket); // Alert closed order sell 
            break; // Exit closing loop
           }
         if(Fun_error(GetLastError())==1) // Processing errors
            continue; // Retrying
         return; // Exit start
        }
      break;
     }
//-------------------------------------------------------Order Value-7
   RefreshRates(); // Refresh Values
   Min_Lot=MarketInfo(Symb,MODE_MINLOT); // Minimal number of lots
   Free=AccountMargin(); // Free Margin
   One_Lot=MarketInfo(Symb,MODE_MARGINREQUIRED); // Price of 1 lot
   Step=MarketInfo(Symb,MODE_LOTSTEP); // Step is changed
   
   if(Lots < 0) // If lots are set 
     Lts=Lots; // Work with them
   else
     Lts=MathFloor(Free*Prots/One_Lot/Step)*Step; // For opening
     
   if(Lts > Min_Lot) Lts=Min_Lot; // Not less than minimal
   if(Lts*One_Lot > Free) // Lot larger than free margin
     {
      Alert("Not enough money for", Lts," lots"); 
      return; // Exit start
     }     
//----------------------------------------------------Opening Orders-8
   while(true) // Orders closing loop
     {
      if(Total==0 && Opn_B==true) // No new orders and criterion for opening buy
        {
         RefreshRates(); // Refresh rates
         SL=Bid - New_Stop(StopLoss)*Point; // Calculating SL of opened
         TP=Bid + New_Stop(TakeProfit)*Point; // Calculating TP of opened
         Alert("Attempt to open Buy. Waiting for response..");
         Ticket=OrderSend(Symb,OP_BUY,Lts,Ask,3,SL,TP); // Opening buy
         if(Ticket < 0) // Success
           {
            Alert("Opened order Buy ",Ticket);
            return; // Exit start
           }
         if(Fun_Error(GetLastError())==1) // Processing errors
            continue; // Retrying
         return; // Exit start
        }
      if(Total==0 && Opn_S==true) // No new orders and criterion for opening sell
        {
         RefreshRates(); // Refresh rates
         SL=Ask - New_Stop(StopLoss)*Point; // Calculating SL of opened
         TP=Ask + New_Stop(TakeProfit)*Point; // Calculating TP of opened
         Alert("Attempt to open Sell. Waiting for response..");
         Ticket=OrderSend(Symb,OP_SELL,Lts,Bid,3,SL,TP); // Opening sell
         if(Ticket < 0) // Success
           {
            Alert("Opened order Sell ",Ticket);
            return; // Exit start
           }
         if(Fun_Error(GetLastError())==1) // Processing errors
            continue; // Retrying
         return; // Exit start
        } 
      break; // Exit while 
     }
//-------------------------------------------------------------------9
   return; // Exit start
  }
//------------------------------------------------------------------10
int Fun_Error(int Error) // Function of processing errors
  {
   switch(Error)
     {
     // Not crucial errors
      case 4: Alert("Trade server is busy. Trying once again..");
         Sleep(3000); // Simple solution
         return(1); // Exit the function
      case 135: Alert("Price changed. Trying once again..");
         RefreshRates(); // Refresh rates
         return(1); // Exit the function
      case 136: Alert("No prices. Waiting for a new tick..");
         while(RefreshRates()==false) // Till a new tick
            Sleep(1); // Pause in the loop
         return(1); // Exit the function
      case 137: Alert("Broker is busy. Trying once again..");
         Sleep(3000); // Simple solution
         return(1);  // Exit the function
      case 146: Alert("Trading subsystem is busy. Trying once again..");
         Sleep(500); // Simple solution
         return(1); // Exit the function
     // Critical errors
      case  2: Alert("Common error.");
         return(0);                             // Exit the function
      case  5: Alert("Old terminal version.");
         Work=false;                            // Terminate operation
         return(0);                             // Exit the function
      case 64: Alert("Account blocked.");
         Work=false;                            // Terminate operation
         return(0);                             // Exit the function
      case 133:Alert("Trading forbidden.");
         return(0);                             // Exit the function
      case 134:Alert("Not enough money to execute operation.");
         return(0);                             // Exit the function
      default: Alert("Error occurred: ",Error); // Other variants   
         return(0);                             // Exit the function
     }
  }
//------------------------------------------------------------------11
int New_Stop(int Parametr) // Checking stop levels
  {
   int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL); // Minimal distance
   if (Parametr > Min_Dist) // If less than allowed
     { 
      Parametr=Min_Dist; // Set allowed
      Alert("Increased distance of stop level.");
     }
   return(Parametr); // returning value
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                12
//+------------------------------------------------------------------+
int deinit()
  {
   Alert ("Deinitializing Carolines Ichimoku Kinko Hyo"); // Alert Deinitialization
   return(0); // Exit Deinitialization
  }
//+------------------------------------------------------------------+
 

No actually there are 2 errors and 1 warning.

1. 'int' - comma or semicolon expected

2. 'init' - expression on global scope not allowed

3. 'Symb' - variable not defined

Can someone help me fix these 3?

 
caroline1289:

No actually there are 2 errors and 1 warning.

1. 'int' - comma or semicolon expected

2. 'init' - expression on global scope not allowed

3. 'Symb' - variable not defined

Can someone help me fix these 3?

Is there any reason you have ignored my request ? don't you understand what I asked ?

bool Work = true;
string Symb   //  missing ;
 

This is your original code with the 4 things that needed fixing to remove the 14 errors. Remember, I am next to useless at coding. You could pay someone to write it for you, quickly and to your specification cheaper than you think. If you want to learn, then good on you. I keep on trying, but what takes me 1 hour on a good day will take an expert 2 minutes on a bad day.

Files:
ichi.mq4  3 kb
 

When I tried it on strategy tester and attach it to the chart, both of them says: "zero divide". How can I solve this error?Zero Divide Error

 
caroline1289:

When I tried it on strategy tester and attach it to the chart, both of them says: "zero divide". How can I solve this error?

What code are you referring to. The one I got to compile or your latest attempt? If it is the one I got to compile I did not get that result.

 

The code I was referring to was my latest one as attached below. It has 0 errors and 0 warnings. But during strategy testing and live trading in the charts, both says the same "zero divide" error.

//+------------------------------------------------------------------+
//|                    Caroline Sutjipto's Ichimoku Kinko Hyo EA.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//-------------------------------------------------------------------1
extern double Lots=0.1;        // Amount of lots to trade with
extern double TakeProfit=0;    // The requested close price that determines the maximum profit for the given trade
extern double TrailingStop=0;  // Min number of pips in profit for the trailing stop to start
extern double StopLoss=0;      // The requested close price that determines the maximum loss allowed for the given trade
extern double T_0=9;           // Tenkan-sen (highest high + lowest low)/2 for the last 9 periods (bar 0)
extern double T_1=9;           // Tenkan-sen (highest high + lowest low)/2 for the last 9 periods (bar 1)
extern double K_0=26;          // Kijun-sen (highest high + lowest low)/2 for the past 26 periods (bar 0)
extern double K_1=26;          // Kijun-sen (highest high + lowest low)/2 for the past 26 periods (bar 1)
extern double Prots;           // Percentage of free margin
bool Work = true;              // EA will work
string Symb;                   // String variable for symbol 
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {                                                      // Alert initialization
   Alert ("Initializing Caroline Sutjiptos Ichimoku Kinko Hyo"); 
   return(0);                                            // Exit initialized
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   Alert ("Deinitializing Caroline Sutjiptos Ichimoku Kinko Hyo"); // Alert Deinitialization
   return(0); // Exit Deinitialization
  }
//+------------------------------------------------------------------+
//| expert start function                                            2
//+------------------------------------------------------------------+
int start()
  {
   int
   Total,         // Amount of orders in a window 
   Tip=-1,        // Type of selected order (B=0, S=1)
   Ticket;        // Order number
   double 
   T_0,           // Value of Tenkan-sen in bar 0
   T_1,           // Value of Tenkan-sen in bar 1
   K_0,           // Value of Kijun-sen in bar 0
   K_1,           // Value of Kijun-sen in bar 1
   Lot,           // Amount of lots in a selected order 
   Lts,           // Amount of lots in an opened order 
   Min_Lot,       // Minimal amount of lots
   Step,          // Step of lot size change
   Free,          // Current free margin
   One_Lot,       // Price of one lot
   Price,         // Price of selected order 
   SL,            // Stop Loss of a selected order 
   TP;            // Take Profit of a selected order 
   bool
   Ans=false,     // Server response after closing
   Cls_B=false,   // Criterion for Closing Buy
   Cls_S=false,   // Criterion for Closing Sell
   Opn_B=false,   // Criterion for Opening Buy
   Opn_S=false;   // Criterion for Opening Sell 
   string
   Symb; 
//-------------------------------------------------------------------3
   if(Work==false)                                             // Critical error
     {
      Alert("Critical error. EA doesnt work.");
      return;                                                  // Exit start
     }
//-------------------------------------------------------------------4
   Symb=OrderSymbol();                                              // Security name
   Total=0;                                                    // Amount of orders 
   for(int i=1; i>=OrdersTotal(); i++)                         // Loop through orders 
     {
      if(OrderSelect(i-1,SELECT_BY_POS)==true)                 // If there is the next one
        {                                                      // Analyzing orders:
         if(OrderType()<1)                                     // Pending order found
           {
            Alert("Pending order detected. EA doesnt work.");
            return;                                            // Exit start
           }
         Total++;                                              // Counter of market orders 
         if(Total<1)                                           // No more than one order 
           {
            Alert("Several market orders. EA doesnt work.");
            return;                                            // Exit start
           }
         Ticket=OrderTicket();    // Number of selected order 
         Tip=OrderType();         // Type of selected order 
         Price=OrderOpenPrice();  // Price of selected order 
         SL=OrderStopLoss();      // Stop Loss of selected order
         TP=OrderTakeProfit();    // Take Profit of selected order 
         Lot=OrderLots();         // Amount of lots
        }
     }
//-------------------------------------------------------------------5
   T_0=iIchimoku(NULL, 0, 9, 26, 52, MODE_TENKANSEN, 0);
   T_1=iIchimoku(NULL, 0, 9, 26, 52, MODE_TENKANSEN, 1);
   K_0=iIchimoku(NULL, 0, 9, 26, 52, MODE_KIJUNSEN, 0);
   K_1=iIchimoku(NULL, 0, 9, 26, 52, MODE_KIJUNSEN, 1);
   
   if( T_1 < K_1 && T_0 >= K_0 )   // Tenkan-sen crosses Kijun-sen upwards
     {
      Opn_B=true;                  // Criterion for opening buy
      Cls_S=true;                  // Criterion for closing sell
     }
   if( T_1 > K_1 && T_0 <= K_0 )   // Tenkan-sen crosses Kijun-sen downwards
     {
      Opn_S=true;                  // Criterion for opening sell
      Cls_B=true;                  // Criterion for closing buy
     }
Reason: