# ICHIMOKU STRATEGY

91

I've been trying to create an expert advisor which will automatically buy 1 unit when the tenkan-sen>kijun-sen (when the tenkan-sen line is above the kijun-sen line) and then sell it once the tenkan-sen is lower than the kijun-sen.

I'm a complete newbie and started it yesterday by trying to piece samples together etc.

Below is the result and I appreciate that's it's probably a load of crap.

I get the classic "'(' - function definition unexpected" on the int start() lines (copied from the "trade" script in the navigator) when trying to compile

Please can you suggest how I can get this idea to work and thanks in advance.

extern double Lots = 0.1;
extern double Tenkan = 9;
extern double Kijun = 26;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
Bars<=Tenkan, Bars<=Kijun;
//----
if(Bars<=Tenkan>Bars<=Kijun)
{
Print ("Tenkan-sen greater than Kijun-sen");
return(0);
}
//----
//+------------------------------------------------------------------+
//| script "trading for all money" |
//+------------------------------------------------------------------+
int start()
{
//----
if(MessageBox("Do you really want to BUY 1.00 "+Symbol()+" at ASK price? ",
"Script",MB_YESNO|MB_ICONQUESTION)!=IDYES) return(1);
//----
if(ticket<1)
{
int error=GetLastError();
Print("Error = ",ErrorDescription(error));
return;
}
//----
OrderPrint();
return(0);
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
Bars<=Tenkan, Bars<=Kijun;
//----
if(Bars<=Tenkan<Bars<=Kijun)
{
Print ("Tenkan-sen less than Kijun-sen");
return(0);
}
//----
int start()
{
//----
if(MessageBox("Do you really want to SELL 1.00 "+Symbol()+" at Bid price? ",
"Script",MB_YESNO|MB_ICONQUESTION)!=IDYES) return(1);
//----
int ticket=OrderSend(Symbol(),OP_SELL,1.0,Bid,3,0,0,"expert comment",255,0,CLR_NONE);
if(ticket<1)
{
int error=GetLastError();
Print("Error = ",ErrorDescription(error));
return;
}
//----
OrderPrint();
return(0);
}
//+------------------------------------------------------------------+

887

Look at the MACD Sample Code.

Change the variables fetched for internal Use By MACD indicator to get Ichmoku Indicator values.

Make small chages and then complie code to check your syntax is correct.

edit - Also of use is how to use Ichmoku Indicator here

3

Hi ToBa,

Three things;

There should only be 1 x init, 1 x deinit + 1 x start.

The start function is the main body of your script which processes the market movement (or ticks).

As well as that, ensure every curly brace is closed properly and declare every variable.

91

Ickyrus:

Look at the MACD Sample Code.

Change the variables fetched for internal Use By MACD indicator to get Ichmoku Indicator values.

Make small chages and then complie code to check your syntax is correct.

edit - Also of use is how to use Ichmoku Indicator here

Ok i've redone it based upon the MACD sample, but with these errors:

'\end_of_program' - ending bracket '}' expected C:\Program Files (x86)\MetaTrader 4\experts\ICHIMOKU.mq4 (80, 13)

'\end_of_program' - unbalanced left parenthesis C:\Program Files (x86)\MetaTrader 4\experts\ICHIMOKU.mq4 (80, 13)

This is the new version:

//+------------------------------------------------------------------+
//| ICHIMOKU.mq4 |
//| Copyright © 2012, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+

extern double TakeProfit = 50;
extern double Lots = 0.1;
extern double TrailingStop = 30;
extern double Tenkan = 9;
extern double Kijun = 26;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int start()
{
double tenkan_sen;
double kijun_sen;
int cnt, ticket, total;
//----
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
//----
total=OrdersTotal();
if(total<1)
{
// no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
// check for long position (BUY) possibility
if(tenkan_sen>kijun_sen)
{
if(ticket>0)
{
}
else Print("Error opening BUY order : ",GetLastError());
return(0);

// it is important to enter the market correctly,
// but it is more important to exit it correctly...
for(cnt=0;cnt<total;cnt++)
{
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if((tenkan_sen<kijun_sen)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
}
}
return(0);
}

17265

At this point in "your" code . . .

}
else Print("Error opening BUY order : ",GetLastError());
return(0);

//   <------  here  ! ! !

// it is important to enter the market correctly,
// but it is more important to exit it correctly...
for(cnt=0;cnt<total;cnt++)
{

. . . marked here ! ! ! , you have switched from placing Orders to closing them . . . but you are missing maybe 2 closing ( } ) braces from the buying part of the code . . . if you indent your code in a reasonable way it's easy to see.

91

Ok thanks. I've made this change which removed '\end_of_program' - ending bracket '}' expected C:\Program Files (x86)\MetaTrader 4\experts\ICHIMOKU.mq4 (80, 13)

It is marked <=======

// check for long position (BUY) possibility
if(tenkan_sen>kijun_sen)
{
if(ticket>0)
} <=======
{
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
// it is important to enter the market correctly,

// but it is more important to exit it correctly...

It doesn't seem as though it should be there though

17265

You have a surplus opening bracket on this line . . .

if  ( ( tenkan_sen<kijun_sen )   // <--  surplus  (
17265

Please use this to post code . . . it makes it easier to read.

17265

You initialize these variables but don't give them any values . . .

if (  tenkan_sen  > kijun_sen  )

. . . so the test will always be false. This EA will never place an Order.

17265

Your code in a version with indenting and that compiles . . .

//+------------------------------------------------------------------+
//| ICHIMOKU.mq4                                                     |
//| Copyright © 2012, MetaQuotes Software Corp.                      |
//| http://www.metaquotes.net                                        |
//+------------------------------------------------------------------+

extern double TakeProfit = 50;
extern double Lots = 0.1;
extern double TrailingStop = 30;
extern double Tenkan = 9;
extern double Kijun = 26;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int start()
{
double tenkan_sen;
double kijun_sen;
int cnt, ticket, total;
//----
if(Bars<100)
{
Print("bars less than 100");
return(0);
}

if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}

//----
total=OrdersTotal();
if(total<1)
{
// no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
// check for long position (BUY) possibility
if(tenkan_sen>kijun_sen)
{
if(ticket>0)
{
}
else Print("Error opening BUY order : ",GetLastError());
return(0);

}   //  added by RaptorUK
}   //  added by RaptorUK

// it is important to enter the market correctly,
// but it is more important to exit it correctly...
for(cnt=0;cnt<total;cnt++)
{
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if(tenkan_sen<kijun_sen)   //  removed surplus (  RaptorUK
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}

// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
}
}
return(0);
}
91

Thank you very much :)