Help: can't store the price of the last candle in an if condition

 

Hi everyone, I am new to MQL4 programming and try to learn and build some simple EA with nearly 0 programming background. Any help or suggestion will be appreciated very much.

Please kindly help a look at the below for the code and screenshots:


...

extern int Stoch_Period = 8;

...



//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   double MyPoint=Point;
   if(Digits==3 || Digits==5)
      MyPoint=Point*1;

  
   double StochMain2 = iStochastic (NULL, Timeframes, Stoch_Period, 3, 3, MODE_SMA, 0, MODE_MAIN, 2);
   double StochMain1 = iStochastic (NULL, Timeframes, Stoch_Period, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
   double StochMain0 = iStochastic (NULL, Timeframes, Stoch_Period, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);


//---------------- BUY SIGNAL: Stochastic crosses  20  ---------------// 

   if ( (StochMain2 > 20) && (StochMain1 < 20)  )

      {
      double BuyPrice = iClose(NULL, PERIOD_M15, 1);
      Print ("In If condition, Buy Price is: ", BuyPrice);  
      }

      Print ("After if condition, Buy Price is: ", BuyPrice); 

 
//---------------- Trading Start ---------------// 

     

  if( TotalOrdersCount()==0 ) 
  {
     int result=0;
     if( if buy signal is met )  
     
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Testing EA",MagicNumber,0,Blue);
        if(result>0)
        {

         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         OrderSelect(result,SELECT_BY_TICKET);
         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green); 

         Print ("When trading, Value of Buy Price: ", BuyPrice);

    
        }
        return(0);
     }

...

Recently, I have some problem when trying to store the close price value of the last candle if the condition is true, I am not sure why it was store at first, then went back to 0 in next candle (Screenshot attached, I use Print Function to find out)

Please kindly help me if there is something wrong in my code or the logic in my if condition.

Once again, thank you very much for your time!

Documentation on MQL5: Common Functions / Print
Documentation on MQL5: Common Functions / Print
  • www.mql5.com
Print - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
Screenshot_2.png  176 kb
 
  1.          if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
             if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
    

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).


  2. JayCee13: I am not sure why it was store at first, then went back to 0 in next candle
       double BuyPrice = 0;
       if ( (StochMain2 > 20) && (StochMain1 < 20)  )
          {
          BuyPrice = iClose(NULL, PERIOD_M15, 1);
          Print ("In If condition, Buy Price is: ", BuyPrice);  
          }
       Print ("After if condition, Buy Price is: ", BuyPrice); 
    
    

    Since you are not using strict, the default for automatic variables is zero. The first time you set the variable. The next time you don't, so you got zero.

 
William Roeder #:
  1. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).


  2. Since you are not using strict, the default for automatic variables is zero. The first time you set the variable. The next time you don't, so you got zero.

Hi William Roede, it was very kind of you when your help was more than I asked.

About issue number 2, I am not sure if I could correct it. Could you kindly show me how to correct the code/ logic please? or kindly point me to the correct way/ material for the issue.

Sorry for my silliness.

 
JayCee13 #Could you kindly show me how to correct the code/ logic please? or kindly point me to the correct way/ material for the issue.

Make the variable static so you remember the value.

 
William Roeder #:

Make the variable static so you remember the value.

Hi William Roede, it works already.

Million thanks for your help!!!

Reason: