//+------------------------------------------------------------------+ //| SetPivotPoint.mq4 | //| Copyright © 2009, Randy Gardner | //| http://www..com | //| Author | //+------------------------------------------------------------------+ // ------ PREPROCESSOR #property copyright "Copyright © 2009, Randy Gardner" #property link "http://www..com" // ------ USER DEFINED INPUTS extern double Lots = 0.1; extern double StopLoss = 25; extern double TakeProfit = 50; extern double TrailingStop = 25; extern double SetPivotPoint = 1.3035; extern double Close_Lots_At_X_Pips=160, Close_Percent_OfLots=100; extern bool Use_Percent_Method=false; extern double Trade_Lots_Percent= 10; extern bool On_Standard_Account=false; extern bool Trail_At_Break_Even=true; extern int Max_Lots_Std=100, Max_Lots_Mini=50; extern int Slip=2; // ------- 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; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { // ********* MODIFIED SYNTAX if( IsTesting()) { if( AccountFreeMargin() < 1000) { Alert("Back testing mode fails to execute if freemargin reaches less than $1000 at any Point during back-test. 1 lot ties $1000 in freemargin."); return(0); } } else if( IsDemo()) { if(AccountFreeMargin() < 100) { Alert("Alert: Free margin less than 100 on this demo account. Freemargin = ", AccountFreeMargin()); } } else if( AccountFreeMargin() < 1000) { Alert("Free margin less than 1000. If this is a standard account, the trade station will prevent trading. 1 lot ties $1000 in freemargin. Freemargin = ", AccountFreeMargin()); return(0); } // end if IsTesting time1 = Time[1]; //initialize to match, so that new period will be detected. for( i=1; i < 100; i++) { } } return(0); //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { // **************************************** // Each Tick Re-initializations // ***************************************** currentTrade = false; longPosition = false; shortPosition = false; commitLong = false; commitShort = false; commitLongExit = false; commitShortExit = false; partialLots = 0; // ClosePercentOfLots = Close_Percent_OfLots / 100; // ************************************************** // 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 > SetPivotPoint )) { commitLong = true; } if( ( Ask < SetPivotPoint )) { commitShort = true; } } if( !currentTrade) { tradeLotsPercent = Trade_Lots_Percent / 100; if( Use_Percent_Method) { if( On_Standard_Account) { userDefLots = (AccountFreeMargin() * tradeLotsPercent) / 1000; userDefLots = MathFloor(userDefLots); if( userDefLots > Max_Lots_Std) userDefLots = Max_Lots_Std; } else { userDefLots = (AccountFreeMargin() * 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 * Point; if( TakeProfit == 0) tp = 0; else tp = Ask + TakeProfit * Point; 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 * Point; if( TakeProfit == 0) tp = 0; else tp = Bid - TakeProfit * Point; 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( ( Ask < SetPivotPoint ) ) { commitLongExit = true; // } } else if( shortPosition ) { if( ( Bid > SetPivotPoint ) ) { 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 * Point) <= Bid) { verifyHalfClose = true; OrderClose( tradeNum, partialLots, Bid, Slip, Orange); return(0); } } else { if( shortPosition) { if( tradeOpen - (Close_Lots_At_X_Pips * Point) >= Ask) { verifyHalfClose = true; OrderClose( tradeNum, partialLots, Ask, Slip, Orange); return(0); } } } } } // ****************************************** // Trailing Stop // ******************************************- // ********* MODIFIED SYNTAX tStop = TrailingStop * Point; if( longPosition) { if( tStop > 0 && Trail_At_Break_Even) { if( tradeOpen + tStop < Bid) { if( tradeStop < Bid - tStop) { OrderModify( tradeNum, tradeOpen, Bid - tStop, tradeLimit, 0, DarkGreen); return(0); } } } else if( tStop > 0 && !Trail_At_Break_Even) { if( tradeStop < Bid - tStop) { OrderModify( tradeNum, tradeOpen, Bid - tStop, tradeLimit, 0, DarkGreen); return(0); } } } else if( shortPosition) { if( tStop > 0 && Trail_At_Break_Even) { if( tradeOpen - tStop > Ask) { if( tradeStop > Ask + tStop || tradeStop == 0) { OrderModify( tradeNum, tradeOpen, Ask + tStop, tradeLimit, 0, DarkGreen); return(0); } } } else if( tStop > 0 && !Trail_At_Break_Even) { if( tradeStop > Ask + tStop || tradeStop == 0) { OrderModify( tradeNum, tradeOpen, Ask + tStop, tradeLimit, 0, DarkGreen); return(0); } } } } // **************** E N D ***************