a first step by beginner needs your help..

 

Hi,

 I'm a new user and now struggling with mql4.

Thanks for some of reference stuffs, I just wrote first code as follows, but could not work well.
It seems

The message of below code is "if price is above ichimoku and open<close then buy at market and exit entry bar + 2 bars,"
vanilla coding. An error said "OrderClose error 418" and "unknown ticket 123 for OrderClose function

 Any help would be highly appreciated.  

 

 

 int start()

  {

int ticket,i;

datetime EntryTime;


   if(Bars<100)

      return(0);

 

   if(OrdersTotal()<1 )

   {

      if(MathMin(Close[0],Open[0])>iIchimoku(NULL,0,Tenkan,Kijun,Senkou,MODE_SENKOUSPANA,0))

      {

         if(Open[0]<Close[0])

         {

            ticket = OrderSend(Symbol(),OP_BUY,Lot,Ask,0,0,0,"Long",123,0,Red);

            if(OrderSelect(123,SELECT_BY_TICKET) == true)  

               {

               EntryTime = OrderOpenTime();

               Print(TimeToStr(EntryTime));

               }

         }      

      }   

   }

   

  if(OrdersTotal()>=1)

      if(Time[0]>=EntryTime+(60*2*PERIOD_M5))

      {

         OrderClose(123,Lot,Bid,0,Red);

         ticket=0;

      }  

      return(0);

  }

 

Thanks,

Jeff 

 

  1. For large amounts of code, attach it
  2.  int start(){
    
    int ticket,i;
    :
               ticket = OrderSend(Symbol(),OP_BUY,Lot,Ask,0,0,0,"Long",123,0,Red);
                if(OrderSelect(123,SELECT_BY_TICKET) == true)  
    :
    
             OrderClose(123,Lot,Bid,0,Red);
    
             ticket=0;
    
    You are trying to select order number 123. That's not likely the ticket number of the open order. Loops and Closing or Deleting Orders - MQL4 forum
  3. Also since ticket is a local variable not global, its value won't be saved between calls to start().
  4. You are trying to close order number 123. That's not likely the ticket number of any open order. What are Function return values ? How do I use them ? - MQL4 forum
 

Hi,

Thanks for shinning the light on this. I just re-wrote mql code like below still does not work well, just bought and sold on the same time.
What should I focus on? 

  

int start()
  {
//----

   int KeepPosition=0; //0=yes 1=no
   int total = OrdersTotal();
   int i;
   
   for(i=0;i<OrdersTotal();i++)
   {
      if(OrderSelect(i,SELECT_BY_POS)==true)
         if(Symbol()==OrderSymbol()&&OrderMagicNumber()==MagicNo)
         {
            KeepPosition =1;
            ticket = OrderTicket();
            break;
         }
   }
      

   if(KeepPosition==0)
   {
      if(MathMin(Close[0],Open[0])>iIchimoku(Symbol(),0,Tenkan,Kijun,Senkou,MODE_SENKOUSPANA,0))
      {
         if(Open[0]<Close[0])
         {
            ticket = OrderSend(Symbol(),OP_BUY,Lot,Ask,0,0,0,"Long",MagicNo,0,Red);
            EntryTime = OrderOpenTime();
         }      
      }   
   }
   
   
   if(KeepPosition==1)
      if(OrderType()==OP_BUY)
         if(Time[0]>=EntryTime+(60*PERIOD_M5*2))
            OrderClose(OrderTicket(),OrderLots(),Bid,0,Red);

   
//----
   return(0);
  }

 

 Rgds,
 Jeff

 
hardybarracks:

Hi,

Thanks for shinning the light on this. I just re-wrote mql code like below still does not work well, just bought and sold on the same time.
What should I focus on? 

Lots of things,  check your return values from your OrderXxxx() functions and report any errors:  What are Function return values ? How do I use them ? - MQL4 forum

EntryTime . . .   perhaps you should make sure it is correct before you close an order based on it's value,  how ?  well you could retrieve it from the order that is open just like you do in the earlier code where you have a loop.

Make KeepPosition  a bool,  it makes much more sense to say  .  .  .

KeepPosition = true;

if( KeepPosition )

if( !KeepPosition )
 

Jeff,

It seems you have interest in using Ichimoku Kinko Hyo indicator for signals of entry/exit.

You might consider writing a list of actions you wish your expert advisor to perform then begin writing the code in MetaEditor which will enable performance of these actions through your expert advisor.

Hope this was of value.

Thank you.

 

Thanks guys! Today is Day-2, trying to mql4.

Your advise was highly appreciated and I got order entry system on MT4 and re-wrote my first code.
I think it should be work well though if we take a look at behavior of EA, unrealistic prices were met (pls see the attached).

Fuethermore, testing result was curious because of getting not exact trading minutes (timestamp) even though setting to be M5 
as duration and I can see exact 5min (ex 11:00, 11:05..) timeseries at HistoryCenter. What's the problem??

Jeff 

 

int start()
  {
   if( ( Ticket_L == 0 || Ticket_L == -1 ) && ( Ticket_S == 0 || Ticket_S == -1 ))
   {
      if(MathMin(Close[0],Open[0])>iIchimoku(Symbol(),0,Tenkan,Kijun,Senkou,MODE_SENKOUSPANB,0))//is above downside cloud
         if(Open[0]<Close[0])
            Ticket_L = OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,0,0,"Long",MagicNo,0,Red);
   }


   //Select current long position
   OrderSelect(Ticket_L, SELECT_BY_TICKET);

   //Time exit after 10minutes from enter
   if(Ticket_L != 0 && Ticket_L != -1)
      if(OrderOpenTime( )+60*ExitTime <= TimeCurrent())
      {     
         Exit_L = OrderClose(Ticket_L,Lot,Bid,Slippage,Red);
         if(Exit_L==1)
            Ticket_L = 0;
      }      
   return(0);
  }
timestamps are strange
trigerred at out-of-range prices
 
I strongly recommend you follow the Book to get the Basics and don't skip Chapters.
 
hardybarracks:

Thanks guys! Today is Day-2, trying to mql4.

Your advise was highly appreciated and I got order entry system on MT4 and re-wrote my first code.
I think it should be work well though if we take a look at behavior of EA, unrealistic prices were met (pls see the attached).

Fuethermore, testing result was curious because of getting not exact trading minutes (timestamp) even though setting to be M5 
as duration and I can see exact 5min (ex 11:00, 11:05..) timeseries at HistoryCenter. What's the problem??

07:37  plus 10 mins = 07:47 . . .  where is the problem ?

 

You need to re-write your code . . .  

Exit_L = OrderClose(Ticket_L,Lot,Bid,Slippage,Red);
         if(Exit_L==1)
            Ticket_L = 0;

 OrderClose() returns a bool not an int . . .  use a bool and make your code readable.

If you stop your EA with an open Order and then start it again will if find the open order ?


Also:   What are Function return values ? How do I use them ? - MQL4 forum

 

If your OrderSend() fails . . .

Ticket_L = OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,0,0,"Long",MagicNo,0,Red);
   }


   //Select current long position
   OrderSelect(Ticket_L, SELECT_BY_TICKET);

 . . .  how do you expect to select and order with a ticket number of  -1  ?

Reason: