Stuck in while-loop (MT4)

To add comments, please log in or register
Simon Liu
683
Simon Liu  

Hi,

In my code i have two lines EntryPriceLine and StopLossLine

If i drag my EntryPriceLine i would like to have real time feedback of the StopLossLine.

So the two lines should always be 12 pips apart. Even while dragging.

I thought using a while loop and WindowRedraw() would fix this, but if i let go of my mousebutton the script is still stuck in the while-loop. As if im still dragging.

Anyone can help? I am using MT4

       
while(id==CHARTEVENT_OBJECT_DRAG && sparam=="EntrypriceLine")
       {
         Print(ObjectGet("EntrypriceLine",OBJPROP_PRICE1));       
         ObjectMove("StopLossLine", "StopLossLine",0,0,ObjectGet("EntrypriceLine",OBJPROP_PRICE1)-StopLoss*Point);
         WindowRedraw();
       }
Keith Watford
Moderator
20377
Keith Watford  
What while loop?
Andrey Barinov
50178
Andrey Barinov  
Simon Liu:

Hi,

In my code i have two lines EntryPriceLine and StopLossLine

If i drag my EntryPriceLine i would like to have real time feedback of the StopLossLine.

So the two lines should always be 12 pips apart. Even while dragging.

I thought using a while loop and WindowRedraw() would fix this, but if i let go of my mousebutton the script is still stuck in the while-loop. As if im still dragging.

Anyone can help? I am using MT4

Please, share the complete code.

Simon Liu
683
Simon Liu  
Keith Watford:
What while loop?

I edited the code. (if to while). Was testing things late night.

Simon Liu
683
Simon Liu  
Andrey Barinov:

Please, share the complete code.


#include <Controls/Button.mqh>

CButton CloseEA;
CButton DrawEP;
CButton Scalp;

bool drawLine = false;

int magic0 = 0654;
int magic1 = 1987;
int magic2 = 2234;
int magic3 = 3363;

//Percentage of available balance to risk in each individual trade
extern double MaxRiskPerTrade=1; //% of balance to risk in one trade
extern double StopLoss=16; //Stop Loss in pips
extern double TimeToClose=10; //Time after which scalp-orders cancel

bool SetEntryPrice = false;


//int x = 0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   //DrawEP button
   DrawEP.Create(0,"DrawEP",0,20,20,50,50);
   DrawEP.Text(CharToStr(33));
   DrawEP.Font("Wingdings");
   DrawEP.FontSize(12);
   DrawEP.Color(clrBlack);
   DrawEP.ColorBackground(clrAquamarine);
   DrawEP.ColorBorder(clrBlack);  
   
   //Close EA button
   CloseEA.Create(0,"CloseEA",0,20,50,50,80);
   CloseEA.Text("X");
   CloseEA.Font("Verdana");
   CloseEA.FontSize(12);
   CloseEA.Color(clrWhite);
   CloseEA.ColorBackground(clrRed);
   //CloseEA.ColorBorder(clrBlack);
   
   //Scalp button
   Scalp.Create(0,"Scalp",0,0,170,50,230);
   Scalp.Text("Scalp");
   Scalp.FontSize(12);
   Scalp.Color(clrRed);
   Scalp.ColorBackground(clrKhaki);
   Scalp.ColorBorder(clrBlack);
   
   ObjectCreate("TextBox", "TextBox", OBJ_BUTTON,0,0,0);
   ObjectSetText("TextBox","");
   ObjectSetInteger("TextBox","TextBox",OBJPROP_XDISTANCE, 0);
   ObjectSetInteger("TextBox","TextBox",OBJPROP_YDISTANCE, 230);
   ObjectSetInteger("TextBox","TextBox",OBJPROP_XSIZE,200);
   ObjectSetInteger("TextBox","TextBox",OBJPROP_YSIZE, 100);
   
   ObjectCreate("StoplossText", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("StoplossText","Stoploss:",14, "Verdana", clrBlack);
   ObjectSet("StoplossText", OBJPROP_CORNER, 0);
   ObjectSet("StoplossText", OBJPROP_XDISTANCE, 0);
   ObjectSet("StoplossText", OBJPROP_YDISTANCE, 240);
   
   ObjectCreate("Stoploss",OBJ_EDIT,0,0,0);
   ObjectSetText("Stoploss", DoubleToStr(StopLoss,0),20, "Verdana", clrBlack);
   ObjectSet("Stoploss", OBJPROP_CORNER, 0);
   ObjectSet("Stoploss", OBJPROP_XDISTANCE, 120);
   ObjectSet("Stoploss", OBJPROP_YDISTANCE, 242);
   ObjectSet("StopLoss",OBJPROP_BGCOLOR, clrWhite);
   
   ObjectCreate("MaxRiskPerTradeText", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("MaxRiskPerTradeText","Risk:",14, "Verdana", clrBlack);
   ObjectSet("MaxRiskPerTradeText", OBJPROP_CORNER, 0);
   ObjectSet("MaxRiskPerTradeText", OBJPROP_XDISTANCE, 0);
   ObjectSet("MaxRiskPerTradeText", OBJPROP_YDISTANCE, 262);
   
   ObjectCreate("MaxRiskPerTrade",OBJ_EDIT,0,0,0);
   ObjectSetText("MaxRiskPerTrade",DoubleToStr(MaxRiskPerTrade,2),20, "Verdana", clrBlack);
   ObjectSet("MaxRiskPerTrade", OBJPROP_CORNER, 0);
   ObjectSet("MaxRiskPerTrade", OBJPROP_XDISTANCE, 120);
   ObjectSet("MaxRiskPerTrade", OBJPROP_YDISTANCE, 264);
   
   ObjectCreate("TimeToCloseText", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("TimeToCloseText","Close time:",14, "Verdana", clrBlack);
   ObjectSet("TimeToCloseText", OBJPROP_CORNER, 0);
   ObjectSet("TimeToCloseText", OBJPROP_XDISTANCE, 0);
   ObjectSet("TimeToCloseText", OBJPROP_YDISTANCE, 284);
   
   ObjectCreate("TimeToClose",OBJ_EDIT,0,0,0);
   ObjectSetText("TimeToClose",DoubleToStr(TimeToClose,0),20, "Verdana", clrBlack);
   ObjectSet("TimeToClose", OBJPROP_CORNER, 0);
   ObjectSet("TimeToClose", OBJPROP_XDISTANCE, 120);
   ObjectSet("TimeToClose", OBJPROP_YDISTANCE, 286);
   
   
   
   //Convert points to Pips
   StopLoss = StopLoss *10;
//---
   return(INIT_SUCCEEDED);
  }

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
  
      //Print("The coordinates of the mouse click on the chart are: x = ",lparam,"  y = ",dparam);
      datetime time;
      double price;
      int subwindow;
      ChartXYToTimePrice(0,lparam,dparam,subwindow,time,price);
      
      if(id==CHARTEVENT_OBJECT_CLICK && sparam=="DrawEP")
      {
         //set stoploss
         StopLoss = 10*StrToDouble(ObjectDescription("Stoploss"));
         
         //place horizontal line at current pric
         ObjectCreate("EntrypriceLine","EntrypriceLine",OBJ_HLINE,0,0,Bid);
         ObjectSetInteger("EntrypriceLine","EntrypriceLine",OBJPROP_COLOR,clrBlack);
         ObjectSetInteger("EntryPriceLine","EntrypriceLine",OBJPROP_SELECTED, TRUE);
         //ObjectSetText("EntryPriceLine",DoubleToStr(ObjectGet("EntryPrice",OBJPROP_PRICE1)),12);
         //ObjectSetText("EntryPriceLine","test",12);
         
         ObjectCreate("EntryPriceText", "EntryPriceText",OBJ_TEXT, 0,Time[0],Bid+50*Point);
         //ObjectSetText("EntryPriceText",DoubleToStr(ObjectGet("EntryPrice",OBJPROP_PRICE1),5),14, "Verdana", clrBlack);
         ObjectSetText("EntryPriceText","Open",14, "Verdana", clrBlack);
         
         //place stoploss line
         ObjectCreate("StopLossLine","StopLossLine",OBJ_HLINE,0,0,(Bid-StopLoss*Point));
         ObjectSetInteger("StopLossLine","StopLossLine",OBJPROP_COLOR,clrRed); 
         ObjectSetInteger("StopLossLine","StopLossLine",OBJPROP_SELECTED, TRUE);
       }
       
       if(id==CHARTEVENT_OBJECT_DRAG && sparam=="EntrypriceLine")
       {
         Print(ObjectGet("EntrypriceLine",OBJPROP_PRICE1));       
         ObjectMove("EntryPriceText","EntryPriceText",0,Time[0],ObjectGet("EntrypriceLine",OBJPROP_PRICE1)+50*Point);
         ObjectMove("StopLossLine", "StopLossLine",0,0,ObjectGet("EntrypriceLine",OBJPROP_PRICE1)-StopLoss*Point);
         WindowRedraw();
       }
}
To add comments, please log in or register