Download MetaTrader 5

To send opposite Order based on previous closed order. Thank for the help.

To add comments, please log in or register
begineer
8
begineer  
Hi All

Any ideas why after hit STOPLOSS, new Sell order cannot be send?

Thanks in Advance

(My First Try for EA)


   if (TotalOrder == 0) //If there is no working order
     {
      int last_trade = OrdersHistoryTotal(); //Determine last order ticket number


      if(OrderSelect(last_trade,SELECT_BY_POS,MODE_HISTORY) == true) //Select last order
           {
            if(OrderClosePrice() == OrderStopLoss()) //If close price = Stop Loss
              {
               if (OrderType()==OP_BUY) //If order type is Sell Order
                 {


                  OpenPrice = Bid;                       //define Open Price
                  SellStopLoss = OpenPrice + PipSize;    //define Stop Loss
                  SellTakeProfit = OpenPrice - PipSize;  //define Take Profit
            
                  OrderSend(Symbol(), OP_SELL, LotSize, OpenPrice, Slippage, SellStopLoss, SellTakeProfit, "Sell Order", 0, 0, clrNONE); //Opening SELL

} }

}

}


Jack Thomas
2076
Jack Thomas  
begineer:
Hi All
Any ideas why after hit STOPLOSS, new Sell order cannot be send?
Thanks in Advance

(My First Try for EA)


   if (TotalOrder == 0) //If there is no working order

You didn't define TotalOrder anyplace. This will not work.

If you are trying to determine the current total of tickets in your order basket you can use OrdersTotal() instead.


int last_trade = OrdersHistoryTotal(); //Determine last order ticket number

This will NOT give you the ticket number of the last ticket that was closed.


 if (OrderType()==OP_BUY) //If order type is Sell Order

This does NOT check for a SELL order.


                  OpenPrice = Bid;                       //define Open Price

This does NOT define the Open price. This determines the current Bid (sell) price for the chart symbol.


SellStopLoss = OpenPrice + PipSize;    //define Stop Loss

You didn't define SellStopLoss anywhere. Is this a double?

You didn't define OpenPrice anywhere. Is this a double?

You didn't define PipSize anywhere. is this a double?


OrderSend(Symbol(), OP_SELL, LotSize, OpenPrice, Slippage, SellStopLoss, SellTakeProfit, "Sell Order", 0, 0, clrNONE); //Opening SELL

You didn't define lotsize, slippage, other stuff here.

You didn't refresh your rates to make sure they are current.

You should also be checking for the returned value of OrderSend and not just blindly sending it like that.

Validate your variables before sending the ticket.


When asking for help, please show ALL the relevant code. You're code as provided will never work.

- Jack

nicholishen
946
nicholishen  
begineer:
Hi All

Any ideas why after hit STOPLOSS, new Sell order cannot be send?

Thanks in Advance

(My First Try for EA)




You could use something like this... You'd obviously need to implement you own way of adding new stops...

bool OppositeLastClosed()
{
   int ticket = -1;
   datetime last_close =0;
   int type = -1;
   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)
         &&OrderCloseTime()>last_close
         &&OrderSymbol()==_Symbol
         &&OrderType()<2)
      {
         last_close  = OrderCloseTime();
         ticket      = OrderTicket();
         type        = OrderType();
      }
   }
   if(OrderSelect(ticket,SELECT_BY_TICKET))
   {
      type = type==OP_BUY?OP_SELL:OP_BUY;
      double price = type==OP_BUY?Ask:Bid;
      return OrderSend(_Symbol,type,OrderLots(),price,0,0,0) >= 0;
   }
   return false;
}

To add comments, please log in or register