新人对MQL4和MQL5的任何问题,对算法和代码的帮助和讨论 - 页 1865

 
Mihail Matkovskij #:

不过,并非如此。你需要保持跟踪的结果是

或你在OnTradeTransaction中的新函数。而如果没有位置,你应该进入。或者在buySignal或sellSignal 中写入信号,然后在OnTimer中处理,就像我在例子中展示的那样

ClosePosWithMaxProfitInCurrency

这个功能已不再适用。这是不合适的。取而代之的是,我们需要替代将先关闭一个已开订单的函数,如果它存在的话。

我不明白写在全局范围内buySignal和sellSignal 变量。但是,当我试图编译它时,它给了我以下错误

'sellSignal' - some operator expected   

expression has no effect        

'buySignal' - some operator expected    

expression has no effect        



 

下午好。

我当然读过这些,我可以分别写出每个订单,但问题是要缩短代码。

 
各位同事下午好!请教一位新手,如何获得指标的当前值(此刻),而不是从上一栏中获得?专家顾问只在前一栏结束时触发,而我需要它更早。
 
makssub #:

下午好。

我当然读过这些,我可以分别写出每个订单,但问题是要缩短代码。

到底是什么原因导致代码缩短不成功?

 
Shockeir #:
同事们好,请教一下新手,如何获得指标的当前值(此刻),而不是前一栏的值?EA只在前一栏结束时触发,而我需要它更早。

也许更详细地描述情况以及什么对你不起作用会更有帮助。

 
Andrey Sokolov #:

也许更详细地描述情况和你自己无法做到的事情会产生更多的结果。

该指标是一个标准的随机指数。专家顾问应该在K线和D线的交汇处触发。在非常大的交叉点上,直到出现一个新栏,什么都没有发生。当一个新的条形图出现时,如果条件仍然得到满足,那么就会发生行动。据我所知,这是因为指标缓冲区中的最后一个值是在最后一个完成的条形图上计算的值。因此,我希望触发发生在一个未完成的酒吧。

 
Shockeir #:

该指标是一个标准的随机指数。专家顾问应该在K线和D线的交汇处被触发。在十字路口,直到出现一个新的横杠,什么都没有发生。一旦有新的柱状物出现,如果条件仍然得到满足,就会发生行动。据我所知,发生这种情况是因为指标缓冲区中的最后一个值是在最后一个完成的条形图上计算的值。因此,我希望在一个未完成的条形图上触发这个动作。

最后一根蜡烛的指数为0。

那么,你是如何尝试解决这个问题的呢?你读过帮助吗?究竟是什么原因造成的不成功?

 
Andrey Sokolov #:

你能把代码放进去吗?至少要清楚地说明你使用的是什么语言。

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

//--- input parameters
input double   crossDepthOpen=7;    //Глубина пересечения K и D для открытия
input double   crossDepthClose=-2;  //Глубина пересечения K и D для закрытия
input double   closeCoef=5;         //Коэффициент изменения глубины закрытия
input int      k_period=8;          //Период K
input int      d_period=3;          //Период В
input int      slowing=3;           //Сглаживание
input double   tp_1=20;             //Тейк
input double   sl_1=60;             //Лосс
input double   maxPos=2.0;          //Размер лота

int      stoch_handle;
double   k_buffer[1];
double   d_buffer[1];
double   TKP;
double   STL;
double   CDO;
double   CDC;
int      lossCount=0;
int      profitCount=0;
int      EA_magic=12345;


//| Expert initialization function

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   TKP=tp_1;
   STL=sl_1;

   stoch_handle=iStochastic(_Symbol,PERIOD_CURRENT,k_period,d_period,slowing,MODE_EMA,STO_LOWHIGH);
   if(stoch_handle<0)
     {
      Alert("Ошибка при создании индикаторов ",GetLastError());
      return(0);
     }
   return(INIT_SUCCEEDED);
  }

