High-Low Trading

 
int LotSize = 1 ;
double StopLoss = 1;
double pips;
int MagicNumber1 = 1234;
int MagicNumber2 = 5678;
double high = iHigh(NULL, PERIOD_D1, 1);
double low = iLow(NULL, PERIOD_D1, 1);



int OnInit()
 {
  
double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if (ticksize == 0.00001 || ticksize ==0.001)
   pips = ticksize*10;
   else pips = ticksize;
   
   return(INIT_SUCCEEDED);
 }


void OnTick()
{
    
   int OpenOrders = OrdersTotal();
   
         if( OpenOrders < 1)

         {     
         
            if( Bid > high)
            
            {
   
    bool Buy = OrderSend(NULL,OP_BUY,LotSize,Ask,10,Ask - StopLoss,0,NULL,MagicNumber1,0,clrNONE);
            
            }
            
            
            else if( Bid < low)
               
            {
               
    bool Sell =  OrderSend(NULL,OP_SELL,LotSize,Bid,10,Bid + StopLoss,0,NULL,MagicNumber2,0,clrNONE);
          
               
            }
                  
            
         }
         
         else
         
         {
         
         for(int b=OrdersTotal()-1;b>=0;b--)
         
            {
            if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
               if(OrderMagicNumber()== MagicNumber1)
                  if(OrderSymbol()==Symbol())
                     if(OrderType()==OP_BUY)
                        if(Bid-OrderOpenPrice()>StopLoss*pips)
                           if(OrderStopLoss()<Bid-pips*StopLoss)
                              OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(pips*StopLoss),OrderTakeProfit(),0,clrNONE);
            }
            
         for(int s=OrdersTotal()-1;s>=0;s--)
         
            {
            if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
               if(OrderMagicNumber()==MagicNumber2)
                  if(OrderSymbol()==Symbol())
                     if(OrderType()==OP_SELL)
                        if(OrderOpenPrice()-Ask>StopLoss*pips)
                           if(OrderStopLoss()>Ask+pips*StopLoss||OrderStopLoss()==0)
                              OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(pips*StopLoss),OrderTakeProfit(),0,clrNONE);
            
            
            }                   
         
         }
      
 }

Hello everyone, hope everybody is doing well and healthy. 

My expert advisor doesn't work properly with my circumstances. May somebody take a look into that, please?


My main trading strategy is;


1- Is there an open order? (No)

2-Then what are the highest/lowest figures of the day?

3-If the price passes these levels(Up or down), jump in the trend and open an order.


So basically

-open a buy order if the price passed to the highest level of the day

-open a sell order if the price passed to the lowest level of the day


*And lastly, use a trailing stop.


Thank you guys in advance,

Cheers!

 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.

 
  1. double high = iHigh(NULL, PERIOD_D1, 1);
    double low = iLow(NULL, PERIOD_D1, 1);

    That is not an assignment; it's initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

      Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 2013.02.10

  2.    int OpenOrders = OrdersTotal();
       
             if( OpenOrders < 1)

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select 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 programming forum 2013.02.15
              PositionClose is not working - MQL5 programming forum 2020.02.21
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles 24 July 2006
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles Small>1 February 2011

  3.    bool Buy = OrderSend(NULL,OP_BUY,LotSize,Ask,10,Ask - StopLoss,0,NULL,MagicNumber1,0,clrNONE);
       bool Sell =  OrderSend(NULL,OP_SELL,LotSize,Bid,10,Bid + StopLoss,0,NULL,MagicNumber2,0,clrNONE);
    

    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).

 
William Roeder:
  1. That is not an assignment; it's initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

      Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 2013.02.10

  2. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select 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 programming forum 2013.02.15
              PositionClose is not working - MQL5 programming forum 2020.02.21
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles 24 July 2006
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles Small>1 February 2011

  3. 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).

Wow, these lines are fabulous William. First of all, thank you for this awesome detailed answer. It seems quite complicated to me. I'm not a coder, even not an IT guy. But will try to understand all of it and give feedback to you. Thanks again, and again.