#property strict #include CAppDialog OurInterface; #include CButton OrderButton,NewTPSLButton,AllClose,ResetTPSL; #include CEdit LotSize,StartSL,StartTP,FirstHour,FirstMin,FirstAMPM,SpreadLimitEdit; #include CLabel LotLabel1,StartSLLabel,StartTPLabel,FirstSchedule,Colon1,SpreadLimitLabel; #include bool removeFromThisChartOnly = true; double TradeVolume = 1; double sl = 200; double tp = 500; int MagicBuyNews = 111; int MagicSellNews = 222; double SpreadLimit = 0; double buynewsl = 10000000; double sellnewsl = 0; double buysl = 10000000; double sellsl = 0; int first_start_hour = 5; int first_start_min = 30; string first_AMPM = "PM"; string OK_TO_ORDER="NG"; string OK_TO_CLOSE="NG"; const string LotName = "Lot_Volume1"; const string Start_SL = "Start_SL"; const string Start_TP = "Start_TP"; const string SpreadLimitEdit1="SpreadLimitEdit"; const string First_Hour = "First_Hour"; const string First_Min = "First_Min"; const string First_AMPM = "First_AMPM"; double InitialAccountBalance = AccountBalance(); double MaxAccountBalance = 0; double MinAccountBalance = 1000000; double MaxEquity = 0; double MinEquity = 1000000; double LastOrderClosePrice=0; datetime Today = StrToTime(StringConcatenate(Year(), ".", Month(), ".", Day())); int first_start_hour_set=first_start_hour; 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; int OnInit() { string SymbolNews =ChartSymbol(); if (SymbolNews=="XAUUSD") { TradeVolume = 1; sl = 200; tp = 500; SpreadLimit = 5; // Minimum 5 first_start_hour = 4; first_start_min = 44; first_AMPM = "PM"; MagicBuyNews = 100111800; MagicSellNews = 100222800; } if (SymbolNews=="AUDUSD") { TradeVolume = 0.2; sl = 50; tp = 200; SpreadLimit = 0; // Minimum 0 first_start_hour = 5; first_start_min = 30; first_AMPM = "PM"; MagicBuyNews = 310111800; MagicSellNews = 310222800; } if (SymbolNews=="EURUSD") { TradeVolume = 0.2; sl = 100; tp = 200; SpreadLimit = 0; // Minimum 0 first_start_hour = 12; first_start_min = 30; first_AMPM = "PM"; MagicBuyNews = 320111800; MagicSellNews = 320222800; } if (SymbolNews=="GBPUSD") { TradeVolume = 0.2; // 2 for Full sl = 100; tp = 150; SpreadLimit = -1; // Minimum 0 first_start_hour = 1; first_start_min = 30; first_AMPM = "PM"; MagicBuyNews = 330111800; MagicSellNews = 330222800; } if (SymbolNews=="USDCAD") { TradeVolume = 0.2; sl = 70; tp = 100; SpreadLimit = 0; // Minimum 0 first_start_hour = 3; first_start_min = 45; first_AMPM = "AM"; MagicBuyNews = 340111800; MagicSellNews = 340222800; } if (SymbolNews=="USDCHF") { TradeVolume = 0.2; // 3 Tested and found OK sl = 70; tp = 100; SpreadLimit = 0; // Minimum 0 first_start_hour = 12; first_start_min = 30; first_AMPM = "PM"; MagicBuyNews = 350111800; MagicSellNews = 350222800; } if (SymbolNews=="USDJPY") { TradeVolume = 0.2; sl = 70; tp = 100; SpreadLimit = 0; // Minimum 0 first_start_hour = 3; first_start_min = 45; first_AMPM = "AM"; MagicBuyNews = 360111800; MagicSellNews = 360222800; } if (SymbolNews=="NZDUSD") { TradeVolume = 0.2; sl = 70; tp = 100; SpreadLimit = 1; // Minimum 1 first_start_hour = 3; first_start_min = 45; first_AMPM = "AM"; MagicBuyNews = 370111800; MagicSellNews = 370222800; } ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true); OurInterface.Create(0,StringConcatenate("EA News Time for ",SymbolNews),0,100,100,380,320); FirstSchedule.Create(0,"FirstScheduleLabel",0,10,10,50,30); OurInterface.Add(FirstSchedule); FirstSchedule.Text("News Time:"); FirstHour.Create(0,First_Hour,0,100,10,130,30); OurInterface.Add(FirstHour); FirstHour.Text(string(first_start_hour)); Colon1.Create(0,"Colon",0,132,10,135,30); OurInterface.Add(Colon1); Colon1.Text(":"); FirstMin.Create(0,First_Min,0,140,10,170,30); OurInterface.Add(FirstMin); FirstMin.Text(string(first_start_min)); FirstAMPM.Create(0,First_AMPM,0,175,10,205,30); OurInterface.Add(FirstAMPM); FirstAMPM.Text(string(first_AMPM)); SpreadLimitLabel.Create(0,"SpreadLabel",0,10,40,90,60); OurInterface.Add(SpreadLimitLabel); SpreadLimitLabel.Text("Min Spread: "); SpreadLimitEdit.Create(0,SpreadLimitEdit1,0,100,40,130,60); OurInterface.Add(SpreadLimitEdit); SpreadLimitEdit.Text(string(SpreadLimit)); LotLabel1.Create(0,"LotLabel1",0,10,70,50,90); OurInterface.Add(LotLabel1); LotLabel1.Text("Lot Size:"); LotSize.Create(0,LotName,0,100,70,130,90); OurInterface.Add(LotSize); LotSize.Text(string(TradeVolume)); StartSLLabel.Create(0,"StartSLLabel",0,10,100,50,120); OurInterface.Add(StartSLLabel); StartSLLabel.Text("Set SL:"); StartSL.Create(0,Start_SL,0,100,100,140,120); OurInterface.Add(StartSL); StartSL.Text(string(sl)); StartTPLabel.Create(0,"StartTPLabel",0,10,130,50,150); OurInterface.Add(StartTPLabel); StartTPLabel.Text("Set TP:"); StartTP.Create(0,Start_TP,0,100,130,140,150); OurInterface.Add(StartTP); StartTP.Text(string(tp)); NewTPSLButton.Create(0,"NewTPSLButton",0,150,100,260,120); OurInterface.Add(NewTPSLButton); NewTPSLButton.Text("Update TP/SL"); ResetTPSL.Create(0,"ResetButton",0,150,130,260,150); OurInterface.Add(ResetTPSL); ResetTPSL.Text("Reset TP/SL"); OrderButton.Create(0,"OrderButton",0,15,160,125,180); OurInterface.Add(OrderButton); OrderButton.Text("Place Orders"); AllClose.Create(0,"AllCloseButton",0,150,160,260,180); OurInterface.Add(AllClose); AllClose.Text("Close Orders"); OurInterface.Run(); return(INIT_SUCCEEDED); } void OnTick() { LastOrderClosePrice= last_Lose_Close_Price(MagicBuyNews, MagicSellNews); double Spread = MarketInfo(Symbol(), MODE_SPREAD); double gap= Spread+20; Today = StrToTime(StringConcatenate(Year(), ".", Month(), ".", Day())); first_start_hour_set=first_start_hour; if (first_AMPM == "PM" && first_start_hour!=12) first_start_hour_set=first_start_hour+12; first_start_time = Today+first_start_hour_set*3600+first_start_min*60; firstpreviousmin = first_start_time-61; firstnextmin = first_start_time-57; firstpreviousfullmin = first_start_time-60; firstnextfullmin = first_start_time+60; // Stepwise StopLoss for (int b= OrdersTotal()-1;b>=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol()==Symbol()) { if((LocalTime()>firstpreviousfullmin && LocalTime()=buysl && buysl>OrderStopLoss()) { buysl = buysl-(sl/2)*_Point; buysl = NormalizeDouble(buysl, _Digits); int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(), buysl,OrderTakeProfit(),0,clrAqua); } // Reduce Loss if any one of SL hit and return after some points gain if(Bid>sellnewsl && sellnewsl>OrderStopLoss()) { buysl = sellnewsl-10*_Point; buysl = NormalizeDouble(buysl, _Digits); int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(), buysl,OrderTakeProfit(),0,clrAqua); } if(Bid>LastOrderClosePrice && LastOrderClosePrice>OrderStopLoss()) { buysl = LastOrderClosePrice; buysl = NormalizeDouble(buysl, _Digits); int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(), buysl,OrderTakeProfit(),0,clrAqua); } // Eliminate Loss if any one of SL hit and return after some points gain if(Bid>(buysl+gap*_Point) && buysl>OrderStopLoss()) { buysl = buysl+gap*_Point; buysl = NormalizeDouble(buysl, _Digits); int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(), buysl,OrderTakeProfit(),0,clrAqua); } // Various TP Positions if(Bid>buysl+tp*_Point && buysl+tp*_Point>OrderStopLoss()) { buysl= buysl+tp*_Point; buysl = NormalizeDouble(buysl, _Digits); int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(), buysl,OrderTakeProfit(),0,clrAqua); } } if (OrderType()==OP_SELL && OrderMagicNumber()==MagicSellNews && CheckBuyOpenOrders(MagicBuyNews)==false && CheckSellOpenOrders(MagicSellNews)==true) { // Minimize Loss if any one of SL hit and return if(Bid<=sellsl && sellslfirstpreviousmin-3600 && LocalTime()firstnextfullmin-50)) && (CheckBuyOpenOrders(MagicBuyNews)==true && CheckSellOpenOrders(MagicSellNews)==true)) { ResetTPSL(MagicBuyNews, MagicSellNews); } if (MaxAccountBalance < AccountBalance()) MaxAccountBalance = AccountBalance(); if (MinAccountBalance > AccountBalance()) MinAccountBalance = AccountBalance(); if (MaxEquity < AccountEquity()) MaxEquity = AccountEquity(); if (MinEquity > AccountEquity()) MinEquity = AccountEquity(); Comment ("Initial Account Balance was ",InitialAccountBalance,", Minimun Account Balance was ",MinAccountBalance,", Max Balance was ",MaxAccountBalance," and Current Balance is ",AccountBalance(), "\nMinimum Equity was ",MinEquity,", Max Equity was ",MaxEquity," and Current Equity is ",AccountEquity(), "\nOK to Order: ",OK_TO_ORDER," and OK to Close: ",OK_TO_CLOSE, "\nLast Order Close Price is ",LastOrderClosePrice, "\nTime to Order ", timetosetTPSL()); } // 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 == SpreadLimitEdit1) { string volumeText = ObjectGetString(0, SpreadLimitEdit1, OBJPROP_TEXT); SpreadLimit = StringToDouble(volumeText); ObjectSetString(0, SpreadLimitEdit1, OBJPROP_TEXT, string(SpreadLimit)); return; } if(sparam == First_Hour) { string volumeText = ObjectGetString(0, First_Hour, OBJPROP_TEXT); first_start_hour = StrToInteger(volumeText); ObjectSetString(0, First_Hour, OBJPROP_TEXT, string(first_start_hour)); return; } if(sparam == First_Min) { string volumeText = ObjectGetString(0, First_Min, OBJPROP_TEXT); first_start_min = StrToInteger(volumeText); ObjectSetString(0, First_Min, OBJPROP_TEXT, string(first_start_min)); return; } if(sparam == First_AMPM) { string volumeText = ObjectGetString(0, First_AMPM, OBJPROP_TEXT); first_AMPM = volumeText; ObjectSetString(0, First_AMPM, OBJPROP_TEXT, string(first_AMPM)); return; } } // if an object was clicked if(id==CHARTEVENT_OBJECT_CLICK) { if(sparam=="NewTPSLButton") UpdateTPSL(MagicBuyNews, MagicSellNews); if(sparam=="ResetButton") ResetTPSL(MagicBuyNews, MagicSellNews); if(sparam=="OrderButton") OK_TO_ORDER="OK"; if(sparam=="AllCloseButton") { OK_TO_CLOSE="OK"; } }} void OnDeinit(const int reason) { OurInterface.Destroy(reason); } void BuyOrder(int MagicBuy) { double buy1 = Ask; buy1 = NormalizeDouble(buy1, _Digits); int orderb1 = OrderSend (_Symbol,OP_BUY,TradeVolume,buy1,0,0,0,NULL,MagicBuy,0,clrBlue); } void SellOrder(int MagicSell) { double sell1 = Bid; sell1 = NormalizeDouble(sell1, _Digits); int orders1 = OrderSend (_Symbol,OP_SELL,TradeVolume,sell1,0,0,0,NULL,MagicSell,0,clrBlue); } void UpdateTPSL(int MagicBuy, int MagicSell) { buynewsl = Ask - sl*_Point; buynewsl = NormalizeDouble(buynewsl, _Digits); sellnewsl = Bid + sl*_Point; sellnewsl = NormalizeDouble(sellnewsl, _Digits); buysl = sellnewsl; sellsl = buynewsl; for (int b= OrdersTotal()-1;b>=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol()==Symbol()) { if(OrderType()==OP_BUY && OrderMagicNumber()==(MagicBuy)) { int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(),buynewsl,0,0,clrAqua); } if(OrderType()==OP_SELL && OrderMagicNumber()==(MagicSell)) { int SellModify = OrderModify (OrderTicket(),OrderOpenPrice(),sellnewsl,0,0,clrBlue); } }}}} void ResetTPSL(int MagicBuy, int MagicSell) { for (int b= OrdersTotal()-1;b>=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol()==Symbol()) { if(OrderType()==OP_BUY && OrderStopLoss()!=0 && OrderMagicNumber()==MagicBuy) { int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(),0,0,0,clrAqua); } if(OrderType()==OP_SELL && OrderStopLoss()!=0 && OrderMagicNumber()==MagicSell) { int SellModify = OrderModify (OrderTicket(),OrderOpenPrice(),0,0,0,clrBlue); } }}}} bool CheckBuyOpenOrders(int MagicBuy){ for( int i = 0 ; i < OrdersTotal() ; i++ ) { int OrderBO = OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); if(OrderType()==OP_BUY && OrderMagicNumber()==(MagicBuy)) return(true); } return(false); } bool CheckSellOpenOrders(int MagicSell){ for( int i = 0 ; i < OrdersTotal() ; i++ ) { int OrderSO = OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); if(OrderType()==OP_SELL && OrderMagicNumber()==(MagicSell)) return(true); } return(false); } bool timetosetTPSL(){ Today = StrToTime(StringConcatenate(Year(), ".", Month(), ".", Day())); first_start_hour_set=first_start_hour; if (first_AMPM == "PM" && first_start_hour!=12) first_start_hour_set=first_start_hour+12; first_start_time = Today+first_start_hour_set*3600+first_start_min*60; firstpreviousmin = first_start_time-61; firstnextmin = first_start_time-57; firstpreviousfullmin = first_start_time-60; firstnextfullmin = first_start_time+60; if(LocalTime()>firstpreviousmin && LocalTime()