Can I close order with indicator calculate? I stuck, need help. This ea profit, but not finished!

To add comments, please log in or register
Glaadiator
9
Glaadiator  

I studied personally in the forum, no coding skills at all, I did it for 2 years. thank you for everything.

and this time, I had difficulty closing the current order with the indicator. I want to close it with the indicator I chose, I think it might be done. but I was at my wits end and finally here.

this is my code:


//+------------------------------------------------------------------+
//|                                                     horrrder.mq4 |
//|                                                   Copyright 2020 |
//|                                                         mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020"
#property link      "mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//---- Trades Call
extern string           UrLuckyName="I DONT NEED MONEY";
extern int              UrLuckyNumber=6662;
extern double           Lots=0.01;
//
double    TrailingStop=20;
bool      UseClose=true;
bool      UseCalc=true;
int       Slippage =2;
int x;
int y;
//
int Buy = OP_BUY;
int Sell = OP_SELL;
double AO1= iAO(NULL,PERIOD_D1,1);
double AO2= iAO(NULL,PERIOD_D1,2);
double AO3= iAO(NULL,PERIOD_D1,3);
//
double AO_up   = (AO1 < AO2);
double AO_dw   = (AO1 > AO2);
//
double close1 =iClose(NULL,PERIOD_D1,1);
double close2 =iClose(NULL,PERIOD_D1,3);
double high2 =iHigh(NULL,PERIOD_D1,2);
double low2 =iLow(NULL,PERIOD_D1,2);
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int Crossed()
  {
   double Emamain=iMA(NULL,PERIOD_D1,24,0,MODE_EMA, PRICE_CLOSE,1);
   double Emalow=iMA(NULL,PERIOD_D1,40,0,MODE_EMA, PRICE_LOW,1);
   double Emahigh=iMA(NULL,PERIOD_D1,40,0,MODE_EMA, PRICE_HIGH,1);
//---- MACD
   double  macd_signal   = iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
   double  macd_signal2  = iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2);
   double  macd_signal3  = iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,3);
   double  macd_signal4  = iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,4);
//--- declare condition
   double macd_signal_buy  = (macd_signal4 < macd_signal3
                              && macd_signal4 < macd_signal2
                              && macd_signal4 < macd_signal3
                              && macd_signal4 < macd_signal
                              && macd_signal3 < macd_signal2
                              && macd_signal3 < macd_signal
                              && macd_signal2 < macd_signal);
   double  macd_signal_sell = (macd_signal4 > macd_signal3
                               && macd_signal4 > macd_signal2
                               && macd_signal4 > macd_signal3
                               && macd_signal4 > macd_signal
                               && macd_signal3 > macd_signal2
                               && macd_signal3 > macd_signal
                               && macd_signal2 > macd_signal);
   double macd_signal_13buy   = (macd_signal3 < macd_signal);
   double macd_signal_13sell  = (macd_signal3 < macd_signal);

//--
   double  macd_main     = iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   double  macd_main2    = iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,2);
   double  macd_main3    = iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,3);
   double  macd_main4    = iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,4);
//--- declare condition
   double macd_main_buy = (macd_main4 < macd_main3
                           && macd_main4 < macd_main2
                           && macd_main4 < macd_main
                           && macd_main3 < macd_main2
                           && macd_main3 < macd_main
                           && macd_main2 < macd_main);
   double macd_main_sell = (macd_main4 > macd_main3
                            && macd_main4 > macd_main2
                            && macd_main4 > macd_main
                            && macd_main3 > macd_main2
                            && macd_main3 > macd_main
                            && macd_main2 > macd_main);
   double macd_main_13buy=(macd_main3 < macd_main);
   double macd_main_13sell=(macd_main3 > macd_main);
//--- declare condition
   double confirm_buy   = (macd_main > macd_signal);
   double confirm_sell  = (macd_main < macd_signal);

//----

