First Steps - EA does not run as it should - page 2

 
TheOneWhoKnocks:
I can't use ticket1 as a global variable, because mt would open a new order every tick. I've tried this already.

That is because you are not applying logical steps

 
   //Globalscope
   int ticket =0;
   
   //in main code
   if(ticket==0)
      //No value assigned to ticket
      {
      //Check for conditions and if satisfied
      ticket = OrderSend();
      }
   else
      if(OrderSelect(ticket,SELECT_BY_TICKET))
         {
         if(OrderCloseTime!=0)
            {
            // Trade has been closed
            ticket=0;
            //check for conditions for the next trade
            }
         else
            {
            //Order is open, do whatever, check for trailing stop etc
            }
         
         }
      

 This is only intended as a basic outline

 

...


int start()

{

double fastMA = iMA(Symbol(), PERIOD_CURRENT, PeriodFastMA, 0, MODE_SMA, PRICE_CLOSE, 0);

double slowMA = iMA(Symbol(), PERIOD_CURRENT, PeriodSlowMA, 0, MODE_SMA, PRICE_CLOSE, 0);

   int ticket1 = OrderSend(Symbol(), OP_BUY, lots, Ask, 0, 0, 0);


//Buy Condition:

if(fastMA > slowMA && fastMA > 0 && slowMA > 0 && OrdersTotal() == 0)

   {

      if(ticket1 > 0)

         {

         Alert("Order Sended Successfully. Order No.: ", ticket1);

         }

      else

         {

         Alert("Order Sending Error!");

         }    

  ....

 
int start()

{

double fastMA = iMA(Symbol(), PERIOD_CURRENT, PeriodFastMA, 0, MODE_SMA, PRICE_CLOSE, 0);

double slowMA = iMA(Symbol(), PERIOD_CURRENT, PeriodSlowMA, 0, MODE_SMA, PRICE_CLOSE, 0);

   int ticket1 = OrderSend(Symbol(), OP_BUY, lots, Ask, 0, 0, 0);



//Buy Condition:

if(fastMA > slowMA && fastMA > 0 && slowMA > 0 && OrdersTotal() == 0)

   {

      if(ticket1 > 0)

         {

         Alert("Order Sended Successfully. Order No.: ", ticket1);

         }

      else

         {

         Alert("Order Sending Error!");

         }    
Your order does not depend on any conditions, simply a new tick.
 

exactly! but if I take my order into the //Buy Condition which starts if fastMA is greater then slowMa. I can't take my //Sell Condition into my //Buy Condition. I would like for mt to remembering my ticket1 in my sell condition. But the ticket1 variable is just a local variable. Is there something like:

//Buy Condition:
if(fastMA > slowMA && fastMA > 0 && slowMA > 0 && OrdersTotal() == 0)
   {
   RememberThis(int ticket1 = OrderSend(Symbol(), OP_BUY, lots, Ask, 0, 0, 0) );
      if(ticket1 > 0)
         {
         Alert("Order Sended Successfully. Order No.: ", ticket1);
         }
      else
         {
         Alert("Order Sending Error!");
         }    
    }     
//Sell Condition:
   if(fastMA < slowMA)
      {
      OrderClose(ticket1, lots, Bid, 0);
      }
 
To be honest, what exactly you want to achieve?
 

I want to try the simplest of all strategies.


if Fast Moving Average is greater than the slow one.
->Buy order
if the fast moving average is lower than  the slow one. 
->close that position

 

What you want to achieve is quite easy actually.

The ticket1 is for what? If you need to close all, just use loop.

 
TheOneWhoKnocks:

exactly! but if I take my order into the //Buy Condition which starts if fastMA is greater then slowMa. I can't take my //Sell Condition into my //Buy Condition. I would like for mt to remembering my ticket1 in my sell condition. But the ticket1 variable is just a local variable. Is there something like:

Did you see my post  with a logical outline?
 
@GumRai yes I did. But if I take my order into the Buy Condition. The Ticketnumber of my order, opened in the buy condition, which I need in the CloseOrder function, will be forgotten in my sell condition. So my question was how can be the ticketnumber be remembered in the sell condition?
 

If you need to close all, just use loop to close when the condition to close is met such as signal is changing from buy to sell.

No need to use ticket number. We use ticket number if we want to find a specific trade.

Just use loop to close trade if you just want to close any open trades.