//+------------------------------------------------------------------+ //| Contradictions.mq4 | //| Copyright © 2009, Randy Gardner | //| http://www..com | //| Author | //| Apr 8, 2009 | //| Robert Hill | //| modified code to work with 5 digit prices | //| | //| added breakeven trailing used as follows | //| | //| When TrailingStop = 0 and TrailAtBreakeven = true | //| then breakeven trail will move stoploss | //| when breakeven level is hit to lock in value of LockInPips | //| | //| When TrailingStop = 0 and TrailArBreakeven = false | //| then no trail is done | //| | //| When TrailingStop > 0 the original trailing method is used. | //+------------------------------------------------------------------+ // ------ PREPROCESSOR #property copyright "Copyright © 2009, Randy Gardner" #property link "http://www..com" #include // ------ USER DEFINED INPUTS extern double Lots = 1.0; extern double StopLoss = 30; extern double TakeProfit =100; extern double TrailingStop = 0.0; extern string _______Indicators______; extern int Extra_Pips=1; extern int Keltner_Period = 10; extern double Close_Lots_At_X_Pips=0, Close_Percent_OfLots=0.0; extern bool Use_Percent_Method= false; extern double Trade_Lots_Percent= 20; extern bool On_Standard_Account=true; extern bool Trail_At_Break_Even=false; extern double BreakEven = 3; extern int LockInPips = 1; // Profit Lock in pips extern int Max_Lots_Std=100, Max_Lots_Mini=50; extern int Slip= 0.0; // ------- GLOBAL VARS double stop, tp, tStop; double tradeLots, tradeOpen, tradeLimit, tradeStop; int tradeNum, ticket; bool closed=false; int item, i=1, j=2; // (userDefinedLots) bool firstBarCompleted=false; bool currentTrade = false, longPosition = false, shortPosition = false; // bool previousLong = false, previousShort = false; bool commitLong = false, commitShort = false, commitLongExit = false, commitShortExit = false; double userDefLots=1, tradeLotsPercent; double ClosePercentOfLots, prevTradeLots; double partialLots; bool verifyHalfClose=false, disableCloseHalf=false; datetime time1 = 0; double myPoint; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { myPoint = SetPoint(Symbol()); //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { double r0 = iCustom(Symbol(), Period(), "Keltner_Channels", Keltner_Period, 300, 0, 0); double s0 = iCustom(Symbol(), Period(), "Keltner_Channels", Keltner_Period, 300, 2, 0); // **************************************** // Each Tick Re-initializations // ***************************************** partialLots = 0; // ClosePercentOfLots = Close_Percent_OfLots / 100; currentTrade = false; longPosition = false; shortPosition = false; commitLong = false; commitShort = false; commitLongExit = false; commitShortExit = false; // ************************************************** // ORDER TRACKING // ************************************************** for( item=0; item < OrdersTotal(); item++) { if( OrderSelect( item, SELECT_BY_POS, MODE_TRADES)==false) break; if( OrderSymbol() == Symbol() ) { if( OrderType()==OP_BUY){ longPosition = true; } else { shortPosition = true; } currentTrade = true; tradeLots = OrderLots(); tradeStop = OrderStopLoss(); tradeOpen = OrderOpenPrice(); tradeLimit = OrderTakeProfit(); tradeNum = OrderTicket(); break; } } // ****************************************** // TRADE LOG // ***************************************** if( verifyHalfClose && tradeLots < prevTradeLots) { verifyHalfClose = false; disableCloseHalf = true; } prevTradeLots = tradeLots; if( !currentTrade) { disableCloseHalf = false; } // **************************************************************** // Test for ENTRY Filters // *************************************************************** if( !currentTrade ) { if( ( Bid > s0 )) { commitLong = true; } if( ( Ask < r0 )) { commitShort = true; } } if( !currentTrade) { tradeLotsPercent = Trade_Lots_Percent / 100; if( Use_Percent_Method) { if( On_Standard_Account) { userDefLots = (AccountEquity() * tradeLotsPercent) / 1000; userDefLots = MathFloor(userDefLots); if( userDefLots > Max_Lots_Std) userDefLots = Max_Lots_Std; } else { userDefLots = (AccountEquity() * tradeLotsPercent) / 50; userDefLots = MathFloor(userDefLots); if( userDefLots > Max_Lots_Mini) userDefLots = Max_Lots_Mini; } } else { userDefLots = Lots; } if( userDefLots <= 0 ) { //Alert("Lots calculation <= 0. Rounding up to minimum .1."); userDefLots = 0.1; } } // ****************************************** // Open Trades // ****************************************** if( !currentTrade && commitLong ) { if( StopLoss == 0) stop = 0; else stop = Ask - StopLoss * myPoint; if( TakeProfit == 0) tp = 0; else tp = Ask + TakeProfit * myPoint; ticket=OrderSend(Symbol(),OP_BUY,userDefLots,Ask,Slip,stop,tp,NULL,0,0,Red); if( ticket<0) { Print("A BUY OrderSend failed with error #",GetLastError()); return(0); } else { PlaySound("alert3.wav"); return(0); } } if( !currentTrade && commitShort ) { if( StopLoss == 0) stop = 0; else stop = Bid + StopLoss * myPoint; if( TakeProfit == 0) tp = 0; else tp = Bid - TakeProfit * myPoint; ticket=OrderSend(Symbol(),OP_SELL,userDefLots,Bid,Slip,stop,tp,NULL,0,0,Red); if( ticket<0) { Print("A SELL OrderSend failed with error #",GetLastError()); return(0); } else { PlaySound("alert3.wav"); return(0); } } // ****************************************** // DETECT NEW BAR // ***************************************** if( longPosition ) { if ( ( Bid > r0 ) ) { commitLongExit = true; // } } else if( shortPosition ) { if ( ( Ask < s0 ) ) { commitShortExit = true; } } // ************************************************ // Order Control / Close Positions // ************************************************ if( commitLongExit) { closed=OrderClose(tradeNum, tradeLots, Bid, Slip, Orange); if( !closed) { Print("A BUY OrderClose failed with error #",GetLastError()); return(0); } else { PlaySound("alert3.wav"); return(0); } } else if( commitShortExit) { closed=OrderClose(tradeNum, tradeLots, Ask, Slip, Orange); if( !closed) { Print("A SELL OrderClose failed with error #",GetLastError()); return(0); } else { PlaySound("alert3.wav"); return(0); } } //****************************************** // Close_Lots_At_X_Pips //******************************************- if( Close_Lots_At_X_Pips != 0) { if( !disableCloseHalf) { partialLots = tradeLots * ClosePercentOfLots; partialLots = MathCeil(partialLots); if( longPosition) { if( tradeOpen + (Close_Lots_At_X_Pips * myPoint) <= Bid) { verifyHalfClose = true; OrderClose( tradeNum, partialLots, Bid, Slip, Orange); return(0); } } else { if( shortPosition) { if( tradeOpen - (Close_Lots_At_X_Pips * myPoint) >= Ask) { verifyHalfClose = true; OrderClose( tradeNum, partialLots, Ask, Slip, Orange); return(0); } } } } } // ****************************************** // Trailing Stop // ******************************************- // ********* MODIFIED SYNTAX tStop = TrailingStop * myPoint; if( longPosition) { if (tStop == 0 && Trail_At_Break_Even) { if (tradeStop < tradeOpen) BreakEven_TrailingStop(OP_BUY,tradeNum,tradeOpen,tradeStop,tradeLimit); } else if( tStop > 0 && Trail_At_Break_Even) { if( tradeOpen + tStop < Bid) { if( tradeStop < Bid - tStop) { OrderModify( tradeNum, tradeOpen, iLow(NULL,PERIOD_H1,+1), tradeLimit, 0, DarkGreen); return(0); } } } else if( tStop > 0 && !Trail_At_Break_Even) { if( tradeStop < Bid - tStop) { OrderModify( tradeNum, tradeOpen, iLow(NULL,PERIOD_H1,+1), tradeLimit, 0, DarkGreen); return(0); } } } else if( shortPosition) { if (tStop == 0 && Trail_At_Break_Even) { if (tradeStop > tradeOpen || tradeStop < 0.1) BreakEven_TrailingStop(OP_SELL,tradeNum,tradeOpen,tradeStop,tradeLimit); } else if( tStop > 0 && Trail_At_Break_Even) { if( tradeOpen - tStop > Ask) { if( tradeStop > Ask + tStop || tradeStop == 0) { OrderModify( tradeNum, tradeOpen, iHigh(NULL,PERIOD_H1,+1), tradeLimit, 0, DarkGreen); return(0); } } } else if( tStop > 0 && !Trail_At_Break_Even) { if( tradeStop > Ask + tStop || tradeStop == 0) { OrderModify( tradeNum, tradeOpen, iHigh(NULL,PERIOD_H1,+1), tradeLimit, 0, DarkGreen); return(0); } } } } int ModifyOrder(int ord_ticket,double op, double price,double tp, color mColor) { int CloseCnt, err; CloseCnt=0; while (CloseCnt < 3) { if (OrderModify(ord_ticket,op,price,tp,0,mColor)) { CloseCnt = 3; } else { err=GetLastError(); Print(CloseCnt," Error modifying order : (", err , ") " + ErrorDescription(err)); if (err>0) CloseCnt++; } } } double ValidStopLoss(int type, double price, double SL) { double mySL; double minstop; minstop = MarketInfo(Symbol(),MODE_STOPLEVEL); if (Digits == 3 || Digits == 5) minstop = minstop / 10; mySL = SL; if (type == OP_BUY) { if((price - mySL) < minstop*myPoint) mySL = price - minstop*myPoint; } if (type == OP_SELL) { if((mySL-price) < minstop*myPoint) mySL = price + minstop*myPoint; } return(NormalizeDouble(mySL,MarketInfo(Symbol(), MODE_DIGITS))); } //+------------------------------------------------------------------+ //| BreakEvenExpert_v1.mq4 | //| Copyright © 2006, Forex-TSD.com | //| Written by IgorAD,igorad2003@yahoo.co.uk | //| http://finance.groups.yahoo.com/group/TrendLaboratory | //| Move stop to breakeven + Lockin, no trail | //+------------------------------------------------------------------+ void BreakEven_TrailingStop(int type, int ticket, double op, double os, double tp) { int digits; double pBid, pAsk, BuyStop, SellStop; digits = MarketInfo(Symbol(), MODE_DIGITS); if (type==OP_BUY) { pBid = MarketInfo(Symbol(), MODE_BID); if ( pBid-op > myPoint*BreakEven ) { BuyStop = op + LockInPips * myPoint; if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits); BuyStop = ValidStopLoss(OP_BUY,pBid, BuyStop); if (os < BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen); return; } } if (type==OP_SELL) { pAsk = MarketInfo(Symbol(), MODE_ASK); if ( op - pAsk > myPoint*BreakEven ) { SellStop = op - LockInPips * myPoint; if (digits > 0) SellStop = NormalizeDouble( SellStop, digits); SellStop = ValidStopLoss(OP_SELL, pAsk, SellStop); if (os > SellStop || os < 0.1) ModifyOrder(ticket,op,SellStop,tp,DarkOrange); return; } } } double SetPoint(string mySymbol) { double mPoint, myDigits; myDigits = MarketInfo (mySymbol, MODE_DIGITS); if (myDigits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } // **************** E N D ***************