//+------------------------------------------------------------------+ //| Mouteki EA v0.4.mq4 | //| Copyright ?2006, Hua Ai (aha) | //| | //+------------------------------------------------------------------+ #property copyright "Copyright ?2006, Hua Ai (aha)" #property link "" // Ver 0.4 Added parameters to control the mark hours the EA is allowed // to enter a trade. // // Ver 0.3 Fixed a minor bug causing problems on finding right point // for trend lines. // // Ver 0.2 // Modiciations: // 1. On a trend reversal, when the PP calculated based on the highs // after the first point is smaller (consolidating) than that based // on the highs between the first point and the 2nd point, use // latter PP for calculating ST and TP. // 2. When both TL are broken, only signal this special situation, not // to close any trades. When price break the Top TL again from that // situation, close all the short trades and let the long trades // run. When price break the bottom TL again from that situation, // close all the long trades and let the short trades run. // Ver 0.1 // Modifications: // 1. Allow EA to enter a trade even 3 bars after TL breaks. This is // because sometimes we got whipsaw back below TL and then breaks // again, or sometimes, the break is not detectation by only // checking the recent two bars. // Determine trend lines: // 1. Each time determine two trend lines: top trend line and bottom // trend line. // 2. Each trend line is determined by two points. Top trend line has // to be slopped downward from left to right. Bottom trend line // has to be slopped upward from left to right. // 3. The 1st point for the top trend line is the most recent high. // The 2nd point is the most recent high that is higher than the // 1st point. // 4. The 1st point for the bottom trend line is the most recent low. // The 2nd point is the most recent low that is lower than the // 1st point. // 5. A bar is qualified as a high when it's high is the highest among // the 5 bars centered at this bar. A bar is qualified as a low when // it's low is the lowest among the 5 bars centered at this bar. // Entry Conditions: // // 1. Enter long trade // 1) Enter long when price open higher than the top trend line. The // entry price is the first open price above the top trend line. // 2) Profit projection is determined by calculating the distance // between the lowest low from the current bar to the 2nd point // of the top trend line, and the trend line. // 3) TP is calculated by adding the profit projection to the first // open price above the top trend line. // 4) SL is calulated based on profit projection: // i. When profit projection is less than 90 pips, set SL to half // of the profit projection to create a 2:1 win/loss ratio. // ii. When profit project is greater than 90 pips, set SL to 33% // of the profit projection to create a 3:1 win/loss ratio. // // 2. Enter short trade // 1) Enter short when price open lower than the bottom trend line. // The entry price is the first open price below the bottom trend // line. // 2) Profit projection is determined by calculating the distance // between the highest high from the current bar to the 2nd point // of the bottom trend line, and the trend line. // 3) TP is calculated by adding the profit projection to the first // open price below the bottom trend line. // 4) SL is calulated based on profit projection: // i. When profit projection is less than 90 pips, set SL to half // of the profit projection to create a 2:1 win/loss ratio. // ii. When profit project is greater than 90 pips, set SL to 33% // of the profit projection to create a 3:1 win/loss ratio. // // Condition to modify a trade: // // 1. When profit is greater than ProfitToMoveSL pips, move the SL to +PositiveSL. // 2. When a new trade setup is formed in the same direction of an // existing trade: // 1) Adjusting TP if the new TP is farther than the existing one. // 2) Adjusting SL if the new SL is closer than the existing one. // 3) May add a position based on the new setup if there is only // one trade exists. // // Conditions to close a trade: // // 1. TP is hit. // 2. SL is hit. // 3. A new trade setup is formed in a reversed direction of the // existing trade. // // Timing: // // 1. The first 10 seconds of every 4 hours: // 1) Check entry conditions. // 2) Check new setups -- may open new position and/or close old // positions. // 3) Condition to modify SL. (This items was checked every tick but // was proved not good, it tends to cut profits a lot. So now this // is checked every 4 hours as well) // extern int ExpertID = 30050610; extern bool AlertsOn=true; extern bool MultiPositions=false; extern double LotsPerTrade=1; extern int ProfitToMoveSL=40; extern int PositiveSL=10; extern int SL_Offset=0; extern int TP_Offset=0; extern bool LondonOpen=true; extern bool NewYorkOpen=true; extern bool TokyoOpen=true; int spread; bool TD=False;/*Default is false. True setting draws up and down arrows instead of dots on TD Points creating more clutter.*/ int BackSteps=0;/*Used to be extern int now just int. Leave at 0*/ int ShowingSteps=1;/*Used to be extern int now just int. Leave at 1*/ bool FractalAsTD=false;/*Used to be extern bool now just bool. Leave at false, otherwise Trend Lines based on Fractal Points not TD Points*/ double TrendLineBreakUp=-1;//Line added. double TrendLineBreakUpPrev=-1;//Line added. double TrendLineBreakUpPrev1=-1;//Line added. bool TrendLineBreakUpFlag=False;//Line added. double TrendLineBreakDown=-1;//Line added. double TrendLineBreakDownPrev=-1;//Line added. double TrendLineBreakDownPrev1=-1;//Line added. bool TrendLineBreakDownFlag=False;//Line added. bool BothTLBroken=false; //---- buffers double highs[100000]; double lows[100000]; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- spread = MarketInfo(Symbol(),MODE_SPREAD); //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //-------------------------------------------------------------------- // Find the highs and lows in the current chart. int SetTDPoint(int B) { int shift; if (FractalAsTD==false) { //Print("B = ",B); //Print("Bars = ",Bars); //Print("IndicatorCounted() = ",IndicatorCounted()); //It seems B is the same as IndicatorCounted()function for (shift=B;shift>2;shift--) { // If the bar highs at the left are equal to current bar high, // the current bar high is still a valid high. if (High[shift+2]<=High[shift] && High[shift+1]<=High[shift] && High[shift-1]TrendLineBreakUp && Open[0]>TrendLineBreakDown && (Open[1]TrendLineBreakUp && // (Open[1]TrendLineBreakUp&&Open[0]>TrendLineBreakDown && ( Open[1]TrendLineBreakUp&&Open[0]>TrendLineBreakDown && ( Open[1]",TrendLineBreakUp," on ",Symbol()," ",Period()," @ ",Ask); //TrendLineBreakUpFlag=True; // Calculate profit projection, stop loss, take profit ii=Lowest(NULL,0,MODE_LOW,H2-1,1); iii=Lowest(NULL,0,MODE_LOW,H2-H1,H1); pp=MathMax(High[H2]-kH*(H2-ii)-Low[ii], High[H2]-kH*(H2-iii)-Low[iii]); // Calculate TP and SL tp=pp-spread*Point+Open[0]+TP_Offset*Point; if (pp>90*Point) sl=Open[0]-spread*Point-MathRound(10000*pp*0.33)/10000-SL_Offset*Point; else sl=Open[0]-spread*Point-MathRound(10000*pp*0.5)/10000-SL_Offset*Point; // Short exists? Close it. Long exists? Change it. total=OrdersTotal(); pos=0; for(cnt=0;cnt0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Long order opened : ",OrderOpenPrice()); /* Print("H1=", H1, " High[H1]=", High[H1]); Print("H2=", H2, " High[H2]=", High[H2]); Print("L1=", L1, " Low[L1]=", Low[L1]); Print("L2=", L2, " Low[L2]=", Low[L2]); Print("TrendLineBreakUp=", TrendLineBreakUp); Print("TrendLineBreakDown=", TrendLineBreakDown); Print("Open[0]=", Open[0], " Open[1]=", Open[1]); */ } else Print("Error opening Long order : ",GetLastError()); } } //Bottom trend line broken? /*if(Open[0]TrendLineBreakDownPrev || Open[2]>TrendLineBreakDownPrev) && H2<50 && L2<50)*/ //if(Open[0]TrendLineBreakDownPrev || Open[2]>TrendLineBreakDownPrev)) if((Open[0]TrendLineBreakDownPrev||Open[2]>TrendLineBreakDownPrev1|| Open[3]>TrendLineBreakDownPrev1) ) || ( BothTLBroken==true && Open[0]TrendLineBreakUpPrev||Open[2]>TrendLineBreakUpPrev1|| Open[3]>TrendLineBreakUpPrev1) )) { BothTLBroken=false; //Print("Lower Trendline Break ",Symbol()," ",Period()," ",Bid); if(AlertsOn) Alert("LTL Break<",TrendLineBreakDown," on ",Symbol()," ",Period()," @ ",Bid); //TrendLineBreakDownFlag=True; // Calculate profit projection, stop loss, take profit ii=Highest(NULL,0,MODE_HIGH,L2-1,1); iii=Highest(NULL,0,MODE_HIGH,L2-L1,L1); pp=MathMax(High[ii]-(Low[L2]+kL*(L2-ii)), High[iii]-(Low[L2]+kL*(L2-iii))); // Calculate TP and SL tp=Open[0]-pp+spread*Point-TP_Offset*Point; if (pp>90*Point) sl=Open[0]+spread*Point+MathRound(10000*pp*0.33)/10000+SL_Offset*Point; else sl=Open[0]+spread*Point+MathRound(10000*pp*0.5)/10000+SL_Offset*Point; // Long exists? Close it. Short exists? Change it. total=OrdersTotal(); pos=0; for(cnt=0;cnt=7 && Hour()<=16) || (NewYorkOpen==true && Hour()>=12 && Hour()<=21) || (TokyoOpen==true && ((Hour()>=0 && Hour()<=8) || (Hour()>=23 && Hour()<=24))) ) && (pos==0||MultiPositions) ) { ticket=OrderSend(Symbol(),OP_SELL,LotsPerTrade,Bid,3,sl,tp,"Mouteki",MagicNumber,0,Red); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Short order opened : ",OrderOpenPrice()); /* Print("H1=", H1, " High[H1]=", High[H1]); Print("H2=", H2, " High[H2]=", High[H2]); Print("L1=", L1, " Low[L1]=", Low[L1]); Print("L2=", L2, " Low[L2]=", Low[L2]); Print("TrendLineBreakUp=", TrendLineBreakUp); Print("TrendLineBreakDown=", TrendLineBreakDown); Print("Open[0]=", Open[0], " Open[1]=", Open[1]); */ } else Print("Error opening Long order : ",GetLastError()); } } ArrayInitialize(highs, 0.0); ArrayInitialize(lows, 0.0); } //---- return(0); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ int MakeMagicNumber( int ExpertID, bool TimeSpecific ) { int SymbolCode = 0; int PeriodCode = 0; int MagicNumber = 0; //---- Symbol Code if( Symbol() == "AUDCAD" || Symbol() == "AUDCADm" ) { SymbolCode = 1000; } else if( Symbol() == "AUDJPY" || Symbol() == "AUDJPYm" ) { SymbolCode = 2000; } else if( Symbol() == "AUDNZD" || Symbol() == "AUDNZDm" ) { SymbolCode = 3000; } else if( Symbol() == "AUDUSD" || Symbol() == "AUDUSDm" ) { SymbolCode = 4000; } else if( Symbol() == "CHFJPY" || Symbol() == "CHFJPYm" ) { SymbolCode = 5000; } else if( Symbol() == "EURAUD" || Symbol() == "EURAUDm" ) { SymbolCode = 6000; } else if( Symbol() == "EURCAD" || Symbol() == "EURCADm" ) { SymbolCode = 7000; } else if( Symbol() == "EURCHF" || Symbol() == "EURCHFm" ) { SymbolCode = 8000; } else if( Symbol() == "EURGBP" || Symbol() == "EURGBPm" ) { SymbolCode = 9000; } else if( Symbol() == "EURJPY" || Symbol() == "EURJPYm" ) { SymbolCode = 1000; } else if( Symbol() == "EURUSD" || Symbol() == "EURUSDm" ) { SymbolCode = 1100; } else if( Symbol() == "GBPCHF" || Symbol() == "GBPCHFm" ) { SymbolCode = 1200; } else if( Symbol() == "GBPJPY" || Symbol() == "GBPJPYm" ) { SymbolCode = 1300; } else if( Symbol() == "GBPUSD" || Symbol() == "GBPUSDm" ) { SymbolCode = 1400; } else if( Symbol() == "NZDJPY" || Symbol() == "NZDJPYm" ) { SymbolCode = 1500; } else if( Symbol() == "NZDUSD" || Symbol() == "NZDUSDm" ) { SymbolCode = 1600; } else if( Symbol() == "USDCAD" || Symbol() == "USDCADm" ) { SymbolCode = 1700; } else if( Symbol() == "USDCHF" || Symbol() == "USDCHFm" ) { SymbolCode = 1800; } else if( Symbol() == "USDJPY" || Symbol() == "USDJPYm" ) { SymbolCode = 1900; } //---- Period Code if( TimeSpecific ) { if( Period() == 1 ) { PeriodCode = 10; } else if( Period() == 5 ) { PeriodCode = 20; } else if( Period() == 15 ) { PeriodCode = 30; } else if( Period() == 30 ) { PeriodCode = 40; } else if( Period() == 60 ) { PeriodCode = 50; } else if( Period() == 240 ) { PeriodCode = 60; } else if( Period() == 1440 ) { PeriodCode = 70; } else if( Period() == 10080 ){ PeriodCode = 80; } } else { PeriodCode = 0; } //---- Calculate MagicNumber MagicNumber = ExpertID+SymbolCode+PeriodCode; return(MagicNumber); }