//+------------------------------------------------------------------+//| 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//---inputdouble InpLots =0.1; // Lotsinputint InpTakeProfit =50; // Take Profit (in pips)inputbool InpVariant =false; // Optioninputint InpBar =1; // Barinputbool 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 handleint ExtTimeOut=10; // time out in seconds between trade operations//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
//--- 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 digitsint 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 |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
//---
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick(void)
{
staticdatetime limit_time=0; // last trade processing time + timeout//--- don't process if timeoutif(TimeCurrent()>=limit_time)
{
//--- check for dataif(Bars(Symbol(),Period())>2)
{
//--- change limit time by timeout in seconds if processedif(!InpVariant && Processing())
limit_time=TimeCurrent()+ExtTimeOut;
//--- change limit time by timeout in seconds if processedif(InpVariant && Processing_1())
limit_time=TimeCurrent()+ExtTimeOut;
}
}
}
//+------------------------------------------------------------------+//| main function returns true if any position processed |//+------------------------------------------------------------------+bool Processing(void)
{
//--- refresh ratesif(!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 positionif(LongClosed())
return(true);
//--- try to close or modify short positionif(ShortClosed())
return(true);
}
else
{
//--- check for long position (BUY) possibilityif(LongOpened())
return(true);
//--- check for short position (SELL) possibilityif(ShortOpened())
return(true);
}
//--- exit without position processingreturn(false);
}
//+------------------------------------------------------------------+//| main function returns true if any position processed |//+------------------------------------------------------------------+bool Processing_1(void)
{
//--- refresh ratesif(!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 Signalif(StNRUp)
{
if(InpClOp)
if(ShortClosed())
Sleep(1000);
if(price_uno<0)
LongOpened();
price_uno=+1;
return(true);
}
//--- SELL Signalif(StNRDn)
{
if(InpClOp)
if(LongClosed())
Sleep(1000);
if(price_uno>0)
ShortOpened();
price_uno=-1;
return(true);
}
//--- exit without position processingreturn(false);
}
//+------------------------------------------------------------------+//| Check for long position closing |//+------------------------------------------------------------------+bool LongClosed(void)
{
bool res=false;
//--- should it be closed?if(m_macd_current>m_signal_current)
{
//--- close positionif(InpClOp)
ClosePositions(POSITION_TYPE_BUY);
//--- processed and cannot be modified
res=true;
}
//--- resultreturn(res);
}
//+------------------------------------------------------------------+//| Check for short position closing |//+------------------------------------------------------------------+bool ShortClosed(void)
{
bool res=false;
//--- should it be closed?if(m_macd_current<m_signal_current)
{
//--- close positionif(InpClOp)
ClosePositions(POSITION_TYPE_SELL);
//--- processed and cannot be modified
res=true;
}
//--- resultreturn(res);
}
//+------------------------------------------------------------------+//| Check for long position opening |//+------------------------------------------------------------------+bool LongOpened(void)
{
bool res=false;
//--- check for long position (BUY) possibilityif(m_macd_current<m_signal_current)
{
double price=m_symbol.Ask();
double tp =m_symbol.Bid()+m_take_profit;
//--- check for free moneyif(m_account.FreeMarginCheck(Symbol(),ORDER_TYPE_BUY,InpLots,price)<0.0)
printf("We have no money. Free Margin = %f",m_account.FreeMargin());
else
{
//--- open positionif(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;
}
//--- resultreturn(res);
}
//+------------------------------------------------------------------+//| Check for short position opening |//+------------------------------------------------------------------+bool ShortOpened(void)
{
bool res=false;
//--- check for short position (SELL) possibilityif(m_macd_current>m_signal_current)
{
double price=m_symbol.Bid();
double tp =m_symbol.Ask()-m_take_profit;
//--- check for free moneyif(m_account.FreeMarginCheck(Symbol(),ORDER_TYPE_SELL,InpLots,price)<0.0)
printf("We have no money. Free Margin = %f",m_account.FreeMargin());
else
{
//--- open positionif(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;
}
//--- resultreturn(res);
}
//+------------------------------------------------------------------+//| Refreshes the symbol quotes data |//+------------------------------------------------------------------+bool RefreshRates()
{
//--- refresh ratesif(!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 levelsdouble coeff=(double)1;
if(!RefreshRates() || !m_symbol.Refresh())
return;
//--- FreezeLevel -> for pending order and modificationdouble 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 StopLossdouble 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(constENUM_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 positionsif(m_position.SelectByIndex(i)) // selects the position by index for further access to its propertiesif(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_symbolPrint(__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_symbolPrint(__FILE__," ",__FUNCTION__,", ERROR: ","SELL PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription());
}
PlaySound("ok.wav");
}
}
//+------------------------------------------------------------------+//| Filling the indicator buffers from the indicator |//+------------------------------------------------------------------+bool iGetArray(constint handle,constint buffer,constint start_pos,
constint count,double &arr_buffer[])
{
bool result=true;
if(!ArrayIsDynamic(arr_buffer))
{
return(false);
}
ArrayFree(arr_buffer);
//--- reset error codeResetLastError();
//--- fill a part of the iBands array with values from the indicator bufferint copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
if(copied!=count)
{
return(false);
}
return(result);
}
//+------------------------------------------------------------------+
我是自学成才的,不可能向你解释--我只是选择了我需要的功能,我知道这个去那里,那个去那里。
基本上所有的功能我都是从弗拉基米尔-卡尔普托夫的 代码中复制的,为此我非常感谢他!- 如果你有任何问题,他会告诉你。
你是自学成才,而我刚刚开始编程,我的脑子里充满了问题。
我脑子里有这个EA的算法,如果在这个过程中要改变指标也没有问题,因为只有两个任务(方向点--向上和方向点--向下)。
继承涉及到创建一个类。
所以我在写 -- 我应该从哪个类继承我的类来 使用他们标准库中的这些方法?
这里有另一个选择--一旦触发了取舍--将在同一方向上再次打开
完全正确,有利润!!。
乍一看--一切都很正确。
还有,不应该有止损 的事实--也都是正确的。
但我不明白,当价格方向发生变化时,没有在Take Profit....... 关闭的订单会发生什么情况....。(它只是默认关闭,即在趋势变化指标的信号下关闭)?如果是这样,这不是我的本意。
而根据我的想法--命令应该保留,但在这里应该生效--一个有设置的马汀。
即在与趋势相反的方向设置订单,并增加手数和平均止盈。简而言之,像一个简单的马丁一样工作,例如(Adviser Autoprofit 3),它在网络的开放源码中,但用mql4编写。
顺便说一下,原来的选项--趋势工作,你正确地写道--与马丁合作时 没有人取消。
简而言之,趋势工作是一项任务,你已经出色地展示了部分内容,而第二项任务是一个马汀。
这两项任务不一定是相互关联的--在我看来,它们可以完全独立工作。
完全正确,有利润!!。
乍看之下--一切都很正确。
而事实上,止损--也不应该出现在那里--都是正确的。
但我不明白的是,当价格方向发生变化时,没有在Take Profit....... 收盘的Oder在哪里?(它只是默认关闭,即在趋势变化指标的信号下关闭)?如果是这样,这不是我的本意。
但根据我的想法,这个命令应该保留,但在这里应该有一个马特的设置生效。
另一个方面也是如此,即在与趋势相反的方向设置订单,增加手数和平均止盈。简而言之,像一个简单的马丁一样工作--例如(Autoprofit 3),它在网上是开源的,但用mql4编写。一句话:不是 "顺序",而是 "位置"。
一句话:不是 "顺序 "而是 "位置"。
我可以找到的所有选项--似乎没有任何更多的选项。
我可以找到的所有选项--似乎没有任何更多的选项。
我已经在上面写了。
这两项任务不一定要相互关联--在我看来,它们完全可以独立工作。
我提出的这样一种算法(在我看来)还没有被程序员使用。在任何情况下,我都没有找到这样的解决方案--在互联网上。
这可能是不可能实现的
它不给 - 在符号上,必须有一个位置(如果它被关闭的利润和重新打开一个位置)(当应用相反的信号 - 它不能在其他方向打开
如果只是从一个信号(从一个点),没有问题。(而这一点不应该出现在代码中(上面一行))。
这可能是不可能实现的
它不给 - 在符号上,必须有一个位置(如果它被关闭的利润和重新打开一个位置)(当应用相反的信号 - 它不能在其他方向打开
如果只是从一个信号(从一个点),没有问题。(然后,它不应该出现在代码中(上面一行)))
在升级到2981版本后,开始出现一个错误行