Help on Code

 

Hi does somebody could help me?


I want the EA to set a Pending Sell Stop Order when I open manually a Buy Market Order, why this code isnt working? 



// External

extern double LotSize = 0.1;

extern double StopLoss = 150;

extern double TakeProfit = 120;

extern double PendingPips = 30;

extern int MagicNumber = 123;


// Globals

int BuyTicket;

int SellTicket;

double UsePoint;

double PendingPrice;

double SellStopLoss;

double BuyStopLoss;

double SellStopTP;

double BuyStopTP;

int OrdersTotal;

int OrderType;

double OrderOpenPrice;


int start()

  {

  if (OrdersTotal = 1 && OrderSelect(1,SELECT_BY_POS,MODE_TRADES) == OP_BUY)

   {

         // Calculando Ordem

         PendingPrice = OrderOpenPrice - (PendingPips * 0.01); //- 0.01 because its on a 2 decimal digits currency, would be 0.0001 for 4 and 5 digits

         SellStopLoss = PendingPrice + (StopLoss * 0.01);

         SellStopTP = PendingPrice - (TakeProfit * 0.01);

      

          SellTicket = OrderSend (Symbol(),OP_SELLSTOP,LotSize,PendingPrice,15,SellStopLoss,SellStopTP,"Sell Stop Order",MagicNumber,0,clrRed);

    }

   return(0);

   }  

 

Moved to mql4 section.

 

Hello.

Please specify correct country in Seller profile in order to continue with registration.

 
Pedro Moreira:

Hi does somebody could help me?

I want the EA to set a Pending Sell Stop Order when I open manually a Buy Market Order, why this code isnt working? 


At least:

if (OrdersTotal == 1 && OrderSelect(0,SELECT_BY_POS,MODE_TRADES) == OP_BUY)

But your code is really strange

 
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. int OrdersTotal;
    int OrderType;
    double OrderOpenPrice;
    
    OrdersTotal, OrderType, and OrderOpenPrice are predefine functions. Your variables aren't going to do what you think. Does that even compile?

  3. if (OrdersTotal = 1 && OrderSelect(1,SELECT_BY_POS,MODE_TRADES) == OP_BUY)
    Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  4.           SellTicket = OrderSend (Symbol(),OP_SELLSTOP,LotSize,PendingPrice,15,SellStopLoss,SellStopTP,"Sell Stop Order",MagicNumber,0,clrRed);
    Check your return codes for errors, report them and you would know why.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

Reason: