input double Lots=0.1;
input int Slippage=2;
input int Fast_TimeFrame=0;
input int Fast_Period=28;
input int Fast_Price = PRICE_OPEN;
input int Fast_Mode = MODE_SMA;
input int Fast_Shift=0;
input int Slow_TimeFrame=0;
input int Slow_Period=73;
input int Slow_Price = PRICE_OPEN;
input int Slow_Mode = MODE_SMA;
input int Slow_Shift=0;
input double DVBuySell=0.002;
input int MAXTrades=5;
input double ProfitMade=100;
input double LossLimit=72;
input int TrailStop=999;
input int PLBreakEven=999;
input int StartHour=0;
input int StopHour=24;
input int BasketProfit=9999;
input int BasketLoss=9999;
int MagicNumber = 20190404000;
string TradeComment = "两线发散交易法";
datetime bartime=0;
int bartick=0;
int objtick=0;
int tickcount=0;
bool TradeAllowed=true;
double maxOrders;
double maxEquity;
double minEquity;
double CECount;
double CEProc;
double CEBuy;
double CESell;
int OnInit()
{
//---
int i;
string o;
//remove the old objects
for(i=0; i<Bars; i++)
{
o=DoubleToStr(i,0);
ObjectDelete("myx"+o);
ObjectDelete("myz"+o);
}
objtick=0;
Print("Init happened ",CurTime());
Comment(" ");
//---
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
//---
int i;
string o;
//remove the old objects
for(i=0; i<Bars; i++)
{
o=DoubleToStr(i,0);
ObjectDelete("myx"+o);
ObjectDelete("myz"+o);
}
objtick=0;
Print("MAX number of orders ",maxOrders);
Print("MAX equity ",maxEquity);
Print("MIN equity ",minEquity);
Print("Close Everything ",CECount);
Print("Close Proc ",CEProc);
Print("Proc Buy ",CEBuy);
Print("Proc Sell ",CESell);
Print("DE-Init happened ",CurTime());
Comment(" ");
}
void OnTick()
{
//---
double p=Point();
double spread=Ask-Bid;
int cnt=0;
int gle=0;
int OrdersPerSymbol=0;
int OrdersBUY=0;
int OrdersSELL=0;
int iFileHandle;
double SL=0;
double TP=0;
double CurrentProfit=0;
double CurrentBasket=0;
bool BUYme=false;
bool SELLme=false;
double diverge;
if(bartime!=Time[0])
{
bartime=Time[0];
bartick++;
objtick++;
TradeAllowed=true;
Print("Bartick=",bartick);
}
OrdersPerSymbol=0;
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
OrdersPerSymbol++;
if(OrderType()==OP_BUY) {OrdersBUY++;}
if(OrderType()==OP_SELL){OrdersSELL++;}
}
}
if(OrdersPerSymbol>maxOrders) maxOrders=OrdersPerSymbol;
diverge=divergence(0);
Comment("Current Divergence = ",diverge);
if( diverge>=DVBuySell ) BUYme=true;
if( diverge<=(DVBuySell*(-1)) ) SELLme=true;
if(OrdersPerSymbol<=MAXTrades && TradeAllowed && BUYme)
{
//Ask(buy, long)
if(LossLimit ==0) SL=0; else SL=Ask-((LossLimit+7)*Point() );
if(ProfitMade==0) TP=0; else TP=Ask+((ProfitMade+7)*Point() );
OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SL,TP,TradeComment,MagicNumber,White);
gle=GetLastError();
if(gle==0)
{
Print("BUY Ask=",Ask," bartick=",bartick);
ObjectCreate("myx"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], High[0]+(5*p));
ObjectSetText("myx"+DoubleToStr(objtick,0),"B",15,"Arial",Red);
bartick=0;
TradeAllowed=false;
}
else
{
Print("-----ERROR----- BUY Ask=",Ask," error=",gle," bartick=",bartick);
}
}
if(OrdersPerSymbol<=MAXTrades && TradeAllowed && SELLme )
{
//Bid (sell, short)
if(LossLimit ==0) SL=0; else SL=Bid+((LossLimit+7)*Point() );
if(ProfitMade==0) TP=0; else TP=Bid-((ProfitMade+7)*Point() );
OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,SL,TP,TradeComment,MagicNumber,Red);
gle=GetLastError();
if(gle==0)
{
Print("SELL Bid=",Bid," bartick=",bartick);
ObjectCreate("myx"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], High[0]+(5*p));
ObjectSetText("myx"+DoubleToStr(objtick,0),"S",15,"Arial",Red);
bartick=0;
TradeAllowed=false;
}
else
{
Print("-----ERROR----- SELL Bid=",Bid," error=",gle," bartick=",bartick);
}
}
CurrentBasket=AccountEquity()-AccountBalance();
if(CurrentBasket>maxEquity) maxEquity=CurrentBasket;
if(CurrentBasket<minEquity) minEquity=CurrentBasket;
if( CurrentBasket>=BasketProfit || CurrentBasket<=(BasketLoss*(-1)) )
{
CloseEverything();
CECount++;
}
for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber )
{
if(OrderType()==OP_BUY)
{
CurrentProfit=Bid-OrderOpenPrice() ;
// modify for break even
if (CurrentProfit >= PLBreakEven*p && OrderOpenPrice()>OrderStopLoss())
{
SL=OrderOpenPrice()+(spread*2);
TP=OrderTakeProfit();
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP, White);
gle=GetLastError();
if(gle==0)
{
Print("MODIFY BREAKEVEN BUY Bid=",Bid," bartick=",bartick);
ObjectCreate("myz"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], Low[0]-(7*p));
ObjectSetText("myz"+DoubleToStr(objtick,0),"BE",15,"Arial",White);
}
else
{
Print("-----ERROR----- MODIFY BREAKEVEN BUY Bid=",Bid," error=",gle," bartick=",bartick);
}
}
if(CurrentProfit >= TrailStop*p )
{
SL=Bid-(TrailStop*p);
TP=OrderTakeProfit();
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP, White);
gle=GetLastError();
if(gle==0)
{
Print ("MODIFY TRAILSTOP BUY StopLoss=",SL," bartick=",bartick,"OrderTicket=",OrderTicket()," CurrProfit=",CurrentProfit);
ObjectCreate("myz"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], Low[0]-(7*p));
ObjectSetText("myz"+DoubleToStr(objtick,0),"TS",15,"Arial",White);
}
else
{
Print("-----ERROR----- MODIFY TRAILSTOP BUY Bid=",Bid," error=",gle," bartick=",bartick);
}
}
if(CurrentProfit>=(ProfitMade*p) || CurrentProfit<=((LossLimit*(-1))*p) )
{
OrderClose(OrderTicket(),Lots,Bid,Slippage,White);
gle=GetLastError();
if(gle==0)
{
Print("CLOSE BUY Bid=",Bid," bartick=",bartick);
ObjectCreate("myz"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], Low[0]-(7*p));
ObjectSetText("myz"+DoubleToStr(objtick,0),"C",15,"Arial",White);
}
else
{
Print("-----ERROR----- CLOSE BUY Bid=",Bid," error=",gle," bartick=",bartick);
}
}
}
if(OrderType()==OP_SELL)
{
CurrentProfit=OrderOpenPrice()-Ask;
// modify for break even
if (CurrentProfit >= PLBreakEven*p && OrderOpenPrice()<OrderStopLoss())
{
SL=OrderOpenPrice()-(spread*2);
TP=OrderTakeProfit();
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP, Red);
gle=GetLastError();
if(gle==0)
{
Print("MODIFY BREAKEVEN SELL Ask=",Ask," bartick=",bartick);
ObjectCreate("myz"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], Low[0]-(7*p));
ObjectSetText("myz"+DoubleToStr(objtick,0),"BE",15,"Arial",Red);
}
else
{
Print("-----ERROR----- MODIFY BREAKEVEN SELL Ask=",Ask," error=",gle," bartick=",bartick);
}
}
if(CurrentProfit >= TrailStop*p)
{
SL=Ask+(TrailStop*p);
TP=OrderTakeProfit();
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP, Red);
gle=GetLastError();
if(gle==0)
{
Print ("MODIFY TRAILSTOP SELL StopLoss=",SL," bartick=",bartick,"OrderTicket=",OrderTicket()," CurrProfit=",CurrentProfit);
ObjectCreate("myz"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], Low[0]-(7*p));
ObjectSetText("myz"+DoubleToStr(objtick,0),"TS",15,"Arial",Red);
}
else
{
Print("-----ERROR----- MODIFY TRAILSTOP SELL Ask=",Ask," error=",gle," bartick=",bartick);
}
}
// did we make our desired SELL profit?
if( CurrentProfit>=(ProfitMade*p) || CurrentProfit<=((LossLimit*(-1))*p) )
{
OrderClose(OrderTicket(),Lots,Ask,Slippage,Red);
gle=GetLastError();
if(gle==0)
{
Print("CLOSE SELL Ask=",Ask," bartick=",bartick);
ObjectCreate("myz"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], Low[0]-(7*p));
ObjectSetText("myz"+DoubleToStr(objtick,0),"C",15,"Arial",Red);
}
else
{
Print("-----ERROR----- CLOSE SELL Ask=",Ask," error=",gle," bartick=",bartick);
}
}
}
}
}
}
//+------------------------------------------------------------------+
void CloseEverything()
{
double myAsk;
double myBid;
int myTkt;
double myLot;
int myTyp;
int i;
bool result = false;
for(i=OrdersTotal();i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
myAsk=MarketInfo(OrderSymbol(),MODE_ASK);
myBid=MarketInfo(OrderSymbol(),MODE_BID);
myTkt=OrderTicket();
myLot=OrderLots();
myTyp=OrderType();
switch( myTyp )
{
//Close opened long positions
case OP_BUY :result = OrderClose(myTkt, myLot, myBid, Slippage, Red);
CEBuy++;
break;
//Close opened short positions
case OP_SELL :result = OrderClose(myTkt, myLot, myAsk, Slippage, Red);
CESell++;
break;
//Close pending orders
case OP_BUYLIMIT :
case OP_BUYSTOP :
case OP_SELLLIMIT:
case OP_SELLSTOP :result = OrderDelete( OrderTicket() );
}
if(result == false)
{
Alert("Order " , myTkt , " failed to close. Error:" , GetLastError() );
Print("Order " , myTkt , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
Sleep(1000);
CEProc++;
} //for
} // closeeverything
double divergence(int mypos)
{
int i;
double maF1, maF2, maS1, maS2;
double dv1, dv2;
maF1=iMA(Symbol(),Fast_TimeFrame,Fast_Period,Fast_Shift,Fast_Mode,Fast_Price,mypos);
maS1=iMA(Symbol(),Slow_TimeFrame,Slow_Period,Slow_Shift,Slow_Mode,Slow_Price,mypos);
dv1=(maF1-maS1);
maF2=iMA(Symbol(),Fast_TimeFrame,Fast_Period,Fast_Shift,Fast_Mode,Fast_Price,mypos+1);
maS2=iMA(Symbol(),Slow_TimeFrame,Slow_Period,Slow_Shift,Slow_Mode,Slow_Price,mypos+1);
dv2=((maF1-maS1)-(maF2-maS2));
return(dv1-dv2);
}
|