新手求教,改编了一个EA,却不能循环运行

 

本人菜鸟,改编了网上的一个EA,使用的时候却只能够运行一次,之后就停止了,不能循环运行,请各位高手帮忙指出错误,万分感谢!

#include <stdlib.mqh>
#include <stderror.mqh>

//---- input parameters
extern double Lots=0.10;

extern double StopDistance = 30;
extern double TakeProfit = 0;
extern double StopLoss= 0;
extern bool FirstOrderIsLong = true;
extern int MagicNumber=999;

int Slippage = 2;

double OpenBuy=0;
double OpenSell=0;
double OpenBuyLimit=0;
double OpenSellLimit=0;
double OpenBuyStop=0;
double OpenSellStop=0;
double EntryPrice=0;

double LastBuyStopPrice=0;
double LastSellStopPrice=0;
double BuyStopPrice=0;
double SellStopPrice=0;
double aLots[]={0.1,0.3,0.9,2.7,8.1,24.3,72.9,218.7,656.1,1968.3};
double MaxLots=0;

//Trade Signal
#define _NONE 0
#define _BUY 1
#define _SELL 2
#define _CLOSEBUY 4
#define _CLOSESELL 8
#define _OPENBUYSTOP 16
#define _OPENSELLSTOP 32
#define _DELETEBUYSTOP 64
#define _DELETESELLSTOP 128

int cur_signal=_NONE;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- TODO: add your code here
CheckOpenPositions();
return(0);
}

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here
return(0);
}

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
double llots;
double lSL;
double ordernumber=OrdersTotal();

if(MaxLots==0) llots=Lots; else
if(MaxLots==Lots) llots=3*Lots; else
llots=MaxLots*3;

lSL=StopLoss;

CheckSignal();

if(cur_signal & _BUY !=0)
OpenOrder(OP_BUY, llots, Ask, Slippage, lSL, TakeProfit, WindowExpertName(), MagicNumber, 0, Blue);

if(cur_signal & _SELL !=0)
OpenOrder(OP_SELL, llots, Bid, Slippage, lSL, TakeProfit, WindowExpertName(), MagicNumber, 0, Red);

if(cur_signal & _CLOSEBUY !=0)
{
CloseLongs(MagicNumber);
}

if(cur_signal & _CLOSESELL !=0)
{
CloseShorts(MagicNumber);
}

if(cur_signal & _DELETEBUYSTOP !=0)
DeleteOpenOrder(OP_BUYSTOP);

if(cur_signal & _DELETESELLSTOP !=0)
DeleteOpenOrder(OP_SELLSTOP);

if(cur_signal & _OPENBUYSTOP !=0)
{
DeleteOpenOrder(OP_BUYSTOP);
if(FirstOrderIsLong) BuyStopPrice=EntryPrice; else BuyStopPrice=EntryPrice+StopDistance*Point;
OpenOrder(OP_BUYSTOP, llots, BuyStopPrice, Slippage, lSL, TakeProfit, WindowExpertName(), MagicNumber, 0, Blue);
}

if(cur_signal & _OPENSELLSTOP !=0)
{
DeleteOpenOrder(OP_SELLSTOP);
if(FirstOrderIsLong) SellStopPrice=EntryPrice-StopDistance*Point; else SellStopPrice=EntryPrice;
OpenOrder(OP_SELLSTOP, llots, SellStopPrice, Slippage, lSL, TakeProfit, WindowExpertName(), MagicNumber, 0, Red);
}

CheckOpenPositions();
HandleOpenPositions();
closeall();

return(0);
}
//+------------------------------------------------------------------+


void CheckSignal()
{
cur_signal=_NONE;

if(OpenBuy==0 && OpenSell==0)
{
if(OpenBuyStop>0) cur_signal |= _DELETEBUYSTOP;
if(OpenSellStop>0) cur_signal |= _DELETESELLSTOP;

if(cur_signal==_NONE)
{
if(FirstOrderIsLong) cur_signal |= _BUY; else cur_signal |= _SELL;
EntryPrice=0;
}
}

if(OpenBuy!=0 || OpenSell!=0)
{
if(OpenBuy>OpenSell && OpenSellStop==0)
{
cur_signal |= _OPENSELLSTOP;
}
if(OpenBuy<OpenSell && OpenBuyStop==0)
{
cur_signal |= _OPENBUYSTOP;
}
}
}


//+------------------------------------------------------------------+
//| Handle Open Positions |
//| Check if any open positions need to be closed or modified |
//+------------------------------------------------------------------+
int HandleOpenPositions()
{
int cnt;
bool YesClose;
double pt;

for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
if ( OrderSymbol() != Symbol()) continue;
if ( OrderMagicNumber() != MagicNumber) continue;

if(OrderType() == OP_BUY)
{
}

if(OrderType() == OP_SELL)
{
}
}

return(0);
}


int OpenOrder(int aCmd, double aLots, double aPrice, int aSlipPage, int aStopLoss, int aTakeProfit, string aComment, int aMagic, datetime aExpiration, color aColor)
{
int order_ticket = 0;
int err = 0;
int l_Try = 0;
int l_MaxTry = 10;
double aPrice2=aPrice+0.0002;

switch (aCmd) {
case OP_BUY:
case OP_BUYLIMIT:
case OP_BUYSTOP:
for (l_Try = 0; l_Try < l_MaxTry; l_Try++)
{
if(aCmd==OP_BUY) { RefreshRates(); aPrice=Ask; aPrice2=Bid; }
order_ticket = OrderSend(Symbol(), aCmd, aLots, aPrice, aSlipPage, StopLong(aPrice, aStopLoss), TakeLong(aPrice, aTakeProfit), aComment, aMagic, aExpiration, aColor);
if (order_ticket > 0) break;
err = GetLastError();
if (err == 0) break;
if (err == 4 || err == 137 || err == 146 || err == 136) Sleep(5000);
else {
Print("Error Code= ", err);
break;
}
}
break;
case OP_SELL:
case OP_SELLLIMIT:
case OP_SELLSTOP:
for (l_Try = 0; l_Try < l_MaxTry; l_Try++) {
if(aCmd==OP_SELL) { RefreshRates(); aPrice=Bid; aPrice2=Ask; }
order_ticket = OrderSend(Symbol(), aCmd, aLots, aPrice, aSlipPage, StopShort(aPrice2, aStopLoss), TakeShort(aPrice2, aTakeProfit), aComment, aMagic, aExpiration, aColor);
if (order_ticket > 0) break;
err = GetLastError();
if (err == 0) break;
if (err == 4 || err == 137 || err == 146 || err == 136) Sleep(5000);
else {
Print("Error Code= ", err);
break;
}
}
}
return (order_ticket);
}

double StopLong(double aPrice, int aPips) {
if (aPips == 0) return (0);
else return (aPrice - aPips * Point);
}

double StopShort(double aPrice, int aPips) {
if (aPips == 0) return (0);
else return (aPrice + aPips * Point);
}

double TakeLong(double aPrice, int aPips) {
if (aPips == 0) return (0);
else return (aPrice + aPips * Point);
}

double TakeShort(double aPrice, int aPips) {
if (aPips == 0) return (0);
else return (aPrice - aPips * Point);
}

double CheckOpenPositions(bool InclOpenOrder=true)
{
int cnt, total;
double NumPositions;

NumPositions = 0;
total=OrdersTotal();
OpenBuy=0;
OpenSell=0;
OpenBuyLimit=0;
OpenSellLimit=0;
OpenBuyStop=0;
OpenSellStop=0;
MaxLots=0;

for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
if ( OrderSymbol() != Symbol()) continue;

if ( OrderMagicNumber() != MagicNumber) continue;

if(MaxLots<OrderLots()) MaxLots=OrderLots();

if(OrderType() == OP_BUY )
{
OpenBuy=OpenBuy+OrderLots();
if(EntryPrice==0) EntryPrice=OrderOpenPrice();
}
if(OrderType() == OP_SELL )
{
OpenSell=OpenSell+OrderLots();
if(EntryPrice==0) EntryPrice=OrderOpenPrice();
}

if(OrderType() == OP_BUYLIMIT ) OpenBuyLimit=OpenBuyLimit+OrderLots();
if(OrderType() == OP_SELLLIMIT ) OpenSellLimit=OpenSellLimit+OrderLots();
if(OrderType() == OP_BUYSTOP ) {OpenBuyStop=OpenBuyStop+OrderLots(); LastBuyStopPrice=OrderOpenPrice();}
if(OrderType() == OP_SELLSTOP ) {OpenSellStop=OpenSellStop+OrderLots(); LastSellStopPrice=OrderOpenPrice();}
}
NumPositions = OpenBuy-OpenSell;
if(InclOpenOrder) NumPositions += OpenBuyLimit+OpenBuyStop-OpenSellLimit-OpenSellStop;
return (NumPositions);
}


