ICHIMOKU ORDER CLOSE

 

I'm having trouble closing my opened order.

Any ideas?

//+------------------------------------------------------------------+
//|                                              ICHIMOKU_SIMPLE.mq4 |
//|                      Copyright © 2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern double Lots = 1.0;
#define MAGICMA  20050610 
//----

int start()
{
   double tenkan_sen=iIchimoku(NULL, 0, 9, 26, 52, MODE_TENKANSEN, 1);
   double kijun_sen=iIchimoku(NULL, 0, 9, 26, 52, MODE_KIJUNSEN, 1);
   int ticket,total, magic;
   
   total=OrdersTotal();
   if(total<1 && tenkan_sen>kijun_sen)
   {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"ichimoku",MAGICMA,0,Green);
   }
//----   
   {
      if(OrderSelect(MAGICMA, SELECT_BY_TICKET)==true && tenkan_sen<kijun_sen)                       
      {
         OrderClose(MAGICMA,1,Bid,0,Red);
      }
   }           
   return(0);
} 
 
if(OrderSelect(MAGICMA, SELECT_BY_TICKET)==true && tenkan_sen<kijun_sen)
wrong code to select an order
 

This was your coding before you did that stupid thing to "simplifie"

Compare it with your new code.....

//+------------------------------------------------------------------+
//| ICHIMOKU.mq4                                                     |
//| Copyright © 2012, MetaQuotes Software Corp.                      |
//| http://www.metaquotes.net                                        |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, MetaQuotes Software Corp."
#property link "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)
         {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"ichimoku",16384,0,Green);
         if(ticket>0)
            {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
            }
         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++)
      {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      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);
   }
 

Some parts of the "unsimplified" code are unnecessary.

qjol, what is wrong with that line?

 
Is anyone here kind enoguh to tell me exactly what needs changing or are they just going to tell me that it's wrong and leave it at that?
 

Read the documentation . . . OrderSelect

Your magic number ( MAGICMA ) is not a Order ticket number . . .

OrderSelect( int index, int select, int pool=MODE_TRADES)

Parameters:

index - Order index or order ticket depending on the second parameter.
select - Selecting flags. It can be any of the following values:
SELECT_BY_POS - index in the order pool,
SELECT_BY_TICKET - index is order ticket.
pool - Optional order pool index. Used when the selected parameter is SELECT_BY_POS. It can be any of the following values:
MODE_TRADES (default)- order selected from trading pool(opened and pending orders),
MODE_HISTORY - order selected from history pool (closed and canceled order).
 
ToBa:
Is anyone here kind enoguh to tell me exactly what needs changing or are they just going to tell me that it's wrong and leave it at that?
If you want someone else to fix all your code go here : MT4 and MT5 coding . . . . if you want someone to teach you how to code go to school . . . if you want some help you are in the right place . . . but you have to do it with a little help, you need to study, learn and read . . .
 

change

OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position

to

if(OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet) == false) // close position
   {
   Alert("order cant be closed, error #" + GetLastError());
   }

2 find out what's the problem
 
I know that the current OrderClose does not recognise the OrderOpen above it.
 
ToBa:
I know that the current OrderClose does not recognise the OrderOpen above it.


Then how can an Expert Advisor recognise a trade of its own ?

What makes it specific to recognise...

 
The only thing I can think of is the magic number - "May be used as user defined identifier."
Reason: