Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 681

 
Igor Makanu:

probably like this, for selling:

profit=NormalizeDouble((OrderOpenPrice()-Ask)*MarketInfo(_Symbol,MODE_TICKVALUE)*lot/Point,2);

to buy by Bid, i.e. ( Bid -OrderOpenPrice())

TakeProfit =100 points Lot = 0.1, how much will be the profit of the open position in the currency, when the position is closed by TP?

profit=NormalizeDouble((OrderOpenPrice()-Тейк_Профит*Point)*MarketInfo(_Symbol,MODE_TICKVALUE)*lot/Point,2);

it doesn't work that way.

 
Vitaly Muzichenko:

Need to operate:Lot -> Money -> Distance, and the cost of the tick

Money is just the thing to find out )

 
Vitaly Muzichenko:

The solution does not take into account commissions and swaps. The points seem to be in profit, but the costs are a loss.

I don't like the formula OrderProfit()+ OrderComission()+OrderSwap() with commissions and swaps

The help says that commission may be in points or in deposit currency, this formula doesn't take this into account, and may do more harm than good in testing, but this is a matter of taste

Ghabo:

Take_Profit =100 points, lot =0.1, how much will be the profit of an open position in the currency, when the position is closed by TP?

It doesn't work like that.

Isn't maths your thing? ;)

otherwise:

profit=NormalizeDouble((Take_Profit *Point)*MarketInfo(_Symbol,MODE_TICKVALUE)*lot/Point,2);

if shortened, it will be

profit=NormalizeDouble(lot *TakeProfit*MarketInfo(_Symbol,MODE_TICKVALUE),2);

 
Vitaly Muzichenko:

What is meant here is thatOrderProfit() should be used in the calculation

How to calculate the lot for a position before sending an order to get a profit of USD 20 when TakeProfit kicks in? The OrderProfit() will not help here - there is no position yet.

Or otherwise: What would be the TakeProfit of the future position in order to get a profit of 20 USD when opening with 0.1 lot? And here OrderProfit() will not help - for the same reason.

And another way: What Stop Loss should be in the future position, so that when I open with 0.1 lot I get a loss of no more than 3% of available funds? And here OrderProfit() will not help - for the same reason.

Update: all calculations will be approximate without knowing the commission and swap. We will have to adjust stop-orders "on the spot" if we need accuracy to a cent or a point.

 
Artyom Trishkin:

Before sending an order to open a position, how do I calculate the lot for the future position, so that when the takeprofit is triggered, I get a profit of 20 USD? OrderProfit() is not helpful here - there is no position yet.

Or in other words: What would be the TakeProfit of the future position, so that if I open with 0.1 lot I would get a profit of 20 USD? And here OrderProfit() will not help - for the same reason.

I just wrote and understood what I had to calculate before opening and immediately deleted the message. How smart of you to reply)

I once wrote such, and entered an amount per 1 lot in the input parameters as a commission.

 
Hello, I can't figure it out, please help. What is wrong with this code? I display the average value in the comment and it increases with every tick? Help:-))))
 for(shift = 0; shift <= Bars-1; shift++)
  {
      zz = iCustom(NULL, 0, "ZigZag", ExtDepth, ExtDeviation, ExtBackstep, 0, shift);
           if(zz > 0.0)
           {
           HZZ[ww]=zz;
           ww++;          
           }
  }
   
  
       for(ww=0;ww<=nn;ww++){
      if(HZZ[ww]>HZZ[ww+1]){SredRazmax += HZZ[ww];}     
      if(HZZ[ww]<HZZ[ww+1]){SredRazmin += HZZ[ww+1];}
      
       
  Comment("Средний размах = ", (SredRazmax-SredRazmin)/nn,",",HZZ[0],",",HZZ[1],",",HZZ[2],",",HZZ[3],",",HZZ[4]); 
 
Dmitry Belov:
does it increase with each tick? Help:-))))

it means you are counting on every tick the code you submitted and not initializingSredRazmax andSredRazmin variables

it's a good habit to initialize variables before use - that's what universities teach you, it reduces the time it takes to find bugs ;)

 

Colleagues, why does this code refuse to execute in the strategy tester, but executes correctly in real time. I'm talking specifically about the OnChartEvent() function. In the visualization mode in the tester, it is not executed when the buttons are clicked.

input double lot=0.1;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   ButtonCreate("ButtonBuy",100,100,200,40,"Buy",15,clrBlue);
   ButtonCreate("ButtonSell",100,200,200,40,"Sell",15,clrRed);
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   long cid=ChartID();
   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="ButtonBuy")
     {
      if(OrderSend(_Symbol,OP_BUY,lot,Ask,200,0,0,NULL,0,0,clrBlue)==-1)
         Print("Error: ",GetLastError());
      ObjectSetInteger(cid,"ButtonBuy",OBJPROP_STATE,false);
     }
   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="ButtonSell")
     {
      if(OrderSend(_Symbol,OP_SELL,lot,Bid,200,0,0,NULL,0,0,clrRed)==-1)
         Print("Error: ",GetLastError());
      ObjectSetInteger(cid,"ButtonSell",OBJPROP_STATE,false);
     }
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+ 
//| ButtonCreate                                                     | 
//+------------------------------------------------------------------+ 
void ButtonCreate(string name,int x,int y,int width,int height,
                  string text,int font_size,color back_clr)
  {
   long cid=ChartID();
   int subWind=0;
   ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER;
   string font="Cambria";
   color text_clr=clrBlack;
   color border_clr=clrBlack;
   bool state=false;
   bool back=false;
   bool selectable=false;
   bool selected=false;
   bool hidden=true;
   long zorder=0;
   if(ObjectFind(cid,name)==-1)
     {
      ObjectCreate(cid,name,OBJ_BUTTON,subWind,0,0);
      ObjectSetInteger(cid,name,OBJPROP_XDISTANCE,x);
      ObjectSetInteger(cid,name,OBJPROP_YDISTANCE,y);
      ObjectSetInteger(cid,name,OBJPROP_XSIZE,width);
      ObjectSetInteger(cid,name,OBJPROP_YSIZE,height);
      ObjectSetInteger(cid,name,OBJPROP_CORNER,corner);
      ObjectSetString(cid,name,OBJPROP_TEXT,text);
      ObjectSetString(cid,name,OBJPROP_FONT,font);
      ObjectSetInteger(cid,name,OBJPROP_FONTSIZE,font_size);
      ObjectSetInteger(cid,name,OBJPROP_COLOR,text_clr);
      ObjectSetInteger(cid,name,OBJPROP_BGCOLOR,back_clr);
      ObjectSetInteger(cid,name,OBJPROP_BORDER_COLOR,border_clr);
      ObjectSetInteger(cid,name,OBJPROP_BACK,back);
      ObjectSetInteger(cid,name,OBJPROP_STATE,state); 
      ObjectSetInteger(cid,name,OBJPROP_SELECTABLE,selectable);
      ObjectSetInteger(cid,name,OBJPROP_SELECTED,selected);
      ObjectSetInteger(cid,name,OBJPROP_HIDDEN,hidden);
      ObjectSetInteger(cid,name,OBJPROP_ZORDER,zorder);
     }
  }
//+------------------------------------------------------------------+
 
Oleg Remizov:

Colleagues, why does this code refuse to execute in the strategy tester, but executes correctly in real time. I'm talking specifically about the OnChartEvent() function. In the visualization mode in the tester, it is not executed when the buttons are clicked.

Because OnChartEvent() does not work in the tester in MT4. Check the button state flag in the tester.
 
Artyom Trishkin:
Because in MT4 OnChartEvent() does not work in the tester. Check the button state flag in the tester.

Thank you! I had that suspicion, but I didn't find information in the help that OnChartEvent() doesn't work in the tester.