#property strict #include CAppDialog OurInterface; #include CButton OrderButton,SellButton,BuyButton,NewTPSLButton,TSLButton,PendingClose,AllClose,ResetTPSL; #include CEdit LotSize1,Slippage,StartSL,StartTP,TrailingSL,FirstHour,FirstMin,FirstAMPM,SpreadLimitEdit; #include CLabel LotLabel1,SlippageLabel,StartSLLabel,StartTPLabel,TrailingSLLabel,ScheduleHeadingLabel,FirstSchedule,Colon1,SpreadLimitLabel; #include bool removeFromThisChartOnly = true; double TradeVolume1 = 1; // 0.03 int slip=0; double sl = 200; double tp = 1700; double tsl= 0; int MagicBuyGoldNews = 100111800; int MagicSellGoldNews = 100222800; double SpreadLimit = 15; double buynewtsl = 10000000; double sellnewtsl = 0; double buynewsl = 10000000; double sellnewsl = 0; int closestprice = 100; int first_start_hour = 3; int first_start_min = 30; string first_AMPM = "PM"; double firstTPbase = 10000000; double secondTPbase = 1000000; double thirdTPbase = 1000000; double sellfirstTPbase = 10000000; double sellsecondTPbase = 1000000; double sellthirdTPbase = 1000000; string OK_TO_ORDER="NG"; const string LotName1 = "Lot_Volume1"; const string Slippage_st = "Start_Slippage"; const string Start_SL = "Start_SL"; const string Start_TP = "Start_TP"; const string Trailing_SL = "Trailing_SL"; const string SpreadLimitEdit1="SpreadLimitEdit"; const string First_Hour = "First_Hour"; const string First_Min = "First_Min"; const string First_AMPM = "First_AMPM"; double MaxAccountBalance = AccountBalance(); double MinAccountBalance = 1000000; double InitialAccountBalance = AccountBalance(); double MaxEquity = AccountEquity(); double MinEquity = 1000000; int OnInit() { ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true); OurInterface.Create(0,"News Items for Gold",0,100,70,400,400); ScheduleHeadingLabel.Create(0,"ScheduleHeading",0,30,10,100,30); OurInterface.Add(ScheduleHeadingLabel); ScheduleHeadingLabel.Text("Set News Schedule"); FirstSchedule.Create(0,"FirstScheduleLabel",0,10,40,50,60); OurInterface.Add(FirstSchedule); FirstSchedule.Text("1st News:"); FirstHour.Create(0,First_Hour,0,90,40,120,60); OurInterface.Add(FirstHour); FirstHour.Text(string(first_start_hour)); Colon1.Create(0,"Colon",0,122,40,125,60); OurInterface.Add(Colon1); Colon1.Text(":"); FirstMin.Create(0,First_Min,0,130,40,160,60); OurInterface.Add(FirstMin); FirstMin.Text(string(first_start_min)); FirstAMPM.Create(0,First_AMPM,0,165,40,195,60); OurInterface.Add(FirstAMPM); FirstAMPM.Text(string(first_AMPM)); LotLabel1.Create(0,"LotLabel1",0,10,70,50,90); OurInterface.Add(LotLabel1); LotLabel1.Text("1st Lot:"); LotSize1.Create(0,LotName1,0,90,70,150,90); OurInterface.Add(LotSize1); LotSize1.Text(string(TradeVolume1)); SpreadLimitLabel.Create(0,"SpreadLabel",0,200,40,250,60); OurInterface.Add(SpreadLimitLabel); SpreadLimitLabel.Text("Min Spread"); SpreadLimitEdit.Create(0,SpreadLimitEdit1,0,220,70,260,90); OurInterface.Add(SpreadLimitEdit); SpreadLimitEdit.Text(string(SpreadLimit)); SlippageLabel.Create(0,"SlippageLabel",0,10,100,50,120); OurInterface.Add(SlippageLabel); SlippageLabel.Text("Slip:"); Slippage.Create(0,Slippage_st,0,90,100,150,120); OurInterface.Add(Slippage); Slippage.Text(string(slip)); StartSLLabel.Create(0,"StartSLLabel",0,10,130,50,150); OurInterface.Add(StartSLLabel); StartSLLabel.Text("Set SL:"); StartSL.Create(0,Start_SL,0,90,130,150,150); OurInterface.Add(StartSL); StartSL.Text(string(sl)); StartTPLabel.Create(0,"StartTPLabel",0,10,160,50,180); OurInterface.Add(StartTPLabel); StartTPLabel.Text("Set TP:"); StartTP.Create(0,Start_TP,0,90,160,150,180); OurInterface.Add(StartTP); StartTP.Text(string(tp)); TrailingSLLabel.Create(0,"TrailingSLLabel",0,10,190,50,240); OurInterface.Add(TrailingSLLabel); TrailingSLLabel.Text("Trail SL:"); TrailingSL.Create(0,Trailing_SL,0,90,190,150,210); OurInterface.Add(TrailingSL); TrailingSL.Text(string(tsl)); AllClose.Create(0,"AllCloseButton",0,165,100,275,120); OurInterface.Add(AllClose); AllClose.Text("Close All"); PendingClose.Create(0,"PendingCloseButton",0,165,130,275,150); OurInterface.Add(PendingClose); PendingClose.Text("Pending Close"); NewTPSLButton.Create(0,"NewTPSLButton",0,165,160,275,180); OurInterface.Add(NewTPSLButton); NewTPSLButton.Text("Update TP/SL"); TSLButton.Create(0,"TSLButton",0,165,190,275,210); OurInterface.Add(TSLButton); TSLButton.Text("Update TSL"); OrderButton.Create(0,"OrderButton",0,15,220,125,240); OurInterface.Add(OrderButton); OrderButton.Text("Both Orders"); ResetTPSL.Create(0,"ResetButton",0,165,220,275,240); OurInterface.Add(ResetTPSL); ResetTPSL.Text("Reset TP/SL"); BuyButton.Create(0,"BuyButton",0,15,250,125,270); OurInterface.Add(BuyButton); BuyButton.Text("Buy Order"); SellButton.Create(0,"SellButton",0,165,250,275,270); OurInterface.Add(SellButton); SellButton.Text("Sell Order"); OurInterface.Run(); return(INIT_SUCCEEDED); } void OnTick() { double CurrentAccountBalance = AccountBalance(); double Spread = MarketInfo(Symbol(), MODE_SPREAD); datetime Today = StrToTime(StringConcatenate(Year(), ".", Month(), ".", Day())); int first_start_hour_set=first_start_hour; if (first_AMPM == "PM" && first_start_hour!=12) first_start_hour_set=first_start_hour+12; datetime first_start_time = Today+first_start_hour_set*3600+first_start_min*60; datetime firstpreviousmin = first_start_time-61; datetime firstnextmin = first_start_time-57; datetime firstpreviousfullmin = first_start_time-60; datetime firstnextfullmin = first_start_time+60; // Trailing StopLoss for (int b= OrdersTotal()-1;b>=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol()==Symbol()) { if((LocalTime()>firstpreviousfullmin && LocalTime()sellnewsl && sellnewsl>OrderStopLoss() && OrderType()==OP_BUY && OrderMagicNumber()==MagicBuyGoldNews) {int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(), sellnewsl,OrderTakeProfit(),0,clrAqua); buynewtsl=sellnewsl; firstTPbase= sellnewsl;} if(Bid< buynewsl && buynewslfirstTPbase+500*_Point && firstTPbase+500*_Point>OrderStopLoss() && OrderType()==OP_BUY && OrderMagicNumber()==MagicBuyGoldNews) { double buyfirstTPSL= firstTPbase+500*_Point; int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(), buyfirstTPSL,OrderTakeProfit(),0,clrAqua); buynewtsl=buyfirstTPSL; secondTPbase = buyfirstTPSL;} if(Ask>secondTPbase+500*_Point && secondTPbase+500*_Point>OrderStopLoss() && OrderType()==OP_BUY && OrderMagicNumber()==MagicBuyGoldNews) { double buysecondTPSL= secondTPbase+500*_Point; int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(), buysecondTPSL,OrderTakeProfit(),0,clrAqua); buynewtsl=buysecondTPSL; thirdTPbase = buysecondTPSL;} if(Ask>thirdTPbase+500*_Point && thirdTPbase+500*_Point>OrderStopLoss() && OrderType()==OP_BUY && OrderMagicNumber()==MagicBuyGoldNews) { double buythirdTPSL= thirdTPbase+500*_Point; int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(), buythirdTPSL,OrderTakeProfit(),0,clrAqua); buynewtsl=buythirdTPSL; } if(Bidsellnewsl+tsl*_Point && OrderStopLoss()Bid+tsl*_Point && OrderType()==OP_SELL && OrderMagicNumber()==MagicSellGoldNews) { sellnewtsl = Bid +tsl*_Point; sellnewtsl = NormalizeDouble(sellnewtsl, _Digits); int SellModify = OrderModify (OrderTicket(),OrderOpenPrice(),sellnewtsl,OrderTakeProfit(),0,clrBlue); } }}}} if (OK_TO_ORDER=="OK" && Spread<=SpreadLimit && CheckBuyOpenOrders()==false && CheckSellOpenOrders()==false) { BuyOrder(); SellOrder(); OK_TO_ORDER="NG"; } if(LocalTime()>firstpreviousmin-1800 && LocalTime()firstpreviousmin && LocalTime()1) UpdateTPSL(); } if((LocalTime()firstnextfullmin) && OrdersTotal()==2) { for (int b= OrdersTotal()-1;b>=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol()==Symbol()) { // New StopLoss for Buy if(OrderType()==OP_BUY && OrderStopLoss()!=0) { int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(),0,0,0,clrAqua); } if(OrderType()==OP_SELL && OrderStopLoss()!=0) { int SellModify = OrderModify (OrderTicket(),OrderOpenPrice(),0,0,0,clrBlue); } } } } } if((LocalTime()>firstnextmin+65 && LocalTime() AccountBalance()) MinAccountBalance = AccountBalance(); if (MinEquity > AccountEquity()) MinEquity = AccountEquity(); Comment ("Initial Account Balance was ",InitialAccountBalance,", Minimun Account Balance was ",MinAccountBalance,", Max Balance was ",MaxAccountBalance," and Current Balance is ",CurrentAccountBalance, "\nMinimum Equity was ",MinEquity," and Current Equity is ",AccountEquity(), "\nSpread = ",Spread, "\nOK to Order: ",OK_TO_ORDER); } // 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 == LotName1) { string volumeText1 = ObjectGetString(0, LotName1, OBJPROP_TEXT); SetVolume1(volumeText1); ObjectSetString(0, LotName1, OBJPROP_TEXT, string(TradeVolume1)); return; } if(sparam == Start_TP) { string TPText = ObjectGetString(0, Start_TP, OBJPROP_TEXT); SetTP(TPText); ObjectSetString(0, Start_TP, OBJPROP_TEXT, string(tp)); return; } if(sparam == Start_SL) { string SLText = ObjectGetString(0, Start_SL, OBJPROP_TEXT); SetSL(SLText); ObjectSetString(0, Start_SL, OBJPROP_TEXT, string(sl)); return; } if(sparam == Trailing_SL) { string TSLText = ObjectGetString(0, Trailing_SL, OBJPROP_TEXT); SetTSL(TSLText); ObjectSetString(0, Trailing_SL, OBJPROP_TEXT, string(tsl)); return; } if(sparam == SpreadLimitEdit1) { string SpreadText = ObjectGetString(0, SpreadLimitEdit1, OBJPROP_TEXT); SetSpread(SpreadText); ObjectSetString(0, SpreadLimitEdit1, OBJPROP_TEXT, string(SpreadLimit)); return; } if(sparam == First_Hour) { string FHText = ObjectGetString(0, First_Hour, OBJPROP_TEXT); SetFH(FHText); ObjectSetString(0, First_Hour, OBJPROP_TEXT, string(first_start_hour)); return; } if(sparam == First_Min) { string FMText = ObjectGetString(0, First_Min, OBJPROP_TEXT); SetFM(FMText); ObjectSetString(0, First_Min, OBJPROP_TEXT, string(first_start_min)); return; } if(sparam == First_AMPM) { string FAPText = ObjectGetString(0, First_AMPM, OBJPROP_TEXT); SetFAP(FAPText); ObjectSetString(0, First_AMPM, OBJPROP_TEXT, string(first_AMPM)); return; } } // if an object was clicked if(id==CHARTEVENT_OBJECT_CLICK) { // Comment (sparam+" Value is Changed"); if(sparam=="OrderButton") { OK_TO_ORDER="OK"; // Order(); } if(sparam=="BuyButton") { BuyOrder(); } if(sparam=="SellButton") { SellOrder(); } if(sparam=="PendingCloseButton") { ClosePendingOrders(MagicBuyGoldNews); ClosePendingOrders(MagicSellGoldNews); } if(sparam=="AllCloseButton") { CloseAllOrders(MagicBuyGoldNews); CloseAllOrders(MagicSellGoldNews); } if(sparam=="NewTPSLButton") UpdateTPSL(); if(sparam=="TSLButton") { // Chart Output // Comment ("Trailing SL has been updated"); buynewsl = Ask - tsl*_Point; buynewsl = NormalizeDouble(buynewsl, _Digits); sellnewsl = Bid + tsl*_Point; sellnewsl = NormalizeDouble(sellnewsl, _Digits); if(tsl==0){ buynewsl=0; sellnewsl=0; } for (int b= OrdersTotal()-1;b>=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol()==Symbol()) { // New StopLoss for Buy if(OrderType()==OP_BUY) { int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(),buynewsl,OrderTakeProfit(),0,clrAqua); } // New StopLoss for Sell if(OrderType()==OP_SELL) { int SellModify = OrderModify (OrderTicket(),OrderOpenPrice(),sellnewsl,OrderTakeProfit() ,0,clrBlue); } } } } } if(sparam=="ResetButton") { for (int b= OrdersTotal()-1;b>=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol()==Symbol()) { // New StopLoss for Buy if(OrderType()==OP_BUY) { int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(),0,0,0,clrAqua); } // New StopLoss for Sell if(OrderType()==OP_SELL) { int SellModify = OrderModify (OrderTicket(),OrderOpenPrice(),0,0,0,clrBlue); } } } } } } } void OnDeinit(const int reason) { OurInterface.Destroy(reason); } void SetVolume1(string volumeText1) { double newVolume1 = StringToDouble(volumeText1); if (newVolume1 < 0) { Print("Invalid volume specified"); return; } TradeVolume1 = newVolume1; } void SetTP(string TPText) { double newTP = StringToDouble(TPText); if (newTP < 0) { Print("Invalid volume specified"); return; } tp = newTP; } void SetSL(string SLText) { double newSL = StringToDouble(SLText); if (newSL < 0) { Print("Invalid volume specified"); return; } sl = newSL; } void SetTSL(string TSLText) { double newTSL = StringToDouble(TSLText); if (newTSL < 0) { Print("Invalid volume specified"); return; } tsl = newTSL; } void SetSpread(string SpreadText) { double newTSL = StringToDouble(SpreadText); if (newTSL < 0) { Print("Invalid volume specified"); return; } SpreadLimit = newTSL; } void SetFH(string FHText) { int newSL = StrToInteger(FHText); if (newSL < 0) { Print("Invalid volume specified"); return; } first_start_hour = newSL; // StringToInteger } void SetFM(string FMText) { int newSL = StrToInteger(FMText); if (newSL < 0) { Print("Invalid volume specified"); return; } first_start_min = newSL; } void SetFAP(string FAPText) { string newSL = FAPText; first_AMPM = newSL; } void BuyOrder() { double buy1 = Ask; // Bid-8*_Point; buy1 = NormalizeDouble(buy1, _Digits); int orderb1 = OrderSend (_Symbol,OP_BUY,TradeVolume1,buy1,0,0,0,NULL,MagicBuyGoldNews,0,clrBlue); } void SellOrder() { double sell1 = Bid; //Ask+8*_Point; sell1 = NormalizeDouble(sell1, _Digits); int orders1 = OrderSend (_Symbol,OP_SELL,TradeVolume1,sell1,0,0,0,NULL,MagicSellGoldNews,0,clrBlue); } void UpdateTPSL() { // Chart Output // Comment ("New Taking Profit/Stop Loss has been updated"); double buynewtp = Ask + tp*_Point; buynewtp = NormalizeDouble(buynewtp, _Digits); double sellnewtp = Bid - tp*_Point; sellnewtp = NormalizeDouble(sellnewtp, _Digits); if(tp==0){ buynewtp=0; sellnewtp=0; } buynewsl = Ask - sl*_Point; buynewsl = NormalizeDouble(buynewsl, _Digits); sellnewsl = Bid + sl*_Point; sellnewsl = NormalizeDouble(sellnewsl, _Digits); if(sl==0){ buynewsl=0; sellnewsl=0; } 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) { int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(),buynewsl,buynewtp,0,clrAqua); } // New Take Profit for Sell if(OrderType()==OP_SELL) { int SellModify = OrderModify (OrderTicket(),OrderOpenPrice(),sellnewsl,sellnewtp ,0,clrBlue); } } } } } void ClosePendingOrders(int Magic) { // Comment ("All Pending Orders are Closed"); for(int i = OrdersTotal() - 1; i >= 0 ; i--){ int OrderPending = OrderSelect(i,SELECT_BY_POS); // if(OrderSymbol() != Symbol() && removeFromThisChartOnly) continue; double price = MarketInfo(OrderSymbol(),MODE_ASK); if(OrderType() == OP_BUY) price = MarketInfo(OrderSymbol(),MODE_BID); if((OrderType() == OP_BUY )|| (OrderType() == OP_SELL)){ // OrderClose(OrderTicket(), OrderLots(),price,5); }else if (OrderMagicNumber()==(Magic)) { int PendingDelete = OrderDelete(OrderTicket()); } Sleep(100); int error = GetLastError(); if(error > 0) Print("Unanticipated error: "); RefreshRates(); } } void CloseAllOrders(int Magic) { // Comment ("All Orders are Closed"); for(int i = OrdersTotal() - 1; i >= 0 ; i--){ int OrderAll = OrderSelect(i,SELECT_BY_POS); if(OrderSymbol() != Symbol() && removeFromThisChartOnly) continue; double price = MarketInfo(OrderSymbol(),MODE_ASK); if(OrderType() == OP_BUY) price = MarketInfo(OrderSymbol(),MODE_BID); if((OrderType() == OP_BUY && OrderMagicNumber()==(Magic))|| (OrderType() == OP_SELL && OrderMagicNumber()==(Magic))){ int OpenDelete = OrderClose(OrderTicket(), OrderLots(),price,5); }else{ int AllDelete = OrderDelete(OrderTicket()); } Sleep(100); int error = GetLastError(); if(error > 0) Print("Unanticipated error: "); RefreshRates(); } } bool CheckBuyOpenOrders(){ // 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) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); } bool CheckSellOpenOrders(){ // 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) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); }