2017/5/1
一.多周期多指标EA
1.效果图
2.策略原理
开多单
a.当前k线碰到boll线底部
b.30分钟短周期价格大于长周期
c.kdj金叉 (当前大于,前一根小于)
平多单
a.kdj死叉
开空单
a.当前k线碰到boll线顶部
b.30分钟短周期价格小于长周期
c.kdj死叉 (当前小于,前一根大于)
平空单
a.kdj金叉
3.实现
//+------------------------------------------------------------------+
//| multiIndicatorAndMultiTimeFrameEA.mq5 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#include <wz\Trade.mqh>
#include <wz\Kline.mqh>
input int magic=170501;
input double lots=0.1;//开单量
input int slPoint=200;//止损点数
input int tpPoint=200;//止盈点数
input int fast_period=5;//小均线周期
input int slow_period=10;//大均线周期
input int boll_period=20;//布林带周期
input double boll_deviations=2;//布林带偏差
input int kdj_kPeriod=5;//KDJ K period
input int kdj_dPeriod=3;//KDJ D period
input int kdj_jPeriod=3;//KDJ J period
Trade td;//交易对象
Kline kl;//行情对象
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//为了函数间可以访问数组,声明生全局变量(注意指标里面都做过倒序处理)
//1.定义获取指标数据的数组
//数据索引最大是1,所有只需要获取2个值即可
int count=2;
double m30_fast[];
double m30_slow[];
double h4_fast[];
double h4_slow[];
double m30_bollUp[],m30_bollLow[],m30_bollMid[];
double h4_bollUp[],h4_bollLow[],h4_bollMid[];
double m30_kdj_main[],m30_kdj_signal[];
double h4_kdj_main[],h4_kdj_signal[];
MqlRates m30_rates[];
MqlRates h4_rates[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
//2.获取指标值
kl.MA(m30_fast,count,Symbol(),PERIOD_M30,fast_period,0,MODE_SMA,PRICE_CLOSE);
kl.MA(m30_slow,count,Symbol(),PERIOD_M30,slow_period,0,MODE_SMA,PRICE_CLOSE);
kl.MA(h4_fast,count,Symbol(),PERIOD_H4,fast_period,0,MODE_SMA,PRICE_CLOSE);
kl.MA(h4_slow,count,Symbol(),PERIOD_H4,slow_period,0,MODE_SMA,PRICE_CLOSE);
kl.Bands(m30_bollUp,m30_bollLow,m30_bollMid,count,Symbol(),PERIOD_M30,boll_period,0,boll_deviations,PRICE_CLOSE);
kl.Bands(h4_bollUp,h4_bollLow,h4_bollMid,count,Symbol(),PERIOD_H4,boll_period,0,boll_deviations,PRICE_CLOSE);
kl.Stochastic(m30_kdj_main,m30_kdj_signal,count,Symbol(),PERIOD_M30,kdj_kPeriod,kdj_dPeriod,kdj_jPeriod,MODE_SMA,STO_LOWHIGH);
kl.Stochastic(h4_kdj_main,h4_kdj_signal,count,Symbol(),PERIOD_H4,kdj_kPeriod,kdj_dPeriod,kdj_jPeriod,MODE_SMA,STO_LOWHIGH);
kl.getRates(m30_rates,count,Symbol(),PERIOD_M30);
kl.getRates(h4_rates,count,Symbol(),PERIOD_H4);
//3.条件判断进行开仓和平仓
if(satifyOpenBuy())
{
td.buyPlus(Symbol(),lots,slPoint,tpPoint,Symbol()+"buy",magic);
}
if(satifyCloseBuy())
{
td.closeAllBuy(Symbol(),magic);
}
if(satifyOpenSell())
{
td.sellPlus(Symbol(),lots,slPoint,tpPoint,Symbol()+"sell",magic);
}
if(satifyCloseSell())
{
td.closeAllSell(Symbol(),magic);
}
}
//+------------------------------------------------------------------+
bool satifyOpenBuy()
{
bool result=false;
if(h4_rates[0].low<h4_bollLow[0] && m30_fast[0]>m30_slow[0] && m30_kdj_main[0]>m30_kdj_signal[0] && m30_kdj_main[1]<m30_kdj_signal[1])
{
result=true;
}
return result;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool satifyCloseBuy()
{
bool result=false;
if(m30_kdj_main[0]<m30_kdj_signal[0] && m30_kdj_main[1]>m30_kdj_signal[1])
{
result=true;
}
return result;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool satifyOpenSell()
{
bool result=false;
if(h4_rates[0].high>h4_bollUp[0] && m30_fast[0]<m30_slow[0] && m30_kdj_main[0]<m30_kdj_signal[0] && m30_kdj_main[1]>m30_kdj_signal[1])
{
result=true;
}
return result;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool satifyCloseSell()
{
bool result=false;
if(m30_kdj_main[0]>m30_kdj_signal[0] && m30_kdj_main[1]<m30_kdj_signal[1])
{
result=true;
}
return result;
}
//+------------------------------------------------------------------+
4.印证(看看会不会按策略正常运行)
二.亏损加仓马丁格EA
1.效果
2.基本策略,
a.首次k线第二根突破boll带,开始做同方向第一次开单。
b. 符合加仓条件的,之后先将所有的订单止盈点修改成上一张最新单一样,再开始按倍数下单
3.实现
//+------------------------------------------------------------------+
//| 亏损加仓马丁格EA.mq5 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#include <wz\Trade.mqh>
#include <wz\Kline.mqh>
input int magic=170501;
input double lots=0.01;//开单量
input int reOpen_IntervalPoint=1000; //亏损加仓间隔点数
input double reOpen_multiple=2;//加仓倍数
input int reOpen_maxTimes=5;//同向最多加仓次数
input int slow_period=10;//大均线周期
input int boll_period=20;//布林带周期
input double boll_deviations=2;//布林带偏差
input int tpPoint=200;//回调止盈点数
Trade td;
Kline kl;
int count=4;//获取k线数据个数
MqlRates rates[];
double bollUp[],bollLow[],bollMid[];
//1.1一根k线只开一次单
datetime buyTime=0;
datetime sellTime=0;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
kl.getRates(rates,count);
kl.Bands(bollUp,bollLow,bollMid,count,Symbol(),0,boll_period,0,boll_deviations,PRICE_CLOSE);
//2.1多单策略
int buy_PositionNum=td.getPositionNum(Symbol(),POSITION_TYPE_BUY,magic);
if(buy_PositionNum==0)
{
if(satifyOpenBuy() && buyTime!=rates[0].time)
{
int t=td.buyPlus(Symbol(),lots,0,tpPoint,Symbol()+"buy",magic);
if(t>0){buyTime=rates[0].time;}
}
}
//亏损加仓
else
{
double openPrice,openLots,openStopLoss,openTakeProfit;
datetime openTime;
td.getNewestPositionOrder(Symbol(),POSITION_TYPE_BUY,openPrice,openTime,openLots,openStopLoss,openTakeProfit,magic);
//PrintFormat("buynewest_openTakeProfit=%lf",openTakeProfit);
//修改所有其他持仓单的止盈,修改为最新单的止盈单
td.mofiyStoplossAndTakeProfit(Symbol(),POSITION_TYPE_BUY,-1,openTakeProfit,magic);
if(buy_PositionNum<=reOpen_maxTimes && (openPrice-kl.getAsk(Symbol()))>=reOpen_IntervalPoint*Point())
{
td.buyPlus(Symbol(),td.formatLots(Symbol(),openLots*reOpen_multiple),0,tpPoint,Symbol()+"buy"+IntegerToString(buy_PositionNum),magic);
}
}
//2.2空单策略
int sell_PositionNum=td.getPositionNum(Symbol(),POSITION_TYPE_SELL,magic);
if(sell_PositionNum==0)
{
if(satifyOpenSell() && sellTime!=rates[0].time)
{
int t=td.sellPlus(Symbol(),lots,0,tpPoint,Symbol()+"sell",magic);
if(t>0){sellTime=rates[0].time;}
}
}
//加仓
else
{
double openPrice,openLots,openStopLoss,openTakeProfit;
datetime openTime;
td.getNewestPositionOrder(Symbol(),POSITION_TYPE_SELL,openPrice,openTime,openLots,openStopLoss,openTakeProfit,magic);
//修改所有其他持仓单的止盈
//PrintFormat("sellnewest_openTakeProfit=%lf",openTakeProfit);
td.mofiyStoplossAndTakeProfit(Symbol(),POSITION_TYPE_SELL,-1,openTakeProfit,magic);
if(sell_PositionNum<=reOpen_maxTimes && (kl.getBid(Symbol())-openPrice)>=reOpen_IntervalPoint*Point())
{
td.sellPlus(Symbol(),td.formatLots(Symbol(),openLots*reOpen_multiple),0,tpPoint,Symbol()+"sell"+IntegerToString(buy_PositionNum),magic);
}
}
}
//+------------------------------------------------------------------+
bool satifyOpenBuy()
{
bool result=false;
if(rates[1].open>bollLow[1] && rates[1].close>bollLow[1] && rates[2].low<bollLow[2])
{
result=true;
}
return result;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool satifyOpenSell()
{
bool result=false;
if(rates[1].open<bollUp[1] && rates[1].close<bollUp[1] && rates[2].high>bollUp[2])
{
result=true;
}
return result;
}
//+------------------------------------------------------------------+
三顺势加仓EA
1.效果图
2.基本原理
a.首次k线第二根突破boll带,开始做同方向第一次开单。
b. 符合顺势加仓条件的,再开始按倍数下单
c.符合平仓条件的 平掉所有同方向的订单。
3.实现
//+------------------------------------------------------------------+
//| 顺势加仓EA.mq5 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#include <wz\Trade.mqh>
#include <wz\Kline.mqh>
input int magic=170501;
input double lots=0.01;//开单量
input int reOpen_IntervalPoint=1000; //顺势加仓间隔点数
input double reOpen_multiple=2;//加仓倍数
input int reOpen_maxTimes=5;//同向最多加仓次数
input int boll_period=20;//布林带周期
input double boll_deviations=2;//布林带偏差
input int totalEarning_takeprofit_level=100;//总盈利多少美金全部平仓
Trade td;
Kline kl;
int count=4;//获取k线数据个数
MqlRates rates[];
double bollUp[],bollLow[],bollMid[];
//1.1一根k线只开一次单
datetime buyTime=0;
datetime sellTime=0;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
kl.getRates(rates,count);
kl.Bands(bollUp,bollLow,bollMid,count,Symbol(),0,boll_period,0,boll_deviations,PRICE_CLOSE);
//2.1多单策略
int buy_PositionNum=td.getPositionNum(Symbol(),POSITION_TYPE_BUY,magic);
if(buy_PositionNum==0)
{
if(satifyOpenBuy() && buyTime!=rates[0].time)
{
int t=td.buyPlus(Symbol(),lots,0,0,Symbol()+"buy",magic);
if(t>0){buyTime=rates[0].time;}
}
}
//顺势加仓
else
{
double openPrice,openLots,openStopLoss,openTakeProfit;
datetime openTime;
td.getNewestPositionOrder(Symbol(),POSITION_TYPE_BUY,openPrice,openTime,openLots,openStopLoss,openTakeProfit,magic);
//PrintFormat("buynewest_openTakeProfit=%lf",openTakeProfit);
if(buy_PositionNum<=reOpen_maxTimes && (kl.getAsk(Symbol())-openPrice)>=reOpen_IntervalPoint*Point())
{
td.buyPlus(Symbol(),td.formatLots(Symbol(),openLots*reOpen_multiple),0,0,Symbol()+"buy"+IntegerToString(buy_PositionNum),magic);
}
}
//平仓条件
if(satifyCloseBuy())
{
td.closeAllBuy(Symbol(),magic);
}
//2.2空单策略
int sell_PositionNum=td.getPositionNum(Symbol(),POSITION_TYPE_SELL,magic);
if(sell_PositionNum==0)
{
if(satifyOpenSell() && sellTime!=rates[0].time)
{
int t=td.sellPlus(Symbol(),lots,0,0,Symbol()+"sell",magic);
if(t>0){sellTime=rates[0].time;}
}
}
//顺势加仓
else
{
double openPrice,openLots,openStopLoss,openTakeProfit;
datetime openTime;
td.getNewestPositionOrder(Symbol(),POSITION_TYPE_SELL,openPrice,openTime,openLots,openStopLoss,openTakeProfit,magic);
//PrintFormat("sellnewest_openTakeProfit=%lf",openTakeProfit);
if(sell_PositionNum<=reOpen_maxTimes && (openPrice-kl.getBid(Symbol()))>=reOpen_IntervalPoint*Point())
{
td.sellPlus(Symbol(),td.formatLots(Symbol(),openLots*reOpen_multiple),0,0,Symbol()+"sell"+IntegerToString(buy_PositionNum),magic);
}
}
//平仓条件
if(satifyCloseSell())
{
td.closeAllSell(Symbol(),magic);
}
}
//+------------------------------------------------------------------+
bool satifyOpenBuy()
{
bool result=false;
if(rates[1].open>bollLow[1] && rates[1].close>bollLow[1] && rates[2].low<bollLow[2])
{
result=true;
}
return result;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool satifyOpenSell()
{
bool result=false;
if(rates[1].open<bollUp[1] && rates[1].close<bollUp[1] && rates[2].high>bollUp[2])
{
result=true;
}
return result;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool satifyCloseBuy()
{
bool result=false;
double allBuyProfit=td.profit(Symbol(),POSITION_TYPE_BUY,magic);
if(allBuyProfit>=totalEarning_takeprofit_level)
{
result=true;
}
return result;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool satifyCloseSell()
{
bool result=false;
double allSellProfit=td.profit(Symbol(),POSITION_TYPE_SELL,magic);
if(allSellProfit>=totalEarning_takeprofit_level)
{
result=true;
}
return result;
}
//+------------------------------------------------------------------+
四.多货币套利交易EA
1.多货币指标实现
1.1效果
1.2实现
//+------------------------------------------------------------------+
//| multiSymbolIndicator.mq5 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots 1
//--- plot symbol
#property indicator_label1 "symbol"
#property indicator_type1 DRAW_COLOR_CANDLES
#property indicator_color1 clrRed,clrYellow,C'0,0,0',C'0,0,0',C'0,0,0',C'0,0,0',C'0,0,0',C'0,0,0'
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- indicator buffers
double symbolBuffer1[];
double symbolBuffer2[];
double symbolBuffer3[];
double symbolBuffer4[];
double symbolColors[];
#include <wz\Trade.mqh>
#include <wz\Kline.mqh>
Trade td;
Kline kl;
input string symbolName="GBPUSD";//货币名称
input int showKlineNum=500; //显示k线个数(设置太多,有时候会加载比较慢)
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,symbolBuffer1,INDICATOR_DATA);
SetIndexBuffer(1,symbolBuffer2,INDICATOR_DATA);
SetIndexBuffer(2,symbolBuffer3,INDICATOR_DATA);
SetIndexBuffer(3,symbolBuffer4,INDICATOR_DATA);
SetIndexBuffer(4,symbolColors,INDICATOR_COLOR_INDEX);
//1.1序列化
setGlobalArrayAsSeries();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//1.2序列化
setSystemReturnArrayAsSeries(time,open,high,low,close,tick_volume,volume,spread);
//1.3获取数据 高开低收
MqlRates rates[];
int bars=showKlineNum;
kl.getRates(rates,bars,symbolName,0);
//1.4放大缩小因子
double scaleFactor=open[1]/rates[1].open;
//1.5 赋值缓存数组
int limit=(prev_calculated>0) ?(rates_total-prev_calculated+1) : rates_total;
limit=(limit>showKlineNum)? showKlineNum : limit;
for(int i=0;i<limit;i++)
{
symbolBuffer1[i]=rates[i].high*scaleFactor;
symbolBuffer2[i]=rates[i].open*scaleFactor;
symbolBuffer3[i]=rates[i].low*scaleFactor;
symbolBuffer4[i]=rates[i].close*scaleFactor;
if(symbolBuffer2[i]>symbolBuffer4[i])
{
symbolColors[i]=0;
}
else
{
symbolColors[i]=1;
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
void setGlobalArrayAsSeries()
{
ArraySetAsSeries(symbolBuffer1,true);
ArraySetAsSeries(symbolBuffer2,true);
ArraySetAsSeries(symbolBuffer3,true);
ArraySetAsSeries(symbolBuffer4,true);
ArraySetAsSeries(symbolColors,true);
}
//+------------------------------------------------------------------+
void setSystemReturnArrayAsSeries(
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
ArraySetAsSeries(time,true);
ArraySetAsSeries(open,true);
ArraySetAsSeries(high,true);
ArraySetAsSeries(low,true);
ArraySetAsSeries(close,true);
ArraySetAsSeries(tick_volume,true);
ArraySetAsSeries(volume,true);
ArraySetAsSeries(spread,true);
}
//+------------------------------------------------------------------+
2.三货币对套利EA(掌握原理即可,不推荐使用)
2.1不推荐原因
a.(考虑到滑点,基本上是套不到利的。暂时没有事物回滚功能)
b.(注意下单量 0.1*0.1=0.1 不一定正确,需要换算,小数点位数可能很多,)
2.1原理
eurusd*usdjpy=eurjpy
2.2实现
//+------------------------------------------------------------------+
//| threeCurrencyArbitrageEA.mq5 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#include <wz\Trade.mqh>
#include <wz\Kline.mqh>
input int positiveMagic=170501;
input int negativeMagic=170502;
Trade td;
Kline kl;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
// eurusd*usdjpy=eurjpy
double eurusd_usdjpy=kl.getBid("EURUSD")*kl.getBid("USDJPY");
double eurjpy=kl.getBid("EURJPY");
printf(DoubleToString(eurusd_usdjpy,Digits())+" "+DoubleToString(eurjpy,Digits()));
//1.套利开仓
if((eurusd_usdjpy-eurjpy)>=150*Point())
{
//1.1大的做空,小的做多 (注意下单量 0.1*0.1=0.1 不一定正确)
td.sellPlus("EURUSD",0.1,0,0,"EURUSD",positiveMagic);
td.sellPlus("USDJPY",0.1,0,0,"USDJPY",positiveMagic);
td.buyPlus("EURJPY",0.1,0,0,"EURJPY",positiveMagic);
}
//2.套利平仓
if((eurusd_usdjpy-eurjpy)<=0*Point())
{
td.closeAllSell("EURUSD",positiveMagic);
td.closeAllSell("USDJPY",positiveMagic);
td.closeAllBuy("EURJPY",positiveMagic);
}
//1.套利开仓
if((eurusd_usdjpy-eurjpy)<=150*Point())
{
//1.1大的做空,小的做多
td.buyPlus("EURUSD",0.1,0,0,"EURUSD",negativeMagic);
td.buyPlus("USDJPY",0.1,0,0,"USDJPY",negativeMagic);
td.sellPlus("EURJPY",0.1,0,0,"EURJPY",negativeMagic);
}
//2.套利平仓
if((eurusd_usdjpy-eurjpy)<=0*Point())
{
td.closeAllBuy("EURUSD",negativeMagic);
td.closeAllBuy("USDJPY",negativeMagic);
td.closeAllSell("EURJPY",negativeMagic);
}
}
//+------------------------------------------------------------------+
五.系统自动生成EA
1.设置步骤
2.对应的文档位置
3.自动生产的代码
//+------------------------------------------------------------------+
//| autoGenerateEA.mq5 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Include |
//+------------------------------------------------------------------+
#include <Expert\Expert.mqh>
//--- available signals
#include <Expert\Signal\SignalAMA.mqh>
//--- available trailing
#include <Expert\Trailing\TrailingFixedPips.mqh>
//--- available money management
#include <Expert\Money\MoneyFixedLot.mqh>
//+------------------------------------------------------------------+
//| Inputs |
//+------------------------------------------------------------------+
//--- inputs for expert
input string Expert_Title ="autoGenerateEA"; // Document name
ulong Expert_MagicNumber =19635; //
bool Expert_EveryTick =false; //
//--- inputs for main signal
input int Signal_ThresholdOpen =10; // Signal threshold value to open [0...100]
input int Signal_ThresholdClose =10; // Signal threshold value to close [0...100]
input double Signal_PriceLevel =0.0; // Price level to execute a deal
input double Signal_StopLevel =50.0; // Stop Loss level (in points)
input double Signal_TakeLevel =50.0; // Take Profit level (in points)
input int Signal_Expiration =4; // Expiration of pending orders (in bars)
input int Signal_AMA_PeriodMA =10; // Adaptive Moving Average(10,...) Period of averaging
input int Signal_AMA_PeriodFast =2; // Adaptive Moving Average(10,...) Period of fast EMA
input int Signal_AMA_PeriodSlow =30; // Adaptive Moving Average(10,...) Period of slow EMA
input int Signal_AMA_Shift =0; // Adaptive Moving Average(10,...) Time shift
input ENUM_APPLIED_PRICE Signal_AMA_Applied =PRICE_CLOSE; // Adaptive Moving Average(10,...) Prices series
input double Signal_AMA_Weight =1.0; // Adaptive Moving Average(10,...) Weight [0...1.0]
//--- inputs for trailing
input int Trailing_FixedPips_StopLevel =30; // Stop Loss trailing level (in points)
input int Trailing_FixedPips_ProfitLevel=50; // Take Profit trailing level (in points)
//--- inputs for money
input double Money_FixLot_Percent =10.0; // Percent
input double Money_FixLot_Lots =0.1; // Fixed volume
//+------------------------------------------------------------------+
//| Global expert object |
//+------------------------------------------------------------------+
CExpert ExtExpert;
//+------------------------------------------------------------------+
//| Initialization function of the expert |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Initializing expert
if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber))
{
//--- failed
printf(__FUNCTION__+": error initializing expert");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- Creating signal
CExpertSignal *signal=new CExpertSignal;
if(signal==NULL)
{
//--- failed
printf(__FUNCTION__+": error creating signal");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//---
ExtExpert.InitSignal(signal);
signal.ThresholdOpen(Signal_ThresholdOpen);
signal.ThresholdClose(Signal_ThresholdClose);
signal.PriceLevel(Signal_PriceLevel);
signal.StopLevel(Signal_StopLevel);
signal.TakeLevel(Signal_TakeLevel);
signal.Expiration(Signal_Expiration);
//--- Creating filter CSignalAMA
CSignalAMA *filter0=new CSignalAMA;
if(filter0==NULL)
{
//--- failed
printf(__FUNCTION__+": error creating filter0");
ExtExpert.Deinit();
return(INIT_FAILED);
}
signal.AddFilter(filter0);
//--- Set filter parameters
filter0.PeriodMA(Signal_AMA_PeriodMA);
filter0.PeriodFast(Signal_AMA_PeriodFast);
filter0.PeriodSlow(Signal_AMA_PeriodSlow);
filter0.Shift(Signal_AMA_Shift);
filter0.Applied(Signal_AMA_Applied);
filter0.Weight(Signal_AMA_Weight);
//--- Creation of trailing object
CTrailingFixedPips *trailing=new CTrailingFixedPips;
if(trailing==NULL)
{
//--- failed
printf(__FUNCTION__+": error creating trailing");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- Add trailing to expert (will be deleted automatically))
if(!ExtExpert.InitTrailing(trailing))
{
//--- failed
printf(__FUNCTION__+": error initializing trailing");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- Set trailing parameters
trailing.StopLevel(Trailing_FixedPips_StopLevel);
trailing.ProfitLevel(Trailing_FixedPips_ProfitLevel);
//--- Creation of money object
CMoneyFixedLot *money=new CMoneyFixedLot;
if(money==NULL)
{
//--- failed
printf(__FUNCTION__+": error creating money");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- Add money to expert (will be deleted automatically))
if(!ExtExpert.InitMoney(money))
{
//--- failed
printf(__FUNCTION__+": error initializing money");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- Set money parameters
money.Percent(Money_FixLot_Percent);
money.Lots(Money_FixLot_Lots);
//--- Check all trading objects parameters
if(!ExtExpert.ValidationSettings())
{
//--- failed
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- Tuning of all necessary indicators
if(!ExtExpert.InitIndicators())
{
//--- failed
printf(__FUNCTION__+": error initializing indicators");
ExtExpert.Deinit();
return(INIT_FAILED);
}
//--- ok
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Deinitialization function of the expert |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ExtExpert.Deinit();
}
//+------------------------------------------------------------------+
//| "Tick" event handler function |
//+------------------------------------------------------------------+
void OnTick()
{
ExtExpert.OnTick();
}
//+------------------------------------------------------------------+
//| "Trade" event handler function |
//+------------------------------------------------------------------+
void OnTrade()
{
ExtExpert.OnTrade();
}
//+------------------------------------------------------------------+
//| "Timer" event handler function |
//+------------------------------------------------------------------+
void OnTimer()
{
ExtExpert.OnTimer();
}
如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。