//SupRes_EA_v1.6f.mq4 Copyright © 2008, Arshad Qureshi #property copyright "Copyright © 2008, Arshad Qureshi. ArshadFX" #include #include extern string ___Order_Params___; extern double FixedLots = 0.0; // if zero, use Percent extern int Percent = 10; // Allocated funds in percentage to use this function "Lot" size must be 0.0 extern double TakeProfit = 50; extern double StopLoss = 50; extern string ___ZZ_Params___; extern int ZZDepth=12; extern int ZZDeviation=5; extern int ZZBackstep=3; extern int back = 0; // start at this bar, 0 or 1 int MagicNumber=1234567; // Global Variables for counting orders by type ... int OrderTypeCount[6]; // 0=Buy,1=Sell,2=BuyLimit,3=SellLimit,4=BuyStop,5=SellStop int OrderCount=0; // Total order count // Global Variables for High TL and Low TL flags bool high_flag=true, low_flag= true; double myPoint; int init(){ myPoint=SetPoint(); return(0); } int deinit(){return(0);} int start() { int ticket; bool buy = false, sell = false; string symb=Symbol(); double zz_p[5]; int zz_i[5]; double latestHigh, prevHigh, latestLow, prevLow; int latestHighBar,prevHighBar,latestLowBar,prevLowBar; int count = 0; for(int i = 0 ; i < Bars ; i++) { // Record the last 5 ZZ values ... double zz = iCustom(NULL,0,"zigzag",ZZDepth,ZZDeviation,ZZBackstep,0,i); if (zz != 0) { count++; // record value and bar number ... if(count == back + 1) {zz_p[0] = zz; zz_i[0] = i;} if(count == back + 2) {zz_p[1] = zz; zz_i[1] = i;} if(count == back + 3) {zz_p[2] = zz; zz_i[2] = i;} if(count == back + 4) {zz_p[3] = zz; zz_i[3] = i;} if(count == back + 5) {zz_p[4] = zz; zz_i[4] = i; break;} } } if (zz_p[0] > zz_p[1]) { // ZZ last formed a trough ... latestHigh = zz_p[2]; latestHighBar = zz_i[2]; prevHigh = zz_p[4]; prevHighBar = zz_i[4]; latestLow = zz_p[1]; latestLowBar = zz_i[1]; prevLow = zz_p[3]; prevLowBar = zz_i[3]; } else { // ZZ last formed a peak ... latestHigh = zz_p[1]; latestHighBar = zz_i[1]; prevHigh = zz_p[3]; prevHighBar = zz_i[3]; latestLow = zz_p[2]; latestLowBar = zz_i[2]; prevLow = zz_p[4]; prevLowBar = zz_i[4]; } DrawTrendline("high_1",prevHighBar,latestHighBar,prevHigh,latestHigh,Green); DrawTrendline("Low_1", prevLowBar, latestLowBar, prevLow, latestLow,Red); CountOrdersByType(); if (Ask >= latestHigh-10*myPoint && OrderTypeCount[OP_BUYSTOP] == 0 && OrderTypeCount[OP_BUY] == 0 && high_flag == true) { Alert (" price is near Upper Line TradingCriteria triggered "); buy = true; high_flag = false; low_flag = true; } if (Bid <= latestLow+10*myPoint && OrderTypeCount[OP_SELLSTOP] == 0 && OrderTypeCount[OP_SELL] == 0 && low_flag == true) { Alert (" price is near Lower Line TradingCriteria triggered "); sell = true; low_flag = false; high_flag = true; } double lots=CalculateLots(); if(lots>0) { double entryPrice, sl=0.0,tp=0.0; if (buy) { DeleteOrders(OP_SELLSTOP); entryPrice = latestHigh; entryPrice = ValidEntryPrice(OP_BUYSTOP, entryPrice); sl=entryPrice-StopLoss *myPoint; tp=entryPrice+TakeProfit*myPoint; if (Digits>0) { // why would they not be? entryPrice = NormalizeDouble(entryPrice, Digits); sl=NormalizeDouble(sl, Digits); tp=NormalizeDouble(tp, Digits); } ticket=OrderSend(symb,OP_BUYSTOP,lots,latestHigh,2,sl,tp,NULL,MagicNumber); if(ticket<0) { Alert("OrderSend BUYSTOP failed - ",ErrorDescription(GetLastError())); } } if (sell) { DeleteOrders(OP_BUYSTOP); entryPrice = latestLow; entryPrice = ValidEntryPrice(OP_SELLSTOP, entryPrice); sl=latestLow+StopLoss *myPoint; tp=latestLow-TakeProfit*myPoint; if (Digits > 0) { entryPrice = NormalizeDouble(entryPrice, Digits); sl = NormalizeDouble( sl, Digits); tp = NormalizeDouble( tp, Digits); } ticket=OrderSend(symb,OP_SELLSTOP,lots,latestLow,2,sl,tp,NULL,MagicNumber); if(ticket<0) { Alert("OrderSend SELLSTOP failed - ",ErrorDescription(GetLastError())); } } } return(0); } void DrawTrendline (string objName, int t1, int t2, double p1, double p2, color clr) { ObjectDelete (objName); ObjectCreate (objName,OBJ_TREND,0,iTime(NULL,0,t1),p1,iTime(NULL,0,t2),p2); ObjectSet (objName,OBJPROP_COLOR,clr); ObjectSet (objName,OBJPROP_RAY,FALSE); ObjectSetText(objName,objName+" "+p1,10); } void CountOrdersByType() { int result=0; ArrayInitialize(OrderTypeCount,0); OrderCount=0; for(int i=0; i0) { double marginReqd=FixedLots*marginPerLot; // If insufficient margin use all available margin ... if(marginReqd<=freeMargin) result=FixedLots; else result=MathFloor(freeMargin/marginPerLot/lotStep)*lotStep; } else { // Use percentage of free margin ... if (Percent > 100) Percent=100; if (Percent==0) // If 0 is preset .. result=minLot; // ..then the min. lot else // Desired amount of lots: result=MathFloor(freeMargin*Percent/100/marginPerLot/lotStep)*lotStep;//Calc } if (result < minLot) result=minLot; if (result*marginPerLot > freeMargin) { Alert ("Insufficient margin to trade"); result=0.0; } return(result); } int DeleteOrders(int orderType) { Print("Deleting all orders with type ",orderType); for(int i=OrdersTotal()-1;i>=0;i--) { if(!OrderSelect(i,SELECT_BY_POS)) { Alert("OrderSelect failed - ",ErrorDescription(GetLastError())); } else { if(OrderSymbol()==Symbol() && (MagicNumber==0 || OrderMagicNumber()==MagicNumber) && OrderType()==orderType) { if(!OrderDelete(OrderTicket())) { Alert("OrderDelete failed - ",ErrorDescription(GetLastError())); } } } } return; } double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } double ValidEntryPrice(int type, double price) { double newPrice, temp; temp = MarketInfo(Symbol(), MODE_STOPLEVEL) * myPoint; newPrice = price; if (type == OP_BUYSTOP) { if((newPrice - Bid) < temp) newPrice = Bid + temp; } if (type == OP_SELLSTOP) { if((Bid - newPrice) < temp) newPrice = Bid - temp; } newPrice = NormalizeDouble(newPrice,Digits); return(newPrice); }