#property strict #include CAppDialog OurInterface; #include CButton NewSLButton,NewTPButton; #include CEdit LotSize,StartSL,StartTP,StartTSL; #include CLabel LotLabel,StartSLLabel,StartTPLabel,StartTSLLabel; bool removeFromThisChartOnly = true; double TradeVolume = 1; double sd = 100; double sl = 493; // 493 double tp = 507; // 507 double tsl= 0; double slorg = sl; int closestprice = 20; double buynewtsl = 10000000; double sellnewtsl = 0; double buyprice = 100000; double sellprice=1; double buylimitprice = 1; double selllimitprice=100000; datetime orderholdtime = 5; // in minutes datetime timeremaining = 0; string OK_TO_BUY_afterLoss = "OK"; string OK_TO_SELL_afterLoss = "OK"; string OK_TO_TRADE= "NG"; int MagicBuyMAYT =100111400; int MagicSellMAYT = 100222400; int MagicBuyGoldRSI5 = 100111905; int MagicSellGoldRSI5 = 100222905; double UpperRSIValue=98; double LowerRSIValue=2; double RSI_Max = 0; double RSI_Min = 100; string RSI_OK_TO_BUY = "NG"; string RSI_OK_TO_SELL = "NG"; string Last_Buy_Profit = "NG"; string Last_Sell_Profit = "NG"; double MaxAccountBalance = AccountBalance(); double MinAccountBalance = 1000000; double InitialAccountBalance = AccountBalance(); int start_hour = 5; // 10 is best and 5:30 int start_min = 30; int end_hour = 15; // 15 is best int end_min = 0; int second_start_hour = 18; // 18 is best int second_start_min = 0; int second_end_hour = 22; // 22:30 is best int second_end_min = 30; const string LotName = "Lot_Volume"; const string Start_SL = "Start_SL"; const string Start_TP = "Start_TP"; const string Start_TSL = "Start_TSL"; // int speedSlowness = 10000000; // intended for 32 speed, adjust as desired int speedSlowness = 0; int OnInit() { ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true); OurInterface.Create(0,"Moving Average YT v5.2",0,50,70,320,240); LotLabel.Create(0,"LotLabel",0,10,10,50,30); OurInterface.Add(LotLabel); LotLabel.Text("Lot Size:"); LotSize.Create(0,LotName,0,90,10,130,30); OurInterface.Add(LotSize); LotSize.Text(string(TradeVolume)); StartSLLabel.Create(0,"StartSLLabel",0,10,40,50,60); OurInterface.Add(StartSLLabel); StartSLLabel.Text("Set SL:"); StartSL.Create(0,Start_SL,0,90,40,130,60); OurInterface.Add(StartSL); StartSL.Text(string(sl)); StartTPLabel.Create(0,"StartTPLabel",0,10,70,50,90); OurInterface.Add(StartTPLabel); StartTPLabel.Text("Set TP:"); StartTP.Create(0,Start_TP,0,90,70,130,90); OurInterface.Add(StartTP); StartTP.Text(string(tp)); NewSLButton.Create(0,"NewSLButton",0,140,40,250,60); OurInterface.Add(NewSLButton); NewSLButton.Text("Update SL"); NewTPButton.Create(0,"NewTPButton",0,140,70,250,90); OurInterface.Add(NewTPButton); NewTPButton.Text("Update TP"); StartTSLLabel.Create(0,"StartTSLLabel",0,10,100,50,120); OurInterface.Add(StartTSLLabel); StartTSLLabel.Text("Set TSL:"); StartTSL.Create(0,Start_TSL,0,90,100,130,120); OurInterface.Add(StartTSL); StartTSL.Text(string(tsl)); OurInterface.Run(); return(INIT_SUCCEEDED); } void OnTick() { for(int i=0; istart_time && TimeCurrent()second_start_time && TimeCurrent()TimeCurrent()) { timeremaining = newordertime-TimeCurrent(); OK_TO_BUY_afterLoss = "NG"; OK_TO_SELL_afterLoss = "OK"; } else {timeremaining = 0; OK_TO_BUY_afterLoss = "OK"; } } if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicSellMAYT ) { datetime ctm=OrderCloseTime(); datetime newordertime = ctm+orderholdtime*60; if (newordertime>TimeCurrent()) { timeremaining = newordertime-TimeCurrent(); OK_TO_BUY_afterLoss = "OK"; OK_TO_SELL_afterLoss = "NG"; } else { timeremaining = 0; OK_TO_SELL_afterLoss = "OK"; } } }} if (lastlosscheck()==false) { if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY)) { if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicBuyMAYT) { Last_Buy_Profit = "OK"; } if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicSellMAYT) { Last_Sell_Profit = "OK"; } }} // Trailing BuyStop Pending Order double buyclosestprice = Ask + closestprice*_Point; buyclosestprice = NormalizeDouble(buyclosestprice, _Digits); if (buyprice >Ask + sd*_Point) { double buystopnew = Ask + sd*_Point; buystopnew = NormalizeDouble(buystopnew, _Digits); buyprice = buystopnew; } if (buyprice-buyclosestprice=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol()==Symbol()) { if(OrderType()==OP_BUYSTOP && OrderMagicNumber()==MagicBuyGoldRSI5) { if (OrderOpenPrice() != buyprice && OrderOpenPrice() > buyprice) { double buystopsl = buyprice-sl*_Point; buystopsl = NormalizeDouble(buystopsl, _Digits); double buystoptp = buyprice+tp*_Point; buystoptp = NormalizeDouble(buystoptp, _Digits); if(tp==0){buystoptp=0;} if(sl==0){buystopsl=0;} // int BuyStopModify = OrderModify (OrderTicket(),buyprice,buystopsl,buystoptp,0,clrAqua); }}}}}} // Trailing SellStop Pending Order double sellclosestprice = Bid - closestprice*_Point; sellclosestprice = NormalizeDouble(sellclosestprice, _Digits); if (sellprice< (Bid - sd*_Point)) { double sellstopnew = Bid - sd*_Point; sellstopnew = NormalizeDouble(sellstopnew, _Digits); sellprice = sellstopnew;} if (sellclosestprice-sellprice=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol()==Symbol()) { if(OrderType()==OP_SELLSTOP && OrderMagicNumber()==MagicSellGoldRSI5) { if(OrderOpenPrice() != sellprice && OrderOpenPrice() < sellprice) { double sellstopsl = sellprice +sl*_Point; sellstopsl = NormalizeDouble(sellstopsl, _Digits); double sellstoptp = sellprice -tp*_Point; sellstoptp = NormalizeDouble(sellstoptp, _Digits); if(tp==0){sellstoptp=0;} if(sl==0){sellstopsl=0;} // int SellStopModify = OrderModify (OrderTicket(),sellprice,sellstopsl,sellstoptp,0,clrBlue); }}}}}} // Trailing BuyLimit Pending Order if (buylimitprice =0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol()==Symbol()) { if(OrderType()==OP_BUYLIMIT && OrderMagicNumber()==MagicBuyGoldRSI5) { if (OrderOpenPrice() != buylimitprice && OrderOpenPrice() < buylimitprice) { double buylimitsl = buylimitprice-sl*_Point; buylimitsl = NormalizeDouble(buylimitsl, _Digits); double buylimittp = buylimitprice+tp*_Point; buylimittp = NormalizeDouble(buylimittp, _Digits); if(tp==0){buylimittp=0;} if(sl==0){buylimitsl=0;} int BuylimitModify = OrderModify (OrderTicket(),buylimitprice,buylimitsl,buylimittp,0,clrAqua); }}}}}} // Trailing SellLimit Pending Order if (selllimitprice> (Bid + sd*_Point)) { double sellstopnew = Bid + sd*_Point; sellstopnew = NormalizeDouble(sellstopnew, _Digits); selllimitprice = sellstopnew;} if (selllimitprice-Bid=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol()==Symbol()) { if(OrderType()==OP_SELLLIMIT && OrderMagicNumber()==MagicSellGoldRSI5) { if(OrderOpenPrice() != selllimitprice && OrderOpenPrice() > selllimitprice) { double sellstopsl = selllimitprice +sl*_Point; sellstopsl = NormalizeDouble(sellstopsl, _Digits); double sellstoptp = selllimitprice -tp*_Point; sellstoptp = NormalizeDouble(sellstoptp, _Digits); if(tp==0){sellstoptp=0;} if(sl==0){sellstopsl=0;} int SellStopModify = OrderModify (OrderTicket(),selllimitprice,sellstopsl,sellstoptp,0,clrBlue); }}}}}} // Trailing StopLoss for (int b= OrdersTotal()-1;b>=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol()==Symbol()) { // Trailing StopLoss for Buy if(tsl!=0 && Ask> OrderOpenPrice()+ tsl*_Point) { if(OrderType()==OP_BUY && OrderMagicNumber()==MagicBuyMAYT) { if (buynewtsl Bid + tsl*_Point || OrderStopLoss()==0) { sellnewtsl = Bid +tsl*_Point; sellnewtsl = NormalizeDouble(sellnewtsl, _Digits); } if (sellnewtsl-Bid0 && ((maFast1-maFast2)>0.5 || (maFast1-maFast3)>0.5) && (Open[0]>maFast1 || Close[1]>maFast1) && Ask-Open[0]>1 && CheckBuyStopOpenOrders(MagicBuyMAYT)==false && CheckBuyOpenOrders(MagicBuyMAYT)==false && OK_TO_BUY_afterLoss == "OK" && OK_TO_TRADE=="OK"){ // && Close[1]>Open[1] maFast1>maSlow1 && Sleep (5000); if (Last_Buy_Profit == "OK") sl=sl/2; BuyOrder(MagicBuyMAYT); Last_Buy_Profit = "NG"; sl = slorg; } if ( (maFast2-maFast1)-(maSlow2-maSlow1)>0 && ((maFast2-maFast1)>0.3 || (maFast3-maFast1)>0.3) && (Open[0]1 && CheckSellStopOpenOrders(MagicSellMAYT)==false && CheckSellOpenOrders(MagicSellMAYT)==false && OK_TO_SELL_afterLoss == "OK" && OK_TO_TRADE=="OK"){ Sleep (5000); if (Last_Sell_Profit == "OK") sl=sl/2; SellOrder(MagicSellMAYT); Last_Sell_Profit = "NG"; sl = slorg; // && Close[1]maFast1 && CheckSellOpenOrders(MagicSellMAYT)==true) { // CloseSellOrders(MagicSellMAYT); } double RSI = iRSI(_Symbol,PERIOD_M5,2,0,0); if (RSI>RSI_Max) RSI_Max = RSI; if (RSIfirst_start_time && TimeCurrent()second_start_time && TimeCurrent()=UpperRSIValue && CheckSellStopOpenOrders()==false && CheckSellOpenOrders()==false && ((TimeCurrent()>first_start_time && TimeCurrent()second_start_time && TimeCurrent()=UpperRSIValue && CheckSellStopOpenOrders(MagicSellGoldRSI5)==false && CheckSellOpenOrders(MagicSellGoldRSI5)==false && OK_TO_TRADE=="OK") { // SellStopOrder(MagicSellGoldRSI5); RSI_Max = 0; RSI_OK_TO_SELL = "NG"; } double CurrentAccountBalance = AccountBalance(); if (MaxAccountBalance < AccountBalance()) MaxAccountBalance = AccountBalance(); if (MinAccountBalance > AccountBalance()) MinAccountBalance = AccountBalance(); Comment ("Initial Account Balance was ",InitialAccountBalance,", Minimum Balance was ",MinAccountBalance,", Max Balance was ",MaxAccountBalance,", Current Balance is ",CurrentAccountBalance," and Current Equity is ",AccountEquity(), "\nmaFast1 - Ask", maFast1-Ask, "\n Server Time: ", TimeCurrent(), "\n Local Time: ", LocalTime(), ""); if (CheckBuyOpenOrders(MagicBuyMAYT)==true && tsl!=0) { ObjectCreate(0,"Buy New TSL",OBJ_HLINE,0,0,buynewtsl); ObjectSetDouble(0,"Buy New TSL",OBJPROP_PRICE,buynewtsl); ObjectSet("Buy New TSL", OBJPROP_STYLE,STYLE_DASHDOT); ObjectSetText("Buy New TSL", "tsl-buy", 10, "Times New Roman", clrBlue) ; ObjectSetInteger(0,"Buy New TSL",OBJPROP_COLOR,clrRed); } if (CheckBuyOpenOrders(MagicBuyMAYT)==false || tsl==0) ObjectDelete(0,"Buy New TSL"); if (CheckSellOpenOrders(MagicSellMAYT)==true && tsl!=0) { ObjectCreate(0,"Sell New TSL",OBJ_HLINE,0,0,sellnewtsl); ObjectSetDouble(0,"Sell New TSL",OBJPROP_PRICE,sellnewtsl); ObjectSet("Sell New TSL", OBJPROP_STYLE,STYLE_DASHDOT); ObjectSetText("Sell New TSL", "tsl-sell", 10, "Times New Roman", clrBlue) ; ObjectSetInteger(0,"Sell New TSL",OBJPROP_COLOR,clrRed); } if (CheckSellOpenOrders(MagicSellMAYT)==false || tsl==0) ObjectDelete(0,"Sell New TSL"); if (CheckBuyLimitOpenOrders(MagicBuyMAYT)==true) { ObjectCreate(0,"BuyLimit New Expected",OBJ_HLINE,0,0,buylimitprice); ObjectSetDouble(0,"BuyLimit New Expected",OBJPROP_PRICE,buylimitprice); ObjectSet("BuyLimit New Expected", OBJPROP_STYLE,STYLE_DASHDOT); ObjectSetText("BuyLimit New Expected", "New BuyLimit", 10, "Times New Roman", clrBlue) ; ObjectSetInteger(0,"BuyLimit New Expected",OBJPROP_COLOR,clrGreen); } if (CheckBuyLimitOpenOrders(MagicBuyMAYT)==false) ObjectDelete(0,"BuyLimit New Expected"); if (CheckSellLimitOpenOrders(MagicSellMAYT)==true) { ObjectCreate(0,"SellLimit New Expected",OBJ_HLINE,0,0,selllimitprice); ObjectSetDouble(0,"SellLimit New Expected",OBJPROP_PRICE,selllimitprice); ObjectSet("SellLimit New Expected", OBJPROP_STYLE, STYLE_DASHDOT); ObjectSetText("SellLimit New Expected", "New SellLimit", 10, "Times New Roman", clrBlue) ; ObjectSetInteger(0,"SellLimit New Expected",OBJPROP_COLOR,clrBlue); } if (CheckSellLimitOpenOrders(MagicSellMAYT)==false) ObjectDelete(0,"SellLimit New Expected"); } // Event Handling void OnChartEvent (const int id, const long &lparam, const double &dparam, const string &sparam) { OurInterface.OnEvent(id,lparam,dparam, sparam); if (id == CHARTEVENT_OBJECT_ENDEDIT){ if(sparam == LotName) { string volumeText = ObjectGetString(0, LotName, OBJPROP_TEXT); TradeVolume = StringToDouble(volumeText); ObjectSetString(0, LotName, OBJPROP_TEXT, string(TradeVolume)); return; } if(sparam == Start_TP) { string volumeText = ObjectGetString(0, Start_TP, OBJPROP_TEXT); tp = StringToDouble(volumeText); ObjectSetString(0, Start_TP, OBJPROP_TEXT, string(tp)); return; } if(sparam == Start_SL) { string volumeText = ObjectGetString(0, Start_SL, OBJPROP_TEXT); sl = StringToDouble(volumeText); ObjectSetString(0, Start_SL, OBJPROP_TEXT, string(sl)); return; } if(sparam == Start_TSL) { string volumeText = ObjectGetString(0, Start_TSL, OBJPROP_TEXT); tsl = StringToDouble(volumeText); ObjectSetString(0, Start_TSL, OBJPROP_TEXT, string(tsl)); return; } } // if an object was clicked if(id==CHARTEVENT_OBJECT_CLICK) { if(sparam=="NewSLButton") { // Chart Output Comment ("New Stop Loss has been updated"); double buynewsl = Ask - sl*_Point; buynewsl = NormalizeDouble(buynewsl, _Digits); double sellnewsl = Bid + sl*_Point; sellnewsl = NormalizeDouble(sellnewsl, _Digits); for (int b= OrdersTotal()-1;b>=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol()==Symbol()) { // New Take Profit for Buy if(OrderType()==OP_BUY && OrderMagicNumber()==MagicBuyMAYT) { int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(),buynewsl,OrderTakeProfit(),0,clrAqua); } // New Take Profit for Sell if(OrderType()==OP_SELL&& OrderMagicNumber()==MagicSellMAYT) { int SellModify = OrderModify (OrderTicket(),OrderOpenPrice(),sellnewsl,OrderTakeProfit() ,0,clrBlue); } } } } } if(sparam=="NewTPButton") { // Chart Output Comment ("New Taking Profit has been updated"); double buynewtp = Ask + tp*_Point; buynewtp = NormalizeDouble(buynewtp, _Digits); double sellnewtp = Bid - tp*_Point; sellnewtp = NormalizeDouble(sellnewtp, _Digits); for (int b= OrdersTotal()-1;b>=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol()==Symbol()) { // New Take Profit for Buy if(OrderType()==OP_BUY && OrderMagicNumber()==MagicBuyMAYT) { int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(),OrderStopLoss(),buynewtp,0,clrAqua); } // New Take Profit for Sell if(OrderType()==OP_SELL && OrderMagicNumber()==MagicSellMAYT) { int SellModify = OrderModify (OrderTicket(),OrderOpenPrice(),OrderStopLoss(),sellnewtp ,0,clrBlue); } } } } } } } void OnDeinit(const int reason) { OurInterface.Destroy(reason); } void BuyOrder(int MagicBuy) { if (sl==0 && tsl!=0) sl=tsl; double buy = Ask; buy = NormalizeDouble(buy, _Digits); double slbuy = buy-sl*Point; slbuy = NormalizeDouble(slbuy, _Digits); double tpbuy = buy+tp*Point; tpbuy = NormalizeDouble(tpbuy, _Digits); if (sl==0) slbuy=0; int orderbuystop = OrderSend (_Symbol,OP_BUY,TradeVolume,buy,0,slbuy,tpbuy,NULL,MagicBuy,0,clrBlue); buyprice = buy; if (sl!=0 && tsl!=0) buynewtsl=slbuy; } void SellOrder(int MagicSell) { if (sl==0 && tsl!=0) sl=tsl; double sell = Bid; sell = NormalizeDouble(sell, _Digits); double tpsellnew = sell-tp*Point; tpsellnew = NormalizeDouble(tpsellnew, _Digits); double slsellnew = sell+sl*Point; slsellnew = NormalizeDouble(slsellnew, _Digits); if (sl==0) slsellnew=0; int ordersell = OrderSend (_Symbol,OP_SELL,TradeVolume,sell,0,slsellnew,tpsellnew,NULL,MagicSell,0,clrGreen); sellprice = sell; if (sl!=0 && tsl!=0) sellnewtsl=slsellnew; } void BuyLimitOrder(int MagicBuy) { if (sl==0 && tsl!=0) sl=tsl; double buy = Ask-sd*Point; // Bid-8*_Point; buy = NormalizeDouble(buy, _Digits); double slbuy = buy-sl*Point; slbuy = NormalizeDouble(slbuy, _Digits); double tpbuy = buy+tp*Point; tpbuy = NormalizeDouble(tpbuy, _Digits); if(sl==0){slbuy=0;} if(tp==0){tpbuy=0;} int orderlimitstop = OrderSend (_Symbol,OP_BUYLIMIT,TradeVolume,buy,0,slbuy,tpbuy,NULL,MagicBuy,0,clrBlue); buylimitprice = buy; if (sl!=0 && tsl!=0) buynewtsl=slbuy; } void SellLimitOrder(int MagicSell) { if (sl==0 && tsl!=0) sl=tsl; double sell = Bid+sd*Point; // Ask+8*_Point; sell = NormalizeDouble(sell, _Digits); double tpsellnew = sell-tp*Point; tpsellnew = NormalizeDouble(tpsellnew, _Digits); double slsellnew = sell+sl*Point; slsellnew = NormalizeDouble(slsellnew, _Digits); if(sl==0){slsellnew=0;} if(tp==0){tpsellnew=0;} int ordersell = OrderSend (_Symbol,OP_SELLLIMIT,TradeVolume,sell,0,slsellnew,tpsellnew,NULL,MagicSell,0,clrGreen); if (sl!=0 && tsl!=0) sellnewtsl=slsellnew; } void BuyStopOrder(int MagicBuy) { if (sl==0 && tsl!=0) sl=tsl; double buy = Ask+sd*Point; buy = NormalizeDouble(buy, _Digits); double slbuy = buy-sl*Point; slbuy = NormalizeDouble(slbuy, _Digits); double tpbuy = buy+tp*Point; tpbuy = NormalizeDouble(tpbuy, _Digits); if(sl==0){slbuy=0;} if(tp==0){tpbuy=0;} int orderbuystop = OrderSend (_Symbol,OP_BUYSTOP,TradeVolume,buy,0,slbuy,tpbuy,NULL,MagicBuy,0,clrBlue); buyprice = buy; if (sl!=0 && tsl!=0) buynewtsl=slbuy; } void SellStopOrder(int MagicSell) { if (sl==0 && tsl!=0) sl=tsl; double sell = Bid-sd*Point; sell = NormalizeDouble(sell, _Digits); double tpsellnew = sell-tp*Point; tpsellnew = NormalizeDouble(tpsellnew, _Digits); double slsellnew = sell+sl*Point; slsellnew = NormalizeDouble(slsellnew, _Digits); if(sl==0){slsellnew=0;} if(tp==0){tpsellnew=0;} int ordersell = OrderSend (_Symbol,OP_SELLSTOP,TradeVolume,sell,0,slsellnew,tpsellnew,NULL,MagicSell,0,clrGreen); sellprice = sell; if (sl!=0 && tsl!=0) sellnewtsl=slsellnew; } void CloseBuyOrders(int MagicBuy) { Comment ("Buy Orders are Closed"); for(int i = OrdersTotal() - 1; i >= 0 ; i--){ int OrderAll = OrderSelect(i,SELECT_BY_POS); if(OrderSymbol() != Symbol() && removeFromThisChartOnly) continue; if(OrderType() == OP_BUY) { if (OrderMagicNumber()==MagicBuy) { double price = MarketInfo(OrderSymbol(),MODE_BID); int OpenDelete = OrderClose(OrderTicket(), OrderLots(),price,5); }} Sleep(100); int error = GetLastError(); if(error > 0) Print("Unanticipated error: "); RefreshRates(); } } void CloseSellOrders(int MagicSell) { Comment ("Sell Orders are Closed"); for(int i = OrdersTotal() - 1; i >= 0 ; i--){ int OrderAll = OrderSelect(i,SELECT_BY_POS); if(OrderSymbol() != Symbol() && removeFromThisChartOnly) continue; if(OrderType() == OP_SELL) { if (OrderMagicNumber()==MagicSell) { double price = MarketInfo(OrderSymbol(),MODE_ASK); int OpenDelete = OrderClose(OrderTicket(), OrderLots(),price,5); }} Sleep(100); int error = GetLastError(); if(error > 0) Print("Unanticipated error: "); RefreshRates(); } } bool CheckBuyOpenOrders(int MagicBuy){ // We need to scan all the open and pending orders to see if there are any. // OrdersTotal returns the total number of market and pending orders. // What we do is scan all orders and check if they are of the same symbol as the one where the EA is running. for( int i = 0 ; i < OrdersTotal() ; i++ ) { // We select the order of index i selecting by position and from the pool of market/pending trades. int OrderBO = OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); // If the pair of the order is equal to the pair where the EA is running. if(OrderType()==OP_BUY && OrderMagicNumber()==MagicBuy) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); } bool CheckSellOpenOrders(int MagicSell){ // We need to scan all the open and pending orders to see if there are any. // OrdersTotal returns the total number of market and pending orders. // What we do is scan all orders and check if they are of the same symbol as the one where the EA is running. for( int i = 0 ; i < OrdersTotal() ; i++ ) { // We select the order of index i selecting by position and from the pool of market/pending trades. int OrderSO = OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); // If the pair of the order is equal to the pair where the EA is running. if(OrderType()==OP_SELL && OrderMagicNumber()==MagicSell) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); } bool CheckBuyStopOpenOrders(int MagicBuy){ // We need to scan all the open and pending orders to see if there are any. // OrdersTotal returns the total number of market and pending orders. // What we do is scan all orders and check if they are of the same symbol as the one where the EA is running. for( int i = 0 ; i < OrdersTotal() ; i++ ) { // We select the order of index i selecting by position and from the pool of market/pending trades. int OrderBO = OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); // If the pair of the order is equal to the pair where the EA is running. if(OrderType()==OP_BUYSTOP && OrderMagicNumber()==MagicBuy) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); } bool CheckSellStopOpenOrders(int MagicSell){ // We need to scan all the open and pending orders to see if there are any. // OrdersTotal returns the total number of market and pending orders. // What we do is scan all orders and check if they are of the same symbol as the one where the EA is running. for( int i = 0 ; i < OrdersTotal() ; i++ ) { // We select the order of index i selecting by position and from the pool of market/pending trades. int OrderSO = OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); // If the pair of the order is equal to the pair where the EA is running. if(OrderType()==OP_SELLSTOP && OrderMagicNumber()==MagicSell) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); } bool CheckBuyLimitOpenOrders(int MagicBuy){ // We need to scan all the open and pending orders to see if there are any. // OrdersTotal returns the total number of market and pending orders. // What we do is scan all orders and check if they are of the same symbol as the one where the EA is running. for( int i = 0 ; i < OrdersTotal() ; i++ ) { // We select the order of index i selecting by position and from the pool of market/pending trades. int OrderBO = OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); // If the pair of the order is equal to the pair where the EA is running. if(OrderType()==OP_BUYLIMIT && OrderMagicNumber()==MagicBuy) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); } bool CheckSellLimitOpenOrders(int MagicSell){ // We need to scan all the open and pending orders to see if there are any. // OrdersTotal returns the total number of market and pending orders. // What we do is scan all orders and check if they are of the same symbol as the one where the EA is running. for( int i = 0 ; i < OrdersTotal() ; i++ ) { // We select the order of index i selecting by position and from the pool of market/pending trades. int OrderSO = OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); // If the pair of the order is equal to the pair where the EA is running. if(OrderType()==OP_SELLLIMIT && OrderMagicNumber()==MagicSell) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); } bool lastlosscheck(){ int OrderCheck = OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY); if(OrderProfit()<0) return(true); else return(false); }