[MT4-EA]通道用ea指标
通道用ea指标
//+------------------------------------------------------------------+
//| Charles.mq4 |
//| Copyright 2012, AlFa Corp. |
//| alessio.fabiani @ gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, AlFa"
#property link "alessio.fabiani @ gmail.com"
#define VER "2.1.8"
#define DECIMAL_CONVERSION 10
extern string Comment_1 = " -- Comma separated list of Symbol Pairs: EURUSD,USDCHF";
extern string Comment_1a = " --- must be equal to the string of your account provider.";
//extern string Symbols = "EURUSD,EURGBP,EURJPY,CHFJPY,GBPUSD,GBPJPY,GBPCHF,GBPAUD,USDCAD,USDCHF,USDJPY,AUDNZD,AUDUSD,NZDUSD";
extern string Symbols = "";
extern int MagicNumber = 3939;
extern double xFactor = 2;
extern string TimeSet = "07:32";
extern string Comment_2 = " -- Balance percentage order balance and Lot value";
extern double RiskPercent = 10;
extern string Comment_3 = " -- Percentage of the risk for each Lot";
extern string Comment_3a = " --- auto-adapted to number of pairs.";
extern double RiskPerTrade = 30;
extern int MaxAllowedOrders = 15;
extern int MaxOpenHours = 0;
extern int ProfitCheckPeriod = 3;
extern string Comment_4 = " -- Whether or not use more strict supports and resistances";
extern bool Aggressive = false;
extern bool UsePivots = true;
extern string Comment_5 = " -- Whether or not leave open profitable orders";
extern int FastPeriod = 18;
extern int SlowPeriod = 60;
extern int Selectivity = 14;
extern string Comment_6 = " -- Fixed value if RiskPerTrade == 0";
extern double Lots = 0.01;
extern int Slippage = 3;
extern string Comment_7 = " -- Set to true if broker is ECN/STP needing stops adding after order";
extern bool ECN = true;
extern double MarginPercent = 20;
extern string Comment_8 = " -- On all orders";
extern int StopLoss = 0;
extern int TrailStart = 25;
extern int TrailingAmount = 5;
extern int TakeProfit = 25;
extern string Comment_9 = " -- Whether or not manage ALL opened orders (manual too)";
extern bool AllOrders = true;
extern string Comment_10 = " -- Whether or not compute the Profit on ALL pairs";
extern bool AllSymbols = false;
extern string Comment_11 = " -- Log to console or to a file";
extern bool LogToFile = false;
extern string Comment_12 = " -- On init delete all pending orders";
extern bool InitResetOrders = true;
extern string Comment_13 = " -- Trading Time Management";
extern int StartHour = 0;
extern int EndHour = 24;
extern bool CloseAllNow = false;
extern int FridayCloseTime = 0;
int Anchor, PendingBuy, PendingSell, Buys, Sells, i, StopLevel, Spread;
int MaxOpenOrders;
double BuyLots, SellLots, PendingBuyLots, PendingSellLots;
double Amount, Profit, LastProfit, Up, Dw, SL, TP;
double LotsValue, LotsHedgeValue;
double MaxPrice,MinPrice,MaxOpenPrice,MinOpenPrice;
bool isInGain=false, blockConditionHit, unlockOrdersPlaced=false;
int LastOrderTicket, UnlockingOrderTicket, Result, Error;
int Delta; //Order price shift (in points) from High/Low price
int LastDay;
double stopLoss, delta;
double trailStart, trailAmount, takeProfit;
double lastBarTime,lastpM5BarTime,lastpM15BarTime,lastpH1BarTime;
string SymbolsArray[0], CurrentSymbol;
bool canSell[0], canBuy[0], bullish[0], bearish[0];
bool Inited[0], DayStart[0], inited=false;
bool tradingAllowed=false;
double Pivot[0];
double Resist1[0];
double Resist2[0];
double Resist3[0];
double Support1[0];
double Support2[0];
double Support3[0];
double pPoint[0];
bool isCals[0];
double r1[0] , s1[0] ,s2[0], r2[0], r3[0], s3[0];
//+------------------------------------------------------------------+
//| Init function |
//+------------------------------------------------------------------+
void init()
{
RefreshRates();
if(LogToFile){startFile();}
Amount = 1.0;
Anchor = 250;
Delta = 5;
LastDay = 1;
PendingBuy = 0;
PendingSell = 0;
Buys = 0;
Sells = 0;
BuyLots = 0;
SellLots = 0;
PendingBuyLots = 0;
PendingSellLots = 0;
LastOrderTicket = -1;
UnlockingOrderTicket = -1;
string delim = ",";
int size;
if(StringLen(Symbols)==0)
{
size = 1;
}
else
{
size = 1+StringFindCount(Symbols,delim);
}
MaxOpenOrders = MaxAllowedOrders/size;
ArrayResize(SymbolsArray,size);
if(StringLen(Symbols)>0){StrToStringArray(Symbols,SymbolsArray,delim);}
ArrayResize(canSell,size);
ArrayResize(canBuy,size);
ArrayResize(Inited,size);
ArrayResize(DayStart,size);
ArrayResize(blockConditionHit,size);
ArrayResize(Pivot,size);
ArrayResize(Resist1,size);
ArrayResize(Resist2,size);
ArrayResize(Support1,size);
ArrayResize(Support2,size);
ArrayResize(Support3,size);
ArrayResize(Resist3,size);
ArrayResize(pPoint,size);
ArrayResize(isCals,size);
ArrayResize(r1,size);
ArrayResize(s1,size);
ArrayResize(s2,size);
ArrayResize(r2,size);
ArrayResize(r3,size);
ArrayResize(s3,size);
ArrayResize(bullish,size);
ArrayResize(bearish,size);
for(i=0; i0)
{for(i=Total-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == TRUE)
{
Pos=OrderType();
if((Pos==OP_BUYSTOP)||(Pos==OP_SELLSTOP)||(Pos==OP_BUYLIMIT)||(Pos==OP_SELLLIMIT)){Result=OrderDelete(OrderTicket(), CLR_NONE);}
if(Result<=0){Error=GetLastError();}
else Error=0;
}
}
}
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
bool pM5=false,pM15=false,pH1=false;
if((TimeCurrent()-lastpM5BarTime)/60>=PERIOD_M5)
{
pM5=true;
lastpM5BarTime=TimeCurrent();
}
if((TimeCurrent()-lastpM15BarTime)/60>=PERIOD_M15)
{
pM15=true;
lastpM15BarTime=TimeCurrent();
}
if((TimeCurrent()-lastpH1BarTime)/60>=PERIOD_H1)
{
pH1=true;
lastpH1BarTime=TimeCurrent();
}
int SystemPeriod=1;
tradingAllowed=false;
if((StartHour < EndHour && TimeHour(TimeCurrent()) >= StartHour && TimeHour(TimeCurrent()) < EndHour) || (StartHour > EndHour && TimeHour(TimeCurrent()) >= StartHour ||
TimeHour(TimeCurrent()) < EndHour))
{
if(DayOfWeek() != 5 || FridayCloseTime==0 || Hour()0 && Hour()>=FridayCloseTime) )
{
ForceCloseAllOpenOrders();
DeleteAllPendingOrders();
}
if( inited==false || (TimeCurrent() - lastBarTime)/60 >= SystemPeriod )
{
if(inited==false) inited=true;
int s;
for(s=0; s0)
{
CurrentSymbol = StringTrim(SymbolsArray[s]);
}
else
{
CurrentSymbol = Symbol();
}
if(Inited[s] == false)
{
if(InitResetOrders == true)
{
int Pos;
int Total=OrdersTotal();
if(Total>0)
{
for(i=Total-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == TRUE)
{
if(OrderSymbol()!=CurrentSymbol)
{
continue;
}//if
if(AllOrders==false && OrderMagicNumber()!=MagicNumber)
{
continue;
}//if
Pos=OrderType();
if((Pos==OP_BUYSTOP)||(Pos==OP_SELLSTOP)||(Pos==OP_BUYLIMIT)||(Pos==OP_SELLLIMIT)){Result=OrderDelete(OrderTicket(), CLR_NONE);}
//-----------------------
if(Result<0){Error=GetLastError();Log("LastError = "+Error);}
else Error=0;
//-----------------------
}//if
}//for
}//if
}//if
Inited[s]=true;
}//if
Spread = MarketInfo(CurrentSymbol, MODE_SPREAD);
StopLevel = MarketInfo(CurrentSymbol, MODE_STOPLEVEL) + Spread;
if (MarketInfo(CurrentSymbol, MODE_DIGITS) == 3 || MarketInfo(CurrentSymbol, MODE_DIGITS) == 5) {
stopLoss = NormalizeDouble(StopLoss* DECIMAL_CONVERSION * MarketInfo(CurrentSymbol, MODE_POINT),MarketInfo(CurrentSymbol, MODE_DIGITS));
delta = NormalizeDouble(Delta * DECIMAL_CONVERSION * MarketInfo(CurrentSymbol, MODE_POINT),MarketInfo(CurrentSymbol, MODE_DIGITS));
trailStart = NormalizeDouble(TrailStart * DECIMAL_CONVERSION * MarketInfo(CurrentSymbol, MODE_POINT),MarketInfo(CurrentSymbol, MODE_DIGITS));
trailAmount = NormalizeDouble(TrailingAmount * DECIMAL_CONVERSION * MarketInfo(CurrentSymbol, MODE_POINT),MarketInfo(CurrentSymbol, MODE_DIGITS));
takeProfit = NormalizeDouble(TakeProfit * DECIMAL_CONVERSION * MarketInfo(CurrentSymbol, MODE_POINT),MarketInfo(CurrentSymbol, MODE_DIGITS));
}
else
{
stopLoss = NormalizeDouble(StopLoss * MarketInfo(CurrentSymbol, MODE_POINT),MarketInfo(CurrentSymbol, MODE_DIGITS));
delta = NormalizeDouble(Delta * MarketInfo(CurrentSymbol, MODE_POINT),MarketInfo(CurrentSymbol, MODE_DIGITS));
trailStart = NormalizeDouble(TrailStart * MarketInfo(CurrentSymbol, MODE_POINT),MarketInfo(CurrentSymbol, MODE_DIGITS));
trailAmount = NormalizeDouble(TrailingAmount * MarketInfo(CurrentSymbol, MODE_POINT),MarketInfo(CurrentSymbol, MODE_DIGITS));
takeProfit = NormalizeDouble(TakeProfit * MarketInfo(CurrentSymbol, MODE_POINT),MarketInfo(CurrentSymbol, MODE_DIGITS));
}
if( stopLoss>0 && stopLossMaxPrice) MaxOpenPrice = MarketInfo(CurrentSymbol,MODE_ASK)+StopLevel*MarketInfo(CurrentSymbol, MODE_POINT);
if (MarketInfo(CurrentSymbol,MODE_BID)-StopLevel*MarketInfo(CurrentSymbol, MODE_POINT)=20)
{
//Alert(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS) + " -- " + Volatility1 + " / " + Volatility2);
if( iClose(CurrentSymbol,PERIOD_M15,1)>iOpen(CurrentSymbol,PERIOD_M15,1) )
{
//SignalBufferSellLimit=iHigh(CurrentSymbol,PERIOD_M5,iHighest(CurrentSymbol,PERIOD_M5,MODE_HIGH,3,0));
SignalBufferSellStop=NormalizeDouble( iLow(CurrentSymbol,PERIOD_M15,1)-delta+((iHigh(CurrentSymbol,PERIOD_M15,1)-iLow(CurrentSymbol,PERIOD_M15,1))/2), MarketInfo(CurrentSymbol,MODE_DIGITS));
}
else
{
//SignalBufferBuyLimit=iLow(CurrentSymbol,PERIOD_M5,iLowest(CurrentSymbol,PERIOD_M5,MODE_LOW,3,0));
SignalBufferBuyStop=NormalizeDouble( iLow(CurrentSymbol,PERIOD_M15,1)+delta-((iLow(CurrentSymbol,PERIOD_M15,1)-iHigh(CurrentSymbol,PERIOD_M15,1))/2), MarketInfo(CurrentSymbol,MODE_DIGITS));
}
}
if(SignalBufferSellStop>0 && (Sells+Buys)<=MaxOpenOrders)
{
OpenOrder(CurrentSymbol,LotsValue,OP_SELLSTOP,SignalBufferSellLimit);
}
if(SignalBufferBuyStop>0 && (Sells+Buys)<=MaxOpenOrders)
{
OpenOrder(CurrentSymbol,LotsValue,OP_BUYSTOP,SignalBufferBuyLimit);
}
if(SignalBufferSellLimit>0 && (Sells+Buys)<=MaxOpenOrders)
{
OpenOrder(CurrentSymbol,LotsValue,OP_SELLLIMIT,SignalBufferSellLimit);
}
if(SignalBufferBuyLimit>0 && (Sells+Buys)<=MaxOpenOrders)
{
OpenOrder(CurrentSymbol,LotsValue,OP_BUYLIMIT,SignalBufferBuyLimit);
}
}
if(pM15==true)
{
// CHECK TREND CONDITION
bullish[s]=((RSI>55&&RSIM1>55&&RSIL1>55)&&(RSIL2<50&&RSIM2<50&&RSI2<50)&&EMAS1>EMAF1);
bearish[s]=((RSI<55&&RSIM1<55&&RSIL1<55)&&(RSIL2>50&&RSIM2>50&&RSI2>50)&&EMAS1 NormalizeDouble(MarketInfo(CurrentSymbol,MODE_BID)+delta+(Spread*MarketInfo(CurrentSymbol, MODE_POINT)),MarketInfo(CurrentSymbol, MODE_DIGITS)))
{
Up = Up1;
}
else if (Up4 > NormalizeDouble(MarketInfo(CurrentSymbol,MODE_BID)+delta+(Spread*MarketInfo(CurrentSymbol, MODE_POINT)),MarketInfo(CurrentSymbol, MODE_DIGITS)))
{
Up = Up4;
}
else
{
Up = UpA;
}
if((PendingSell==0 && PendingBuy==0) || (TimeStr(CurTime())==TimeSet))
{
RefreshRates();
if(TimeStr(CurTime())==TimeSet && DayStart[s]==false)
{
Log("[DAY RESET: "+TimeToStr(TimeCurrent())+"]");
DeleteAllPendingOrders();
Dw=MinOpenPrice;
Up=MaxOpenPrice;
DayStart[s]=true;
}
else if(TimeStr(CurTime())!=TimeSet && DayStart[s]==true)
{
DayStart[s]=false;
}
SL=0;
TP=0;
if(PendingSell == 0)
{
if(canSell[s])
{
if(ECN == false && stopLoss > 0){SL=Dw+stopLoss;}
if(tradingAllowed==true && AccountFreeMarginCheck(CurrentSymbol,OP_SELL,LotsHedgeValue)>=(AccountEquity()*MarginPercent/100) && GetLastError()!=134)
{
//MaloMax: la chiusura di molti ordini puo dare problemi per la lentezza di esecuzione. Tra una chiusura e l'altra potresti inserire questo codice
while (!IsTradeAllowed()) Sleep(100);
RefreshRates();
Result=OrderSend(CurrentSymbol,OP_SELLSTOP,LotsHedgeValue,Dw,Slippage,SL,TP,"Charles_"+VER,MagicNumber,0,Red);
//-----------------------
if(Result<0){Error=GetLastError();Log("LastError [CORE;OP_SELLSTOP]= "+Error+" - Dw# "+Dw+" - Ask#"+MarketInfo(CurrentSymbol,MODE_ASK));}
else Error=0;
//-----------------------
}
}
}
if(PendingBuy == 0 && Error==0)
{
if(canBuy[s])
{
if(ECN == false && stopLoss > 0){SL=Up-stopLoss;}
if(tradingAllowed==true && AccountFreeMarginCheck(CurrentSymbol,OP_BUY,LotsHedgeValue)>=(AccountEquity()*MarginPercent/100) && GetLastError()!=134)
{
//MaloMax: la chiusura di molti ordini puo dare problemi per la lentezza di esecuzione. Tra una chiusura e l'altra potresti inserire questo codice
while (!IsTradeAllowed()) Sleep(100);
RefreshRates();
Result=OrderSend(CurrentSymbol,OP_BUYSTOP,LotsHedgeValue,Up,Slippage,SL,TP,"Charles_"+VER,MagicNumber,0,Blue);
//-----------------------
if(Result<0){Error=GetLastError();Log("LastError [CORE;OP_BUYSTOP] = "+Error+" - Dw# "+Up+" - Bid#"+MarketInfo(CurrentSymbol,MODE_BID));}
else Error=0;
//-----------------------
}
}
}
}
else
{
if((PendingSell == 0 && PendingBuy > 0) || (PendingSell > 0 && PendingBuy == 0))
{
RefreshRates();
SL=0;
TP=0;
if(PendingSell == 0 && PendingBuy > 0)
{
if(canSell[s])
{
if(ECN == false && stopLoss > 0){SL=Dw+stopLoss;}
if(tradingAllowed==true && AccountFreeMarginCheck(CurrentSymbol,OP_SELL,LotsHedgeValue)>=(AccountEquity()*MarginPercent/100) && GetLastError()!=134)
{
//MaloMax: la chiusura di molti ordini puo dare problemi per la lentezza di esecuzione. Tra una chiusura e l'altra potresti inserire questo codice
while (!IsTradeAllowed()) Sleep(100);
RefreshRates();
Result=OrderSend(CurrentSymbol,OP_SELLSTOP,LotsHedgeValue,Dw,Slippage,SL,TP,"Charles_"+VER,MagicNumber,0,Red);
//-----------------------
if(Result<0){Error=GetLastError();Log("LastError [CORE - HEDGE;OP_SELLSTOP]= "+Error);}
else if(Result!=0)
{
Error=0;
}
//-----------------------
}
}
}
if(PendingSell > 0 && PendingBuy == 0 && Error==0)
{
if(canBuy[s])
{
if(ECN == false && stopLoss > 0){SL=Up-stopLoss;}
if(tradingAllowed==true && AccountFreeMarginCheck(CurrentSymbol,OP_BUY,LotsHedgeValue)>=(AccountEquity()*MarginPercent/100) && GetLastError()!=134)
{
//MaloMax: la chiusura di molti ordini puo dare problemi per la lentezza di esecuzione. Tra una chiusura e l'altra potresti inserire questo codice
while (!IsTradeAllowed()) Sleep(100);
RefreshRates();
Result=OrderSend(CurrentSymbol,OP_BUYSTOP,LotsHedgeValue,Up,Slippage,SL,TP,"Charles_"+VER,MagicNumber,0,Blue);
//-----------------------
if(Result<0){Error=GetLastError();Log("LastError [CORE - HEDGE;OP_BUYSTOP] = "+Error);}
else if(Result!=0)
{
Error=0;
}
//-----------------------
}
}
}
}
}
}
[/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s][/s]