初学者的问题 MQL5 MT5 MetaTrader 5 - 页 1319

 
SanAlex:

我是自学成才的,不可能向你解释--我只是选择了我需要的功能,我知道这个去那里,那个去那里。

基本上所有的功能我都是从弗拉基米尔-卡尔普托夫的 代码中复制的,为此我非常感谢他!- 如果你有任何问题,他会告诉你。

你是自学成才,而我刚刚开始编程,我的脑子里充满了问题。

我脑子里有这个EA的算法,如果在这个过程中要改变指标也没有问题,因为只有两个任务(方向点--向上和方向点--向下)。

目前,我正从最简单的就是最好的这一经典出发。这就是为什么我想最大限度地简化......,同时在我想创造的东西中获得最大的收益。我提议在我的算法上一起工作--我认为这对我们双方都很有趣。
如果你有兴趣,请当面与我联系。
 
Vladimir Karputov:

继承涉及到创建一个类。

所以我在写 -- 我应该从哪个类继承我的类来 使用他们标准库中的这些方法?

 
SanAlex:

这里有另一个选择--一旦触发了取舍--将在同一方向上再次打开

完全正确,有利润!!。

乍一看--一切都很正确。

还有,不应该有止损 的事实--也都是正确的。

但我不明白,当价格方向发生变化时,没有在Take Profit....... 关闭的订单会发生什么情况....。(它只是默认关闭,即在趋势变化指标的信号下关闭)?如果是这样,这不是我的本意。

而根据我的想法--命令应该保留,但在这里应该生效--一个有设置的马汀。

input group "======== МАРТИН ========"; 
input int      MMType        = 1;        // 1 - вкл. , 2 - выкл.
input double   Multiplikator = 1.667;    // множитель следующего лота
input double   Step          = 150.0;    // Расстояние между ордерами

即在与趋势相反的方向设置订单,并增加手数和平均止盈。简而言之,像一个简单的马丁一样工作,例如(Adviser Autoprofit 3),它在网络的开放源码中,但用mql4编写。

顺便说一下,原来的选项--趋势工作,你正确地写道--与马丁合作 没有人取消。

简而言之,趋势工作是一项任务,你已经出色地展示了部分内容,而第二项任务是一个马汀。

这两项任务不一定是相互关联的--在我看来,它们可以完全独立工作。

 
Sprut 185:

完全正确,有利润!!。

乍看之下--一切都很正确。

而事实上,止损--也不应该出现在那里--都是正确的。

但我不明白的是,当价格方向发生变化时,没有在Take Profit....... 收盘的Oder在哪里?(它只是默认关闭,即在趋势变化指标的信号下关闭)?如果是这样,这不是我的本意。

但根据我的想法,这个命令应该保留,但在这里应该有一个马特的设置生效。

另一个方面也是如此,即在与趋势相反的方向设置订单,增加手数和平均止盈。简而言之,像一个简单的马丁一样工作--例如(Autoprofit 3),它在网上是开源的,但用mql4编写。

一句话:不是 "顺序",而是 "位置"。

 
Vladimir Karputov:

一句话:不是 "顺序 "而是 "位置"。

我对这个错误表示歉意。
我将考虑到这一点,并在今后努力纠正自己。
 
Sprut 185:

我可以找到的所有选项--似乎没有任何更多的选项。

4 Sprut 185

//+------------------------------------------------------------------+
//|                                                  4 Sprut 185.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//---
#define  MACD_MAGIC 1234502
//---
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\AccountInfo.mqh>
//---
double            m_adjusted_point;             // point value adjusted for 3 or 5 points
CTrade            m_trade;                      // trading object
CSymbolInfo       m_symbol;                     // symbol info object
CPositionInfo     m_position;                   // trade position object
CAccountInfo      m_account;                    // account info wrapper
//---
input double InpLots          =0.1;   // Lots
input int    InpTakeProfit    =50;    // Take Profit (in pips)
input bool   InpVariant       =false; // Option
input int    InpBar           =1;     // Bar
input bool   InpClOp          =false; // Close opposite
//---
double m_macd_current;
double m_signal_current;
double m_take_profit;
int    price_uno;
int    m_handle_macd; // MACD indicator handle
int    ExtTimeOut=10; // time out in seconds between trade operations
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- initialize common information
   m_symbol.Name(Symbol());                  // symbol
   m_trade.SetExpertMagicNumber(MACD_MAGIC); // magic
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(Symbol());
//--- tuning for 3 or 5 digits
   int digits_adjust=1;
   if(m_symbol.Digits()==3 || m_symbol.Digits()==5)
      digits_adjust=10;
   m_adjusted_point=m_symbol.Point()*digits_adjust;
