What's wrong with my code??

 

Hello all

I trying to develop an EA...so I define some base condition but the expert doesn't attend them...

bool Buy_Condition1 = (mrate[1].low<=LBB[1] && mrate[1].close>LBB[1] && mrate[1].close<mrate[1].open && mrate[2].high<TBB[2] && mrate[2].low>LBB[2] && mrate[2].open>LBB[2] && mrate[0].close<=LBB[0] && mrate[0].spread<=15);

 if(Buy_Condition1 && !PositionSelect(_Symbol))         // buy condition1
     {
      LongPositionOpen();
     }

   return;
  }



//+------------------------------------------------------------------+
//|  Open Long Position                                                                |
//+------------------------------------------------------------------+
void LongPositionOpen()
  {
   MqlTradeRequest mrequest;                             // Will be used for trade requests
   MqlTradeResult mresult;                               // Will be used for results of trade requests

   ZeroMemory(mrequest);
   ZeroMemory(mresult);

   double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);    // Ask price
   double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);    // Bid price

   if(!PositionSelect(_Symbol))
     {
      mrequest.action = TRADE_ACTION_DEAL;               // Immediate order execution
      mrequest.position = position_ticket;               // ticket of the position
      mrequest.price = NormalizeDouble(Ask,_Digits);     // Lastest Ask price
      mrequest.sl = (Ask-(_Point*StopLoss));                  // Stop Loss
      mrequest.tp = mahi5Val[0];                          // Take Profit
      mrequest.symbol = _Symbol;                         // Symbol
      mrequest.volume = LOT;                              // Number of lots to trade
      mrequest.magic = EA_Magic;                         // Magic Number
      mrequest.type = ORDER_TYPE_BUY;                    // Buy Order
      mrequest.type_filling = ORDER_FILLING_FOK;         // Order execution type
      mrequest.deviation = 5;                              // Deviation from current price
      OrderSend(mrequest,mresult);                       // Send order
      if(mrequest.volume)
        {
         mrequest.volume=NormalizeDouble(mrequest.volume,2);
         Print("Lots to buy: ",mrequest.volume);
        }
      else
         Print("Problem to buy: ",mrequest.volume);
     }
  }

this is in test... that's similar to my condition

thanks.

 
pouyakhalili:

Hello all

I trying to develop an EA...so I define some base condition but the expert doesn't attend them...


thanks.


Remove this line. it's used to close a position, not to open a position.

mrequest.position = position_ticket;               // ticket of the position


What is this for? It does not make any sense.

if(mrequest.volume)
        {
         mrequest.volume=NormalizeDouble(mrequest.volume,2);
         Print("Lots to buy: ",mrequest.volume);
        }
      else
         Print("Problem to buy: ",mrequest.volume);
 
tickfenix:


Remove this line. it's used to close a position, not to open a position.


What is this for? It does not make any sense.

Actually that line I didn't use yet but i need that .....
And i wanted to define dynamic lot but i have so much error so i search and receive that...
I have 6 condition and Expert just won't work at condition 1 and same condition 1 at sell
 
pouyakhalili:
Actually that line I didn't use yet but i need that .....


This is not an optional line of code.

This is an explicit order to close a position with a specific ticket number.

If you put this line here instead of commenting it out, it will try to close a position instead of opening a new position.

 
tickfenix:

This is not an optional line of code.

This is an explicit order to close a position with a specific ticket number.

If you put this line here instead of commenting it out, it will try to close a position instead of opening a new position.

Oh i think this just let me know ticket number so i can code about close specific position...ok i delete it thank you :) ....but you don't have any idea about why my condition won't execution??
 
pouyakhalili:




if(!PositionSelect(_Symbol))

This is the wrong way to use PositionSelect().

You may want to use

if(PositionsTotal()==0)

instead.

Documentation on MQL5: Trade Functions / PositionSelect
Documentation on MQL5: Trade Functions / PositionSelect
  • www.mql5.com
Chooses an open position for further working with it. Returns true if the function is successfully completed. Returns false in case of failure. To obtain information about the error, call GetLastError(). If individual positions are allowed (ACCOUNT_MARGIN_MODE_RETAIL_HEDGING), multiple positions can be open for one symbol. In this case...
 
tickfenix:



This is the wrong way to use PositionSelect().

You may want to use

if(PositionsTotal()==0)

instead.

So i changed my code as you said but same reason...no attend from expert ... appreciate your help bro 
 
I need when current price touch bb my expert open position so i use mrate[0].close.... But it seems it wait till price close that candle and then it open position.....how can i change my code so it open positions when price hit bb exactly just that time...
And maybe this prob( delay) forced expert won't do condition 1 ?
 

Try relaxing your entry criteria first.

Make sure the position opening function works properly.

Test with more historical data.

What your eye sees may not be accurate.

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
tickfenix:

Try relaxing your entry criteria first.

Make sure the position opening function works properly.

Test with more historical data.

What your eye sees may not be accurate.

Thanks bro... I discovere the problem...at the moment new bar open because of no define of price that time or something else...bb and price hits so it open position at the opening of new bar...now it's nothing in my mind to figure it out :/


I search and find 2 solution 1 using Sleep()  2 use current candle time +2_3 sec ... Anybody know how to use this 2 in my code? 
Reason: