顺水外汇EA交易网MT4

标题: 改编了网上的一个EA [打印本页]

作者: efpe    时间: 2018-1-11 00:41
标题: 改编了网上的一个EA
本人菜鸟,改编了网上的一个EA,使用的时候却只能够运行一次,之后就停止了,不能循环运行,请各位高手帮忙指出错误,万分感谢!
#include
#include //---- 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 128int 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=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  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  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=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; k30) yingli=6;
if(diancha=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()=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderType()>OP_SELL) && !OrderDelete(OrderTicket())) Print("Close Order Error");
}
}
}
return(0);
}
//+------------------------------------------------------------------+

作者: kitgain    时间: 2018-1-11 01:40
菜鸟也会改EA?那我是菜菜鸟了,看到楼主的代码头晕,
请菜鸟楼主大致解释一下咯
[attach]12377[/attach]

作者: xiaoming58123    时间: 2018-1-11 03:02
TODO: add your code here问题在这一句吧
作者: who    时间: 2018-1-11 04:36
学习一下  谢谢分享
[attach]12378[/attach]

作者: 流星划过夜空    时间: 2018-1-11 05:37
~连看看都要......就太........
作者: byrontang    时间: 2018-1-11 06:40
先说说你改了哪个部分?
作者: zw0806    时间: 2018-1-11 07:52
谢谢楼主分享!
作者: alumi999    时间: 2018-1-11 09:25
thanks for sharing..............
作者: 137936    时间: 2018-1-11 10:36
学习一下  谢谢分享
作者: tongyijiang    时间: 2018-1-11 11:34
学习一下  谢谢分享
作者: woshibilly    时间: 2018-1-11 12:38
学习一下  谢谢分享

作者: bls02dr    时间: 2018-1-11 13:12
了解一下
作者: fgmt    时间: 2018-1-11 14:18
路过随便看看
作者: jacket505    时间: 2018-1-11 15:05
学习一下  谢谢分享
作者: jacket505    时间: 2018-1-11 15:27
学习一下  谢谢分享
作者: jacket505    时间: 2018-1-11 16:19
学习一下  谢谢分享
作者: chenbw    时间: 2018-1-11 17:29
谢谢楼主分享




欢迎光临 顺水外汇EA交易网MT4 (http://www.waterforex.com/) Powered by Discuz! X3.2