написание функции для метатрэйдер 4

 
khasan kayumov:
здравствуйте всем,  кто нибудь подскажите как написать функцию которая будет определять тип и магик последнего открытого ордера
int FindLastOrderType()
  {
   for(int i=OrdersTotal()-1; i>=0; i --)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNum)
            return(OrderType());
        }
     }
   return(-1);
  }
 

Поищите тут:

https://www.mql5.com/ru/forum/131859

Только "Полезные функции от KimIV".
Только "Полезные функции от KimIV".
  • www.mql5.com
Все функции взяты из этой ветки - http://forum.mql4...
 
спасибо большое !  сейчас попробую в forex editor вставить эту функцию . надеюсь будет работать
 
не получается. вот что пишет: 'FindLastOrderType' - function can be declared only in the global scope 1.mq4 134 9

 
#include <stdlib.mqh>
#include <WinUser32.mqh>

// exported variables
extern double BuyLots6 = 0.1;
extern int BuyStoploss6 = 0;
extern int BuyTakeprofit6 = 0;
extern int PriceOffset6 = 10;
extern double SellLots7 = 0.1;
extern int SellStoploss7 = 0;
extern int SellTakeprofit7 = 0;
extern int PriceOffset7 = 40;


// 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 OnInit()
{
    NDigits = Digits;
    
    if (false) ObjectsDeleteAll();      // clear the chart
    
    
    
    
    Comment("");    // clear the chart
    return(INIT_SUCCEEDED); 
}

// Expert start
void OnTick()
{
    if (Bars < 10)
    {
        Comment("Not enough bars");
        return ;
    }
    if (Terminated == true)
    {
        Comment("EA Terminated.");
        return ;
    }
    
    OnEveryTick2();
    
}

void OnEveryTick2()
{
    PipValue = 1;
    if (NDigits == 3 || NDigits == 5) PipValue = 10;
    
    IfOrderDoesNotExist3();
    lastordertype1();
    CustomCode14();
    
}

void IfOrderDoesNotExist3()
{
    bool exists = false;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
        {
            exists = true;
        }
    }
    else
    {
        Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    }
    
    if (exists == false)
    {
        IfOrderDoesNotExist4();
        
    }
}

void IfOrderDoesNotExist4()
{
    bool exists = false;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUYSTOP && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
        {
            exists = true;
        }
    }
    else
    {
        Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    }
    
    if (exists == false)
    {
        BuyPendingOrder6();
        
    }
}

void BuyPendingOrder6()
{
    int expire = TimeCurrent() + 60 * 0;
    double price = NormalizeDouble(Ask, NDigits) + PriceOffset6*PipValue*Point;
    double SL = price - BuyStoploss6*PipValue*Point;
    if (BuyStoploss6 == 0) SL = 0;
    double TP = price + BuyTakeprofit6*PipValue*Point;
    if (BuyTakeprofit6 == 0) TP = 0;
    if (0 == 0) expire = 0;
    int ticket = OrderSend(Symbol(), OP_BUYSTOP, BuyLots6, price, 4, SL, TP, "My Expert", 1, expire, Blue);
    if (ticket == -1)
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
    
}

void lastordertype1()
{
    int FindLastOrderType()
    {
        for(int i=OrdersTotal()-1; i>=0; i --)
        {
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
            {
                if(OrderSymbol()==Symbol() && OrderMagicNumber()==1)
                return(OP_BUY());
            }
        }
        return(-1);
    }
    
}

void IfOrderDoesNotExist5()
{
    bool exists = false;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_SELLSTOP && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
        {
            exists = true;
        }
    }
    else
    {
        Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    }
    
    if (exists == false)
    {
        SellPendingOrder7();
        
    }
}

void SellPendingOrder7()
{
    int expire = TimeCurrent() + 60 * 0;
    double price = NormalizeDouble(Bid, NDigits) - PriceOffset7*PipValue*Point;
    double SL = price + SellStoploss7*PipValue*Point;
    if (SellStoploss7 == 0) SL = 0;
    double TP = price - SellTakeprofit7*PipValue*Point;
    if (SellTakeprofit7 == 0) TP = 0;
    if (0 == 0) expire = 0;
    int ticket = OrderSend(Symbol(), OP_SELLSTOP, SellLots7, price, 4, SL, TP, "My Expert", 1, expire, Red);
    if (ticket == -1)
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
    
}

void CustomCode14()
{
    
    
}



void OnDeinit(const int reason)
{
    if (false) ObjectsDeleteAll();
    
    
    
    
}

 
khasan kayumov:


Так удобней читать,    вверху на панели есть кнопочка        SRC вставить код.
#include <stdlib.mqh>
#include <WinUser32.mqh>

