// マイライブラリー
#include <MyLib.mqh>
// マジックナンバー
#define MAGIC 20094060
#define COMMENT "平均足フィルター+RSI"
// 外部パラメータ
extern double Lots=0.1;
extern int Slippage=3;
extern int SLpips= 20; // 損切り値幅(pips)
extern int TPpips= 40; // 利食い値幅(pips)
extern int IsUseExit=true;
datetime LatestTime;
extern ENUM_APPLIED_PRICE applied_price=PRICE_CLOSE;
extern int RSIPeriod=7; // RSIの期間
extern int HighRSI = 80;
extern int LowRSI = 20;
extern int HighRSIExit = 70;
extern int LowRSIExit = 30;
extern int ReverseRSI = 5;
extern int MAPeriod = 21;
extern ENUM_APPLIED_PRICE ma_applied_price=PRICE_OPEN;
extern ENUM_MA_METHOD ma_method=MODE_SMA;
extern double MinSlope=10; // 傾き最小値
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int EntrySignal(int magic)
{
// オープンポジションの計算
double pos=MyCurrentOrders(MY_OPENPOS,magic);
bool isPositiveBar_H1=IsPositiveBar(PERIOD_H1);
bool isPositiveBar_D1=IsPositiveBar(PERIOD_D1);
double MA_0=iMA(_Symbol,PERIOD_H1,MAPeriod,0,ma_method,ma_applied_price,0);
double MA_1=iMA(_Symbol,PERIOD_H1,MAPeriod,0,ma_method,ma_applied_price,1);
double MA_slope=(MA_0-MA_1)/Point;
bool isMASlopeUp=MA_slope>MinSlope;
bool isMASlopeDown=MA_slope<-MinSlope;
bool isUpState=isMASlopeUp;//isPositiveBar_H1 && isPositiveBar_D1;
bool isDownState=isMASlopeDown;//!isPositiveBar_H1 && !isPositiveBar_D1;
double rsi_0=iRSI(NULL,0,RSIPeriod,applied_price,0);
double isHighRSI=GetHighRSI()>HighRSI && rsi_0>HighRSIExit;
double isReverseHighRSI=rsi_0<GetHighRSI()-ReverseRSI;
double isLowRSI=GetLowRSI()<LowRSI && rsi_0<LowRSIExit;
double isReverseLowRSI=rsi_0>GetLowRSI()+ReverseRSI;
bool buySignal=isUpState && isLowRSI && isReverseLowRSI;
bool sellSignal=isDownState && isHighRSI &&isReverseHighRSI;
int ret=0;
// 買いシグナル
if(pos<=0 && buySignal)
ret=1;
// 売りシグナル
if(pos>=0 && sellSignal)
ret=-1;
return(ret);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void ExitPosition(int magic)
{
// オープンポジションの計算
double pos=MyCurrentOrders(MY_OPENPOS,magic);
double rsi_0=iRSI(NULL,0,RSIPeriod,applied_price,0);
double isHighRSIExit=rsi_0>HighRSI;
double isLowRSIExit=rsi_0<LowRSI;
bool buyExitSignal=isHighRSIExit;
bool sellExitSignal=isLowRSIExit;
int ret=0;
//if(pos < 0 && 売りポジションの決済シグナル) ret = 1;
if(pos<0 && sellExitSignal)
ret=1;
//if(pos > 0 && 買いポジションの決済シグナル) ret = -1;
if(pos>0 && buyExitSignal)
ret=-1;
// オープンポジションの決済
if(ret!=0)
MyOrderClose(Slippage,magic);
}
// スタート関数
int start()
{
/*
if(Time[0] != LatestTime)
{
LatestTime = Time[0];
}
else
{
return(0);
}
*/
if(IsUseExit)
{
ExitPosition(MAGIC);
}
// エントリーシグナル
int sig_entry=EntrySignal(MAGIC);
// 買い注文
if(sig_entry>0)
{
MyOrderClose(Slippage,MAGIC);
MyOrderSendSL(OP_BUY,Lots,Ask,Slippage,SLpips,TPpips,COMMENT,MAGIC);
}
// 売り注文
if(sig_entry<0)
{
MyOrderClose(Slippage,MAGIC);
MyOrderSendSL(OP_SELL,Lots,Bid,Slippage,SLpips,TPpips,COMMENT,MAGIC);
}
return(0);
}
//+------------------------------------------------------------------+
bool MyOrderSendSL(int type,double lots,double price,int slippage,int slpips,int tppips,string comment,int magic)
{
int mult=1;
if(Digits == 3 || Digits == 5)
mult=10;
slippage *= mult;
if(type==OP_SELL || type==OP_SELLLIMIT || type==OP_SELLSTOP)
mult*=-1;
double sl=0,tp=0;
if(slpips > 0)
sl = price-slpips*Point*mult;
if(tppips > 0)
tp = price+tppips*Point*mult;
return(MyOrderSend(type, lots, price, slippage, sl, tp, comment, magic));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetHighRSI()
{
int maxRSI=0;
int lastUpdateCount=0;
int LimitNotUpdateCount=2;
for(int i=0; i<Bars; i++)
{
double rsi_i=iRSI(NULL,0,RSIPeriod,PRICE_HIGH,i);
if(rsi_i>maxRSI)
{
maxRSI=rsi_i;
lastUpdateCount=0;
}
else
{
lastUpdateCount++;
}
if(lastUpdateCount>LimitNotUpdateCount)
{
break;
}
}
return maxRSI;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetLowRSI()
{
int minRSI=100;
int lastUpdateCount=0;
int LimitNotUpdateCount=2;
for(int i=0; i<Bars; i++)
{
double rsi_i=iRSI(NULL,0,RSIPeriod,PRICE_LOW,i);
if(rsi_i<minRSI)
{
minRSI=rsi_i;
lastUpdateCount=0;
}
else
{
lastUpdateCount++;
}
if(lastUpdateCount>LimitNotUpdateCount)
{
break;
}
}
return minRSI;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsPositiveBar(ENUM_TIMEFRAMES timeframe)
{
double heikinAshi_Open = iCustom(NULL, timeframe, "HeikinAshi", 0, 0);
double heikinAshi_Close = iCustom(NULL, timeframe, "HeikinAshi", 1, 0);
return heikinAshi_Close>heikinAshi_Open;
}
//+------------------------------------------------------------------+
コメント