//| Expert tick function

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   CDO=crossDepthOpen;
   CDC=crossDepthClose;
   MqlTick latestPrice;
   MqlTradeResult mResult;
   MqlTradeRequest mRequest;
   ZeroMemory(mRequest);
   SymbolInfoTick(_Symbol,latestPrice);
   ArraySetAsSeries(k_buffer,true);
   ArraySetAsSeries(d_buffer,true);
   if(CopyBuffer(stoch_handle,0,0,1,k_buffer)<0)
     {
      Alert("Не удалось скопировать в буфер K",GetLastError());
      return;
     }
   if(CopyBuffer(stoch_handle,1,0,1,d_buffer)<0)
     {
      Alert("Не удалось скопировать в буфер D",GetLastError());
      return;
     }

   bool buyFullOpened=false;
   bool buyPartOpened=false;
   bool sellFullOpened=false;
   bool sellPartOpened=false;

//Состояние позиций
   if(PositionSelect(_Symbol)==true)
     {
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
        {
         if(PositionGetDouble(POSITION_VOLUME)>=maxPos)
           {
            buyFullOpened=true;
           }
         else
           {
            buyPartOpened=true;
           }
        }
      else
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
           {

            if(PositionGetDouble(POSITION_VOLUME)>=maxPos)
              {
               sellFullOpened=true;
              }
            else
              {
               sellPartOpened=true;
              }
           }
     }
//состояние позиций конец

//Условия изменения позиций
   bool buyOpenCondition=k_buffer[0]>((d_buffer[0])+CDO);
   bool buyCloseCondition=k_buffer[0]<((d_buffer[0])-CDC);
   bool buyTPCondition=latestPrice.bid>(PositionGetDouble(POSITION_PRICE_OPEN)+TKP*_Point);
   bool buySLCondition=latestPrice.bid<(PositionGetDouble(POSITION_PRICE_OPEN)-STL*_Point);
   bool sellOpenCondition=k_buffer[0]<((d_buffer[0])-CDO);
   bool sellCloseCondition=k_buffer[0]>((d_buffer[0])+CDC);
   bool sellTPCondition=latestPrice.ask<(PositionGetDouble(POSITION_PRICE_OPEN)-TKP*_Point);
   bool sellSLCondition=latestPrice.ask>(PositionGetDouble(POSITION_PRICE_OPEN)+STL*_Point);
//Условия изменения позиций конец

//Проверка и выполнение действий
   if(buyOpenCondition)
     {
      if(!buyFullOpened && !buyPartOpened && !sellPartOpened && !sellFullOpened)
        {
         Print("Покупка по сигналу");
         Print("Текущий объем позиций ",PositionGetDouble(POSITION_VOLUME));
         mRequest.action = TRADE_ACTION_DEAL;
         mRequest.symbol = _Symbol;
         mRequest.price = NormalizeDouble(latestPrice.ask,_Digits);
         mRequest.sl = 0; //NormalizeDouble(latestPrice.ask - STL*Point(),_Digits);
         mRequest.tp = 0; //NormalizeDouble(latestPrice.ask + TKP*_Point,_Digits);
         mRequest.magic = EA_magic;
         mRequest.type = ORDER_TYPE_BUY;
         mRequest.type_filling = ORDER_FILLING_FOK;
         mRequest.deviation=100;
         mRequest.volume = maxPos;
         OrderSend(mRequest,mResult);
         Print("Открыто ",PositionGetDouble(POSITION_VOLUME)," по ", PositionGetDouble(POSITION_PRICE_OPEN));
        }
     }