//--- set default deviation for trading in adjusted points
   m_take_profit     =InpTakeProfit*m_adjusted_point;
//--- set default deviation for trading in adjusted points
   m_trade.SetDeviationInPoints(3*digits_adjust);
//--- create StepMA_NRTR indicator
   m_handle_macd=iCustom(NULL,0,"StepMA_NRTR");
   if(m_handle_macd==INVALID_HANDLE)
     {
      printf("Error creating StepMA_NRTR indicator");
      return(false);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(void)
  {
   static datetime limit_time=0; // last trade processing time + timeout
//--- don't process if timeout
   if(TimeCurrent()>=limit_time)
     {
      //--- check for data
      if(Bars(Symbol(),Period())>2)
        {
         //--- change limit time by timeout in seconds if processed
         if(!InpVariant && Processing())
            limit_time=TimeCurrent()+ExtTimeOut;
         //--- change limit time by timeout in seconds if processed
         if(InpVariant && Processing_1())
            limit_time=TimeCurrent()+ExtTimeOut;
        }
     }
  }
//+------------------------------------------------------------------+
//| main function returns true if any position processed             |
//+------------------------------------------------------------------+
bool Processing(void)
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
      return(false);
   double m_buff_MACD_main[],m_buff_MACD_signal[];
   ArraySetAsSeries(m_buff_MACD_main,true);
   ArraySetAsSeries(m_buff_MACD_signal,true);
   int start_pos=InpBar,count=3;
   if(!iGetArray(m_handle_macd,0,start_pos,count,m_buff_MACD_main)||
      !iGetArray(m_handle_macd,1,start_pos,count,m_buff_MACD_signal))
     {
      return(false);
     }
//---
   m_macd_current   =m_buff_MACD_main[0];
   m_signal_current =m_buff_MACD_signal[0];
//---
   if(m_position.Select(Symbol()))
     {
      //--- try to close or modify long position
      if(LongClosed())
         return(true);
      //--- try to close or modify short position
      if(ShortClosed())
         return(true);
     }
   else
     {
      //--- check for long position (BUY) possibility
      if(LongOpened())
         return(true);
      //--- check for short position (SELL) possibility
      if(ShortOpened())
         return(true);
     }
//--- exit without position processing
   return(false);
  }
//+------------------------------------------------------------------+
//| main function returns true if any position processed             |
//+------------------------------------------------------------------+
bool Processing_1(void)
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
      return(false);
   double m_buff_MACD_main[],m_buff_MACD_signal[];
   bool StNRUp,StNRDn;
   ArraySetAsSeries(m_buff_MACD_main,true);
   ArraySetAsSeries(m_buff_MACD_signal,true);
   int start_pos=InpBar,count=3;
   if(!iGetArray(m_handle_macd,0,start_pos,count,m_buff_MACD_main)||
      !iGetArray(m_handle_macd,1,start_pos,count,m_buff_MACD_signal))
     {
      return(false);
     }
//---
   m_macd_current   =m_buff_MACD_main[0];
   m_signal_current =m_buff_MACD_signal[0];
//---
   StNRUp=m_buff_MACD_main[0]<m_buff_MACD_signal[0];
   StNRDn=m_buff_MACD_main[0]>m_buff_MACD_signal[0];
//--- BUY Signal
   if(StNRUp)
     {
      if(InpClOp)
         if(ShortClosed())
            Sleep(1000);
      if(price_uno<0)
         LongOpened();
      price_uno=+1;
      return(true);
     }
