没有添加任何其他附加要求的代码,测试结果感觉在调整期还是不太理想,所以还需要增加移动止损及开仓限制,不然赚的不够亏得,先把最基本的发上来供楼主改进。//+------------------------------------------------------------------+
//| Mid721.mq4 |
//| fantasize
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "fantasize"
extern int Period_1 = 21;
extern double TakeProfit = 200;
extern double Lots = 0.1;
double MA7[3000];
double MA21[3000];
double MA3[3000];
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int counted_bars=IndicatorCounted();
//----
double High1;
int cnt, ticket, total;
int i,k;
double sum;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
if(Period_1 <= 1 )
return(0);
if(Bars <= Period_1 )
return(0);
//calculate 7 days Min Open .
i = Bars - Period_1 + 1;
if(counted_bars > Period_1 - 1)
i = Bars - counted_bars - 1;
while(i >= 0)
{
k = i + 7 - 1;
sum=Open[k];
while(k >= i)
{
sum= MathMin(sum,Open[k]);
k--;
}
MA7=sum;
i--;
}
//calculate 21 days Max Open .
i = Bars - Period_1 + 1;
if(counted_bars > Period_1 - 1)
i = Bars - counted_bars - 1;
while(i >= 0)
{
k = i + 21 - 1;
sum=Open[k];
while(k >= i)
{
sum = MathMax(sum,Open[k]);
k--;
}
MA21=sum;
i--;
}
//MA3
i = Bars - Period_1 + 1;
if(counted_bars > Period_1 - 1)
i = Bars - counted_bars - 1;
//--EMA(VAR1,13)
while(i >= 0)
{
MA3 = 0.5*(MA7+MA21)-Close ;
i--;
}
//buy and sell judge
High1=MA3[1];
//Alert(MA7[0],MA21[0],MA3[0],Close[0]);
total=OrdersTotal();
if(total<1)
{ //1
// no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{ //2
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}//2
if(High1<0.00001)
{ //2
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Red);
if(ticket>0)
{//3
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}//3
else Print("Error opening BUY order : ",GetLastError());
return(0);
}//2
// check for short position (SELL) possibility
if(High1>0.00001)
{//2
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",16384,0,Green);
if(ticket>0)
{//3
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}//3
else Print("Error opening SELL order : ",GetLastError());
return(0);
}//2
}//1
// it is important to enter the market correctly,
// but it is more important to exit it correctly...
for(cnt=0;cnt0.00001 )
{//4
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}//4
}//3
else // go to short position
{//3
// should it be closed?
if(High1<0.00001)
{//4
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); // exit
}//4
}//end else 3
}//2
//
}//1
return(0);
}