//Условия покупки
   if(sellTPCondition)
     {
      if(sellFullOpened)
        {
         Print("Тейк-профит шорта");
         Print("Текущий объем позиций ",PositionGetDouble(POSITION_VOLUME));
         Print("Цена открытия ", PositionGetDouble(POSITION_PRICE_OPEN));
         mRequest.action = TRADE_ACTION_DEAL;
         mRequest.symbol = _Symbol;
         mRequest.price = NormalizeDouble(latestPrice.ask,_Digits);
         mRequest.sl = 0;//NormalizeDouble(latestPrice.ask - STL*_Point,_Digits);
         mRequest.tp = 0;//NormalizeDouble(latestPrice.ask + TKP*_Point,_Digits);
         mRequest.magic = EA_magic;
         mRequest.type = ORDER_TYPE_BUY;
         mRequest.type_filling = ORDER_FILLING_FOK;
         mRequest.deviation=100;
         mRequest.volume = MathRound(PositionGetDouble(POSITION_VOLUME)/2);
         OrderSend(mRequest,mResult);
         ++profitCount;
         Print("Текущий объем позиций ",PositionGetDouble(POSITION_VOLUME));
        }
     }

   if(sellSLCondition)
     {
      if(sellFullOpened || sellPartOpened)
        {
         Print("Стоп-лосс шорта");
         Print("Текущий объем позиций ",PositionGetDouble(POSITION_VOLUME));
         Print("Цена открытия ", PositionGetDouble(POSITION_PRICE_OPEN));
         mRequest.action = TRADE_ACTION_DEAL;
         mRequest.symbol = _Symbol;
         mRequest.price = NormalizeDouble(latestPrice.ask,_Digits);
         mRequest.sl = 0;//NormalizeDouble(latestPrice.ask - STL*_Point,_Digits);
         mRequest.tp = 0;//NormalizeDouble(latestPrice.ask + TKP*_Point,_Digits);
         mRequest.magic = EA_magic;
         mRequest.type = ORDER_TYPE_BUY;
         mRequest.type_filling = ORDER_FILLING_FOK;
         mRequest.deviation=100;
         mRequest.volume = PositionGetDouble(POSITION_VOLUME);
         OrderSend(mRequest,mResult);
         ++lossCount;
         Print("Текущий объем позиций ",PositionGetDouble(POSITION_VOLUME));
        }
     }

   if(sellCloseCondition)
     {
      if(sellFullOpened || sellPartOpened)
        {
         Print("Закрытие шорта по сигналу");
         Print("Текущий объем позиций ",PositionGetDouble(POSITION_VOLUME));
         Print("Цена открытия ", PositionGetDouble(POSITION_PRICE_OPEN));
         Print(k_buffer[0]," ",d_buffer[0]);
         mRequest.action = TRADE_ACTION_DEAL;
         mRequest.symbol = _Symbol;
         mRequest.price = NormalizeDouble(latestPrice.ask,_Digits);
         mRequest.sl = 0;//NormalizeDouble(latestPrice.ask - STL*_Point,_Digits);
         mRequest.tp = 0;//NormalizeDouble(latestPrice.ask + TKP*_Point,_Digits);
         mRequest.magic = EA_magic;
         mRequest.type = ORDER_TYPE_BUY;
         mRequest.type_filling = ORDER_FILLING_FOK;
         mRequest.deviation=100;
         mRequest.volume = PositionGetDouble(POSITION_VOLUME);
         OrderSend(mRequest,mResult);
         Print("Текущий объем позиций ",PositionGetDouble(POSITION_VOLUME));
        }
     }