//--- SELL Signal
   if(StNRDn)
     {
      if(InpClOp)
         if(LongClosed())
            Sleep(1000);
      if(price_uno>0)
         ShortOpened();
      price_uno=-1;
      return(true);
     }
//--- exit without position processing
   return(false);
  }
//+------------------------------------------------------------------+
//| Check for long position closing                                  |
//+------------------------------------------------------------------+
bool LongClosed(void)
  {
   bool res=false;
//--- should it be closed?
   if(m_macd_current>m_signal_current)
     {
      //--- close position
      if(InpClOp)
         ClosePositions(POSITION_TYPE_BUY);
      //--- processed and cannot be modified
      res=true;
     }
//--- result
   return(res);
  }
//+------------------------------------------------------------------+
//| Check for short position closing                                 |
//+------------------------------------------------------------------+
bool ShortClosed(void)
  {
   bool res=false;
//--- should it be closed?
   if(m_macd_current<m_signal_current)
     {
      //--- close position
      if(InpClOp)
         ClosePositions(POSITION_TYPE_SELL);
      //--- processed and cannot be modified
      res=true;
     }
//--- result
   return(res);
  }
//+------------------------------------------------------------------+
//| Check for long position opening                                  |
//+------------------------------------------------------------------+
bool LongOpened(void)
  {
   bool res=false;
//--- check for long position (BUY) possibility
   if(m_macd_current<m_signal_current)
     {
      double price=m_symbol.Ask();
      double tp   =m_symbol.Bid()+m_take_profit;
      //--- check for free money
      if(m_account.FreeMarginCheck(Symbol(),ORDER_TYPE_BUY,InpLots,price)<0.0)
         printf("We have no money. Free Margin = %f",m_account.FreeMargin());
      else
        {
         //--- open position
         if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,InpLots,price,0.0,tp))
            printf("Position by %s to be opened",Symbol());
         else
           {
            printf("Error opening BUY position by %s : '%s'",Symbol(),m_trade.ResultComment());
            printf("Open parameters : price=%f,TP=%f",price,tp);
           }
         PlaySound("ok.wav");
        }
      //--- in any case we must exit from expert
      res=true;
     }
//--- result
   return(res);
  }
//+------------------------------------------------------------------+
//| Check for short position opening                                 |
//+------------------------------------------------------------------+
bool ShortOpened(void)
  {
   bool res=false;
//--- check for short position (SELL) possibility
   if(m_macd_current>m_signal_current)
     {
      double price=m_symbol.Bid();
      double tp   =m_symbol.Ask()-m_take_profit;
      //--- check for free money
      if(m_account.FreeMarginCheck(Symbol(),ORDER_TYPE_SELL,InpLots,price)<0.0)
         printf("We have no money. Free Margin = %f",m_account.FreeMargin());
      else
        {
         //--- open position
         if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,InpLots,price,0.0,tp))
            printf("Position by %s to be opened",Symbol());
         else
           {
            printf("Error opening SELL position by %s : '%s'",Symbol(),m_trade.ResultComment());
            printf("Open parameters : price=%f,TP=%f",price,tp);
           }
         PlaySound("ok.wav");
        }
      //--- in any case we must exit from expert
      res=true;
     }
//--- result
   return(res);
  }
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates()
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
     {
      return(false);
     }
//--- protection against the return value of "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
     {
      return(false);
     }
//---
   return(true);
  }
//+------------------------------------------------------------------+
//| Check Freeze and Stops levels                                    |
//+------------------------------------------------------------------+
void FreezeStopsLevels(double &freeze,double &stops)
  {
//--- check Freeze and Stops levels
   double coeff=(double)1;
   if(!RefreshRates() || !m_symbol.Refresh())
      return;
//--- FreezeLevel -> for pending order and modification
   double freeze_level=m_symbol.FreezeLevel()*m_symbol.Point();
   if(freeze_level==0.0)
      if(1>0)
         freeze_level=(m_symbol.Ask()-m_symbol.Bid())*coeff;
//--- StopsLevel -> for TakeProfit and StopLoss
   double stop_level=m_symbol.StopsLevel()*m_symbol.Point();
   if(stop_level==0.0)
      if(1>0)
         stop_level=(m_symbol.Ask()-m_symbol.Bid())*coeff;
//---
   freeze=freeze_level;
   stops=stop_level;
//---
   return;
  }