// exported variables
extern double BuyLots6 = 0.1;
extern int BuyStoploss6 = 0;
extern int BuyTakeprofit6 = 0;
extern int PriceOffset6 = 10;
extern double SellLots7 = 0.1;
extern int SellStoploss7 = 0;
extern int SellTakeprofit7 = 0;
extern int PriceOffset7 = 40;


// 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 OnInit()
{
    NDigits = Digits;
    
    if (false) ObjectsDeleteAll();      // clear the chart
    
    
    
    
    Comment("");    // clear the chart
    return(INIT_SUCCEEDED); 
}

// Expert start
void OnTick()
{
    if (Bars < 10)
    {
        Comment("Not enough bars");
        return ;
    }
    if (Terminated == true)
    {
        Comment("EA Terminated.");
        return ;
    }
    
    OnEveryTick2();
    
}

void OnEveryTick2()
{
    PipValue = 1;
    if (NDigits == 3 || NDigits == 5) PipValue = 10;
    
    IfOrderDoesNotExist3();
    lastordertype1();
    CustomCode14();
    
}

void IfOrderDoesNotExist3()
{
    bool exists = false;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
        {
            exists = true;
        }
    }
    else
    {
        Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    }
    
    if (exists == false)
    {
        IfOrderDoesNotExist4();
        
    }
}

void IfOrderDoesNotExist4()
{
    bool exists = false;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUYSTOP && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
        {
            exists = true;
        }
    }
    else
    {
        Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    }
    
    if (exists == false)
    {
        BuyPendingOrder6();
        
    }
}

void BuyPendingOrder6()
{
    int expire = TimeCurrent() + 60 * 0;
    double price = NormalizeDouble(Ask, NDigits) + PriceOffset6*PipValue*Point;
    double SL = price - BuyStoploss6*PipValue*Point;
    if (BuyStoploss6 == 0) SL = 0;
    double TP = price + BuyTakeprofit6*PipValue*Point;
    if (BuyTakeprofit6 == 0) TP = 0;
    if (0 == 0) expire = 0;
    int ticket = OrderSend(Symbol(), OP_BUYSTOP, BuyLots6, price, 4, SL, TP, "My Expert", 1, expire, Blue);
    if (ticket == -1)
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
    
}

void lastordertype1()
{
    int FindLastOrderType()
    {
        for(int i=OrdersTotal()-1; i>=0; i --)
        {
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
            {
                if(OrderSymbol()==Symbol() && OrderMagicNumber()==1)
                return(OP_BUY());
            }
        }
        return(-1);
    }
    
}

void IfOrderDoesNotExist5()
{
    bool exists = false;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_SELLSTOP && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
        {
            exists = true;
        }
    }
    else
    {
        Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    }
    
    if (exists == false)
    {
        SellPendingOrder7();
        
    }
}

void SellPendingOrder7()
{
    int expire = TimeCurrent() + 60 * 0;
    double price = NormalizeDouble(Bid, NDigits) - PriceOffset7*PipValue*Point;
    double SL = price + SellStoploss7*PipValue*Point;
    if (SellStoploss7 == 0) SL = 0;
    double TP = price - SellTakeprofit7*PipValue*Point;
    if (SellTakeprofit7 == 0) TP = 0;
    if (0 == 0) expire = 0;
    int ticket = OrderSend(Symbol(), OP_SELLSTOP, SellLots7, price, 4, SL, TP, "My Expert", 1, expire, Red);
    if (ticket == -1)
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
    
}

void CustomCode14()
{
    
    
}



void OnDeinit(const int reason)
{
    if (false) ObjectsDeleteAll();
    
    
    
    
}

 
khasan kayumov:
не получается. вот что пишет: 'FindLastOrderType' - function can be declared only in the global scope 1.mq4 134 9


Ну так всё правильно. Не надо объявлять функцию внутри другой функции. Подучите английский, ещё пригодится.
 
Vitalie Postolache:

Ну так всё правильно. Не надо объявлять функцию внутри другой функции. Подучите английский, ещё пригодится.

Зачем его учить если для программирования достаточно этого.

Vitalie, всё что дальше адресовано не тебе...


И перевод достаточно понятный

'FindLastOrderType' - функция может быть объявлена только в глобальной области видимости 1.mq4 и 134 9

1.mq4 - имя файла эксперта

134 - номер строки в этом ужасном коде

9 - номер символа в строке этого ужасного кода

Яндекс.Переводчик – словарь и онлайн перевод на английский, русский, немецкий, французский, украинский и другие языки.
  • translate.yandex.ru
Перевод с английского, немецкого, французского, испанского, польского, турецкого и других языков на русский и обратно. Возможность переводить отдельные слова и фразы, а также целые тексты и веб-страницы.
Причина обращения: