#property copyright "谁懂易经 www.shuidongyijing.cn Q614771879"
#property link "http://www.shuidongyijing.cn"
#include <stdlib.mqh>
#include <stderror.mqh>
#define MAGICMA 614771879
extern double 优化指数 = 3;
extern int ATR周期=4;
extern int 周期波段 = 20;
extern double 追踪价格 = 30;
extern bool 反馈=true; //
extern double Lots=0.1;
extern bool autolots = true;
extern double 按比例仓位 = 0.02;
double Atr;
double Resist;
double ResistPrev;
double Support;
double SupportPrev;
double Pivot;
int CalculateCurrentOrders(string symbol)
{
int buys=0;
int sells=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY)
buys++;
if(OrderType()==OP_SELL)
sells++;
}
}
if(buys>0)
return(buys);
else
return(-sells);
}
double LotsOptimized()
{
double lot=Lots;
if (autolots == true)
{
int orders=HistoryTotal();
int losses=0;
lot=NormalizeDouble(AccountFreeMargin()*按比例仓位/1000.0,1);
if(优化指数>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
{
Print("Error on History!");
break;
}
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
continue;
//----
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;
}
if(losses>1) lot=NormalizeDouble(lot-lot*losses/优化指数,1);
}
}
if(lot<0.1) lot=0.1;
return(lot);
}
void defPcChannel()
{
Resist=High[Highest(NULL,0,MODE_HIGH,周期波段,1)]; // up channel
ResistPrev=High[Highest(NULL,0,MODE_HIGH,周期波段,2)];
Support=Low[Lowest(NULL,0,MODE_LOW,周期波段,1)];
SupportPrev=Low[Lowest(NULL,0,MODE_LOW,周期波段,2)];
Pivot = (Resist+Support+Close[1])/3;
}
bool isOpenBuy()
{
defPcChannel() ;
if ( High[1] >= Resist && Resist == ResistPrev)
return (true);
if ( Close [1] < Resist && Resist == ResistPrev && Close [1] > Pivot)
return(true);
return(false);
}
bool isOpenSell()
{
defPcChannel();
if (Low[1] <= Support && Support==SupportPrev )
return (true);
if (Close [1] > Support && Support==SupportPrev && Close [1] < Pivot )
return (true);
return (false);
}
void CheckForOpen()
{
int res;
string sHeaderLetter;
string sBodyLetter;
if(Volume[0]>1)
return;
Atr = iATR(NULL,0,ATR周期,1);
if (isOpenBuy() == True)
{
res=OrderSend(Symbol(),OP_SELL, LotsOptimized(),Bid,3, Resist+Atr,0,"",MAGICMA,0,Red);
if (反馈 == True && res != -1)
{
sHeaderLetter = "Operation SELL by " + Symbol()+"";
sBodyLetter = "Order Sell by "+ Symbol() + " at " + DoubleToStr(Bid,4)+ ", and set stop/loss at " + DoubleToStr(Resist+Atr,4)+"";
sndMessage(sHeaderLetter, sBodyLetter);
}
return;
}
//---- is Buy?
if (isOpenSell() == true)
{
res=OrderSend(Symbol(),OP_BUY, LotsOptimized() ,Ask,3,Support-Atr,0,"order",MAGICMA,0,Blue);
if ( 反馈 == True && res != -1)
{
sHeaderLetter = "Operation BUY at " + Symbol()+"";
sBodyLetter = "Order Buy at "+ Symbol() + " for " + DoubleToStr(Ask,4)+ ", and set stop/loss at " + DoubleToStr(Support-Atr,4)+"";
sndMessage(sHeaderLetter, sBodyLetter);
}
return;
}
return;
}
bool isCloseSell()
{
defPcChannel();
if (Low[1] <= Support && Support==SupportPrev )
return (true);
return (false);
}
bool isCloseBuy()
{
defPcChannel();
if ( High[1] >= Resist && Resist == ResistPrev)
return (true);
return (false);
}
void CheckForClose()
{
string sHeaderLetter;
string sBodyLetter;
bool CloseOrd;
if(Volume[0]>1) return;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol())
continue;
if(OrderType()==OP_BUY)
{
if (isCloseBuy() == true )
{
CloseOrd=OrderClose(OrderTicket(),OrderLots(),Bid,3,Lime);
if ( 反馈 == True && CloseOrd == True)
{
sHeaderLetter = "Operation CLOSE BUY at" + Symbol()+"";
sBodyLetter = "Close order Buy at "+ Symbol() + " for " + DoubleToStr(Bid,4)+ ", and finish this Trade";
sndMessage(sHeaderLetter, sBodyLetter);
}
break;
}
else
{
if(追踪价格>0)
{
if(Bid-OrderOpenPrice()>Point*追踪价格)
{
if(OrderStopLoss()<Bid-Point*追踪价格)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*追踪价格,OrderTakeProfit(),0,Green);
return ;
}
}
}
}
}
if(OrderType()==OP_SELL)
{
if (isCloseSell() == true)
{
CloseOrd=OrderClose(OrderTicket(),OrderLots(),Ask,3,Lime);
if ( 反馈 == True && CloseOrd == True)
{
sHeaderLetter = "Operation CLOSE SELL at" + Symbol()+"";
sBodyLetter = "Close order Sell at "+ Symbol() + " for " + DoubleToStr(Ask,4)+ ", and finish this Trade";
sndMessage(sHeaderLetter, sBodyLetter);
}
break;
}
else
{
if(追踪价格>0)
{
if((OrderOpenPrice()-Ask)>(Point*追踪价格))
{
if((OrderStopLoss()>(Ask+Point*追踪价格)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*追踪价格,OrderTakeProfit(),0,Red);
return;
}
}
}
}
}
}
}
void sndMessage(string HeaderLetter, string BodyLetter)
{
int RetVal;
SendMail( HeaderLetter, BodyLetter );
RetVal = GetLastError();
if (RetVal!= ERR_NO_MQLERROR)
Print ("Error, message not send: ", ErrorDescription(RetVal));
return;
}
int start()
{
if(Bars<25 || IsTradeAllowed()==false)
return (0);
if (AccountFreeMargin()<(100*Point*Lots))
{
Print("Stop!Our account not enable for trade.Free margin is = ", AccountFreeMargin());
return(0);
}
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
return(0);
} |