Simple condition doesn't work on Bullish side!

 

Hi friends, I am just starting to code. I am intending only three things here. 


1. if no buys open, take a market buy.

2. once a buy opens, give comment "Open Buy Price :" etc

3. if the market goes above the Open Buy Price, say Market is bullish and if it goes down say Market is bearish. But it works only on bearish side correctly when bullish side code is turned off with /* */. otherwise it's always saying Market is bullish. can anyone help me rectify this? Thanks in advance! :)


void OnTick()

   {

      int CountOpenBuys = 0;

      for(int i=0;i<=OrdersTotal(); i++ )

         {

            if(OrderSelect(i, SELECT_BY_POS)==true)

               {

                  if (OrderType()==OP_BUY)

                    CountOpenBuys++;

               }

         }

         if(CountOpenBuys==0)

               {

               double OpenBuyPrice;

               OrderSend (_Symbol,OP_BUY,0.01,Ask,3,Ask-0.005,Ask+0.005,NULL,222,0,Green);

               OrderSelect (OrderTicket(),SELECT_BY_POS,MODE_TRADES);

               OpenBuyPrice=OrderOpenPrice();

               Print("Open Buy Price :", OpenBuyPrice);

               }

            if((Bid - OpenBuyPrice)<0)

               {

                    Print ("Market is BEARISH");

                    Sleep(5000);

               }

             if((Bid - OpenBuyPrice)>=0)

               {

                  Print ("Market is bullish");

                  Sleep(5000);

              }

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

  2. OrderSelect (OrderTicket(),SELECT_BY_POS,MODE_TRADES);

    You can not use any Trade Functions until you first select an order.

  3.       OrderSend (_Symbol,OP_BUY,0.01,Ask,3,Ask-0.005,Ask+0.005,NULL,222,0,Green);

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum (2012.05.20)
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (25 March 2014

  4. 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 (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  5. Your CountOpenBuys loop counts all buy orders, not buy orders on the current chart.

 
William Roeder:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019.05.06)
              Messages Editor

  2. You can not use any Trade Functions until you first select an order.

  3. Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum (2012.05.20)
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (25 March 2014

Thank you for your valuable advice bro. Yes, it's working :)


to the point #5. 

Oops! I just realised. I am trying to modify like this. Hope this will work


if (OrderType()==OP_BUY && Symbol()==_Symbol)