//+------------------------------------------------------------------+
//| Close positions                                                  |
//+------------------------------------------------------------------+
void ClosePositions(const ENUM_POSITION_TYPE pos_type)
  {
   double freeze=0.0,stops=0.0;
   FreezeStopsLevels(freeze,stops);
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==MACD_MAGIC)
            if(m_position.PositionType()==pos_type)
              {
               if(m_position.PositionType()==POSITION_TYPE_BUY)
                 {
                  bool take_profit_level=((m_position.TakeProfit()!=0.0 && m_position.TakeProfit()-m_position.PriceCurrent()>=freeze) || m_position.TakeProfit()==0.0);
                  bool stop_loss_level=((m_position.StopLoss()!=0.0 && m_position.PriceCurrent()-m_position.StopLoss()>=freeze) || m_position.StopLoss()==0.0);
                  if(take_profit_level && stop_loss_level)
                     if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol
                        Print(__FILE__," ",__FUNCTION__,", ERROR: ","BUY PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription());
                 }
               if(m_position.PositionType()==POSITION_TYPE_SELL)
                 {
                  bool take_profit_level=((m_position.TakeProfit()!=0.0 && m_position.PriceCurrent()-m_position.TakeProfit()>=freeze) || m_position.TakeProfit()==0.0);
                  bool stop_loss_level=((m_position.StopLoss()!=0.0 && m_position.StopLoss()-m_position.PriceCurrent()>=freeze) || m_position.StopLoss()==0.0);
                  if(take_profit_level && stop_loss_level)
                     if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol
                        Print(__FILE__," ",__FUNCTION__,", ERROR: ","SELL PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription());
                 }
               PlaySound("ok.wav");
              }
  }
//+------------------------------------------------------------------+
//| Filling the indicator buffers from the indicator                 |
//+------------------------------------------------------------------+
bool iGetArray(const int handle,const int buffer,const int start_pos,
               const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      return(false);
     }
   return(result);
  }
//+------------------------------------------------------------------+
 
SanAlex:

我可以找到的所有选项--似乎没有任何更多的选项。


如果你试图在你的专家顾问中使用2个独立的区块,怎么办?然后,一个将自己的工作,因为你已经在-趋势(使用一个指标)..........。
而第二个 是由第一个区块设置的信号--通过位置--不是通过趋势........。


我已经在上面写了。

这两项任务不一定要相互关联--在我看来,它们完全可以独立工作

我提出的这样一种算法(在我看来)还没有被程序员使用。在任何情况下,我都没有找到这样的解决方案--在互联网上。

 
Sprut 185 我提出的这种算法--(在我看来)还没有被程序员使用。在任何情况下,我都没有在互联网上找到这样的解决方案。

这可能是不可能实现的

不给 - 在符号上,必须有一个位置(如果它被关闭的利润和重新打开一个位置)(当应用相反的信号 - 它不能在其他方向打开

   if(m_position.Select(Symbol()))

如果只是从一个信号(从一个点),没有问题。(而这一点不应该出现在代码中(上面一行))。

 
SanAlex:

这可能是不可能实现的

不给 - 在符号上,必须有一个位置(如果它被关闭的利润和重新打开一个位置)(当应用相反的信号 - 它不能在其他方向打开

如果只是从一个信号(从一个点),没有问题。(然后,它不应该出现在代码中(上面一行)))

我明白了--这可能就是为什么我没有在互联网上找到这样一个解决方案。但在我看来,这个问题必须有一个解决方案。
谢谢--我将寻找这个问题的解决办法.....
那我们就先暂停一下。
 

在升级到2981版本后,开始出现一个错误行

MqlTradeRequest request = {0}
cannot convert 0 to enum 'ENUM_TRADE_REQUEST_ACTIONS'   OrderaiPosicii.mqh      11      34
请告知如何替换这条线路。