我如何编写 mql4 代码 (EA) 用矩形标记列出的蜡烛形态
How can I write mql4 code (EA) that marks the listed candle patterns with rectangles
我是编写 mql4
代码的新手,如果我能在出现以下烛台模式时获得绘制矩形的帮助,我将不胜感激:
图1:
运行 代码片段
<blockquote class="imgur-embed-pub" lang="en" data-id="a/fRoPzsm"><a href="//imgur.com/a/fRoPzsm">Demand Zone 1</a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>
图2:
运行 代码片段
<blockquote class="imgur-embed-pub" lang="en" data-id="a/4E8KE1R" data-context="false"><a href="//imgur.com/a/4E8KE1R">Demand Zone 2</a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>
和
图3:
运行 代码片段
<blockquote class="imgur-embed-pub" lang="en" data-id="a/h6D6o6R"><a href="//imgur.com/a/h6D6o6R">Hidden Demand Zone</a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>
和各自的补给区
并以指定的止损点和获利点打开挂单。
请原谅我没有直接包含图像。我没有足够的赞成票来做到这一点。
以下是对链接图像中烛台形态的解释:
需求区
一般 candlestick pattern
(需求区)出现在至少两个或多个连续的看涨蜡烛(最后一个看涨蜡烛的高点是时间段的高点)之后是一个或多个看跌蜡烛,其高点和低点均低于最后一根看涨蜡烛。最后是形成新高的看涨蜡烛。作为需求区的矩形区域是从开盘价到最后一根看跌蜡烛的低价。
隐藏需求区
当一系列连续的看涨蜡烛中有一根蜡烛的低点低于前一根蜡烛并且其高点与其收盘价重合时,则隐藏需求区从看涨蜡烛的低点到开盘价。
需求区和供应区都有完整的解释here。
我知道 bullish
和 bearish
蜡烛可以由
确定
if ( ( Open[1] - Close[1] ) > 0)
{
// candle is bearish
}
else
{
// candle is bullish
}
非常感谢您的帮助。
似乎这些模式没有完整描述,因此无法正确编码。好的,让我们尝试模式#1。
pattern使用的条件(从图片上看是合理的):
1. 在新柱 (bar#0) 的开始处检查。
2. bar 1(如果我们将 0 计算为当前,则为 MQL4 中的 bar#3)必须看涨。
3. bar 2(bar#2) 看跌。 (或者在模式#2 的情况下有 N 根柱,N 可以是 2 根或更多)
4. bar 3(MT4 中的 bar#1)看涨。
5. 它的高=收盘。
6. 柱线#3 的最高价>最高价。
enum EnmDir
{
LONG = 1,
SHORT=-1,
NONE = 0,
};
int getCandleDirection(const int shift)
{
const double open=iOpen(_Symbol,0,shift), close=iClose(_Symbol,0,shift);
if(close-open>_Point/2.)
return LONG; //bullish
if(open-close>_Point/2.)
return SHORT; //bearish
return NONE; //doji
}
bool isPattern1Detected(const EnmDir dir)
{
if(dir==0)return(false);
if(getCandleDirection(3)!=dir)
return false; //rule#2
if(getCandleDirection(2)+dir!=0)
return false; //rule#3
if(getCandleDirection(1)!=dir)
return false; //rule#4
if(dir>0)
{
if(iHigh(_Symbol,0,1)-iClose(_Symbol,0,1)>_Point/2.)
return false; //rule#5 for long
if(iHigh(_Symbol,0,1)-iHigh(_Symbol,0,3)>_Point/2.)
return true; //rule#6 for long
return false; //if rule#6 is not hold
}
else
{
if(iClose(_Symbol,0,1)-iLow(_Symbol,0,1)>_Point/2.)
return false; //rule#5 for short
if(iLow(_Symbol,0,3)-iLow(_Symbol,0,1)>_Point/2.)
return true; //rule#6 for short
return false; //if rule#6 is not hold
}
}
bool isPattern2Detected(const EnmDir dir,const int numCandlesAgainst=1)
{
if(dir==NONE)return(false);
if(getCandleDirection(1)!=dir)
return false; //rule#4
for(int i=1;i<=numCandlesAgainst;i++)
{
if(getCandleDirection(1+i)!=dir)
return(false); //rule#3 - checking that all numCandlesAgainst must be bearish
}
if(getCandleDirection(2+numCandlesAgainst)!=dir)
return false; //rule#2
if(dir>0)
{
if(iHigh(_Symbol,0,1)-iClose(_Symbol,0,1)>_Point/2.)
return false; //rule#5 for long
if(iHigh(_Symbol,0,1)-iHigh(_Symbol,0,2+numCandlesAgainst)>_Point/2.)
return true; //rule#6 for long
return false; //if rule#6 is not hold
}
else
{
if(iClose(_Symbol,0,1)-iLow(_Symbol,0,1)>_Point/2.)
return false; //rule#5 for short
if(iLow(_Symbol,0,2+numCandlesAgainst)-iLow(_Symbol,0,1)>_Point/2.)
return true; //rule#6 for short
return false; //if rule#6 is not hold
}
}
您还需要什么?要检测矩形的 HL?很简单,规则很明确。让我们假设它们是:对于 LONG,向上 = bar#2 的开盘价,向下 = 该柱的最低价。然后,
void detectRangeOfZone(double &top,double &bottom,const EnmDir dir)
{
if(dir>0)
{
top=iOpen(_Symbol,0,2);
bottom=iLow(_Symbol,0,2);
}
else if(dir<0)
{
top=iClose(_Symbol,0,2);
bottom=iHigh(_Symbol,0,2);
}
}
需要画矩形吗?好的,但是您如何决定停止绘图的时间?让我们假设右边的 N 个柱就足够了,让我们暂时忽略周末(如果在市场关闭时记住周末,那就更复杂一些)。
bool drawRectangle(const int dir,const double top,const double bottom)
{
const datetime starts=iTime(_Symbol,0,2), ends=starts+PeriodSeconds()*N_bars;//time of start and end of the rectangle
const string name=prefix+"_"+(dir>0?"DEMAND":"SUPPLY")+"_"+TimeToString(starts);//name would be unique sinse we use time of start of the range. DO NOT FORGET about prefix - it should be declared globally, you would be able to delete all the objects with 'ObjectsDeleteAll()' function that accepts prefix in one of its implementations.
if(!ObjectCreate(0,name,OBJ_RECTANGLE,0,0,0,0,0))
{
printf("%i %s: failed to create %s. error=%d",__LINE__,__FILE__,name,_LastError);
return false;
}
ObjectSetInteger(0,name,OBJPROP_TIME1,starts);
ObjectSetInteger(0,name,OBJPROP_TIME2,ends);
ObjectSetDouble(0,name,OBJPROP_PRICE1,top);
ObjectSetDouble(0,name,OBJPROP_PRICE2,bottom);
//add color, width, filling color, access modifiers etc, example is here https://docs.mql4.com/ru/constants/objectconstants/enum_object/obj_rectangle
return true;
}
这里是主要块,不要忘记添加一个新的柱检查,否则该工具会在每个订单号上检查对象,这是浪费时间。
字符串前缀=""; //为所有对象添加一些唯一的前缀
const int N_bars = 15; // 本例中为 15 根柱
void OnDeinit(const int reason){ObjectsDeleteAll(0,prefix);}
void OnTick()
{
if(!isNewBar())
return; //not necessary but waste of time to check every second
const bool pattern1Up=isPattern1Detected(1), pattern1Dn=isPattern1Detected(-1);
if(pattern1Up)
{
double top,bottom;
detectRangeOfZone(top,bottom,1);
drawRectangle(1,top,bottom);
PlacePendingOrder(1,top,bottom);
}
if(pattern1Dn)
{
double top,bottom;
detectRangeOfZone(top,bottom,-1);
drawRectangle(-1,top,bottom);
PlacePendingOrder(-1,top,bottom);
}
}
int PlacePendingOrder(const EnmDir dir,const double oop,const double suggestedSl)
{
const double lot=0.10; //FOR EXAMPLE, PUT YOUR DATA HERE
const string comment="example for SOF";
const int magicNumber=123456789;
int cmd=dir>0 ? OP_BUY : OP_SELL;
double price=(dir>0 ? Ask : Bid), spread=(Ask-Bid);
if(dir*(oop-price)>spread)
cmd+=(OP_BUYSTOP-OP_BUY);
else if(dir*(price-oop)>spread)
cmd+=(OP_BUYLIMIT-OP_BUY);
int attempt=0, ATTEMPTS=5, SLEEP=25, SLIPPAGE=10, result=-1, error=-1;
while(attempt<ATTEMPTS)
{
attempt++;
RefreshRates();
if(cmd<=OP_SELL)
{
price=dir>0 ? Ask : Bid;
result=OrderSend(_Symbol,cmd,lot,price,SLIPPAGE,0,0,comment,magicNumber);
}
else
{
result=OrderSend(_Symbol,cmd,lot,oop,SLIPPAGE,0,0,comment,magicNumber);
}
if(result>0)
break;
error=_LastError;
Sleep(SLEEP);
}
if(result>0)
{
if(OrderSelect(result,SELECT_BY_TICKET))
{
price=OrderOpenPrice();
if(!OrderModify(result,price,suggestedSl,0,OrderExpiration()))
printf("%i %s: failed to modify %d. error=%d",__LINE__,__FILE__,result,_LastError);
//tp is zero, sl is suggested SL, put yours when needed
}
return result;
}
printf("%i %s: failed to place %s at %.5f. error=%d",__LINE__,__FILE__,EnumToString((ENUM_ORDER_TYPE)cmd),(cmd>OP_SELL ? oop : price),error);
return -1;
}
我迟到了 :'( 工作让我远离 Whosebug :'( 无论如何,我无法根据赏金提供完整的程序,也无法提供完整的代码来解决这个需求区问题。
不过,我还是想提供一些"starting ground"给大家在MT4(MQL4)上构建自己的模式识别器。
为了使文字简短,我录制了一个 YouTube 视频来描述它。
https://youtu.be/WSiyY52QyBI
无论如何,这里是代码:
//+------------------------------------------------------------------+
//| SO56854700.mq4 |
//| Copyright 2019, Joseph Lee, TELEGRAM JosephLee74 |
//| https://whosebug.com/users/1245195/joseph-lee |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Joseph Lee, TELEGRAM JosephLee74"
#property link "https://whosebug.com/users/1245195/joseph-lee"
#property version "1.00"
#property strict
#include <clsBar.mqh>
#include <stderror.mqh>
#include <stdlib.mqh>
//==========================================================================
//-------------------------------------------------------------------
// System Variables
//-------------------------------------------------------------------
double viPipsToPrice = 0.0001;
double viPipsToPoint = 1;
string vsDisplay = "";
//-------------------------------------------------------------------
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit() {
ObjectsDeleteAll(); Comment("");
// Caclulate PipsToPrice & PipsToPoints (old sytle, but works)
if((Digits == 2) || (Digits == 3)) {viPipsToPrice=0.01;}
if((Digits == 3) || (Digits == 5)) {viPipsToPoint=10;}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
ObjectsDeleteAll();
return(0);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick() {
if(!isNewBar())
return;
clsCandlestickPattern voPatterns[];
clsPatternRecognizer voRec(Symbol(), true), PERIOD_CURRENT, 0);
voRec.sbRecognizePatterns(voPatterns);
for(...
Display the content with Comment() ...
}
//+------------------------------------------------------------------+
更重要的是clsBar.mqh。请注意,这是一个 "include file",应该位于 include 文件夹中。包含文件帮助我们使我们的程序不那么混乱,并帮助我们能够编写 re-usable 代码。写OOP的时候很有用 类.
clsBar.mqh: 下载 (OneDrive) https://1drv.ms/u/s!AoLFy6fRYNsvjTU-xSzAADCwGjPQ
很遗憾,文件太大,无法包含在此 post 中。所以我必须把它上传到 OneDrive。
我是编写 mql4
代码的新手,如果我能在出现以下烛台模式时获得绘制矩形的帮助,我将不胜感激:
图1:
运行 代码片段
<blockquote class="imgur-embed-pub" lang="en" data-id="a/fRoPzsm"><a href="//imgur.com/a/fRoPzsm">Demand Zone 1</a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>
图2:
运行 代码片段
<blockquote class="imgur-embed-pub" lang="en" data-id="a/4E8KE1R" data-context="false"><a href="//imgur.com/a/4E8KE1R">Demand Zone 2</a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>
和
图3:
运行 代码片段
<blockquote class="imgur-embed-pub" lang="en" data-id="a/h6D6o6R"><a href="//imgur.com/a/h6D6o6R">Hidden Demand Zone</a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>
和各自的补给区
并以指定的止损点和获利点打开挂单。
请原谅我没有直接包含图像。我没有足够的赞成票来做到这一点。
以下是对链接图像中烛台形态的解释:
需求区
一般 candlestick pattern
(需求区)出现在至少两个或多个连续的看涨蜡烛(最后一个看涨蜡烛的高点是时间段的高点)之后是一个或多个看跌蜡烛,其高点和低点均低于最后一根看涨蜡烛。最后是形成新高的看涨蜡烛。作为需求区的矩形区域是从开盘价到最后一根看跌蜡烛的低价。
隐藏需求区
当一系列连续的看涨蜡烛中有一根蜡烛的低点低于前一根蜡烛并且其高点与其收盘价重合时,则隐藏需求区从看涨蜡烛的低点到开盘价。
需求区和供应区都有完整的解释here。
我知道 bullish
和 bearish
蜡烛可以由
if ( ( Open[1] - Close[1] ) > 0)
{
// candle is bearish
}
else
{
// candle is bullish
}
非常感谢您的帮助。
似乎这些模式没有完整描述,因此无法正确编码。好的,让我们尝试模式#1。
pattern使用的条件(从图片上看是合理的):
1. 在新柱 (bar#0) 的开始处检查。
2. bar 1(如果我们将 0 计算为当前,则为 MQL4 中的 bar#3)必须看涨。
3. bar 2(bar#2) 看跌。 (或者在模式#2 的情况下有 N 根柱,N 可以是 2 根或更多)
4. bar 3(MT4 中的 bar#1)看涨。
5. 它的高=收盘。
6. 柱线#3 的最高价>最高价。
enum EnmDir
{
LONG = 1,
SHORT=-1,
NONE = 0,
};
int getCandleDirection(const int shift)
{
const double open=iOpen(_Symbol,0,shift), close=iClose(_Symbol,0,shift);
if(close-open>_Point/2.)
return LONG; //bullish
if(open-close>_Point/2.)
return SHORT; //bearish
return NONE; //doji
}
bool isPattern1Detected(const EnmDir dir)
{
if(dir==0)return(false);
if(getCandleDirection(3)!=dir)
return false; //rule#2
if(getCandleDirection(2)+dir!=0)
return false; //rule#3
if(getCandleDirection(1)!=dir)
return false; //rule#4
if(dir>0)
{
if(iHigh(_Symbol,0,1)-iClose(_Symbol,0,1)>_Point/2.)
return false; //rule#5 for long
if(iHigh(_Symbol,0,1)-iHigh(_Symbol,0,3)>_Point/2.)
return true; //rule#6 for long
return false; //if rule#6 is not hold
}
else
{
if(iClose(_Symbol,0,1)-iLow(_Symbol,0,1)>_Point/2.)
return false; //rule#5 for short
if(iLow(_Symbol,0,3)-iLow(_Symbol,0,1)>_Point/2.)
return true; //rule#6 for short
return false; //if rule#6 is not hold
}
}
bool isPattern2Detected(const EnmDir dir,const int numCandlesAgainst=1)
{
if(dir==NONE)return(false);
if(getCandleDirection(1)!=dir)
return false; //rule#4
for(int i=1;i<=numCandlesAgainst;i++)
{
if(getCandleDirection(1+i)!=dir)
return(false); //rule#3 - checking that all numCandlesAgainst must be bearish
}
if(getCandleDirection(2+numCandlesAgainst)!=dir)
return false; //rule#2
if(dir>0)
{
if(iHigh(_Symbol,0,1)-iClose(_Symbol,0,1)>_Point/2.)
return false; //rule#5 for long
if(iHigh(_Symbol,0,1)-iHigh(_Symbol,0,2+numCandlesAgainst)>_Point/2.)
return true; //rule#6 for long
return false; //if rule#6 is not hold
}
else
{
if(iClose(_Symbol,0,1)-iLow(_Symbol,0,1)>_Point/2.)
return false; //rule#5 for short
if(iLow(_Symbol,0,2+numCandlesAgainst)-iLow(_Symbol,0,1)>_Point/2.)
return true; //rule#6 for short
return false; //if rule#6 is not hold
}
}
您还需要什么?要检测矩形的 HL?很简单,规则很明确。让我们假设它们是:对于 LONG,向上 = bar#2 的开盘价,向下 = 该柱的最低价。然后,
void detectRangeOfZone(double &top,double &bottom,const EnmDir dir)
{
if(dir>0)
{
top=iOpen(_Symbol,0,2);
bottom=iLow(_Symbol,0,2);
}
else if(dir<0)
{
top=iClose(_Symbol,0,2);
bottom=iHigh(_Symbol,0,2);
}
}
需要画矩形吗?好的,但是您如何决定停止绘图的时间?让我们假设右边的 N 个柱就足够了,让我们暂时忽略周末(如果在市场关闭时记住周末,那就更复杂一些)。
bool drawRectangle(const int dir,const double top,const double bottom)
{
const datetime starts=iTime(_Symbol,0,2), ends=starts+PeriodSeconds()*N_bars;//time of start and end of the rectangle
const string name=prefix+"_"+(dir>0?"DEMAND":"SUPPLY")+"_"+TimeToString(starts);//name would be unique sinse we use time of start of the range. DO NOT FORGET about prefix - it should be declared globally, you would be able to delete all the objects with 'ObjectsDeleteAll()' function that accepts prefix in one of its implementations.
if(!ObjectCreate(0,name,OBJ_RECTANGLE,0,0,0,0,0))
{
printf("%i %s: failed to create %s. error=%d",__LINE__,__FILE__,name,_LastError);
return false;
}
ObjectSetInteger(0,name,OBJPROP_TIME1,starts);
ObjectSetInteger(0,name,OBJPROP_TIME2,ends);
ObjectSetDouble(0,name,OBJPROP_PRICE1,top);
ObjectSetDouble(0,name,OBJPROP_PRICE2,bottom);
//add color, width, filling color, access modifiers etc, example is here https://docs.mql4.com/ru/constants/objectconstants/enum_object/obj_rectangle
return true;
}
这里是主要块,不要忘记添加一个新的柱检查,否则该工具会在每个订单号上检查对象,这是浪费时间。 字符串前缀=""; //为所有对象添加一些唯一的前缀 const int N_bars = 15; // 本例中为 15 根柱
void OnDeinit(const int reason){ObjectsDeleteAll(0,prefix);}
void OnTick()
{
if(!isNewBar())
return; //not necessary but waste of time to check every second
const bool pattern1Up=isPattern1Detected(1), pattern1Dn=isPattern1Detected(-1);
if(pattern1Up)
{
double top,bottom;
detectRangeOfZone(top,bottom,1);
drawRectangle(1,top,bottom);
PlacePendingOrder(1,top,bottom);
}
if(pattern1Dn)
{
double top,bottom;
detectRangeOfZone(top,bottom,-1);
drawRectangle(-1,top,bottom);
PlacePendingOrder(-1,top,bottom);
}
}
int PlacePendingOrder(const EnmDir dir,const double oop,const double suggestedSl)
{
const double lot=0.10; //FOR EXAMPLE, PUT YOUR DATA HERE
const string comment="example for SOF";
const int magicNumber=123456789;
int cmd=dir>0 ? OP_BUY : OP_SELL;
double price=(dir>0 ? Ask : Bid), spread=(Ask-Bid);
if(dir*(oop-price)>spread)
cmd+=(OP_BUYSTOP-OP_BUY);
else if(dir*(price-oop)>spread)
cmd+=(OP_BUYLIMIT-OP_BUY);
int attempt=0, ATTEMPTS=5, SLEEP=25, SLIPPAGE=10, result=-1, error=-1;
while(attempt<ATTEMPTS)
{
attempt++;
RefreshRates();
if(cmd<=OP_SELL)
{
price=dir>0 ? Ask : Bid;
result=OrderSend(_Symbol,cmd,lot,price,SLIPPAGE,0,0,comment,magicNumber);
}
else
{
result=OrderSend(_Symbol,cmd,lot,oop,SLIPPAGE,0,0,comment,magicNumber);
}
if(result>0)
break;
error=_LastError;
Sleep(SLEEP);
}
if(result>0)
{
if(OrderSelect(result,SELECT_BY_TICKET))
{
price=OrderOpenPrice();
if(!OrderModify(result,price,suggestedSl,0,OrderExpiration()))
printf("%i %s: failed to modify %d. error=%d",__LINE__,__FILE__,result,_LastError);
//tp is zero, sl is suggested SL, put yours when needed
}
return result;
}
printf("%i %s: failed to place %s at %.5f. error=%d",__LINE__,__FILE__,EnumToString((ENUM_ORDER_TYPE)cmd),(cmd>OP_SELL ? oop : price),error);
return -1;
}
我迟到了 :'( 工作让我远离 Whosebug :'( 无论如何,我无法根据赏金提供完整的程序,也无法提供完整的代码来解决这个需求区问题。
不过,我还是想提供一些"starting ground"给大家在MT4(MQL4)上构建自己的模式识别器。
为了使文字简短,我录制了一个 YouTube 视频来描述它。 https://youtu.be/WSiyY52QyBI
无论如何,这里是代码:
//+------------------------------------------------------------------+
//| SO56854700.mq4 |
//| Copyright 2019, Joseph Lee, TELEGRAM JosephLee74 |
//| https://whosebug.com/users/1245195/joseph-lee |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Joseph Lee, TELEGRAM JosephLee74"
#property link "https://whosebug.com/users/1245195/joseph-lee"
#property version "1.00"
#property strict
#include <clsBar.mqh>
#include <stderror.mqh>
#include <stdlib.mqh>
//==========================================================================
//-------------------------------------------------------------------
// System Variables
//-------------------------------------------------------------------
double viPipsToPrice = 0.0001;
double viPipsToPoint = 1;
string vsDisplay = "";
//-------------------------------------------------------------------
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit() {
ObjectsDeleteAll(); Comment("");
// Caclulate PipsToPrice & PipsToPoints (old sytle, but works)
if((Digits == 2) || (Digits == 3)) {viPipsToPrice=0.01;}
if((Digits == 3) || (Digits == 5)) {viPipsToPoint=10;}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
ObjectsDeleteAll();
return(0);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick() {
if(!isNewBar())
return;
clsCandlestickPattern voPatterns[];
clsPatternRecognizer voRec(Symbol(), true), PERIOD_CURRENT, 0);
voRec.sbRecognizePatterns(voPatterns);
for(...
Display the content with Comment() ...
}
//+------------------------------------------------------------------+
更重要的是clsBar.mqh。请注意,这是一个 "include file",应该位于 include 文件夹中。包含文件帮助我们使我们的程序不那么混乱,并帮助我们能够编写 re-usable 代码。写OOP的时候很有用 类.
clsBar.mqh: 下载 (OneDrive) https://1drv.ms/u/s!AoLFy6fRYNsvjTU-xSzAADCwGjPQ
很遗憾,文件太大,无法包含在此 post 中。所以我必须把它上传到 OneDrive。