HELP "unexpected end of program " on Customizable MACD - page 2

 
Valter Zuccoli :
I don't know how to do it. Can't you correct and post back here ?

Correct the error first   'PositionsGetInteger'

 
Vladimir Karputov:

Correct the error first   'PositionsGetInteger'

Ok. this one I did.
//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright   "2009-2017, MetaQuotes Software Corp."
#property link        "http://www.mql5.com"
#property description "1.00"

#include <Trade\Trade.mqh>

CTrade trade;

int handle;

double linha_macd[];
double linha_signal[];

MqlRates rates [];

input double _volume = 2.0; //Quantidade de Lotes:
input double meuStop = 150.0; //Stop Loss (pontos):
input double meuTake = 50.0; //Take Profit (pontos):

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {

   ArraySetAsSeries(rates, true);
   ArraySetAsSeries(linha_macd, true);
   ArraySetAsSeries(linha_signal, true);


   handle = iMACD(Symbol(), PERIOD_CURRENT, 12,26,9, PRICE_CLOSE);

   return (INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {

   CopyRates(Symbol(), PERIOD_CURRENT, 0, 3, rates);
   CopyBuffer(handle, 0, 0, 3, linha_macd);
   CopyBuffer(handle, 1, 0, 3, linha_signal);



   if(linha_macd[2] < linha_signal[2] && linha_macd[1] > linha_signal[1] && linha_macd[0] < 0 && linha_signal[0] < 0)
     {

      //ObjectCreate(0, rates[1].time, OBJ_ARROW_BUY, 0, rates[1].time, rates[1].low);
      if(PositionsTotal() == 0)
        {
         if(trade.Buy(_volume, Symbol(),0,0,0, "Compra")
           {
            addTakeStop(meuStop, meuTake);
           }
        }


      if(linha_macd[2] > linha_signal[2] && linha_macd[1] < linha_signal[1] && linha_macd[0] > 0 && linha_signal[0] > 0)
        {

         //ObjectCreate(0, rates[1].time, OBJ_ARROW_SELL, 0, rates[1].time, rates[1].high);
         if(PositionsTotal() == 0)
           {
            if(trade.Sell(_volume, Symbol(),0,0,0, "Venda")
              {
               addTakeStop(meuStop, meuTake);
              }
           }
        }
     }


   void addTakeStop(double p_sl, double p_tp)
     {

      for(int i = PositionsTotal() -1; i>=0; i--)
        {
         string symbol = PositionGetSymbol(i);


         if(symbol == Symbol())
           {

            ulong ticket =    PositionGetInteger(POSITION_TICKET);
            double precoEntrada =  PositionGetDouble(POSITION_PRICE_OPEN);

            double novoSL;
            double novoTP;


            if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
              {

               novoSL = NormalizeDouble(precoEntrada - (p_sl *_Point), _Digits);
               novoTP = NormalizeDouble(precoEntrada + (p_tp *_Point), _Digits);

               trade.PositionModify(ticket, novoSL, novoTP);

              }
            else
               if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
                 {

                  novoSL = NormalizeDouble(precoEntrada + (p_sl *_Point), _Digits);
                  novoTP = NormalizeDouble(precoEntrada - (p_tp *_Point), _Digits);

                  trade.PositionModify(ticket, novoSL, novoTP);

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

Here's the basics - write your code carefully and you will succeed:

//+------------------------------------------------------------------+
//|                                                            1.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2020, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property description "1.001"
//--- input parameters
input group             "MACD"
input int                  Inp_MACD_fast_ema_period= 12;          // MACD: period for Fast average calculation
input int                  Inp_MACD_slow_ema_period= 26;          // MACD: period for Slow average calculation
input int                  Inp_MACD_signal_period  = 9;           // MACD: period for their difference averaging
input ENUM_APPLIED_PRICE   Inp_MACD_applied_price  = PRICE_CLOSE; // MACD: type of price
//---
int    handle_iMACD;                         // variable for storing the handle of the iMACD indicator
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iMACD
   handle_iMACD=iMACD(Symbol(),Period(),Inp_MACD_fast_ema_period,Inp_MACD_slow_ema_period,
                      Inp_MACD_signal_period,Inp_MACD_applied_price);
//--- if the handle is not created
   if(handle_iMACD==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMACD indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   if(handle_iMACD!=INVALID_HANDLE)
      IndicatorRelease(handle_iMACD);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
  }
//+------------------------------------------------------------------+
//| Add StopLoss and TakeProfit                                      |
//+------------------------------------------------------------------+
void AddStopLossTakeProfit(double p_sl,double p_tp)
  {
  
  }
//+------------------------------------------------------------------+
Files:
1.mq5  6 kb