Need help with code. It is not placing any trades

Koh Jeff  

Not sure whats wrong with my code, its not placing any trades...


It is suppose to place a buy order when Low(1) - High (4) = 100 or more pips.  

Suppose to be able to run on any time frame.

Please help!


//Inputs

input double lotSize = 0.01;

input double takeProfit = 100;

input double stopLoss = 100;



int ticket = 0;



void OnTick()

{

   double high1 = High[1];

   double low1 = Low[1];

   double high4 = High[4];

   double low4 = Low[4];

   double pipSize = MarketInfo(Symbol(), MODE_POINT);

   double pipDistance = (high1 - low4) / pipSize;

   double pipDistanceSell = (high1 - low4) / pipSize;

   double pipDistanceBuy = (low1 - high4) / pipSize;

   

   

   // Display the pip distance on the chart screen

   string text = "Pip distance: " + DoubleToStr(pipDistance, _Digits);

   ObjectCreate("pipdistance", OBJ_LABEL, 0, 0, 0);

   ObjectSet("pipdistance", OBJPROP_CORNER, 0);

   ObjectSet("pipdistance", OBJPROP_XDISTANCE, 10);

   ObjectSet("pipdistance", OBJPROP_YDISTANCE, 30);

   ObjectSetText("pipdistance", text, 10, "Arial", White);



   

   



   if (pipDistanceSell >= 100 && (high1 - Ask) <= 25 * Point)

   {

      Print("SELL Signal: Pip Distance = ", pipDistanceSell);

      ticket = OrderSend(Symbol(), OP_SELL, lotSize, Bid, 0, Bid + stopLoss * Point, Bid - takeProfit * Point, "Sell Trade", 0, 0, Red);

   }



   if (pipDistanceBuy >= 100 && (low1 - Bid) <= 25 * Point)

   {

      Print("BUY Signal: Pip Distance = ", pipDistanceBuy);

      ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, 0, Ask - stopLoss * Point, Ask + takeProfit * Point, "Buy Trade", 0, 0, Blue);

   }



   if (ticket > 0)

   {

      if (OrderSelect(ticket, SELECT_BY_TICKET))

      {

         if (OrderType() == OP_BUY && (Ask - low1) >= takeProfit * Point)

         {

            if (OrderClose(ticket, lotSize, Ask, 0, Blue))

            {

               ticket = 0;

            }

            else

            {

               Print("Error closing order: ", GetLastError());

            }

         }

         else if (OrderType() == OP_SELL && (high1 - Bid) >= takeProfit * Point)

         {

            if (OrderClose(ticket, lotSize, Bid, 0, Red))

            {

               ticket = 0;

            }

            else

            {

               Print("Error closing order: ", GetLastError());

            }

         }

         else if (OrderType() == OP_BUY && (low1 - Ask) >= stopLoss * Point)

         {

            if (OrderClose(ticket, lotSize, Ask, 0, Red))

            {

               ticket = 0;

            }

            else

            {

               Print("Error closing order: ", GetLastError());

            }

         }

         else if (OrderType() == OP_SELL && (Bid - high1) >= stopLoss * Point)

         {

            if (OrderClose(ticket, lotSize, Bid, 0, Blue))

            {

               ticket = 0;

            }

            else

            {

               Print("Error closing order: ", GetLastError());

            }

         }

      }

   }

}



void OnDeinit(const int reason)

{

   if (ticket > 0)

   {

      if (OrderSelect(ticket, SELECT_BY_TICKET))

      {

         if (!OrderClose(ticket, lotSize, OrderClosePrice(), 0, CLR_NONE))

         {

            Print("Error closing order: ", GetLastError());

         }

      }

   }

}


How to become a Signals Provider for MetaTrader 4 and MetaTrader 5
How to become a Signals Provider for MetaTrader 4 and MetaTrader 5
  • www.mql5.com
Do you want to offer your trading signals and make profit? Register on MQL5.com website as a Seller, specify your trading account and offer traders a subscription to copy your trades.
Fernando Carreiro  
Also, don't use trading functions in the OnDeinit event handler. The de-initialisation needs to be quick and it has time limit for execution before it is forcefully terminated.
Fernando Carreiro  
Please clean up your post and remove all the extra unnecessary white-space in your code. Be prideful of your coding.
William Roeder  
ticket = OrderSend(Symbol(), OP_SELL, lotSize, Bid, 0, Bid + stopLoss * Point, Bid - takeProfit * Point, "Sell Trade", 0, 0, Red);
ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, 0, Ask - stopLoss * Point, Ask + takeProfit * Point, "Buy Trade", 0, 0, Blue);

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by 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 at 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, average maximum spread = 134.
    My EURCHF shows average spread = 18 points, average maximum spread = 106.
    (your broker will be similar).
              Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

Reason: