#property strict #include CAppDialog OurInterface; #include CButton OrderButton,SellButton,BuyButton,NewSLButton,NewTPButton,TSLButton; #include CEdit LotSize,StartDifference,StartSL,StartTP,TrailingSL,LotSizeSell,StartDifferenceSell,StartSLSell,StartTPSell,TrailingSLSell; #include CLabel LotLabel,StartDifferenceLabel,StartSLLabel,StartTPLabel,TrailingSLLabel,LotLabelSell,StartDifferenceLabelSell,StartSLLabelSell, StartTPLabelSell,TrailingSLLabelSell; bool removeFromThisChartOnly = true; int slip=20; // Best Setting for One Sided Up is sd 100, sl 243, tp 257, last four 100 double TradeVolume =1.1; double sd=200; // Start Diffrence 200 double sl = 1000; double tp = 1000; double tsl= 0; double TradeVolumeSell = 1.1; double sdSell=sd; // Start Diffrence 200 double slSell = sl; double tpSell = tp; double tslSell= 0; double buynewtsl = 10000000; double sellnewtsl = 0; int closestprice = 40; double buyprice = 100000; double buypriceforminprofit = 100000; double buypriceforfirstprofit = 100000; double sellprice=1; double sellpriceforminprofit = 1; double sellpriceforfirstprofit = 1; double buylimitprice = 1; double selllimitprice=100000; int MagicBuyScalping5 =100111605; int MagicSellScalping5 = 100222605; double MaxAccountBalance = AccountBalance(); double MinAccountBalance = 1000000; double InitialAccountBalance = AccountBalance(); string OK_TO_BUY = "NG"; string OK_TO_BUY_PreviousBar = "NG"; string OK_TO_BUY_ThisBar = "NG"; string OK_TO_SELL = "NG"; string OK_TO_SELL_PreviousBar = "NG"; string OK_TO_SELL_ThisBar = "NG"; string OK_TO_BUY_LastFour = "NG"; string OK_TO_SELL_LastFour = "NG"; string EMA30_OK_TO_BUY = "NG"; string EMA30_OK_TO_SELL = "NG"; string EMA05_OK_TO_BUY = "NG"; string EMA05_OK_TO_SELL = "NG"; int first_start_hour = 1; // 1Pm for Testing. 3pm for actual int first_start_min = 0; string first_start_AMPM = "AM"; int first_end_hour = 11; int first_end_min = 59; string first_end_AMPM = "PM"; int second_start_hour = 9; int second_start_min = 0; string second_start_AMPM = "AM"; int second_end_hour = 9; int second_end_min = 0; string second_end_AMPM = "PM"; string OK_TO_TRADE = "NG"; const string LotName = "Lot_Volume"; const string Start_Diff = "Start_Difference"; const string Start_SL = "Start_SL"; const string Start_TP = "Start_TP"; const string Trailing_SL = "Trailing_SL"; const string LotNameSell = "Lot_Volume Sell"; const string Start_DiffSell = "Start_Difference Sell"; const string Start_SLSell = "Start_SL Sell"; const string Start_TPSell = "Start_TP Sell"; const string Trailing_SLSell = "Trailing_SL Sell"; // int speedSlowness = 500000000; // intended for 32 speed, adjust as desired int speedSlowness = 0; int OnInit() { ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true); OurInterface.Create(0,"Scalping Gold M5 Home Working v2.0",0,50,70,445,290); 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)); StartDifferenceLabel.Create(0,"StartDifference",0,10,40,50,60); OurInterface.Add(StartDifferenceLabel); StartDifferenceLabel.Text("Start Diff:"); StartDifference.Create(0,Start_Diff,0,90,40,130,60); OurInterface.Add(StartDifference); StartDifference.Text(string(sd)); StartSLLabel.Create(0,"StartSLLabel",0,10,70,50,90); OurInterface.Add(StartSLLabel); StartSLLabel.Text("Set SL:"); StartSL.Create(0,Start_SL,0,90,70,130,90); OurInterface.Add(StartSL); StartSL.Text(string(sl)); StartTPLabel.Create(0,"StartTPLabel",0,10,100,50,120); OurInterface.Add(StartTPLabel); StartTPLabel.Text("Set TP:"); StartTP.Create(0,Start_TP,0,90,100,130,120); OurInterface.Add(StartTP); StartTP.Text(string(tp)); TrailingSLLabel.Create(0,"TrailingSLLabel",0,10,130,50,150); OurInterface.Add(TrailingSLLabel); TrailingSLLabel.Text("Trail SL:"); TrailingSL.Create(0,Trailing_SL,0,90,130,130,150); OurInterface.Add(TrailingSL); TrailingSL.Text(string(tsl)); LotLabelSell.Create(0,"LotLabelSell",0,260,10,330,30); OurInterface.Add(LotLabelSell); LotLabelSell.Text("Lot Size:"); LotSizeSell.Create(0,LotNameSell,0,340,10,380,30); OurInterface.Add(LotSizeSell); LotSizeSell.Text(string(TradeVolumeSell)); StartDifferenceLabelSell.Create(0,"StartDifferenceSell",0,260,40,330,60); OurInterface.Add(StartDifferenceLabelSell); StartDifferenceLabelSell.Text("Start Diff:"); StartDifferenceSell.Create(0,Start_DiffSell,0,340,40,380,60); OurInterface.Add(StartDifferenceSell); StartDifferenceSell.Text(string(sdSell)); StartSLLabelSell.Create(0,"StartSLLabelSell",0,260,70,330,90); OurInterface.Add(StartSLLabelSell); StartSLLabelSell.Text("Set SL:"); StartSLSell.Create(0,Start_SLSell,0,340,70,380,90); OurInterface.Add(StartSLSell); StartSLSell.Text(string(slSell)); StartTPLabelSell.Create(0,"StartTPLabelSell",0,260,100,330,120); OurInterface.Add(StartTPLabelSell); StartTPLabelSell.Text("Set TP:"); StartTPSell.Create(0,Start_TPSell,0,340,100,380,120); OurInterface.Add(StartTPSell); StartTPSell.Text(string(tpSell)); TrailingSLLabelSell.Create(0,"TrailingSLLabelSell",0,260,130,330,150); OurInterface.Add(TrailingSLLabelSell); TrailingSLLabelSell.Text("Trail SL:"); TrailingSLSell.Create(0,Trailing_SLSell,0,340,130,380,150); OurInterface.Add(TrailingSLSell); TrailingSLSell.Text(string(tslSell)); NewSLButton.Create(0,"NewSLButton",0,140,70,250,90); OurInterface.Add(NewSLButton); NewSLButton.Text("Update SL"); NewTPButton.Create(0,"NewTPButton",0,140,100,250,120); OurInterface.Add(NewTPButton); NewTPButton.Text("Update TP"); TSLButton.Create(0,"TSLButton",0,140,130,250,150); OurInterface.Add(TSLButton); TSLButton.Text("Update TSL"); BuyButton.Create(0,"BuyButton",0,15,160,125,180); OurInterface.Add(BuyButton); BuyButton.Text("Buy Order"); OrderButton.Create(0,"OrderButton",0,135,160,250,180); OurInterface.Add(OrderButton); OrderButton.Text("Both Orders"); SellButton.Create(0,"SellButton",0,265,160,375,180); OurInterface.Add(SellButton); SellButton.Text("Sell Order"); OurInterface.Run(); return(INIT_SUCCEEDED); } void OnTick() { for(int i=0; iAsk + 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()==MagicBuyScalping5) { 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 - sdSell*_Point)) { double sellstopnew = Bid - sdSell*_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()==MagicSellScalping5) { if(OrderOpenPrice() != sellprice && OrderOpenPrice() < sellprice) { double sellstopsl = sellprice +slSell*_Point; sellstopsl = NormalizeDouble(sellstopsl, _Digits); double sellstoptp = sellprice -tpSell*_Point; sellstoptp = NormalizeDouble(sellstoptp, _Digits); if(tpSell==0){sellstoptp=0;} if(slSell==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()==MagicBuyScalping5) { 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 + sdSell*_Point)) { double sellstopnew = Bid + sdSell*_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()==MagicSellScalping5) { if(OrderOpenPrice() != selllimitprice && OrderOpenPrice() > selllimitprice) { double sellstopsl = selllimitprice +slSell*_Point; sellstopsl = NormalizeDouble(sellstopsl, _Digits); double sellstoptp = selllimitprice -tpSell*_Point; sellstoptp = NormalizeDouble(sellstoptp, _Digits); if(tpSell==0){sellstoptp=0;} if(slSell==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()==MagicBuyScalping5) { if (buynewtsl Bid + tslSell*_Point || OrderStopLoss()==0) { sellnewtsl = Bid +tslSell*_Point; sellnewtsl = NormalizeDouble(sellnewtsl, _Digits); } if (sellnewtsl-Bid AccountBalance()) MinAccountBalance = AccountBalance(); if (CheckPreviousBarBear()==true) OK_TO_BUY_PreviousBar = "OK"; if (CheckThisBarBear()==true) OK_TO_BUY_ThisBar = "OK"; if (CheckPreviousBarBear()==false) OK_TO_BUY_PreviousBar = "NG"; if (CheckThisBarBear()==false) OK_TO_BUY_ThisBar = "NG"; if (CheckPreviousBarBull()==true) OK_TO_SELL_PreviousBar = "OK"; if (CheckThisBarBull()==true) OK_TO_SELL_ThisBar = "OK"; if (CheckPreviousBarBull()==false) OK_TO_SELL_PreviousBar = "NG"; if (CheckThisBarBull()==false) OK_TO_SELL_ThisBar = "NG"; if (CheckLastFourHigh()==true) OK_TO_BUY_LastFour= "OK"; if (CheckLastFourHigh()==false) OK_TO_BUY_LastFour= "NG"; if (CheckLastFourLow()==true) OK_TO_SELL_LastFour= "OK"; if (CheckLastFourLow()==false) OK_TO_SELL_LastFour= "NG"; if (OK_TO_BUY_LastFour=="OK" && CheckBuyStopOpenOrders(MagicBuyScalping5)==false && CheckBuyOpenOrders(MagicBuyScalping5)==false) OK_TO_BUY = "OK"; if (OK_TO_SELL_LastFour=="OK" && CheckSellStopOpenOrders(MagicSellScalping5)==false && CheckSellOpenOrders(MagicSellScalping5)==false) OK_TO_SELL = "OK"; if ((LocalTime()>first_start_time && LocalTime()second_start_time && LocalTime()M30EMALast) { EMA30_OK_TO_SELL = "OK"; EMA30_OK_TO_BUY = "NG"; } if (M30EMA3M05EMALast) { EMA05_OK_TO_SELL = "OK"; EMA05_OK_TO_BUY = "NG"; } if (M05EMA3=0;b--) { if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol()==Symbol()) { // New Take Profit for Buy if(OrderType()==OP_BUY && OrderMagicNumber()==MagicBuyScalping5) { int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(),buynewsl,OrderTakeProfit(),0,clrAqua); } // New Take Profit for Sell if(OrderType()==OP_SELL&& OrderMagicNumber()==MagicSellScalping5) { 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 - tpSell*_Point; sellnewtp = NormalizeDouble(sellnewtp, _Digits); if(tp==0) buynewtp=0; if(tpSell==0) sellnewtp=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 && OrderMagicNumber()==MagicBuyScalping5) { int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(),OrderStopLoss(),buynewtp,0,clrAqua); } // New Take Profit for Sell if(OrderType()==OP_SELL && OrderMagicNumber()==MagicSellScalping5) { int SellModify = OrderModify (OrderTicket(),OrderOpenPrice(),OrderStopLoss(),sellnewtp ,0,clrBlue); } } } } } if(sparam=="TSLButton") { // Chart Output Comment ("Trailing SL has been updated"); double buynewsl = Ask - tsl*_Point; buynewsl = NormalizeDouble(buynewsl, _Digits); double sellnewsl = Bid + tslSell*_Point; sellnewsl = NormalizeDouble(sellnewsl, _Digits); if(tsl==0) buynewsl=0; if(tslSell==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 && OrderMagicNumber()==MagicBuyScalping5) { int BuyModify = OrderModify (OrderTicket(),OrderOpenPrice(),buynewsl,OrderTakeProfit(),0,clrAqua); } // New StopLoss for Sell if(OrderType()==OP_SELL && OrderMagicNumber()==MagicSellScalping5) { int SellModify = OrderModify (OrderTicket(),OrderOpenPrice(),sellnewsl,OrderTakeProfit() ,0,clrBlue); }}}} }} } void OnDeinit(const int reason) { OurInterface.Destroy(reason); } 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 && tsl==0){slbuy=0;} if(tp==0){tpbuy=0;} int orderbuystop = OrderSend (_Symbol,OP_BUYSTOP,TradeVolume,buy,slip,slbuy,tpbuy,NULL,MagicBuy,0,clrBlue); buyprice = buy; buypriceforminprofit = buy; buypriceforfirstprofit= buy; if (sl!=0 && tsl!=0) buynewtsl=slbuy; } void SellStopOrder(int MagicSell) { if (slSell==0 && tslSell!=0) slSell=tslSell; double sell = Bid-sdSell*Point; sell = NormalizeDouble(sell, _Digits); double tpsellnew = sell-tpSell*Point; tpsellnew = NormalizeDouble(tpsellnew, _Digits); double slsellnew = sell+slSell*Point; slsellnew = NormalizeDouble(slsellnew, _Digits); if(slSell==0 && tslSell==0){slsellnew=0;} if(tpSell==0){tpsellnew=0;} int ordersell = OrderSend (_Symbol,OP_SELLSTOP,TradeVolumeSell,sell,slip,slsellnew,tpsellnew,NULL,MagicSell,0,clrGreen); sellprice = sell; sellpriceforminprofit = sell; sellpriceforfirstprofit= sell; if (slSell!=0 && tslSell!=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 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 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 CheckThisBarBear(){ double thisopen = Open[0]; if (Askthisopen+30*_Point) return(true); else return(false); } bool CheckPreviousBarBear(){ double previousopen = Open[1]; double previousclose = Close[1]; double previousopen2 = Open[2]; double previousclose2 = Close[2]; if (previousopen>previousclose) return(true); else return(false); } bool CheckPreviousBarBull(){ double previousopen = Open[1]; double previousclose = Close[1]; double previousopen2 = Open[2]; double previousclose2 = Close[2]; if (previousopentheLowest+200*_Point) return(true); else return(false); }