#property strict #include CAppDialog OurInterface; #include CButton SellStopButton,BuyStopButton,SellLimitButton,BuyLimitButton; #include CEdit LotSize,SD,TP,SL; #include CLabel LotLabel,pricetradingLabel,SDLabel,TPLabel,SLLabel; int slip=20; double TradeVolume = 0.01; double sd = 100; double tp = 100; double sl= 100; int MagicBuyTP =000111500; int MagicSellTP = 000222500; double MaxAccountBalance = AccountBalance(); double MinAccountBalance = 1000000; double InitialAccountBalance = AccountBalance(); int closestprice = 20; double buyprice = 100000; double sellprice=1; double buylimitprice = 1; double selllimitprice=100000; const string LotName = "Lot_Volume"; const string SD_Value = "SD_Value"; const string TP_Value = "TP_Value"; const string SL_Value = "SL_Value"; int OnInit() { ChartSetInteger(0,CHART_EVENT_MOUSE_MOVE,true); OurInterface.Create(0,"TP1 TP2 TP3",0,50,70,270,300); LotLabel.Create(0,"LotLabel",0,10,10,50,30); OurInterface.Add(LotLabel); LotLabel.Text("Lot Size:"); LotSize.Create(0,LotName,0,90,10,160,30); OurInterface.Add(LotSize); LotSize.Text(string(TradeVolume)); SDLabel.Create(0,"SDLabel",0,10,40,50,60); OurInterface.Add(SDLabel); SDLabel.Text("SD:"); SD.Create(0,SD_Value,0,90,40,160,60); OurInterface.Add(SD); SD.Text(string(sd)); TPLabel.Create(0,"TPLabel",0,10,70,50,90); OurInterface.Add(TPLabel); TPLabel.Text("TP:"); TP.Create(0,TP_Value,0,90,70,160,90); OurInterface.Add(TP); TP.Text(string(tp)); SLLabel.Create(0,"SLLabel",0,10,100,50,120); OurInterface.Add(SLLabel); SLLabel.Text("SL:"); SL.Create(0,SL_Value,0,90,100,160,120); OurInterface.Add(SL); SL.Text(string(sl)); BuyStopButton.Create(0,"BuyStopButton",0,10,130,100,150); OurInterface.Add(BuyStopButton); BuyStopButton.Text("Buy Stop"); SellStopButton.Create(0,"SellStopButton",0,110,130,200,150); OurInterface.Add(SellStopButton); SellStopButton.Text("Sell Stop"); BuyLimitButton.Create(0,"BuyLimitButton",0,10,160,100,180); OurInterface.Add(BuyLimitButton); BuyLimitButton.Text("Buy Limit"); SellLimitButton.Create(0,"SellLimitButton",0,110,160,200,180); OurInterface.Add(SellLimitButton); SellLimitButton.Text("Sell Limit"); OurInterface.Run(); return(INIT_SUCCEEDED); } void OnTick() { double CurrentAccountBalance = AccountBalance(); if (MaxAccountBalance < AccountBalance()) MaxAccountBalance = AccountBalance(); if (MinAccountBalance > AccountBalance()) MinAccountBalance = AccountBalance(); // 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()==MagicBuyTP) { if (OrderOpenPrice() != buyprice && OrderOpenPrice() > buyprice) { double buystopsl = buyprice-sl*_Point; buystopsl = NormalizeDouble(buystopsl, _Digits); double buystoptp = buyprice+tp*_Point; buystoptp = NormalizeDouble(buystoptp, _Digits); 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()==MagicSellTP) { if(OrderOpenPrice() != sellprice && OrderOpenPrice() < sellprice) { double sellstopsl = sellprice +sl*_Point; sellstopsl = NormalizeDouble(sellstopsl, _Digits); double sellstoptp = sellprice -tp*_Point; sellstoptp = NormalizeDouble(sellstoptp, _Digits); 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()==MagicBuyTP) { 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()==MagicSellTP) { if(OrderOpenPrice() != selllimitprice && OrderOpenPrice() > selllimitprice) { double sellstopsl = selllimitprice +sl*_Point; sellstopsl = NormalizeDouble(sellstopsl, _Digits); double sellstoptp = selllimitprice -tp*_Point; sellstoptp = NormalizeDouble(sellstoptp, _Digits); int SellStopModify = OrderModify (OrderTicket(),selllimitprice,sellstopsl,sellstoptp,0,clrBlue); }}}}}} Comment ("Initial Account Balance was ",InitialAccountBalance,", Minimum Balance was ",MinAccountBalance,", Max Balance was ",MaxAccountBalance,", Current Balance is ",CurrentAccountBalance," and Current Equity is ",AccountEquity()); // Create and Delete Pending Order and Trailing Stop Loss Lines if (CheckBuyStopOpenOrders()==true) { ObjectCreate(0,"Buy New Expected",OBJ_HLINE,0,0,buyprice); ObjectSetDouble(0,"Buy New Expected",OBJPROP_PRICE,buyprice); ObjectSet("Buy New Expected", OBJPROP_STYLE,STYLE_DASHDOT); ObjectSetText("Buy New Expected", "New BuyStop", 10, "Times New Roman", clrBlue) ; ObjectSetInteger(0,"Buy New Expected",OBJPROP_COLOR,clrGreen); } if (CheckBuyStopOpenOrders()==false) ObjectDelete(0,"Buy New Expected"); if (CheckSellStopOpenOrders()==true) { ObjectCreate(0,"Sell New Expected",OBJ_HLINE,0,0,sellprice); ObjectSetDouble(0,"Sell New Expected",OBJPROP_PRICE,sellprice); ObjectSet("Sell New Expected", OBJPROP_STYLE, STYLE_DASHDOT); ObjectSetText("Sell New Expected", "New SellStop", 10, "Times New Roman", clrBlue) ; ObjectSetInteger(0,"Sell New Expected",OBJPROP_COLOR,clrBlue); } if (CheckSellStopOpenOrders()==false) ObjectDelete(0,"Sell New Expected"); if (CheckBuyLimitOpenOrders()==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()==false) ObjectDelete(0,"BuyLimit New Expected"); if (CheckSellLimitOpenOrders()==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()==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 == SD_Value) { string volumeText = ObjectGetString(0, SD_Value, OBJPROP_TEXT); sd = StringToDouble(volumeText); ObjectSetString(0, SD_Value, OBJPROP_TEXT, string(sd)); return; } if(sparam == TP_Value) { string volumeText = ObjectGetString(0, TP_Value, OBJPROP_TEXT); tp = StringToDouble(volumeText); ObjectSetString(0, TP_Value, OBJPROP_TEXT, string(tp)); return; } if(sparam == SL_Value) { string volumeText = ObjectGetString(0, SL_Value, OBJPROP_TEXT); sl = StringToDouble(volumeText); ObjectSetString(0, SL_Value, OBJPROP_TEXT, string(sl)); return; } } // if an object was clicked if(id==CHARTEVENT_OBJECT_CLICK) { if(sparam=="BuyStopButton") BuyStopOrder(MagicBuyTP); if(sparam=="BuyLimitButton") BuyLimitOrder(MagicBuyTP); if(sparam=="SellStopButton") SellStopOrder(MagicSellTP); if(sparam=="SellLimitButton") SellLimitOrder(MagicSellTP); }} void OnDeinit(const int reason) { OurInterface.Destroy(reason); } void BuyStopOrder(int MagicBuy) { 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); int orderbuystop = OrderSend (_Symbol,OP_BUYSTOP,TradeVolume,buy,slip,slbuy,tpbuy,NULL,MagicBuy,0,clrBlue); buyprice= buy; } void SellStopOrder(int MagicSell) { double sell = Bid-sd*_Point; sell = NormalizeDouble(sell, _Digits); double slsellnew = sell+sl*_Point; slsellnew = NormalizeDouble(slsellnew, _Digits); double tpsellnew = sell-tp*_Point; tpsellnew = NormalizeDouble(tpsellnew, _Digits); int ordersell = OrderSend (_Symbol,OP_SELLSTOP,TradeVolume,sell,slip,slsellnew,tpsellnew,NULL,MagicSell,0,clrGreen); sellprice = sell; } void BuyLimitOrder(int MagicBuy) { 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); int orderlimitstop = OrderSend (_Symbol,OP_BUYLIMIT,TradeVolume,buy,slip,slbuy,tpbuy,NULL,MagicBuy,0,clrBlue); buylimitprice = buy; } void SellLimitOrder(int MagicSell) { double sell = Bid+sd*_Point; // Ask+8*_Point; sell = NormalizeDouble(sell, _Digits); double slsellnew = sell+sl*_Point; slsellnew = NormalizeDouble(slsellnew, _Digits); double tpsellnew = sell-tp*_Point; tpsellnew = NormalizeDouble(tpsellnew, _Digits); int ordersell = OrderSend (_Symbol,OP_SELLLIMIT,TradeVolume,sell,slip,slsellnew,tpsellnew,NULL,MagicSell,0,clrGreen); selllimitprice = sell; } bool CheckBuyStopOpenOrders(){ // 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) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); } bool CheckSellStopOpenOrders(){ // 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) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); } bool CheckBuyLimitOpenOrders(){ // 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) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); } bool CheckSellLimitOpenOrders(){ // 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) return(true); } // If the loop finishes it mean there were no open orders for that pair. return(false); }