//----
     {
      RefreshRates();
        {
           {
            if(Emamain > Emahigh)
               return(3);
            if(AO_up)
               closebyindicator_sell();// I WANT STOP WITH THIS
           }

           {
            if(macd_main_buy && macd_signal_buy && confirm_buy)
               return(1);
            else
               if(macd_main_13buy && macd_signal_13buy && confirm_buy)
                  return(1);

           }
        }

        {
         RefreshRates();
           {
            if(Emamain > Emalow)
               return(4);
            if(AO_dw)
               closebyindicator_buy();//I WANT STOP HERE TOO

           }
           {
            if(macd_main_sell && macd_signal_sell && confirm_sell)
               return(2);
            else
               if(macd_main_13sell  && macd_signal_13sell && confirm_sell)
                  return(2);

           }
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool isNewSumbol(string current_symbol)
  {
   int total =OrdersTotal();
   for(int cnt=0; cnt < total; cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      string selected_symbol=OrderSymbol();
      if(current_symbol==selected_symbol)
         return(False);
     }
   return(True);
  }

//+------------------------------------------------------------------+
int start()
  {
//----

//----
   int cnt, ticket, total;

   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);
     }
     {
      if(close2 > close1)
         Alert("close buy");
     }
     {
      if(close2 < close1)
         Alert("close sell");
     }

   total =OrdersTotal();
//----
   double sma2typical = iMA(NULL,PERIOD_H1,0,0,MODE_SMA,PRICE_TYPICAL,0);
   double close0 = iClose(NULL,PERIOD_H1,0);
   double close1 = iClose(NULL,PERIOD_H1,1);

   double closeup    = (close0 > close1);
   double closedw    = (close0 < close1);

   double sma2buy    = (close0 < sma2typical);
   double sma2sell   = (close0 > sma2typical);
   if(UseCalc==false)
      sma2buy = 0;
   sma2sell = 0;
   closeup = 0;
   closedw = 0;
   if(total < 1 || isNewSumbol(Symbol()))
     {
      if(Crossed()==1)
        {
         x=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,UrLuckyName,UrLuckyNumber,0,Green);
         if(x>0)
           {
            if(OrderSelect(x,SELECT_BY_TICKET,MODE_TRADES))
               Print("BUY order opened : ",OrderOpenPrice());
           }
         else
            Print("Error opening BUY order : ",GetLastError());

        }
      if(Crossed()==2)//
        {
         y=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,UrLuckyName,UrLuckyNumber,0,Red);
         if(y>0)
           {
            if(OrderSelect(y,SELECT_BY_TICKET,MODE_TRADES))
               Print("SELL order opened : ",OrderOpenPrice());
           }
         else
            Print("Error opening SELL order : ",GetLastError());

        }
      if(Crossed()==3) //
        {
         x=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,UrLuckyName,UrLuckyNumber,0,Green);
         if(x>0)
           {
            if(OrderSelect(x,SELECT_BY_TICKET,MODE_TRADES))
               Print("BUY order opened : ",OrderOpenPrice());
           }
         else
            Print("Error opening BUY order : ",GetLastError());

        }
      if(Crossed()==4)//
        {
         y=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,UrLuckyName,UrLuckyNumber,0,Red);
         if(y>0)
           {
            if(OrderSelect(y,SELECT_BY_TICKET,MODE_TRADES))
               Print("SELL order opened : ",OrderOpenPrice());
           }
         else
            Print("Error opening SELL order : ",GetLastError());

        }
      return(0);
     }
   for(cnt=0; cnt<total; cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
        {
         if(OrderType()==OP_BUY)
           {
            if(UseClose)
              {
               if(Crossed()==2)
                 {
                  OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
                  return(0);
                 }
              }


            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>MarketInfo(Symbol(),MODE_POINT)*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MarketInfo(Symbol(),MODE_POINT)*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-MarketInfo(Symbol(),MODE_POINT)*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else
           {
            if(UseClose)
              {
               if(Crossed()==1)
                 {
                  OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
                  return(0);
                 }
              }

            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(MarketInfo(Symbol(),MODE_POINT)*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MarketInfo(Symbol(),MODE_POINT)*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MarketInfo(Symbol(),MODE_POINT)*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
void closebyindicator_buy()
  {
   int xq;
   RefreshRates();
     {
      for(xq = OrdersTotal()-1; xq>=0; xq--)
        {
         OrderSelect(xq,SELECT_BY_POS,MODE_TRADES);
         if(OrderStopLoss()==(close2 > close1))
            continue;

         if(OrderType()== Buy)
            OrderClose(OrderTicket(),OrderLots(),OrderOpenPrice(),Slippage,clrRed);
        }
     }
  }
//
void closebyindicator_sell()
  {
   int yq;
   RefreshRates();
     {
      for(yq=OrdersTotal()-1; yq>=0; yq--)
        {
         if(OrderStopLoss()==(close2 < close1))
            continue;
         OrderSelect(yq,SELECT_BY_POS,MODE_TRADES);
         if(OrderType()== Sell)
            OrderClose(OrderTicket(),OrderLots(),OrderOpenPrice(),Slippage,clrRed);
        }
     }

  }
//+------------------------------------------------------------------+


I study with various codes here, I thank you for everything


I haven't cleaned this code yet, I'm really stuck.

it's a condition that I made and it benefits me.



Files:
horrder.png 45 kb
Glaadiator
9
Glaadiator  
i got solutions, so i work at OrderClose() & OrderCcloseby()... ty
William Roeder
22672
William Roeder  
  1. Glaadiator: I had difficulty closing the current order with the indicator. I
    Glaadiator: i got solutions, so i work at OrderClose() & OrderCcloseby()... ty Indicator's can not trade. Your solutions will not work in an indicator.

    Indicators can not trade. Your solutions will never work.

  2. double AO1= iAO(NULL,PERIOD_D1,1);
    double AO2= iAO(NULL,PERIOD_D1,2);
    double AO3= iAO(NULL,PERIOD_D1,3);
    //
    double AO_up   = (AO1 < AO2);
    double AO_dw   = (AO1 > AO2);
    //
    double close1 =iClose(NULL,PERIOD_D1,1);
    double close2 =iClose(NULL,PERIOD_D1,3);
    double high2 =iHigh(NULL,PERIOD_D1,2);
    double low2 =iLow(NULL,PERIOD_D1,2);
    Those are not assignments; 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

  3. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4 2019.05.20

To add comments, please log in or register