//+------------------------------------------------------------------+ //| MTPplus.mq4 | //| Copyright © 2006, Taylor Stockwell et al | //| Version: 2.0 | //+------------------------------------------------------------------+ #property copyright "Copyright © 2006, Taylor Stockwell" #property link "stockwet@yahoo.com" //////////////////////////// VARS external extern string PreSet_Name = "Default"; extern double First_TP = 15; extern double Second_TP = 50; extern double Third_TP = 800; extern double TP_Increment =10; extern double Close_Lots = 0.1; extern int Close_Method = 1; extern double First_Stop_Target = 15; extern double First_Stop = 5; extern double Second_Stop_Target = 20; extern double Second_Stop = 10; extern double Third_Stop_Target = 30; extern double Third_Stop = 20; extern double Trail_Stop_By = 10; extern double Max_Loss = 30; extern bool Alert_Sound = true; extern bool Alert_Email = false; extern int Magic_Number=0; extern int TextSize = 10; ////////////////////////// VARS internal double nextTP; double sl; double Trail_Stop; double Trail_Trigger; double Open_Spread=0; double Close_Spread=0; double Max_Pips=-99; double Lots2Close; string trailon; string version="MTPplus 2.0 Dec 2, 2010 "; bool tradeFlag; bool prevTradeFlag; double pipVal; string mngMagic; double dOrderOpenPrice; double myPoint; bool s1Flag, s2Flag, s3Flag; int txs; int t; bool ctl; /////////////////////////////////// // init ////////////////////////////////// int init(){ ctl = 0; Open_Spread=0; Close_Spread=0; Max_Pips=-99; txs = TextSize; t = txs + 1; tradeFlag=false; prevTradeFlag=false; s1Flag=false; s2Flag=false; s3Flag=false; Open_Spread=0; Close_Spread=0; pipVal=0; dOrderOpenPrice=0; if(First_Stop==0 && First_Stop_Target !=0) First_Stop=1; if(Max_Loss < 0) Max_Loss=(-1*Max_Loss); if(Max_Loss==0) Max_Loss=999; if(Trail_Stop_By < 0) Trail_Stop_By = -1*Trail_Stop_By; sl = -1*Max_Loss; if(First_TP!=0) nextTP = First_TP; else nextTP = TP_Increment; Trail_Stop = sl; trailon = "Off "; if(Third_Stop!=0) Trail_Trigger=Third_Stop_Target+Trail_Stop_By; else if(Second_Stop!=0) Trail_Trigger=Second_Stop_Target+Trail_Stop_By; else if(First_Stop!=0) Trail_Trigger=First_Stop_Target+Trail_Stop_By; else Trail_Trigger=sl+Trail_Stop_By; Lots2Close = Close_Lots; makeComment(pipVal, mngMagic, dOrderOpenPrice); myPoint = SetPoint(); } ////////////////////////// // deInit() ///////////////////////// int deinit(){ sl = -1*Max_Loss; nextTP = First_TP; Trail_Stop = sl; Comment(""); return(0); } ///////////////////////////////////////////////////// // refreshInit() resets the EA when trades are closed ///////////////////////////////////////////////////// int refreshInit(){ deinit(); init(); } //////////////////////////////// // Start /////////////////////////////// int start(){ //refresh the EA if we just closed all trades for the symbol on this chart // int ot = OrdersTotal(); // if (ot == 0 && ctl == 1) refreshInit(); int totForSymbol=0; for (int i=0;i 0 ) tradeFlag=true; else tradeFlag=false; if (tradeFlag != prevTradeFlag){ if (prevTradeFlag) refreshInit(); prevTradeFlag=tradeFlag; } if(OrdersTotal()==0)OpenTestTrades(); } ////////////////////////////////////// // getPipValue ////////////////////////////////////// double getPipValue(double openPrice,int orderType){ double val; RefreshRates(); openPrice = dOrderOpenPrice; if(orderType==OP_SELL) val=(NormalizeDouble(openPrice,Digits) - NormalizeDouble(Ask,Digits)); else val=(NormalizeDouble(Bid,Digits) - NormalizeDouble(openPrice,Digits)); val = val/myPoint; // To give number of PIPS return(val); } //////////////////////////////////////////////////// // getOpenOrders /////////////////////////////////////////////////// void getOpenOrders() { for(int j=0; j= nextTP) takeProfit(pipVal,OrderTicket()); } if(Magic_Number == 0) mngMagic = "All "+Symbol()+" trades."; else mngMagic = "Trades with magic number = "+Magic_Number; if(Trail_Stop_By != 0 && pipVal >= Trail_Trigger) { trailon = " On"; } makeComment(pipVal, mngMagic, dOrderOpenPrice); } } } ///////////////////////////////////////// // takeProfit ///////////////////////////////////////// void takeProfit(int pips, int ticket){ if(OrderSelect(ticket, SELECT_BY_TICKET)==true && Symbol()==OrderSymbol()){ if(Alert_Sound) PlaySound("mtp2.wav"); if(OrderType()==OP_SELL){ if(!OrderClose(ticket, Lots2Close, Ask, 3, Lime)) Alert("Error closing order : ",GetLastError()); } else { if(!OrderClose(ticket, Lots2Close, Bid, 3, Lime)) Alert("Error closing order : ",GetLastError()); } if(Close_Method==2) Lots2Close = Lots2Close + Close_Lots; if(nextTP == Third_TP) nextTP += TP_Increment; else if (nextTP == Second_TP) nextTP = Third_TP; else if (nextTP == First_TP) nextTP = Second_TP; else nextTP = nextTP + TP_Increment; } } ///////////////////////////////////////////// // checkStops ///////////////////////////////////////////// void checkStops(int pips,int ticket){ if(pips <= sl) stopTrade(pips,OrderTicket()); if(pips > Max_Pips) Max_Pips = pips; if(sl==-1*Max_Loss && First_Stop != 0 && pips >= First_Stop_Target && !s1Flag) if(moveStops(ticket, First_Stop*myPoint)) s1Flag=true; if(First_Stop != 0 && pips >= Second_Stop_Target && Second_Stop !=0 && !s2Flag) if(moveStops(ticket,Second_Stop*myPoint)) s2Flag=true; if(Second_Stop != 0 && pips >= Third_Stop_Target && Third_Stop !=0 && !s3Flag) if(moveStops(ticket,Third_Stop*myPoint)) s3Flag=true; if(Trail_Stop_By != 0 && pips >= Trail_Trigger && pips - Trail_Stop_By > sl) { Trail_Stop = (Max_Pips - Trail_Stop_By)*myPoint; // 110401 JT moveStops(ticket,Trail_Stop); sl = Trail_Stop; trailon = " ON"; } } //////////////////////////////////////// // moveStops /////////////////////////////////////// bool moveStops(int ticket,double stopDiff){ bool success=false; double BrokerStopLoss, NewStopLoss; if(OrderSelect(ticket, SELECT_BY_TICKET) && Symbol()==OrderSymbol() ){ Alert("moveStops called ",ticket, " ",stopDiff); BrokerStopLoss = NormalizeDouble (OrderStopLoss(), Digits); if(OrderType()==OP_SELL) { NewStopLoss = NormalizeDouble (OrderOpenPrice() - stopDiff, Digits); if (NewStopLoss < BrokerStopLoss || BrokerStopLoss == 0) { // Alert("Move stops to: ", OrderOpenPrice()-stopDiff); Alert("Move stops to: ", NewStopLoss); // if (!OrderModify(ticket,OrderOpenPrice(),OrderOpenPrice()-stopDiff, OrderTakeProfit(),0,Plum)) if (!OrderModify(ticket,OrderOpenPrice(),NewStopLoss, OrderTakeProfit(),0,Plum)) Alert("Could not modify order : ",GetLastError()); else { sl=stopDiff/myPoint; //110401 JT changed this, was not using myPoint success=true; } } }else { NewStopLoss = NormalizeDouble (OrderOpenPrice() + stopDiff, Digits); if (NewStopLoss > BrokerStopLoss) { Alert("Move stops to: ", NewStopLoss); if (!OrderModify(ticket,OrderOpenPrice(),NewStopLoss, OrderTakeProfit(),0,Plum)) Alert("Could not modify order : ",GetLastError()); else { sl=stopDiff/myPoint; success=true; Print ("stopDiff = ",sl);//######## ?????? } } } } return (success); } //////////////////////////////////////////// // killTrades //////////////////////////////////////////// void stopTrade(int pips, int ticket){ if(OrderSelect(ticket, SELECT_BY_TICKET)==true && Symbol()==OrderSymbol()){ if(OrderType()==OP_SELL) { if (!OrderClose(ticket,OrderLots(),Ask,3,DarkViolet)) Alert("Error closing order : ",GetLastError()); } else { if (!OrderClose(ticket,OrderLots(),Bid,3,DarkViolet)) Alert("Error closing order : ",GetLastError()); } if(Alert_Email) SendMail("MTP: Trade Stopped "+pips," "); if(Alert_Sound) { if(pips > 0) PlaySound("mtp3.wav"); else PlaySound("mtp1.wav"); } } } void makeComment(double val, string mngMagic, double dOrderOpenPrice) { int y=12; double FS=First_Stop; double SS=Second_Stop; double TS=Third_Stop; textOut(version,y);y+=t; textOut("PreSet . . . "+ PreSet_Name,y);y+=t; textOut("Close Method: "+ Close_Method,y);y+=t; textOut("Close Lots: " + DoubleToStr(Close_Lots,1),y);y+=t; textOut("TP: " + DoubleToStr(First_TP,0)+ " " + DoubleToStr(Second_TP,0)+ " " + DoubleToStr(Third_TP,0)+ " +" + DoubleToStr(TP_Increment,0),y);y+=t; textOut("SL1: " + DoubleToStr(First_Stop_Target,0)+ " " + DoubleToStr(FS,1),y);y+=t; textOut("SL2: " + DoubleToStr(Second_Stop_Target,0)+ " " + DoubleToStr(SS,1),y);y+=t; textOut("SL3: " + DoubleToStr(Third_Stop_Target,0)+ " " + DoubleToStr(TS,1),y);y+=t; textOut("TSL: " + DoubleToStr(Trail_Stop_By,0),y);y+=t; textOut("Max Loss: " + DoubleToStr(Max_Loss,0),y);y+=t; textOut("----------------------------",y);y+=t; textOut("Order Open: "+DoubleToStr(dOrderOpenPrice,5),y);y+=t; textOut("Open Spread: "+DoubleToStr(Open_Spread/10,1),y);y+=t; textOut("Close Spread: "+DoubleToStr(Close_Spread/10,1),y);y+=t; textOut("Max Pips: "+DoubleToStr(Max_Pips,1),y);y+=t; textOut("Pip Count: " + DoubleToStr(val,1),y);y+=t; textOut("Next TP: " + DoubleToStr(nextTP,0)+ " " + DoubleToStr(Lots2Close,1)+ " Lots",y);y+=t; textOut("Curr SL: " + DoubleToStr(sl,0),y);y+=t; textOut("TSL Trigger: " + DoubleToStr(Trail_Trigger,0)+ " "+ trailon,y);y+=t; textOut("Managing: "+ mngMagic,y);y+=t; } void textOut(string comment,int y) { string text = y; if (ObjectFind(text) == -1) ObjectCreate(text, OBJ_LABEL, 0, 0, 0); ObjectSet(text, OBJPROP_XDISTANCE, 2); ObjectSet(text, OBJPROP_YDISTANCE, y); ObjectSet(text, OBJPROP_CORNER, 0); ObjectSetText(text, comment, txs, "Arial", Aqua); } double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } void OpenTestTrades() { if(IsTesting()) { if(OrdersTotal()==0) { if(High[1]>High[2] && High[2]>High[3] && High[3]>High[4] && Low[1]>Low[2] && Low[2]>Low[3] && Low[3]>Low[4]) { OrderSend(Symbol(),OP_BUY,0.2,Ask,1,0,0);ctl = 1; } else { if(High[4]>High[3] && High[3]>High[2] && High[2]>High[1] && Low[4]>Low[3] && Low[3]>Low[2] && Low[2]>Low[1]) { OrderSend(Symbol(),OP_SELL,0.2,Bid,1,0,0);ctl = 1; } } } } }