为稳定盈利 提供动力

这里是我的工作总结和我感性上的碎碎念 3BFund Quant leader CFA 从业6Y Email: wongmanc@88.com

一种基于MQL4语言环境 非常成熟的CTA趋势&震荡资金管理类策略(源码)


string EA_Name = “CTA Momentum Trader PRO”;
enum 策略开关
{
开=0,
关=1,
};
//================================================
//Ea 权限
input int Key_a = 0; //请输入授权密码
input int ALL_Magic = 1000; //订单识别码
//================================================
//说明1 AA组-RSI
input string 说明1=”(趋势器)AA组开仓参数设置”;
input 策略开关 AA_Switch = 0;
input int AA_Ma = 7;
input ENUM_TIMEFRAMES AA_Time = PERIOD_H1;
input ENUM_APPLIED_PRICE AA_Price=PRICE_CLOSE;
input double AA_Uplimit = 70.0;
input double AA_Downlimit = 30.0;
//================================================
//说明2 BB组-CCI
input string 说明2=”(震荡器)BB组参数设置”;
input 策略开关 BB_Switch = 0;
input int BB_Ma = 14;
input ENUM_TIMEFRAMES BB_Time = PERIOD_H1;
input ENUM_APPLIED_PRICE BB_Price=PRICE_CLOSE;
input double BB_Uplimit = -100.0;
input double BB_Downlimit = 100.0;
//================================================
//说明3 CC组-MA
input string 说明3=”均线斜率参数设置”;
input 策略开关 CC_Switch = 0;
input int CC_Ma = 45;
input ENUM_TIMEFRAMES CC_Time = PERIOD_H1;
input ENUM_APPLIED_PRICE CC_Price = PRICE_WEIGHTED;
input ENUM_MA_METHOD CC_Mamode = MODE_EMA;
input int CC_Macount = 12;
input double CC_UplimitMin = 10.0;
input double CC_UplimitMax = 80.0;
input double CC_DownlimitMin = -10.0;
input double CC_DownlimitMax = -80.0;
//================================================
//说明4 α组-Demarker
input string 说明4=”趋势与震荡判定器参数设置α”;
input 策略开关 DD_Switch = 0;
input int DD_Ma = 72;
input ENUM_TIMEFRAMES DD_Time = PERIOD_H1;
input double DD_Uplimit = 0.5;
input double DD_Downlimit = 0.4;
//================================================
//说明5 β组-RSI
input string 说明5=”趋势与震荡判定器参数设置β”;
input 策略开关 EE_Switch = 0;
input int EE_Ma = 60;
input ENUM_TIMEFRAMES EE_Time = PERIOD_H1;
input ENUM_APPLIED_PRICE EE_Price=PRICE_CLOSE;
input double EE_Uplimit = 55;
input double EE_Downlimit = 45;
//================================================
//说明6 策略组资金管理模块
input string 说明6=”策略组资金管理模块”;
input 策略开关 FF_Buy = 0; //多单开关
input 策略开关 FF_Sell = 0; //空单开关
input double FF_Lots = 0.02;
input double FF_Addlevel = 1.55;
input double FF_Maxlots = 1.6;
input int FF_Addmaxcountclose = 4;
input int FF_Lossmaxcount = 6;
input int Init_Hour = 0;
input double FF_减仓比例 = 10.0;//FF_Maxlots-%lost/
input double FF_TP = 70.0;
input double FF_SL = 70.0;
input double FF_MinAccountEquity = 5000.0;
//================================================
datetime Time_Star;
double Price_a = 30.0;
datetime Time_0;
//================================================
int truekey_a = 624648;
datetime stratdate=D’2010.01.01′;
datetime enddate =D’2999.09.01′;
//================================================
int OnInit()
{
return(INIT_SUCCEEDED);
Time_Star = TimeCurrent();

}
//+——————————————————————+
//| Expert deinitialization function |
//+——————————————————————+
void OnDeinit(const int reason)
{
//—
//ObjectsDeleteAll();
}
//+——————————————————————+
//| Expert tick function |
//+——————————————————————+
void OnTick()
{

HideTestIndicators(true);
RefreshRates() ;
Str_taday = TimeToStr(TimeCurrent(), TIME_DATE);
Time_taday = StrToTime(Str_taday);
if(TradeKey())
{
if(His_Losscount<FF_Lossmaxcount)
Open_Order();
comment();
}
}
//+——————————————————————+
//| Key
//+——————————————————————+
bool TradeKey()
{
bool li_0;
if(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))>stratdate&&StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))<enddate
&& Key_a == truekey_a
&& AccountEquity() > FF_MinAccountEquity
//&& Num == trueNum
)
li_0=true;
else
li_0=false;
return(li_0);
}
//+——————————————————————+
//|开仓指标-AA/RSI
//+——————————————————————+
double fAA_RSI(int i)
{
double li_1 = iRSI(Symbol(),AA_Time,AA_Ma,AA_Price,i);
return (li_1);
}
//+——————————————————————+
//| |
//+——————————————————————+
string fAA_RSI_Signal()
{
double do_1 = fAA_RSI(1);
double do_2 = fAA_RSI(2);
string str_0 = “NA”;
if(AA_Switch==0)
{
if(do_1 > AA_Uplimit && do_2 < AA_Uplimit)
{
str_0 = “Up”;
}
if(do_1 < AA_Downlimit && do_2 > AA_Downlimit)
{
str_0 = “Down”;
}
}
else
str_0 = “Yes”;
return(str_0);
}
//+——————————————————————+
//|开仓指标-BB/CCI
//+——————————————————————+
double fBB_CCI(int i)
{
double li_1 = iCCI(Symbol(),BB_Time,BB_Ma,BB_Price,i);
return (li_1);
}
//+——————————————————————+
//| |
//+——————————————————————+
string fBB_CCI_Signal()
{
double do_1 = fBB_CCI(1);
double do_2 = fBB_CCI(2);
string str_0 = “NA”;
if(BB_Switch==0)
{
if(do_1 < BB_Uplimit && do_2 > BB_Uplimit)
{
str_0 = “Up”;
}
if(do_1 > BB_Downlimit && do_2 < BB_Downlimit)
{
str_0 = “Down”;
}
}
else
str_0 = “Yes”;
return(str_0);
}
//+——————————————————————+
//|开仓指标-CC/MA
//+——————————————————————+
double fCC_Ma()
{
double li_1=0;
double li_2=0;
double Angle=0;
int CurShift=1,PreShift=CC_Macount;
li_1 = NormalizeDouble(iMA(Symbol(),CC_Time,CC_Ma,0,CC_Mamode,CC_Price,CurShift),Digits);
li_2 = NormalizeDouble(iMA(Symbol(),CC_Time,CC_Ma,0,CC_Mamode,CC_Price,PreShift),Digits);
if(CC_Time>0)
Angle=NormalizeDouble(MathArctan((li_1-li_2)/Point/(CC_Time*8))*180/3.14,2);
return (Angle);
}
//+——————————————————————+
//| |
//+——————————————————————+
string fCC_Ma_Signal()
{
double do_1 = fCC_Ma();
string str_0 = “NA”;
if(CC_Switch==0)
{
if(do_1 > CC_UplimitMin && do_1 < CC_UplimitMax)
{
str_0 = “Up”;
}
if(do_1 < CC_DownlimitMin && do_1 > CC_DownlimitMax)
{
str_0 = “Down”;
}
}
else
str_0 = “Yes”;
return(str_0);
}
//+——————————————————————+
//|开仓指标-DD/Demarker
//+——————————————————————+
double fDD_Demarker(int i)
{
double li_1 = iDeMarker(Symbol(), DD_Time, DD_Ma, i) ;
return (li_1);
}
//+——————————————————————+
//| |
//+——————————————————————+
string fDD_Demarker_Signal()
{
double do_1 = fDD_Demarker(1);
//double do_2 = fDD_Demarker(2);
string str_0 = “NA”;
if(DD_Switch==0)
{
if(do_1 > DD_Downlimit && do_1 < DD_Uplimit)
{
str_0 = “CCI”;
}
else
{
str_0 = “RSI”;
}
}
else
str_0 = “Yes”;
return(str_0);
}
//+——————————————————————+
//| 开仓指标-EE/RSI
//+——————————————————————+
double fEE_RSI(int i)
{
double li_1 = iRSI(Symbol(),EE_Time,EE_Ma,EE_Price,i);
return (li_1);
}
//+——————————————————————+
//|
//+——————————————————————+
string fEE_RSI_Signal()
{
double do_1 = fEE_RSI(1);
//double do_2 = fEE_RSI(2);
string str_0 = “NA”;
if(DD_Switch==0)
{
if(do_1 > EE_Downlimit && do_1 < EE_Uplimit)
{
str_0 = “CCI”;
}
else
{
str_0 = “RSI”;
}
}
else
str_0 = “Yes”;
return(str_0);
}
//+——————————————————————+
//| 多单开仓-子函数
//+——————————————————————+
bool f9_Buy(double lots,int magic)
{
bool li_0=false;
if(FF_Buy == 0)
{
if(Time[0]!=Time_0)
{
string name = “//”;
li_0 = OrderSend(Symbol(),OP_BUY,lots,MarketInfo(Symbol(), MODE_ASK),3,0,0,name,magic,0,Lime);
if(li_0)
{
Alert(“Open Buy Order Lots :”+DoubleToStr(lots,2));
return(li_0);
}
else
return(li_0);
Time_0=Time[0];
}
}
return(li_0);
}
//+——————————————————————+
//| 空单开仓-子函数
//+——————————————————————+
bool f9_Sell(double lots,int magic)
{
bool li_0=false;
if(FF_Sell == 0)
{
if(Time[0]!=Time_0)
{
string name = “//”;
li_0 = OrderSend(Symbol(),OP_SELL,lots,MarketInfo(Symbol(), MODE_BID),3,0,0,name,magic,0,Lime);
if(li_0)
{
Alert(“Open Sell Order Lots:”+DoubleToStr(lots,2));
return(li_0);
}
else
return(li_0);
Time_0=Time[0];
}
}
return(li_0);
}
//+——————————————————————+
//| 平仓模块
//+——————————————————————+
bool f0_ClosePosition(string st_0)
{
bool li_0 = false;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true
&& OrderSymbol() == Symbol()
&& OrderMagicNumber()== ALL_Magic)
{
if(st_0 ==”Buy” && OrderType()==OP_BUY)
{
li_0 = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),30);
if(li_0)
Alert(“Close Buy Order Lots:”+DoubleToStr(OrderLots(),2));
}
if(st_0 ==”Sell” && OrderType()==OP_SELL)
{
li_0 = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),30);
if(li_0)
Alert(“Close Sell Order Lots:”+DoubleToStr(OrderLots(),2));
}
}
}
return(li_0);
}
//+——————————————————————+
//| 多单平仓-子函数
//+——————————————————————+
void f0_Close_Buy()
{
if(f2_count(“Buy”)>0)
f0_ClosePosition(“Buy”);
}
//+——————————————————————+
//| 空单平仓-子函数
//+——————————————————————+
void f0_Close_Sell()
{
if(f2_count(“Sell”)>0)
f0_ClosePosition(“Sell”);
}
//+——————————————————————+
double f1_HistoryData(datetime time,string as_0) //close order data
{
double l_count_8 = 0;
for(int i = 0; i<=OrdersHistoryTotal() ; i++)
{
if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))
if(OrderSymbol() != Symbol() || OrderMagicNumber() != ALL_Magic)
continue;
if(OrderSymbol() == Symbol() && OrderMagicNumber() == ALL_Magic)
{
if(OrderCloseTime()>= time)
{
if(as_0==”Profit”)
l_count_8 = OrderProfit() + OrderSwap() + OrderCommission();
if(as_0==”Lots”)
l_count_8 = OrderLots();
if(as_0==”Average” && OrderLots()>0)
l_count_8 = NormalizeDouble((OrderProfit() + OrderSwap() + OrderCommission())/(OrderLots()),4);
}
}
}
return (l_count_8);
}
//+——————————————————————+
//| |
//+——————————————————————+
int f1_his_count(datetime time,string st_0)
{
int l_count_8 = 0;
for(int i = 0; i<=OrdersHistoryTotal() ; i++)
{
if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))
if(OrderSymbol() != Symbol() || OrderMagicNumber() != ALL_Magic)
continue;
if(OrderSymbol() == Symbol() && OrderMagicNumber() == ALL_Magic)
{
if(OrderCloseTime()>= time)
{
if(st_0 == “All”)
l_count_8++;
if(st_0 == “Buy”)
if(OrderType() == OP_BUY)
l_count_8++;
if(st_0 == “Sell”)
if(OrderType() == OP_SELL)
l_count_8++;
}
}
}
return (l_count_8);
}
//+——————————————————————+
//| 获取单量
//+——————————————————————+
int f2_count(string st_0)
{
int l_count_8 = 0;
for(int i = OrdersTotal() – 1; i >= 0; i–)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
if(OrderSymbol() != Symbol() || OrderMagicNumber() != ALL_Magic)
continue;
if(OrderSymbol() == Symbol() && OrderMagicNumber() == ALL_Magic)
{
if(st_0 == “All”)
l_count_8++;
if(st_0 == “Buy”)
if(OrderType() == OP_BUY)
l_count_8++;
if(st_0 == “Sell”)
if(OrderType() == OP_SELL)
l_count_8++;
}
}
return (l_count_8);
}
//+——————————————————————+
void Open_Order()
{
double lots = f0_Lots();
if(fDD_Demarker_Signal()==”Yes”
|| fDD_Demarker_Signal()==”CCI”
|| fEE_RSI_Signal()==”Yes”
|| fEE_RSI_Signal()==”CCI”)
if(f2_count(“All”)==0)
{
if(f2_count(“Buy”)==0)
{
if(fCC_Ma_Signal()==”Up” || fCC_Ma_Signal()==”Yes”)
if(fBB_CCI_Signal()==”Up”)
if(f9_Buy(lots,ALL_Magic))
printf(“//////Open Buy,”);
}
if(f2_count(“Sell”)==0)
{
if(fCC_Ma_Signal()==”Down” || fCC_Ma_Signal()==”Yes”)
if(fBB_CCI_Signal()==”Down”)
if(f9_Sell(lots,ALL_Magic))
printf(“//////Open Sell,”);
}
}
//if(fDD_Demarker_Signal()==”Yes”
// || fDD_Demarker_Signal()==”RSI”
// || fEE_RSI_Signal()==”Yes”
// || fEE_RSI_Signal()==”RSI”)
if( fDD_Demarker_Signal()==”RSI”
&& fEE_RSI_Signal()==”RSI”)
if(f2_count(“All”)==0)
{
if(f2_count(“Buy”)==0)
{
if(fAA_RSI_Signal()==”Up”)
if(f9_Buy(lots,ALL_Magic))
printf(“//////Open Buy,”);
}
if(f2_count(“Sell”)==0)
{
if(fAA_RSI_Signal()==”Down”)
if(f9_Sell(lots,ALL_Magic))
printf(“//////Open Sell,”);
}
}
if(f2_count(“Buy”)==1)
{
Modify_SL(“Buy”,FF_SL);
Modify_TP(“Buy”,FF_TP);
}
if(f2_count(“Sell”)==1)
{
Modify_SL(“Sell”,FF_SL);
Modify_TP(“Sell”,FF_TP);
}
}
//+——————————————————————+
double f0_Lots()
{
double lots = 0;
double His_profit = f1_HistoryData(0,”Profit”);
double His_lots = f1_HistoryData(0,”Lots”);

if(His_profit<0)
{
lots = His_lots*FF_Addlevel;
}
else
lots = FF_Lots;
if(lots > FF_Maxlots)
{
if(FF_减仓比例>1)
lots = FF_Maxlots/FF_减仓比例;
else
lots = FF_Lots;
}
return(lots);
}
//+——————————————————————+
int His_Losscount=0;
int SumHisCloseCount = 0;
string Str_taday = TimeToStr(TimeCurrent(), TIME_DATE);
datetime Time_taday = StrToTime(Str_taday);
void f0_wobuzhidao()
{

int in_0 = f1_his_count(Time_taday,”All”);
double do_0 = f1_HistoryData(0,”Profit”);

if(SumHisCloseCount<in_0)
{
printf(“Close count : “+DoubleToStr(in_0,0));
printf(“Close loss : “+DoubleToStr(do_0,2));
if(do_0<0)
His_Losscount++;
else
His_Losscount=0;
SumHisCloseCount = in_0;
}
else
SumHisCloseCount = in_0;
if(Hour()==Init_Hour && Minute()==1)
{
His_Losscount=0;
}
}
//+——————————————————————+
//| |
//+——————————————————————+
void Modify_TP(string as_0,double tp_1)
{
for(int i = OrdersTotal() – 1; i >= 0; i–)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
if(OrderSymbol() != Symbol() || OrderMagicNumber() != ALL_Magic)
continue;
if(OrderSymbol() == Symbol() && OrderMagicNumber() == ALL_Magic)
{
if(as_0 == “Buy”)
{
if(OrderType() == OP_BUY)
{
//double li_1 = NormalizeDouble(OrderOpenPrice()-sl_1*Point,Digits);
double li_2 = NormalizeDouble(OrderOpenPrice()+tp_1*Point*10,Digits);
if(OrderTakeProfit() == 0)
{
bool li_0 = OrderModify(OrderTicket(), OrderOpenPrice(),OrderStopLoss(),li_2, 0, Yellow);
//printf(DoubleToStr(li_1,Digits));
}
}
}
if(as_0 == “Sell”)
{
if(OrderType() == OP_SELL)
{
//double li_1 = NormalizeDouble(OrderOpenPrice()+sl_1*Point,Digits);
double li_2 = NormalizeDouble(OrderOpenPrice()-tp_1*Point*10,Digits);
if(OrderTakeProfit() == 0)
{
bool li_0 = OrderModify(OrderTicket(), OrderOpenPrice(),OrderStopLoss(),li_2, 0, Yellow);
}
}
}
}
}
}
//+——————————————————————+
//| |
//+——————————————————————+
void Modify_SL(string as_0,double sl_1)
{
for(int i = OrdersTotal() – 1; i >= 0; i–)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
if(OrderSymbol() != Symbol() || OrderMagicNumber() != ALL_Magic)
continue;
if(OrderSymbol() == Symbol() && OrderMagicNumber() == ALL_Magic)
{
if(as_0 == “Buy”)
{
if(OrderType() == OP_BUY)
{
double li_1 = NormalizeDouble(OrderOpenPrice()-sl_1*10*Point,Digits);//NormalizeDouble(sl_1,Digits);
//double li_2 = NormalizeDouble(OrderOpenPrice()+tp_1*Point,Digits);
if(OrderStopLoss() < li_1 || OrderStopLoss() ==0)
{
bool li_0 = OrderModify(OrderTicket(), OrderOpenPrice(),li_1,OrderTakeProfit(), 0, Yellow);
//printf(DoubleToStr(li_1,Digits));
}
}
}
if(as_0 == “Sell”)
{
if(OrderType() == OP_SELL)
{
double li_1 = NormalizeDouble(OrderOpenPrice()+sl_1*10*Point,Digits);//NormalizeDouble(sl_1,Digits);
//double li_2 = NormalizeDouble(OrderOpenPrice()-tp_1*Point,Digits);
if(OrderStopLoss() > li_1 || OrderStopLoss() ==0)
{
bool li_0 = OrderModify(OrderTicket(), OrderOpenPrice(),li_1,OrderTakeProfit(), 0, Yellow);
}
}
}
}
}
}
//+——————————————————————+
void comment()
{
f0_wobuzhidao();
Comment(” ”
+ ” \n”
+ ” 日期 :” +TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS)+” 星期 ” + DoubleToStr(DayOfWeek(),0)
+ ” \n”
+ ” 净值 :” + DoubleToStr(AccountEquity(),2)
+ ” \n”
+ ” AA-signal :” + fAA_RSI_Signal()
+ ” \n”
+ ” BB-signal :” + fBB_CCI_Signal()
+ ” \n”
+ ” CC-signal :” + fCC_Ma_Signal() + “//Ma斜率:”+DoubleToStr(fCC_Ma(),2)
+ ” \n”
+ ” DD-signal :” + fDD_Demarker_Signal()+”// α_Value”+DoubleToStr(fDD_Demarker(1),3)
+ ” \n”
+ ” EE-signal :” + fEE_RSI_Signal()+”// α_Value”+DoubleToStr(fEE_RSI(1),3)
+ ” \n”
+ “===================================”
+ ” \n”
+ ” Now Lots :” + DoubleToStr(f0_Lots(),2)
+ ” \n”
+ ” 连续亏损次数:”+DoubleToStr(His_Losscount,0)
+ ” \n”
+ ” 当日平仓次数:”+DoubleToStr(f1_his_count(Time_taday,”All”),0)
+ ” \n”
+ ” 最新平仓盈亏:”+DoubleToStr(f1_HistoryData(0,”Profit”),2)
+ ” \n”
+ ” data : ” + Str_taday

);
}
//+——————————————————————+
//+——————————————————————+