Problem with code

 

Hi guys

Can someone  tell me what is wrong with this code , no position is opned

handle1 = iMACD(NULL, PERIOD_CURRENT,12,26,9,PRICE_CLOSE);
handle2 = iMACD(NULL, PERIOD_CURRENT,12,26,9,PRICE_CLOSE);
handle3 = iMACD(NULL, PERIOD_CURRENT,12,26,9,PRICE_CLOSE);
handle4 = iMACD(NULL, PERIOD_CURRENT,12,26,9,PRICE_CLOSE);

.....

if ((GetIndicator(handle1,MAIN_LINE,1) > GetIndicator(handle2,SIGNAL_LINE,1)) && (GetIndicator(handle3,MAIN_LINE,2) < GetIndicator(handle4,SIGNAL_LINE,2)))

not working but it work only by this code

if ((GetIndicator(handle1,MAIN_LINE,1) > GetIndicator(handle2,SIGNAL_LINE,1))

Thanks

Files:
2121.mq5  18 kb
 
serieux399:

Hi guys

Can someone  tell me what is wrong with this code , no position is opned

.....

not working but it work only by this code

Thanks

1.  Please insert the code correctly: when editing a message, press the button  Codeand paste your code into the pop-up window. (The first time I corrected your message)

2. We do not see what 'GetIndicator' is

3. Attach your file using the button  Attach file

 

#include <Trade\Trade.mqh>

// exported variables
input double Lots4 = 0.2;
input int Stoploss4 = 40;
input int Takeprofit4 = 60;


// local variables
double PipValue=1;    // this variable is here to support 5-digit brokers
bool Terminated = false;
string LF = "\n";  // use this in custom or utility blocks where you need line feeds
int NDigits = 4;   // used mostly for NormalizeDouble in Flex type blocks
int ObjCount = 0;  // count of all objects created on the chart, allows creation of objects with unique names
int current = 0;

int handle1 = 0;



---------------------------------------------------------------+
int OnInit()
{
    //---
    NDigits = Digits();
    if (NDigits == 3 || NDigits == 5) PipValue = 10;
    
    if (AccountInfoInteger(ACCOUNT_TRADE_EXPERT) == false)
    {
        Print("Check terminal options because EA trade option is set to not allowed.");
        Comment("Check terminal options because EA trade option is set to not allowed.");
    }
    
    if (false) ObjectsDeleteAll(0);      // clear the chart
    
    handle1 = iMACD(NULL, PERIOD_CURRENT,12,26,9,PRICE_CLOSE);
    
    
    
    Comment("");    // clear the chart
    //---
    return(0);
}
void OnDeinit(const int reason)
{
    
    if (false) ObjectsDeleteAll(0);
    
    IndicatorRelease(handle1);
    
    
    return;
}
------------------------------------------------------------------+
void OnTick()
{
    
    if (Terminated == true)
    {
        Comment("EA Terminated.");
    }
    
    OnEveryTick2();
    return;
}


double Low(int index)
{
    double arr[];
    double low = 0;
    ArraySetAsSeries(arr, true);
    int copied = CopyLow(Symbol(), PERIOD_CURRENT, 0, Bars(Symbol(), PERIOD_CURRENT), arr);
    if (copied>0 && index<copied) low = arr[index];
    return (low);
}

double High(int index)
{
    double arr[];
    double high = 0;
    ArraySetAsSeries(arr, true);
    int copied = CopyHigh(Symbol(), PERIOD_CURRENT, 0, Bars(Symbol(), PERIOD_CURRENT), arr);
    if (copied>0 && index<copied) high=arr[index];
    return(high);
}

double Close(int index)
{
    double arr[];
    double close = 0;
    ArraySetAsSeries(arr, true);
    int copied = CopyClose(Symbol(), PERIOD_CURRENT, 0, Bars(Symbol(), PERIOD_CURRENT), arr);
    if (copied>0 && index<copied) close = arr[index];
    return (close);
}
double Open(int index)
{
    double arr[];
    double open = 0;
    ArraySetAsSeries(arr, true);
    int copied = CopyOpen(Symbol(), PERIOD_CURRENT, 0, Bars(Symbol(), PERIOD_CURRENT), arr);
    if (copied>0 && index<copied) open = arr[index];
    return (open);
}
//+------------------------------------------------------------------+
//| Get current bid value                                            |
//+------------------------------------------------------------------+
double Bid()
{
    return (SymbolInfoDouble(Symbol(), SYMBOL_BID));
}

//+------------------------------------------------------------------+
//| Get current ask value                                            |
//+------------------------------------------------------------------+
double Ask()
{
    return (SymbolInfoDouble(Symbol(), SYMBOL_ASK));
}

//+------------------------------------------------------------------+
//| Is there an error                                                |
//+------------------------------------------------------------------+
bool IsError(MqlTradeResult& result, string function)
{
    if (result.retcode != 0 && result.retcode != TRADE_RETCODE_DONE && result.retcode != TRADE_RETCODE_PLACED)
    {
        Print("Function: ", function, " Error: ", result.retcode, " ", result.comment);
        return (true);
    }
    else
    Print("> Executed: [", function, "]");
    return (false);
}

bool IsError(CTrade& trade, string function)
{
    if (trade.ResultRetcode() != 0 && trade.ResultRetcode() != TRADE_RETCODE_DONE && trade.ResultRetcode() != TRADE_RETCODE_PLACED)
    {
        Print("Function: ", function, " Error: ", trade.ResultRetcode(), " ", trade.ResultRetcodeDescription());
        return (true);
    }
    else
    Print("> Executed: [", function, "]");
    return (false);
}
double GetIndicator(int handle, int buffer_num, int index)
{
    //--- array for the indicator values
    double arr[];
    //--- obtain the indicator value in the last two bars
    if (CopyBuffer(handle, buffer_num, 0, index+1, arr) <= 0)
    {
        Sleep(200);
        for(int i=0; i<100; i++)
        {
            if (BarsCalculated(handle) > 0)
            break;
            Sleep(50);
        }
        int copied = CopyBuffer(handle, buffer_num, 0, index+1, arr);
        if (copied <= 0)
        {
            Print("CopyBuffer failed. Maybe history has not download yet? Error = ", GetLastError());
            return -1;
        }
        else
        return (arr[index]);
    }
    else
    {
        return (arr[index]);
    }
    
    return 0;
}
void OnEveryTick2()
{
    
    if (NDigits == 3 || NDigits == 5) PipValue = 10;
    
    TechnicalAnalysis2x1();
    
}

void TechnicalAnalysis2x1()
{
    
    if ((GetIndicator(handle1,MAIN_LINE,1) > GetIndicator(handle1,SIGNAL_LINE,1)) && (GetIndicator(handle1,MAIN_LINE,2) < GetIndicator(handle1,SIGNAL_LINE,2)))
    {
        IfPositionDoesNotExist7();
        
    }
}

void IfPositionDoesNotExist7()
{
    
    bool exists = false;
    
    // go through all positions
    for (int i=PositionsTotal()-1;i>=0;i--)
    {
        string symbol = PositionGetSymbol(i);
        if (symbol == Symbol())
        {
            // position with appropriate ORDER_MAGIC, symbol and order type
            if (PositionGetInteger(POSITION_MAGIC) == 1 && PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
            exists = true;
        }
    }
    
    if (exists == false)
    {
        BuyOrder4();
        
    }
    
    
}

void BuyOrder4()
{
    
    //--- prepare a request
    MqlTradeRequest request;
    ZeroMemory(request);
    request.action = TRADE_ACTION_DEAL;          // setting a deal order
    request.magic = 1;                   // ORDER_MAGIC
    request.symbol = Symbol();                   // symbol
    request.volume= Lots4;                      // volume in lots
    request.price = Ask();
    request.sl = Ask() - Stoploss4*PipValue*Point();      // Stop Loss specified
    request.tp = Ask() + Takeprofit4*PipValue*Point();    // Take Profit specified
    request.deviation= 4;             // deviation in points
    request.type = ORDER_TYPE_BUY;
    request.comment = "Order";
    MqlTradeResult result;
    ZeroMemory(result);
    bool ok = OrderSend(request,result);
    // check the result
    if (ok && !IsError(result, __FUNCTION__))
    {
            
    }
    
    
}
Vladimir Karputov #
:

1.  Please insert the code correctly: when editing a message, press the button  and paste your code into the pop-up window. (The first time I corrected your message)

2. We do not see what 'GetIndicator' is

3. Attach your file using the button 

ok i attached the file

Thanks

 

Why do you need four SAME handles of the MACD indicator?

   handle1 = iMACD(NULL, PERIOD_CURRENT,12,26,9,PRICE_CLOSE);
   handle2 = iMACD(NULL, PERIOD_CURRENT,12,26,9,PRICE_CLOSE);
   handle3 = iMACD(NULL, PERIOD_CURRENT,12,26,9,PRICE_CLOSE);
   handle4 = iMACD(NULL, PERIOD_CURRENT,12,26,9,PRICE_CLOSE);
 
Vladimir Karputov #:

Why do you need four SAME handles of the MACD indicator?

Vladimir Karputov #:

Why do you need four SAME handles of the MACD indicator?


i test with 1  and 2 handles that give the same result .On cross the MACD no position opened

 if ((GetIndicator(handle1,MAIN_LINE,1) > GetIndicator(handle1,SIGNAL_LINE,1)) && (GetIndicator(handle1,MAIN_LINE,2) < GetIndicator(handle1,SIGNAL_LINE,2)))

 
serieux399 # :

i test with 1  and 2 handles that give the same result .On cross the MACD no position opened

 if ((GetIndicator(handle1,MAIN_LINE,1) > GetIndicator(handle1,SIGNAL_LINE,1)) && (GetIndicator(handle1,MAIN_LINE,2) < GetIndicator(handle1,SIGNAL_LINE,2)))

I repeat my question: WHY do you need four SAME handles? You need to leave one handle. And  Please insert the code correctly: when editing a message, press the button   Codeand paste your code into the pop-up window.

 
Vladimir Karputov #:

Why do you need four SAME handles of the MACD indicator?


updated  
 
serieux399 # :
updated  

Where is the file? Show your file. Attach a file to a new message.

 
Vladimir Karputov #:

Where is the file? Show your file. Attach a file to a new message.

File attached
Files:
2121.mq5  18 kb
 
serieux399 #:
File attached

You simply trading the MACD - right? Do you know that you have such an EA already on you PC, ready working, with no Errors?

Look here: C:\Users\..\Experts\Examples\MACD\MACD Sample.mq5

Bear in mind that there is almost nothing that has not been programmed for MT4/5 already.

 
Carl Schreiber #:

You simply trading the MACD - right? Do you know that you have such an EA already on you PC, ready working, with no Errors?

Look here: C:\Users\..\Experts\Examples\MACD\MACD Sample.mq5

Bear in mind that there is almost nothing that has not been programmed for MT4/5 already.

I know that 

i like to correct this code

Reason: