下载MetaTrader 5

专家: Modified Indicator KDJ and KDJ Automated Trading System

要添加评论,请登录注册
MetaQuotes Software Corp.
版主
182652
MetaQuotes Software Corp.  

Modified Indicator KDJ and KDJ Automated Trading System:

Indicator KDJ deactivates short-term fluctuations. It carries on a parameter to adjust and increase two new RSV index lines: close price of short-term volatility KDC: K-D difference, to determine the change of price trends and to open positions.

Author: yyy999

MQL4 Comments
16319
MQL4 Comments  

经测试无太大的实用价值

MQL4 Comments
16319
MQL4 Comments  
5125839:

经测试无太大的实用价值

您是怎么测试的,我发现代码里有很多不对。
MQL4 Comments
16319
MQL4 Comments  

2011年05月22日重新调试

//+------------------------------------------------------------------+
//| AutoKdj.mq4 |
//| senlin ge |
//| https://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "senlin ge eMail:806935610@qq.com"
#property link "https://www.mql5.com/go?http://new.qzone.qq.com/806935610"
#define MAGICKDJ 20080220
//---- input parameters
extern int whichmethod = 1;//1;//4; //1: no S/L,no T/P 不设止赢也不设止损
//2: no S/L,has T/P 设止赢,但不设止损
//3: has S/L,no T/P //不设止赢,设止损
//4: has T/P has S/L,设止赢也设止损

extern double Lots=0.01; //最低标准手
extern double MaximumRisk=0.1; //开仓占可用资金比例
extern double DecreaseFactor=0.3; //开仓亏损减少下单手数
extern int tp=20; //最大赢利 止赢点数
extern int sl=50; //最大损失 止损点数
extern int Leverage=100; //交易倍数1:100
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

//---- check for history and trading
if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//----


//----
return(0);
}
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICKDJ)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//---- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{

int res;
double point =MarketInfo(Symbol(),MODE_POINT);

//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;

//---- get Moving Average
double valKDCCurrent=iCustom(NULL, 0, "kdj",5,0); //当前一周期的KDC值
double valKDCPrevious=iCustom(NULL, 0, "kdj",5,1); //前一周期的KDC值
double valKCurrent=iCustom(NULL, 0, "kdj",2,0); //当前周期的K值
double valKPrevious=iCustom(NULL, 0, "kdj",2,1); //前一周期的K值
double valDCurrent=iCustom(NULL, 0, "kdj",3,0); //当前周期的D值
double valDPrevious=iCustom(NULL, 0, "kdj",3,1); //前一周期的D值
Print("valKCurrent=",valKCurrent);
Print("valDCurrent=",valDCurrent);
Print("valKPrevious=",valKPrevious);
Print("valDPrevious=",valDPrevious);
//---- sell conditions
if(valKDCPrevious>0 && valKDCCurrent<0|| (valKDCCurrent<0 && valKPrevious-valKCurrent>0)) //k下穿D K<d do short
{
switch (whichmethod)
{
case 1: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"开空仓",MAGICKDJ,0,Red); break;
case 2: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+sl*Point,0,"开空仓",MAGICKDJ,0,Red); break;
case 3: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,Bid-tp*Point,"开空仓",MAGICKDJ,0,Red); break;
case 4: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,0,Bid+sl*Point,Bid-tp*Point,"开空仓",MAGICKDJ,0,Red); break;
default : res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICKDJ,0,Red); break;
}
if (res <=0)
{
int error=GetLastError();
if(error==134) Print("Received 134 Error after OrderSend() !! "); // not enough money
if(error==135) RefreshRates(); // prices have changed
if(error==131) Print("Received 131 Error after OrderSend() !! "); // not enough money

}
Sleep(5000);
return;
}
//---- buy conditions
if(valKDCPrevious<0 && valKDCCurrent>0|| valKDCCurrent>0&& valKPrevious-valKCurrent<0)//k上穿D k>d 做多
{
switch (whichmethod)
{
case 1: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"开多仓",MAGICKDJ,0,Blue);break;
case 2: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-sl*Point,0,"开多仓",MAGICKDJ,0,Blue); break;
case 3: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,Ask+tp*Point,"开多仓",MAGICKDJ,0,Blue);break;
case 4: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,0,Ask-sl*Point,Ask+tp*Point,"开多仓",MAGICKDJ,0,Blue);break;
default : res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"开多仓",MAGICKDJ,0,Blue);break;
}