//Условия продажи
   if(sellOpenCondition)
     {
      if(!sellFullOpened && !buyPartOpened && !sellPartOpened && !buyFullOpened)
        {
         Print("Продажа по сигналу");
         Print("Текущий объем позиций ",PositionGetDouble(POSITION_VOLUME));
         Print("Цена открытия ", PositionGetDouble(POSITION_PRICE_OPEN));
         Print(k_buffer[0]," ",d_buffer[0]);
         mRequest.action = TRADE_ACTION_DEAL;
         mRequest.symbol = _Symbol;
         mRequest.price = NormalizeDouble(latestPrice.bid,_Digits);
         mRequest.sl = 0; //NormalizeDouble(latestPrice.bid + STL*_Point,_Digits);
         mRequest.tp = 0; //NormalizeDouble(latestPrice.bid - TKP*_Point,_Digits);
         mRequest.magic = EA_magic;
         mRequest.type = ORDER_TYPE_SELL;
         mRequest.type_filling = ORDER_FILLING_FOK;
         mRequest.deviation=100;
         mRequest.volume = maxPos;
         OrderSend(mRequest,mResult);
         Print("Текущий объем позиций ",PositionGetDouble(POSITION_VOLUME));
        }
     }
   if(buyTPCondition)
     {
      if(buyFullOpened)
        {
         Print("Тейк-профит лонга");
         Print("Текущий объем позиций ",PositionGetDouble(POSITION_VOLUME));
         Print("Цена открытия ", PositionGetDouble(POSITION_PRICE_OPEN));
         mRequest.action = TRADE_ACTION_DEAL;
         mRequest.symbol = _Symbol;
         mRequest.price = NormalizeDouble(latestPrice.bid,_Digits);
         mRequest.sl = 0;//NormalizeDouble(latestPrice.bid + STL*_Point,_Digits);
         mRequest.tp = 0;//NormalizeDouble(latestPrice.bid - TKP*_Point,_Digits);
         mRequest.magic = EA_magic;
         mRequest.type = ORDER_TYPE_SELL;
         mRequest.type_filling = ORDER_FILLING_FOK;
         mRequest.deviation=100;
         mRequest.volume = MathRound(PositionGetDouble(POSITION_VOLUME)/2);
         OrderSend(mRequest,mResult);
         ++profitCount;
         Print("Текущий объем позиций ",PositionGetDouble(POSITION_VOLUME));
        }
     }

   if(buySLCondition)
     {
      if(buyFullOpened || buyPartOpened)
        {
         Print("Стоп-лосс лонга");
         Print("Текущий объем позиций ",PositionGetDouble(POSITION_VOLUME));
         Print("Цена открытия ", PositionGetDouble(POSITION_PRICE_OPEN));
         Print(k_buffer[0]," ",d_buffer[0]);
         mRequest.action = TRADE_ACTION_DEAL;
         mRequest.symbol = _Symbol;
         mRequest.price = NormalizeDouble(latestPrice.bid,_Digits);
         mRequest.sl = 0;//NormalizeDouble(latestPrice.bid + STL*_Point,_Digits);
         mRequest.tp = 0;//NormalizeDouble(latestPrice.bid - TKP*_Point,_Digits);
         mRequest.magic = EA_magic;
         mRequest.type = ORDER_TYPE_SELL;
         mRequest.type_filling = ORDER_FILLING_FOK;
         mRequest.deviation=100;
         mRequest.volume = PositionGetDouble(POSITION_VOLUME);
         OrderSend(mRequest,mResult);
         ++lossCount;
         Print("Текущий объем позиций ",PositionGetDouble(POSITION_VOLUME));
        }
     }

   if(buyCloseCondition)
     {
      if(buyFullOpened || buyPartOpened)
        {
         Print("Закрытие лонга по сигналу");
         Print("Текущий объем позиций ",PositionGetDouble(POSITION_VOLUME));
         Print("Цена открытия ", PositionGetDouble(POSITION_PRICE_OPEN));
         Print(k_buffer[0]," ",d_buffer[0]);
         mRequest.action = TRADE_ACTION_DEAL;
         mRequest.symbol = _Symbol;
         mRequest.price = NormalizeDouble(latestPrice.bid,_Digits);
         mRequest.sl = 0;//NormalizeDouble(latestPrice.bid + STL*_Point,_Digits);
         mRequest.tp = 0;//NormalizeDouble(latestPrice.bid - TKP*_Point,_Digits);
         mRequest.magic = EA_magic;
         mRequest.type = ORDER_TYPE_SELL;
         mRequest.type_filling = ORDER_FILLING_FOK;
         mRequest.deviation=100;
         mRequest.volume = PositionGetDouble(POSITION_VOLUME);
         OrderSend(mRequest,mResult);
         Print("Текущий объем позиций ",PositionGetDouble(POSITION_VOLUME));
        }
     }
  }
//+------------------------------------------------------------------+
//| Trade function                                                   |
//+------------------------------------------------------------------+
void OnTrade()
  {
//---

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


//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   IndicatorRelease(stoch_handle);
   Print("loss ",lossCount);
   Print("profit ",profitCount);
  }
//+------------------------------------------------------------------+
Andrey Sokolov#:

你能把代码放进去吗?至少要明确你是用什么语言来做的。

 

关于交易、自动交易系统和策略测试的论坛

新手对MQL4和MQL5的任何问题,对算法和代码的帮助和讨论

GlaVredFX, 2022.01.17 22:52

ClosePosWithMaxProfitInCurrency

你不能再使用这个功能。它并不适合。如果这个函数存在,它应该被先关闭一个已开订单的函数所取代。

我不明白在全局层面规定的 变量buySignal和sellSignal但是,当我试图编译它时,它给了我以下错误

'sellSignal' - some operator expected   

expression has no effect        

'buySignal' - some operator expected    

expression has no effect        



你是如何注册的,编译器在哪里干涉?我无法猜测。你需要源代码来了解它。
 
Shockeir #:
同事们好,请教一下新手,如何获得指标的当前值(此刻),而不是前一栏的值?EA只在前一栏结束时触发,而我需要它更早。

数组k_buffer[0]和d_buffer[0]包含最新的指标值。输出它们并自己看到它们有什么问题?