1
votes

I wanted to make a generic code, where I could set a pricepoint and have the program buy and sell on my demo account.

 {
   double pricepoint = 1.36900;


   if ( Bid < pricepoint )
   {
      int ticket;
      ticket = OrderSend( "EURUSD", OP_BUY, 1.0, Ask, 0, 0, 0, "My 1st Order!" );
   }
   else if ( ticket != -1 ) & ( Ask > pricepoint )
   {
      OrderClose( ticket, 1.0, Bid, 0 );;
   }
3
your code can have several different contexts in MQL4. Kindly post your code-snippets at least with the function header and global-scope declarations --- ref.: >>> stackoverflow.com/a/36240564/3666197 Next, you might have already noticed on your own, that OrderClose( ... );; line is syntactically wrong. For a real trading strategy, there are some more steps to be added to a fully fledged [ EA ] code, so feel free to ask more.user3666197
Would you consider it fair, Rob, to respond to the missing context clarification issue? Posting another three questions without responding to items to clarify is not in line with a StackOverflow Nettiquette. You may edit the post on your own and add the missing parts of the syntax for the full context of the code.user3666197

3 Answers

1
votes

As others mentioned, first you need to declare ticket outside the if() statement. If you want to close the order later, when it's in profit, you can't do it straight inside the else{...} block - the price cannot be lower than pricepoint and higher than pricepoint at the same time.

Since your EA will run with each tick, the variable ticket will contain your new ticket number only for one cycle. If that's what you want your code would look like this:

   double pricepoint = 1.36900;

   int ticket = -1;

   // buy condition is met    
   if (Bid < pricepoint)
   {
      // you should consider using Symbol() to get the chart symbol
      ticket = OrderSend("EURUSD", OP_BUY, 0.01, Ask, 0, 0, 0, "My 1st Order!");

      if (ticket < 0) 
      { 
         Print("OrderSend failed with error #", GetLastError()); 
      } 
      else 
         Print("OrderSend placed successfully"); 
   }

   // ... later in code

   // if you want to close specifically the ticket that was opened in the same tick cycle
   if (ticket > 0)
   {
      if (OrderSelect(ticket, SELECT_BY_TICKET) == true) 
      { 
         // use OrderTicket(), OrderLots() and other Order functions to get the ticket properties
         OrderClose(OrderTicket(), OrderLots(), Bid, 0, clrNONE);   
      } 
      else 
         Print("OrderSelect returned the error of ",GetLastError());   
   }

I don't recommend working with tickets this way though. It will take some time before your orders are in profit (if ever). You can loop through all open tickets and close those that are in profit like this:

  for (int i = 0; i < OrdersTotal(); i++)
  {
     if (OrderSelect(i, SELECT_BY_POS) == true) 
     { 
        // we only care about opened market orders, not pendings
        if (OrderType() > OP_SELL)
           continue;

        // we only want to close orders that are in profit
        if (OrderProfit() < 0)
           continue;

        if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, clrNONE) == false)
           Print("OrderClose returned the error of ",GetLastError());   
        // we need to adjust the control variable for the order we just closed
        else
           i--;   
     } 
     else 
         Print("OrderSelect returned the error of ",GetLastError());   
  }
0
votes

The variable ticket needs to be declared ( and obtain a value )
outside of the if(){...}else{...} block,
and where does the Ask variable come from?

0
votes

A: a commented inventory of errors for [ What's wrong with this code ]:

While MQL4 code is syntactically trivial, some care has to be taken for details:

   {  // -<BegOfBlock>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

      double pricepoint = 1.36900; // =OK: .DEFs a variable visible/known till the <EndOfBlock> "}"
      if ( Bid < pricepoint )      // ERR: <pricepoint> refers to something not known yet, the compiler will object ( <Bid> is a known system-state variable of the code-execution platform [ MetaTrader Terminal 4] )
      {  // -<BegOfBlock>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

         int ticket;               // =OK: .DEFs a variable visible/known till the <EndOfBlock> "}" with an implicit value assignment of 0
         ticket = OrderSend( "EURUSD",
                             OP_BUY,
                             1.0,
                             Ask,
                             0,
                             0,
                             0,
                             "My 1st Order!"
                             );    // =OK:  .SET a value { -1 onFailure | <aTicketNUMBER> onSuccess }
      }                            
   // ^------<EndOfBlock>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   // INF: @@<EndOfBlock> .UNDEF: <ticket>
   // 
      else
         if ( ticket != -1 ) & ( Ask > pricepoint )
         //   ^--------------|------------------------------ ERR: <ticket> refers to something not known here, the compiler will object
         //                  ^------------------------------ ERR: "&" operator does not conform to syntax-rules, use "&&" and/or "||" for { .AND. | .OR. } boolean-constructions
         {  // -<BegOfBlock>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

            OrderClose( ticket, 1.0, Bid, 0 );;
         //             ^---------------------^--------------ERR: <ticket> refers to something not known here, the compiler will object
         //                                   ^--------------ERR: remove the 2nd ";"
         }
      // ^------<EndOfBlock>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// ^------<EndOfBlock>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ERR: an<EndOfBlock> "}" missing
// INF: @@<EndOfBlock> .UNDEF: <pricepoint>