if (res <=0)
{
error=GetLastError();
if(error==134) Print("Received 134 Error after OrderSend() !! "); // not enough money
if(error==135) RefreshRates(); // prices have changed
}
Sleep(5000);
return;
}
//----
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
double ma;
//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//---- get K[1],k[0],d[1],d[0]
double valKDCCurrent=iCustom(NULL, 0, "kdj",5,0); //当前一周期的KDC值
double valKDCPrevious=iCustom(NULL, 0, "kdj",5,1); //前一周期的KDC值
double valKCurrent=iCustom(NULL, 0, "kdj",2,0); //当前周期的K值
double valKPrevious=iCustom(NULL, 0, "kdj",2,1); //前一周期的K值
double valDCurrent=iCustom(NULL, 0, "kdj",3,0); //当前周期的D值
double valDPrevious=iCustom(NULL, 0, "kdj",3,1); //前一周期的D值
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICKDJ || OrderSymbol()!=Symbol()) continue;
//---- check order type
if(OrderType()==OP_BUY) //对多方盘平仓
{
if(valKDCPrevious>32 && valKDCCurrent<0|| valKPrevious>valKCurrent) //k下穿D 或者是K 在D下下行 K<d do short
OrderClose(OrderTicket(),OrderLots(),Bid,3,White); Sleep(5000);
break;
}
if(OrderType()==OP_SELL) //对空方盘平仓
{
if(valKDCPrevious<32 && valKDCCurrent>0||valKPrevious<valKCurrent)//k上穿D or k>d 做多
OrderClose(OrderTicket(),OrderLots(),Ask,3,White); Sleep(5000);
break;
}
}
//----
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=Lots;
int orders=HistoryTotal(); // history orders total
int losses=0; // number of losses orders without a break
//---- select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk*Leverage/100000.0,1);
//---- calcuulate number of losses orders without a break
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
{ Print("Error in history!"); break; }
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
//----
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;
}
if(losses>1) lot=NormalizeDouble(lot-lot*losses*DecreaseFactor,1);
}
//---- return lot size
if(lot<0.01) lot=0.01;
return(lot);
}
//+------------------------------------------------------------------+

指标代码

//+------------------------------------------------------------------+
//| KDJ.mq4 |
//| senlin ge |
//| https://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "senlin ge eMail:806935610@qq.com"
#property link "https://www.mql5.com/go?http://new.qzone.qq.com/806935610"
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 8

#property indicator_color1 LightSlateGray
#property indicator_color2 0x00ff00
#property indicator_color3 Red
#property indicator_color4 Yellow
#property indicator_color5 0Xffccff
#property indicator_color6 MediumSlateBlue

//---- input parameters
extern int M1=5;
extern int M2=50;
extern int KdjPeriod=131;
//---- buffers
double RsvBuffer[],RSV[],K[],D[],J[],KDC[];
double MaxHigh=0,MinLow=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- name for DataWindow and indicator subwindow label
string short_name;
short_name="KDJ("+KdjPeriod+","+M1+","+M2+") "+"Author:Senlin Ge";
IndicatorShortName(short_name);
SetIndexLabel(0,NULL);
SetIndexLabel(1,"RSV:");
SetIndexLabel(2,"K");
SetIndexLabel(3,"D:");
SetIndexLabel(4,"J");
SetIndexLabel(5,"KDC");
//----
//---- indicators
//---- drawing settings
IndicatorDigits(Digits-2); //set小数精度两位
//SetIndexStyle(0,DRAW_NONE);
//SetIndexLabel(0,NULL);
SetIndexShift(0,100);
SetIndexStyle(0,DRAW_LINE,2,0);
SetIndexBuffer(0,RsvBuffer);
//----
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,RSV);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,K);
SetIndexStyle(3,DRAW_LINE);
SetIndexBuffer(3,D);
SetIndexStyle(4,DRAW_LINE);
SetIndexBuffer(4,J);
SetIndexStyle(5,DRAW_HISTOGRAM);
SetIndexBuffer(5,KDC);
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
int i;
if(Bars<=KdjPeriod) return(0);
//----
i=Bars-counted_bars-1;
while(i>=0)
{
MaxHigh=High[iHighest(NULL,0,MODE_HIGH,KdjPeriod,i)];
MinLow=Low[iLowest(NULL,0,MODE_LOW,KdjPeriod,i)];
RSV[i]=(Close[i]-MinLow)/(MaxHigh-MinLow)*100;
if(i==2)
RsvBuffer[i]=50.0;
i--;
}
Ksma(RSV,M1,2); //get K value
Dsma(K,M2,3); //get D value
//---- //
if(counted_bars>0) counted_bars--;
for(i=Bars-counted_bars-1;i>=0;i--)
{
RsvBuffer[i]=50.0;
J[i]=3*K[i]-2*D[i];
if(J[i]<0) J[i]=0;
if(J[i]>100) J[i]=100;
KDC[i]=K[i]-D[i];
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| Simple Moving Average |
//+------------------------------------------------------------------+
double Dsma(double ArrPara[],int MA_Period,int flag)
{
double sum=0;
int ExtCountedBars=IndicatorCounted();
int i,pos=Bars-IndicatorCounted()-1;
//---- initial accumulation
if(pos<MA_Period) pos=MA_Period;
for(i=1;i<MA_Period;i++,pos--)
sum+=K[pos];
//---- main calculation loop
while(pos>=0)
{
sum+=K[pos]; //加上最新的K值
switch(flag)
{
case 3: D[pos]=sum/MA_Period;break;
}
sum-=K[pos+MA_Period-1];
pos--;
}
}
//+------------------------------------------------------------------+
//| Simple Moving Average |
//+------------------------------------------------------------------+
double Ksma(double ArrPara[],int MA_Period,int flag)
{
double sum=0;
int ExtCountedBars=IndicatorCounted();
int i,pos=Bars-IndicatorCounted()-1;
//---- initial accumulation
if(pos<MA_Period) pos=MA_Period;
for(i=1;i<MA_Period;i++,pos--)
sum+=RSV[pos];
//---- main calculation loop
while(pos>=0)
{
sum+=RSV[pos]; //加上最新的RSV[]的值
switch(flag)
{
case 2: K[pos]=sum/MA_Period;break;
}
sum-=RSV[pos+MA_Period-1];
pos--;
}
}
//+------------------------------------------------------------------+

林焕才2011522日阅,谢谢提供,本人试用中交流学习 QQ:561212567 EM:561212567@QQ.COM

要添加评论,请登录注册