//+------------------------------------------------------------------+ //| //| | //| | //+------------------------------------------------------------------+ #define SIGNAL_NONE 0 #define SIGNAL_BUY 1 #define SIGNAL_SELL 2 #define SIGNAL_CLOSEBUY 3 #define SIGNAL_CLOSESELL 4 #property copyright "Copyright © 2009, AKA TradeForexFX" #property link "http://www./" string Expert_Name = "PSAR Scalper v2"; //*********************** extern string EA_Comment = "PS " ; extern int MagicNumber = 786110; extern bool PlaySound_OnTrade_Open = true; extern string SoundFile = "abc.wav" ; string SoundFile2 = "abc2.wav" ; string SoundFile3 = "abc3.wav" ; extern bool ShowComments = true; extern bool ShowDetailedComments = true; bool SignalMail = False; extern int Maximum_Gap = 25; extern int Maximum_Spread = 5; extern int MinPipsMargin = 20; int FullLotMargin = 1500; int FullLotValue = 10; bool EachTickMode = True; bool DoCloseDoNotCloseQty = True; extern string TradingParameters = "------Trading Parameters -------" ; extern bool Use_5M_MA = True; extern bool Use_5M_PSAR = True; extern bool Use_15M_PSAR = True; extern double Lots = 0.6; bool StopLossMode = false; extern int InitialHardStopLoss = 500; bool TakeProfitMode = false; extern int InitialTakeProfit = 500; extern int Slippage = 3; extern bool Close_Lots_Mode = True; extern string DistributeAbove_Lots = "Fully distribute above Lots quantity " ; extern double First_Close = 0.3 ; extern double Second_Close = 0.29; extern double Third_Close = 0.01 ; //extern string REMINDER_Use_Multiples = "Use Multiples of One Tick Move as expalined above" ; extern int First_Close_TP = 15 ; extern int Second_Close_TP = 45 ; extern int Third_Close_TP = 350 ; bool StealthStopLossMode = True; extern int StealthStopLoss = 85; bool TrailingStopMode = false; int TrailingStop = 25; bool MoveStopWhenProfitIs = True; //bool MoveStopOnce = True; extern int MoveStopWhenProfit = 8; extern int MoveStopToProfitOf = -10; bool CloseExcept = True; extern double DoNotCloseQty = 0.01; extern int StopWhenProfit = 2; extern int SlippageForAbove = 3; extern string IMPORTANT = "Above Slippage should ATLEAST be 1 "; bool MoveStopOnceWhenQty = True; extern double MoveStopWhenQty = 0.01; extern int MoveStopBy = 500; //****************************************** // Inputs for trading hours extern string Trading_Hour_Parameters="--Trading Hour Parameters--"; extern string Trading_Hour_Parameters_="Use Hours according to your own Broker"; //extern string Trading_Hours="UseTradingHours - Enter 1 for YES, 0 for NO"; int UseTradingHours = 1; extern string Session1="Trade Session 1"; // - Enter 1 for YES, 0 for NO int TradeSession1 = 1; extern int Session1Start = 001; // Start trades after time extern int Session1Stop = 2359; // Stop trading after time extern string Session2="Trade Session 2"; // - Enter 1 for YES, 0 for NO int TradeSession2 = 1; extern int Session2Start = 800; // Start trades after time extern int Session2Stop = 2359; // Stop trading after time extern string Session3="Trade Session 3"; // - Enter 1 for YES, 0 for NO int TradeSession3 = 1; extern int Session3Start = 800; // Start trades after time extern int Session3Stop = 2200; // Stop trading after time extern string Session4="Trade Session 4"; // - Enter 1 for YES, 0 for NO int TradeSession4 = 1; extern int Session4Start = 800; // Start trades after time extern int Session4Stop = 2200; // Stop trading after time extern string Session5="Trade Session 5"; // - Enter 1 for YES, 0 for NO int TradeSession5 = 1; extern int Session5Start = 800; // Start trades after time extern int Session5Stop = 2200; // Stop trading after time bool YesStop; double myPoint; // Trading Hours variables end here //************************************ int BarCount; int Current; bool TickCheck = False; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { BarCount = Bars; if (EachTickMode) Current = 0; else Current = 1; myPoint = SetPoint(Symbol()); return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { int Order = SIGNAL_NONE; int Total, Ticket; double StopLossLevel, TakeProfitLevel; if (EachTickMode && Bars != BarCount) TickCheck = False; Total = OrdersTotal(); Order = SIGNAL_NONE; if (InitialHardStopLoss > 0 ) DoHardStopLoss(); if (Close_Lots_Mode) ClosePartialLots(); // if (MoveStopOnce) MoveStopOnceMode(); if (MoveStopWhenProfitIs) MoveStopWhenProfitIsMode(); if (ShowComments && !ShowDetailedComments) ShowCommentsMode(); if (ShowDetailedComments) ShowDetailedCommentsMode(); if (MoveStopOnceWhenQty) MoveStopOnceWhenQtyMode(); if (StealthStopLossMode) DoStealthStopLoss(); if (CloseExcept) DoCloseExcept(); // if (CloseQty01) DoCloseQty01(); // if (DoCloseDoNotCloseQty) CloseDoNotCloseQty(); //+------------------------------------------------------------------+ //| Variable Begin | //+------------------------------------------------------------------+ // double myPoint; double MA_5M_Current,MA_5M_Previous; MA_5M_Current = iMA(NULL, PERIOD_M5, 14, 0, MODE_SMA, PRICE_CLOSE, 0); MA_5M_Previous = iMA(NULL, PERIOD_M5, 14, 0, MODE_SMA, PRICE_CLOSE, 1); string M5_MA_Direction = "None"; if( MA_5M_Current > MA_5M_Previous && iClose(NULL,PERIOD_M5,0) > MA_5M_Previous && iOpen(NULL,PERIOD_M5,0) > MA_5M_Previous ) M5_MA_Direction = "Long"; if( MA_5M_Current < MA_5M_Previous && iClose(NULL,PERIOD_M5,0) < MA_5M_Previous && iOpen(NULL,PERIOD_M5,0) < MA_5M_Previous ) M5_MA_Direction = "Short"; string M5_PSAR_Direction = "None"; if( iClose(NULL,PERIOD_M5,0) > iSAR(NULL, PERIOD_M5,0.04,0.05, 0) ) M5_PSAR_Direction = "Long"; if( iClose(NULL,PERIOD_M5,0) < iSAR(NULL, PERIOD_M5,0.04,0.05, 0) ) M5_PSAR_Direction = "Short"; string M15_PSAR_Direction = "None"; if( iClose(NULL,PERIOD_M15,0) > iSAR(NULL, PERIOD_M15,0.04,0.05, 0) ) M15_PSAR_Direction = "Long"; if( iClose(NULL,PERIOD_M15,0) < iSAR(NULL, PERIOD_M15,0.04,0.05, 0) ) M15_PSAR_Direction = "Short"; string Trade_or_NoTrade = "YesTrade"; if( !Use_5M_MA && !Use_5M_PSAR && !Use_15M_PSAR ) Trade_or_NoTrade = "NoTrade"; string TradeDirection = "None"; if ( MathAbs(iClose( NULL, PERIOD_H1, 1) - iOpen( NULL, PERIOD_H1, 0)) < Maximum_Gap * Point && !Use_5M_MA || M5_MA_Direction == "Long" && !Use_5M_PSAR || M5_PSAR_Direction == "Long" && !Use_15M_PSAR || M15_PSAR_Direction == "Long" && Trade_or_NoTrade == "YesTrade" ) TradeDirection = "Long"; if ( MathAbs(iClose( NULL, PERIOD_H1, 1) - iOpen( NULL, PERIOD_H1, 0)) < Maximum_Gap * Point && !Use_5M_MA || M5_MA_Direction == "Short" && !Use_5M_PSAR || M5_PSAR_Direction == "Short" && !Use_15M_PSAR || M15_PSAR_Direction == "Short" && Trade_or_NoTrade == "YesTrade" ) TradeDirection = "Short"; string CloseDirection = "None"; if ( TradeDirection == "Long" ) CloseDirection = "CloseSell"; if( TradeDirection == "Short" ) CloseDirection = "CloseBuy"; double Spread = (Ask - Bid) / Point ; double Trade_0_1 = Maximum_Spread + 1 - Spread ; //+------------------------------------------------------------------+ //| Variable End | //+------------------------------------------------------------------+ //Check position bool IsTrade = False; for (int i = 0; i < Total; i ++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //******************************************* // Important to have code check MagicNumber and Symbol if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != MagicNumber) continue; //******************************************* if(OrderType() == OP_SELL || OrderType() == OP_BUY) { IsTrade = True; if(OrderType() == OP_BUY) { //+------------------------------------------------------------------+ //| Signal Begin(Exit Buy) | //+------------------------------------------------------------------+ if ( CloseDirection == "CloseBuy" ) Order = SIGNAL_CLOSEBUY; //+------------------------------------------------------------------+ //| Signal End(Exit Buy) | //+------------------------------------------------------------------+ if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) { OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen); if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy"); if (!EachTickMode) BarCount = Bars; IsTrade = False; continue; } //Trailing stop if(TrailingStopMode && TrailingStop > 0) { if(Bid - OrderOpenPrice() > Point * TrailingStop) { if(OrderStopLoss() < Bid - Point * TrailingStop) { OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen); if (!EachTickMode) BarCount = Bars; continue; } } } //Close_Lots - Buy } else { //Close //+------------------------------------------------------------------+ //| Signal Begin(Exit Sell) | //+------------------------------------------------------------------+ if ( CloseDirection == "CloseSell" ) Order = SIGNAL_CLOSESELL; //+------------------------------------------------------------------+ //| Signal End(Exit Sell) | //+------------------------------------------------------------------+ if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) { OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange); if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell"); if (!EachTickMode) BarCount = Bars; IsTrade = False; continue; } //Trailing stop if(TrailingStopMode && TrailingStop > 0) { if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) { if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) { OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange); if (!EachTickMode) BarCount = Bars; continue; } } //Close_Lots - Sell } // Trailing Stop } // Close } // OrderType }// for //******************************************* // Check trading sessions int NumOrders = 0; NumOrders = CalculateCurrentOrders(); if(NumOrders == 0) { YesStop = false; if (UseTradingHours == 1) { YesStop = CheckTradingTimes(); //Check position } if (YesStop == false) { //+------------------------------------------------------------------+ //| Signal Begin(Entry) && Buy3_1 > Buy3_2 && Sell3_1 < Sell3_2 && Buy6_1 > Buy6_2 && Sell6_1 < Sell6_2 | && Sell9_1 > Sell9_2 && Buy9_1 > Buy9_2 //+------------------------------------------------------------------+ if ( Trade_0_1 > 0 && // AccountEquity() < GlobalVariableGet("NoTradeEquity") || GlobalVariableGet("NoTradeEquity") == 0 && TradeDirection == "Long" ) Order = SIGNAL_BUY; if ( Trade_0_1 > 0 && // AccountEquity() < GlobalVariableGet("NoTradeEquity") || GlobalVariableGet("NoTradeEquity") == 0 && TradeDirection == "Short" ) Order = SIGNAL_SELL; //+------------------------------------------------------------------+ //| Signal End | //+------------------------------------------------------------------+ //Buy if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) { if(!IsTrade) { //Check free margin if (AccountFreeMargin() < (FullLotMargin * Lots + Lots*FullLotValue*MinPipsMargin)) { Print("We have no money. Free Margin = ", AccountFreeMargin()); return(0); } if (StopLossMode) StopLossLevel = Ask - InitialHardStopLoss * Point; else StopLossLevel = 0.0; if (TakeProfitMode) TakeProfitLevel = Ask + InitialTakeProfit * Point; else TakeProfitLevel = 0.0; Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, EA_Comment + TimeToStr(iTime(NULL,0,0),TIME_MINUTES) + " Buy" + Ask + " " + " " + MagicNumber, MagicNumber, 0, DodgerBlue); if(Ticket > 0) { if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) { Print("BUY order opened : ", OrderOpenPrice()); if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy"); if (PlaySound_OnTrade_Open) PlaySound(SoundFile); if (PlaySound_OnTrade_Open) PlaySound(SoundFile2); if (PlaySound_OnTrade_Open) PlaySound(SoundFile3); } else { Print("Error opening BUY order : ", GetLastError()); } } if (EachTickMode) TickCheck = True; if (!EachTickMode) BarCount = Bars; return(0); } } //Sell if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) { if(!IsTrade) { //Check free margin if (AccountFreeMargin() < (FullLotMargin * Lots + Lots*FullLotValue*MinPipsMargin)) { Print("We have no money. Free Margin = ", AccountFreeMargin()); return(0); } if (StopLossMode) StopLossLevel = Bid + InitialHardStopLoss * Point; else StopLossLevel = 0.0; if (TakeProfitMode) TakeProfitLevel = Bid - InitialTakeProfit * Point; else TakeProfitLevel = 0.0; Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel,EA_Comment + TimeToStr(iTime(NULL,0,0),TIME_MINUTES) + " Sell" + Bid + " " + " " + MagicNumber, MagicNumber, 0, DeepPink); if(Ticket > 0) { if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) { Print("SELL order opened : ", OrderOpenPrice()); if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell"); if (PlaySound_OnTrade_Open) PlaySound(SoundFile); if (PlaySound_OnTrade_Open) PlaySound(SoundFile2); if (PlaySound_OnTrade_Open) PlaySound(SoundFile3); } else { Print("Error opening SELL order : ", GetLastError()); } } if (EachTickMode) TickCheck = True; if (!EachTickMode) BarCount = Bars; return(0); } } } // YesStop } // NumOrders if (!EachTickMode) BarCount = Bars; return(0); } //+------------------------------------------------------------------+ // Return true if time is not in session for trading bool CheckOutsideSession(int SessionStart, int SessionStop, int ct) { if (SessionStart < SessionStop) { if (ct >= SessionStart && ct <= SessionStop) return (false); else return(true); } else { if (ct > SessionStop && ct < SessionStart) return (true); else return(false); } } bool CheckTradingTimes() { bool StopTrading; int ct; ct = Hour() * 100 + Minute(); StopTrading = true; if (TradeSession1 == 1) { StopTrading = CheckOutsideSession(Session1Start, Session1Stop, ct); } if (StopTrading == true) { if (TradeSession2 == 1) { StopTrading = CheckOutsideSession(Session2Start, Session2Stop, ct); } } if (StopTrading == true) { if (TradeSession3 == 1) { StopTrading = CheckOutsideSession(Session3Start, Session3Stop, ct); } } if (StopTrading == true) { if (TradeSession4 == 1) { StopTrading = CheckOutsideSession(Session4Start, Session4Stop, ct); } } if (StopTrading == true) { if (TradeSession5 == 1) { StopTrading = CheckOutsideSession(Session5Start, Session5Stop, ct); } } return(StopTrading); } int CalculateCurrentOrders() { int buys = 0, sells = 0, num = 0; for(int i=0;i= 0; i --) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //******************************************* // Important to have code check MagicNumber and Symbol if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != MagicNumber) continue; //******************************************* if(OrderType() == OP_SELL || OrderType() == OP_BUY) { IsTrade = True; if(OrderType() == OP_BUY) QtyOrders = OrderLots(); if(OrderType() == OP_SELL) QtyOrderSells = OrderLots(); if(OrderType() == OP_BUY) { //Close_Lots - Buy if(QtyOrders == Lots) { if(Bid - OrderOpenPrice() >= Point * First_Close_TP) { OrderClose(OrderTicket(), First_Close , Bid, Slippage, MediumSeaGreen); continue; } } if( ( (QtyOrders - Third_Close) > 0.00 ) && (QtyOrders - (Second_Close + Third_Close ) <= 0.00)) { if(Bid - OrderOpenPrice() >= (Point * Second_Close_TP)) { OrderClose(OrderTicket(), Second_Close , Bid, Slippage, MediumSeaGreen); continue; } } if((QtyOrders - Third_Close <= 0.00) && (QtyOrders > 0.00 )) { if(Bid - OrderOpenPrice() >= Point * Third_Close_TP) { OrderClose(OrderTicket(), QtyOrders , Bid, Slippage, MediumSeaGreen); continue; } } } if(OrderType() == OP_SELL) { //Close_Lots - Sell // Sell trades must exit at Ask price if(QtyOrderSells == Lots) { if(OrderOpenPrice() - Ask >= Point * First_Close_TP) { OrderClose(OrderTicket(), First_Close , Ask, Slippage, MediumSeaGreen); continue; } } if((QtyOrderSells - Third_Close > 0.00 ) &&(QtyOrderSells - (Second_Close + Third_Close ) <= 0.00) ) { if(OrderOpenPrice() - Ask >= (Point * Second_Close_TP)) { OrderClose(OrderTicket(), Second_Close , Ask, Slippage, MediumSeaGreen); continue; } } if((QtyOrderSells - Third_Close <= 0.00 ) && (QtyOrderSells > 0.00 )) { if(OrderOpenPrice() - Ask >= Point * Third_Close_TP) { OrderClose(OrderTicket(), QtyOrderSells , Ask, Slippage, MediumSeaGreen); continue; } } } } } } // Fix of Point for Brokers using 3 and 5 digit pricing double SetPoint(string mySymbol) { double mPoint, myDigits; myDigits = MarketInfo (mySymbol, MODE_DIGITS); if (myDigits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } void ShowCommentsMode() { double Total =0; double QtyOrders = 0.00; //Check position bool IsTrade = False; // Better to do this from highest to lowest for (int i = OrdersTotal(); i >= 0; i --) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); Total = Total + OrderLots(); //******************************************* // Important to have code check MagicNumber and Symbol if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber &&( OrderType() == OP_BUY || OrderType() == OP_SELL)) QtyOrders = OrderLots(); string TradingHour = "We are Trading" ; if (UseTradingHours != 1 || QtyOrders != 0)TradingHour = "We are Trading"; if(QtyOrders == 0) { YesStop = false; if (UseTradingHours == 1) { YesStop = CheckTradingTimes(); //Check position if (YesStop == true && QtyOrders == 0) TradingHour = "Trading is stopped"; }} } Comment ( "", "\n", TradingHour, "\n", "Spread ", MathAbs(Ask - Bid)/ Point ,"\n", "Qty in trade ", QtyOrders ,"\n", "Orders Total " , Total,"\n", "EA_Comment " , EA_Comment ); } void ShowDetailedCommentsMode() { double MA_5M_Current,MA_5M_Previous; MA_5M_Current = iMA(NULL, PERIOD_M5, 14, 0, MODE_SMA, PRICE_CLOSE, 0); MA_5M_Previous = iMA(NULL, PERIOD_M5, 14, 0, MODE_SMA, PRICE_CLOSE, 1); string M5_MA_Direction = "None"; if( MA_5M_Current > MA_5M_Previous && iClose(NULL,PERIOD_M5,0) > MA_5M_Previous && iOpen(NULL,PERIOD_M5,0) > MA_5M_Previous ) M5_MA_Direction = "Long"; if( MA_5M_Current < MA_5M_Previous && iClose(NULL,PERIOD_M5,0) < MA_5M_Previous && iOpen(NULL,PERIOD_M5,0) < MA_5M_Previous ) M5_MA_Direction = "Short"; string M5_PSAR_Direction = "None"; if( iClose(NULL,PERIOD_M5,0) > iSAR(NULL, PERIOD_M5,0.04,0.05, 0) ) M5_PSAR_Direction = "Long"; if( iClose(NULL,PERIOD_M5,0) < iSAR(NULL, PERIOD_M5,0.04,0.05, 0) ) M5_PSAR_Direction = "Short"; string M15_PSAR_Direction = "None"; if( iClose(NULL,PERIOD_M15,0) > iSAR(NULL, PERIOD_M15,0.04,0.05, 0) ) M15_PSAR_Direction = "Long"; if( iClose(NULL,PERIOD_M15,0) < iSAR(NULL, PERIOD_M15,0.04,0.05, 0) ) M15_PSAR_Direction = "Short"; string Trade_or_NoTrade = "YesTrade"; if( !Use_5M_MA && !Use_5M_PSAR && !Use_15M_PSAR ) Trade_or_NoTrade = "NoTrade"; string TradeDirection = "None"; if ( MathAbs(iClose( NULL, PERIOD_H1, 1) - iOpen( NULL, PERIOD_H1, 0)) < Maximum_Gap * Point && !Use_5M_MA || M5_MA_Direction == "Long" && !Use_5M_PSAR || M5_PSAR_Direction == "Long" && !Use_15M_PSAR || M15_PSAR_Direction == "Long" && Trade_or_NoTrade == "YesTrade" ) TradeDirection = "Long"; if ( MathAbs(iClose( NULL, PERIOD_H1, 1) - iOpen( NULL, PERIOD_H1, 0)) < Maximum_Gap * Point && !Use_5M_MA || M5_MA_Direction == "Short" && !Use_5M_PSAR || M5_PSAR_Direction == "Short" && !Use_15M_PSAR || M15_PSAR_Direction == "Short" && Trade_or_NoTrade == "YesTrade" ) TradeDirection = "Short"; string CloseDirection = "None"; if ( TradeDirection == "Long" ) CloseDirection = "CloseSell"; if( TradeDirection == "Short" ) CloseDirection = "CloseBuy"; double Total =0; double QtyOrders = 0.00; double OrderOpenPr = 0.00; //Check position bool IsTrade = False; // Better to do this from highest to lowest for (int i = OrdersTotal(); i >= 0; i --) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); Total = Total + OrderLots(); //******************************************* // Important to have code check MagicNumber and Symbol if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber &&( OrderType() == OP_BUY || OrderType() == OP_SELL)) QtyOrders = OrderLots(); if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber &&( OrderType() == OP_BUY || OrderType() == OP_SELL)) OrderOpenPr = OrderOpenPrice(); string TradingHour = "We are Trading" ; if (UseTradingHours != 1 || QtyOrders != 0)TradingHour = "We are Trading"; if(QtyOrders == 0) { YesStop = false; if (UseTradingHours == 1) { YesStop = CheckTradingTimes(); //Check position if (YesStop == true && QtyOrders == 0) TradingHour = "Trading is stopped"; }} } Comment ( "", "\n", TradingHour, "\n", "Spread ", MathAbs(Ask - Bid)/ Point ,"\n", "Qty in trade ", QtyOrders ,"\n", "Orders Total " , Total, "Bid", " ", Bid, " " , "Ask", " " , Ask, "\n", "Bid - OrderOpenPr " , Bid , " ", OrderOpenPr , " ", (Bid - OrderOpenPr), " ",Point*StopWhenProfit, "\n", "EA_Comment " , EA_Comment,"\n", "M5 MA Direction " , M5_MA_Direction, " ", "\n", "M5 PSAR Direction " , M5_PSAR_Direction,"\n", "M15 PSAR Direction " , M15_PSAR_Direction,"\n", "Trade Direction " , TradeDirection ); } void DoHardStopLoss() { int Total; double QtyOrders = 0.00; double QtyOrderSells = 0.00; Total = OrdersTotal(); //Check position bool IsTrade = False; // Better to do this from highest to lowest // for (int i = 0; i < Total; i ++) { for (int i = Total - 1; i >= 0; i --) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //******************************************* // Important to have code check MagicNumber and Symbol if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != MagicNumber) continue; //******************************************* // if(OrderType() == OP_SELL || OrderType() == OP_BUY) continue; IsTrade = True; if(OrderType() == OP_BUY) { QtyOrders = OrderLots(); //MoveOnce if(InitialHardStopLoss > 0) { if(QtyOrders == Lots) { if(OrderStopLoss() == 0 ) { OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() - Point * InitialHardStopLoss, OrderOpenPrice() + Point * InitialTakeProfit, 0, Red); if(!EachTickMode) BarCount = Bars; continue; } } } } else { QtyOrderSells = OrderLots(); //MoveOnce if(InitialHardStopLoss > 0) { if(QtyOrderSells == Lots) { if(OrderStopLoss() == 0 ) { OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() + Point * InitialHardStopLoss, OrderOpenPrice() - Point * InitialTakeProfit, 0, Red); if(!EachTickMode) BarCount = Bars; continue; } } } } } } //=============================== Move Stop When Profit ======================== void MoveStopWhenProfitIsMode()// when Profit reaches { int Total; double QtyOrders = 0.00; double QtyOrderSells = 0.00; Total = OrdersTotal(); //Check position bool IsTrade = False; // Better to do this from highest to lowest // for (int i = 0; i < Total; i ++) { for (int i = Total - 1; i >= 0; i --) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //******************************************* // Important to have code check MagicNumber and Symbol if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != MagicNumber) continue; //******************************************* // if(OrderType() == OP_SELL || OrderType() == OP_BUY) continue; IsTrade = True; if(OrderType() == OP_BUY) { QtyOrders = OrderLots(); //MoveOnce if(MoveStopWhenProfit > 0 && QtyOrders > MoveStopWhenQty ) { if((Bid - OrderOpenPrice()) >= (Point * MoveStopWhenProfit)) { if(OrderStopLoss() != OrderOpenPrice() + Point * MoveStopToProfitOf) {OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() + Point * MoveStopToProfitOf, OrderTakeProfit(), 0, Red); if(!EachTickMode) BarCount = Bars; continue; } } } } else { QtyOrderSells = OrderLots(); //MoveOnce if(MoveStopWhenProfit > 0 && QtyOrderSells > MoveStopWhenQty ) { if((OrderOpenPrice() - Ask) >= Point * MoveStopWhenProfit) { if(OrderStopLoss() != OrderOpenPrice() - Point * MoveStopToProfitOf) { OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() - Point * MoveStopToProfitOf, OrderTakeProfit(), 0, Red); if(!EachTickMode) BarCount = Bars; continue; } } } } } } void MoveStopOnceWhenQtyMode()// when Qty in hand = 0.01 and { int Total; double QtyOrders = 0.00; double QtyOrderSells = 0.00; Total = OrdersTotal(); //Check position bool IsTrade = False; // Better to do this from highest to lowest // for (int i = 0; i < Total; i ++) { for (int i = Total - 1; i >= 0; i --) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //******************************************* // Important to have code check MagicNumber and Symbol if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != MagicNumber) continue; //******************************************* // if(OrderType() == OP_SELL || OrderType() == OP_BUY) continue; IsTrade = True; if(OrderType() == OP_BUY) { QtyOrders = OrderLots(); //MoveOnce if(QtyOrders == MoveStopWhenQty ) { if(OrderStopLoss() != OrderOpenPrice() - Point * MoveStopBy || OrderTakeProfit() != OrderOpenPrice() + Point * MoveStopBy) { if(OrderStopLoss() != OrderOpenPrice() - Point * MoveStopBy || OrderTakeProfit() != OrderOpenPrice() + Point * MoveStopBy) { OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() - Point * MoveStopBy, OrderOpenPrice() + Point * MoveStopBy, 0, Red); if(!EachTickMode) BarCount = Bars; continue; } } } } else { QtyOrderSells = OrderLots(); //MoveOnce if(QtyOrderSells == MoveStopWhenQty) { if(OrderStopLoss() != OrderOpenPrice() + Point * MoveStopBy || OrderTakeProfit() != OrderOpenPrice() - Point * MoveStopBy) { if(OrderStopLoss() != OrderOpenPrice() + Point * MoveStopBy || OrderTakeProfit() != OrderOpenPrice() - Point * MoveStopBy) { OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() + Point * MoveStopBy, OrderOpenPrice() - Point * MoveStopBy, 0, Red); if(!EachTickMode) BarCount = Bars; continue; } } } } } } void DoStealthStopLoss() { int Total; double QtyOrders = 0.00; double QtyOrderSells = 0.00; double OrderOpenPr = 0.00; Total = OrdersTotal(); //Check position bool IsTrade = False; // Better to do this from highest to lowest // for (int i = 0; i < Total; i ++) { for (int i = Total - 1; i >= 0; i --) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //******************************************* // Important to have code check MagicNumber and Symbol if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != MagicNumber) continue; //******************************************* if(OrderType() == OP_SELL || OrderType() == OP_BUY) { IsTrade = True; // if(OrderType() == OP_BUY) // if(OrderType() == OP_SELL) if(OrderType() == OP_BUY) { //Close_Lots - Buy QtyOrders = OrderLots(); OrderOpenPr =OrderOpenPrice(); // OrderStopLo = OrderStopLoss(); if(QtyOrders == Lots ) { if( (OrderOpenPr - Bid == Point * StealthStopLoss ) || (OrderOpenPr - Bid > Point * StealthStopLoss)) // { OrderClose(OrderTicket(), QtyOrders - DoNotCloseQty , Bid, SlippageForAbove, MediumSeaGreen); continue; } } } if(OrderType() == OP_SELL) { //Close_Lots - Sell // Sell trades must exit at Ask price QtyOrderSells = OrderLots(); OrderOpenPr =OrderOpenPrice(); // OrderStopLo = OrderStopLoss(); if(QtyOrderSells == Lots ) { if((Ask - OrderOpenPr == Point * StealthStopLoss) || (Ask - OrderOpenPr > Point * StealthStopLoss)) // { OrderClose(OrderTicket(), QtyOrderSells - DoNotCloseQty , Ask, SlippageForAbove, MediumSeaGreen); continue; } } } } } } /* void DoCloseExcept() { int Total; Total = OrdersTotal(); //Check position bool IsTrade = False; // Better to do this from highest to lowest // for (int i = 0; i < Total; i ++) { for (int i = Total - 1; i >= 0; i --) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //******************************************* // Important to have code check MagicNumber and Symbol if (OrderSymbol() != Symbol() && OrderMagicNumber() != MagicNumber ) continue; // if (OrderMagicNumber() != MagicNumber) continue; //******************************************* { if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber &&( OrderType() == OP_BUY)) { if(OrderStopLoss() == OrderOpenPrice() + Point * MoveStopToProfitOf ) { if(Bid - OrderOpenPrice() == Point * StopWhenProfit || Bid - OrderOpenPrice() < Point * StopWhenProfit) { OrderClose(OrderTicket(), OrderLots() - DoNotCloseQty , Bid, SlippageForAbove, MediumSeaGreen); continue; } } } // if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL) { if(OrderStopLoss() == OrderOpenPrice() - Point * MoveStopToProfitOf ) { //Close_Lots - Sell // Sell trades must exit at Ask price if(OrderOpenPrice() - Ask == Point * StopWhenProfit || OrderOpenPrice() - Ask < Point * StopWhenProfit) { OrderClose(OrderTicket(), OrderLots() - DoNotCloseQty , Ask, SlippageForAbove, MediumSeaGreen); continue; } } } } } } */ void DoCloseExcept() { for (int i=OrdersTotal()-1; i>= 0; i--) { if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { Alert("DoCloseExcept: OrderSelect failed - ",ErrorDescription(GetLastError())); } else { if(OrderSymbol()==Symbol() && OrderMagicNumber()== MagicNumber) { RefreshRates(); if((OrderType()==OP_BUY && OrderStopLoss()==OrderOpenPrice()+Point*MoveStopToProfitOf && Bid-OrderOpenPrice()<=Point*StopWhenProfit) || (OrderType()==OP_SELL && OrderStopLoss()==OrderOpenPrice()-Point*MoveStopToProfitOf && OrderOpenPrice()-Ask<=Point*StopWhenProfit)) { double closePrice=Ask; if(OrderType()==OP_BUY)closePrice=Bid; if(!OrderClose(OrderTicket(),OrderLots()-DoNotCloseQty,closePrice,SlippageForAbove,MediumSeaGreen)) { Alert("DoCloseExcept: OrderClose failed - ",ErrorDescription(GetLastError())); } } } } } }