void CloseLongs(int MagicNumber, double clots=0)
{
int trade, err;
double llots;

//Print("Close Longs:",MagicNumber,", Lots:",clots);
for(trade=OrdersTotal()-1;trade>=0;trade--)
{
if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)) continue;
if(OrderSymbol()!=Symbol()) continue;
if(OrderMagicNumber()!=MagicNumber) continue;
if(OrderType()!=OP_BUY) continue;

if (clots==0) llots=OrderLots(); else llots=clots;
//Print("Ticket:",OrderTicket(),", MagicNumber:",OrderMagicNumber(),", Lots:",llots);
OrderClose(OrderTicket(),llots,Bid,Slippage,Blue);

err=GetLastError();
if(err!=0) Print("Error = ", err," - ",ErrorDescription(err));
}//for
}

void CloseShorts(int MagicNumber, double clots=0)
{
int trade, err;
double olots;
for(trade=OrdersTotal()-1;trade>=0;trade--)
{
if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==false) continue;
if(OrderSymbol()!=Symbol()) continue;
if(OrderMagicNumber()!=MagicNumber) continue;
if(OrderType()!=OP_SELL) continue;

olots=OrderLots();
if (clots==0) clots=olots;
OrderClose(OrderTicket(),clots,Ask,Slippage,Red);

err=GetLastError();
if(err!=0) Print("Error = ", err," - ",ErrorDescription(err));
}//for
}


int DeleteOpenOrder(int aOrderType=-1)
{
int cnt;
bool YesClose;
double pt;

for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() != Symbol()) continue;
if(OrderMagicNumber() != MagicNumber) continue;

if(aOrderType==OrderType()) OrderDelete(OrderTicket());
else if(aOrderType==-1)
switch(OrderType())
{
case OP_BUYLIMIT:
case OP_SELLLIMIT:
case OP_BUYSTOP:
case OP_SELLSTOP: OrderDelete(OrderTicket()); break;
}
}


return(0);
}
//+------------------------------------------------------------------+
int closeall(){

int k, order_num=OrdersTotal();
double yingli=0;
int diancha=Ask-Bid;
int iOrders=OrdersTotal()-1, i;
bool CloseOpenOrders = true;
bool DeleteAllPendingOrders = true;
double Price[2];
int giSlippage;
double dPoint=MarketInfo(OrderSymbol(),MODE_POINT);

if(dPoint==0) return(false);
RefreshRates();
Price[0]=MarketInfo(OrderSymbol(),MODE_ASK);
Price[1]=MarketInfo(OrderSymbol(),MODE_BID);
giSlippage=2;

for(k=0; k<order_num; k++){
OrderSelect(k, SELECT_BY_POS,MODE_TRADES);
if(diancha<0.0003 && OrderProfit()/OrderLots()>30) yingli=6;
if(diancha<0.0003 && OrdersTotal()>=4 && OrderProfit()/OrderLots()>20) yingli=9;

if(CloseOpenOrders && yingli>=3){
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderType()<=OP_SELL) && !OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage)) Print("Close Order Error");
}
}
if(DeleteAllPendingOrders && OrdersTotal()<2){
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderType()>OP_SELL) && !OrderDelete(OrderTicket())) Print("Close Order Error");
}
}
}
return(0);
}
//+------------------------------------------------------------------+

 
2008.11.04 13:15:57 2008.10.01 03:00 new1104 EURJPY,H1: zero divide //除数为0
2008.11.04 13:15:57 2008.10.01 03:00 new1104 EURJPY,H1: OrderDelete error 4051
2008.11.04 13:15:57 2008.10.01 03:00 new1104 EURJPY,H1: invalid ticket for OrderDelete function
2008.11.04 13:15:57 2008.10.01 03:00 new1104 EURJPY,H1: OrderClose error 4108
2008.11.04 13:15:57 2008.10.01 03:00 new1104 EURJPY,H1: pending order #2 cannot be closed
其它查看相关错误说明信息
原因: