[MT4-EA]顺势而为
61楼 电梯直达
基于五线谱开仓出现的价位的滞后性,自己写了一个简单的EA,因为自己水平,ea写得不好,但其中的开仓条件可以提供大家做参考
//+------------------------------------------------------------------+
//| This MQL is generated by Expert Advisor Builder |
//| http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/ |
//| |
//| In no event will author be liable for any damages whatsoever. |
//| Use at your own risk. |
//| |
//+------------------- DO NOT REMOVE THIS HEADER --------------------+
#define SIGNAL_NONE 0
#define SIGNAL_BUY 1
#define SIGNAL_SELL 2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4
#property copyright "Expert Advisor Builder"
#property link "http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/"
extern int MagicNumber = 0;
extern bool SignalMail = False;
extern bool EachTickMode = False;
extern double Lots = 1.0;
extern int Slippage = 3;
extern bool UseStopLoss = True;
extern int StopLoss = 30;
extern bool UseTakeProfit = True;
extern int TakeProfit = 250;
extern bool UseTrailingStop = True;
extern int TrailingStop = 55;
int BarCount;
int Current;
bool TickCheck = False;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init() {
BarCount = Bars;
if (EachTickMode) Current = 0; else Current = 1;
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit() {
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start() {
int Order = SIGNAL_NONE;
int Total, Ticket;
double StopLossLevel, TakeProfitLevel;
if (EachTickMode && Bars != BarCount) TickCheck = False;
Total = OrdersTotal();
Order = SIGNAL_NONE;
//+------------------------------------------------------------------+
//| Variable Begin |
//+------------------------------------------------------------------+
double Buy1;
double Buy2;
double Buy3;
double Sell1;
double Sell2;
//均线;ADX;
double MA100now =iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, Current + 0);
double MA100a =iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, Current + 1);
double MA200now =iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, Current + 0);
double MA200a =iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, Current + 1);
double Closenow =iClose(NULL, 0, Current + 0);
double Close1 =iClose(NULL, 0, Current + 1);
double pAO =iAO(NULL, 0, Current + 0);
//buy信号
if(MA200now>=MA100now &&Closenow>MA200now&&Close1<=MA100a&&pAO>0)
Buy1=true;
if(MA200now>=MA100now &&Closenow>MA200now&&Close1<=MA200a&&pAO>0)
Buy2=true;
if(MA100now>=MA200now &&Closenow>MA100now&&Close1<=MA200a&&pAO>0)
Buy3=true;
////////////////////////////////////////
/////sell
if(MA100now>=MA200now &&Closenow=MA200a&&pAO<0)
Sell1=true;
if(MA100now<=MA200now &&Closenow=MA200a&&pAO<0)
Sell2=true;
/////////////
double CloseBuy1_1 = pAO;
double CloseBuy1_2 = 0;
double CloseSell1_1 = pAO;
double CloseSell1_2 = 0;
//+------------------------------------------------------------------+
//| Variable End |
//+------------------------------------------------------------------+
//Check position
bool IsTrade = False;
for (int i = 0; i < Total; i ++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) {
IsTrade = True;
if(OrderType() == OP_BUY) {
//Close
//+------------------------------------------------------------------+
//| Signal Begin(Exit Buy) |
//+------------------------------------------------------------------+
if (CloseBuy1_1 <= CloseBuy1_2) Order = SIGNAL_CLOSEBUY;
//+------------------------------------------------------------------+
//| Signal End(Exit Buy) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if(Bid - OrderOpenPrice() > Point * TrailingStop) {
if(OrderStopLoss() < Bid - Point * TrailingStop) {
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
} else {
//Close
//+------------------------------------------------------------------+
//| Signal Begin(Exit Sell) |
//+------------------------------------------------------------------+
if (CloseSell1_1 >= CloseSell1_2) Order = SIGNAL_CLOSESELL;
//+------------------------------------------------------------------+
//| Signal End(Exit Sell) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Signal Begin(Entry) |
//+------------------------------------------------------------------+
if (Buy1==true || Buy2 ==true||Buy3==true) Order = SIGNAL_BUY;
if (Sell1==true || Sell2==true) Order = SIGNAL_SELL;
//+------------------------------------------------------------------+
//| Signal End |
//+------------------------------------------------------------------+
//Buy
if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(!IsTrade) {
//Check free margin
if (AccountFreeMargin() < (1000 * Lots)) {
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;
if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;
Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("BUY order opened : ", OrderOpenPrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");
} else {
Print("Error opening BUY order : ", GetLastError());
}
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
return(0);
}
}
//Sell
if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(!IsTrade) {
//Check free margin
if (AccountFreeMargin() < (1000 * Lots)) {
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;
if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;
Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("SELL order opened : ", OrderOpenPrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");
} else {
Print("Error opening SELL order : ", GetLastError());
}
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
return(0);
}
}
if (!EachTickMode) BarCount = Bars;
return(0);
}
//+------------------------------------------------------------------+
发表于:2008-05-31 01:33只看该作者
62楼
原程序如下:
//平仓=
int modNum = 0.0;
if (OrdersTotal() > 0)
{
for(i=OrdersTotal()-1; i>=0; i--)
{
modNum = 0;
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
modNum = MathMod(OrderMagicNumber(),4.0);
/*
if(OrderMagicNumber()==LHCHMAGIC+1)
{
if(OrderType()==OP_BUY)
{
if((dMA2>=Low[1]&&dMA2<=High[1])|| marketState == -1)
OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
}
if(OrderType()==OP_SELL)
{
if((dMA2>=Low[1]&&dMA2<=High[1])|| marketState == 1)
OrderClose(OrderTicket(),OrderLots(),Ask,3,Green);
}
}
*/
switch(modNum)
{
case 1: //平A仓
if(OrderType()==OP_BUY)
{
if((dMA2>=Low[1]&&dMA2<=High[1])|| marketState == -1)
OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
}
if(OrderType()==OP_SELL)
{
if((dMA2>=Low[1]&&dMA2<=High[1])|| marketState == 1)
OrderClose(OrderTicket(),OrderLots(),Ask,3,Green);
}
break;
case 2:
if(OrderType()==OP_BUY)
{
if((dMA3>=Low[1]&&dMA3<=High[1])|| marketState == -1)
OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
}
if(OrderType()==OP_SELL)
{
if((dMA3>=Low[1]&&dMA3<=High[1])|| marketState == 1)
OrderClose(OrderTicket(),OrderLots(),Ask,3,Green);
}
break;
case 3:
if(OrderType()==OP_BUY)
{
if((dMA4>=Low[1]&&dMA4<=High[1])|| marketState == -1)
OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
}
if(OrderType()==OP_SELL)
{
if((dMA4>=Low[1]&&dMA4<=High[1])|| marketState == 1)
OrderClose(OrderTicket(),OrderLots(),Ask,3,Green);
}
break;
case 4:
if(OrderType()==OP_BUY)
{
if((dMA5>=Low[1]&&dMA5<=High[1])|| marketState == -1)
OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
}
if(OrderType()==OP_SELL)
{
if((dMA5>=Low[1]&&dMA5<=High[1])|| marketState == 1)
OrderClose(OrderTicket(),OrderLots(),Ask,3,Green);
}
break;
default:
break;
}
}
}
//----
return(0);
}
在复盘中,确实这样平仓,因为均线并不总是呈现多头或者空头顺序排列,有些问题,但是的确是按照您过去提的依次触发平仓的啊
韬客社区www.talkfx.co
发表于:2008-06-03 09:14只看该作者
63楼
抱歉,最近几乎没有时间,上面您提到的五线谱平仓问题,是我理解不深
应该在触及均线的时候加入优先级来处理仓位
当时复盘的时候,就发现这个问题了,而且有时候价格剧烈变化的时候,您的平仓条件并不顺序触发
韬客社区www.talkfx.co
发表于:2008-06-06 02:46只看该作者
64楼
俺终于从晋陕蒙边境活着回来了,还在吗lhch兄?
刚才浏览了下您所有回复,除了平仓信号需要加优先级,另外您新的EA开仓信号不同了
您在前面回复中讲,当价格突破通道的时候,往往在5分钟图中下一单,15分钟图中下一单,然后一小时回调的时候再下一单
需要这样子修改下单方式吗?
韬客社区www.talkfx.co
发表于:2008-07-15 15:13只看该作者
65楼
:D :D :D :D
韬客社区www.talkfx.co
发表于:2008-12-04 10:06只看该作者
67楼
55555555555555555555
发表于:2009-01-04 14:36只看该作者
68楼
看过了 有一定的启发
韬客社区www.talkfx.co
发表于:2009-03-20 13:50只看该作者
69楼
ddddddddddddddddddd
韬客社区www.talkfx.co
发表于:2009-03-21 09:03只看该作者
70楼
支持一下 新手学习中
韬客社区www.talkfx.co
发表于:2010-03-16 09:14只看该作者
71楼
奇怪,怎么不能下载呢?
韬客社区www.talkfx.co
发表于:2010-05-27 18:02只看该作者
72楼
支持一下 新手学习中
韬客社区www.talkfx.co
发表于:2010-10-23 19:53只看该作者
73楼
:) :) :) :) :) :) :) :) :)
韬客社区www.talkfx.co
发表于:2010-12-15 12:18只看该作者
74楼
在哪呢?!!!
韬客社区www.talkfx.co
发表于:2010-12-20 12:42只看该作者
75楼
支持,正在学习中
韬客社区www.talkfx.co
发表于:2011-01-13 00:57只看该作者
76楼
ddddddddddd
韬客社区www.talkfx.co
发表于:2011-04-16 12:39只看该作者
77楼
ddddddddddddddddddd
韬客社区www.talkfx.co
发表于:2012-03-04 18:15只看该作者
78楼
你好,我想知道一下,这个系统最后的测试结果令人满意么?能够做成EA使用么?
韬客社区www.talkfx.co
发表于:2012-08-23 10:40只看该作者
79楼
感受很多,受教了。收藏一下。lhch 和雪天兄都是高高手啊。
韬客社区www.talkfx.co
发表于:2012-08-23 11:30只看该作者
80楼
期待下文,学到了很多:handshake