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

 
Fast235 #:

only this line raises the question

-50 extra and an array

It's to draw above/below "0"

But you can do without it, I need the gist of it, how to use handles.

 
Vladimir Simakov #:

I highly recommend it:

Otherwise you get an oops.))

Plus, get it right down to a whole right after division. Otherwise binary counting can do a lot of things.)

 
MakarFX #:

This is to draw above/below "0"

but you can do without it, I need to know how to use handles.

a handle is a pointer to the indicator file, it is usually created in OnInit()

   Handle=iCustom(Symbol(),PERIOD_H1,"_iTrend",10);
//--- Если не удалось получить хендл индикатора
   if(Handle==INVALID_HANDLE)
     {
      PrintFormat("Failed to create handle of the iAO indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //---
      return(INIT_FAILED);
     }

further in the code

    if(CopyBuffer(Handle,0,1,1,Buffer1) <=0 проверка на ошибку
---
в Buffer1[1111] получаем значение
 
Fast235 #:

a handle is a pointer to the indicator file, it is created in OnInit() usually

further in the code

Thanks, but it looks like the help and unfortunately it is not clear to me(

That's why I asked to translate my code into mql5 in order to understand the logic of work

I do it this way

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
   prd = _Period > TimeFrame ? _Period : TimeFrame;
   atrHandle = iATR(_Symbol, prd, Per_Count);
   if(atrHandle == INVALID_HANDLE)
     {
      Print("Can't load indicator."); return INIT_FAILED;
     }
//--- indicator buffers mapping
   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);
   SetIndexBuffer(1,Label2Buffer,INDICATOR_DATA);
   
   ArraySetAsSeries(Label1Buffer, true);
   ArraySetAsSeries(Label2Buffer, true);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   int limit,i;
   int barPlus=iBarShift(_Symbol,_Period,iTime(_Symbol,PERIOD_W1,WeekCount),false);
   limit=rates_total-prev_calculated-3;
   if(WeekCount!=0)limit=barPlus-1;
   if(limit<1) return(0);
   for(i=limit;i>=0;i--)
     {
      TimeToStruct(time[i],inTime);
      index01=iBarShift(_Symbol,PERIOD_D1,time[i],false);
      if(inTime.hour==0&&inTime.min==0)
        {
         Label1Buffer[i]=GetIndicator(atrHandle, index01+1);
        }
     }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
double GetIndicator(const int handle, const int i)
  {
   double res[1];
   if(CopyBuffer(handle, 0, i, 1, res) <= 0) return 0;
   return res[0];
  }
//+------------------------------------------------------------------+

But i have a feeling something is wrong and iMAOnArray is not mentioned in help.

 

Good afternoon.

I can't solve the problem with martin looping,

I can't solve the problem with the looping of the martin. it gets disconnected after execution :(n>=OrdersClose)- and then the martin is not triggered until a profitable trade occurs,

I need to havereturn(dLots) after (n>=OrdersClose) and Martin will start again if the next trade is again losing.

Could you please advise how to do this?

double LOT()
{
   int n=0;
   double OL=dLots;
   for (int j = OrdersHistoryTotal()-1; j >= 0; j--)
   {
      if (OrderSelect(j, SELECT_BY_POS,MODE_HISTORY))
      {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == iMagic)
         {
            if (OrderProfit()<0) 
            {
               if (n==0) OL=NormalizeDouble(OrderLots()*K_Martin,DigitsLot);
               n++;
               if (n>=OrdersClose) {return(dLots);}
            }
            else
            {
               if (n==0) {return(dLots);}
               else {return(OL);}
            }
         }
      }
   }
   return(OL);
}
 

Good day to all!!!

Please advise me in the grid advisor code prescribed the display of the average price on the chart. It would be OK, but not correctly deleted the line after closing the grid, that is, the average price is not. Please tell me what I did wrong. Here is the code and picture.

//+----------------------------------------------------------------------------+
//| Modify group orders |
//+----------------------------------------------------------------------------+
void ModifyOrders(int otype)
{
double avg_price, AveragePriceBuy, AveragePriceSell, order_lots = 0;
price = 0;

for(int i = OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype)
{
price += OrderOpenPrice() * OrderLots();
order_lots += OrderLots() ;
}
}
}
avg_price = NormalizeDouble(price / order_lots, Digits);
AveragePriceBuy = NormalizeDouble(avg_price + Spread, Digits);
AveragePriceSell = NormalizeDouble(avg_price - Spread, Digits);
{
ObjectDelete(0, "AveragePriceLine");
ObjectCreate("AveragePriceLine" ,OBJ_HLINE, 0, 0 ,AveragePriceBuy);
ObjectCreate("AveragePriceLine" ,OBJ_HLINE, 0, 0 ,AveragePriceSell);
ObjectSet("AveragePriceLine",OBJPROP_COLOR,Blue);
}
if ((otype == OP_BUY) && (Drawdown <= DrawdownClosingTakeprofitZero))
tp = NormalizeDouble (AveragePriceBuy + TakeProfitGroupOrder*Point, Digits);
if ((otype == OP_SELL) && (Drawdown <= DrawdownClosingTakeprofitZero))
tp = NormalizeDouble (AveragePriceSell - TakeProfitGroupOrder*Point, Digits);
if ((otype == OP_BUY) && (Drawdown > DrawdownClosingTakeprofitZero))
tp = NormalizeDouble (AveragePriceBuy, Digits);
if ((otype == OP_SELL) &/or& (Drawdown > DrawdownClosingTakeprofitZero))
tp = NormalizeDouble (AveragePriceSell, Digits);

for(int i = OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype)
{
if(OrderModify(OrderTicket(), OrderOpenPrice(), 0, tp, 0))
Print("Orders successfully modified!");
else Print("Error modifying orders!");
}
}
}
}


 
EVGENII SHELIPOV #:

Good day to all!!!

Please advise me in the grid advisor code prescribed the display of the average price on the chart. It would be OK, but not correctly deleted the line after closing the grid, that is, the average price is not. Please tell me what I did wrong. Here is the code and picture.

Try it like this

//+----------------------------------------------------------------------------+
//| Модификация групповых ордеров                                              |
//+----------------------------------------------------------------------------+
void ModifyOrders(int otype)
{
    double avg_price, AveragePriceBuy, AveragePriceSell, order_lots = 0;
    price = 0;
   
    for(int i = OrdersTotal()-1; i>=0; i--)
    {
       if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
       {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype)
         {
            price += OrderOpenPrice() * OrderLots();
            order_lots += OrderLots() ;
         }
       }
    }
    avg_price = NormalizeDouble(price / order_lots, Digits);
    AveragePriceBuy = NormalizeDouble(avg_price + Spread, Digits);
    AveragePriceSell = NormalizeDouble(avg_price - Spread, Digits);
    if(ObjectFind(0,"AveragePriceLineBuy")==0)
     {
     ObjectDelete(0,"AveragePriceLineBuy");
     ObjectCreate(0,"AveragePriceLineBuy" ,OBJ_HLINE, 0, 0 ,AveragePriceBuy);
     ObjectSetInteger(0,"AveragePriceLine",OBJPROP_COLOR,Blue);
     }
    else
     {
     ObjectCreate(0,"AveragePriceLineBuy" ,OBJ_HLINE, 0, 0 ,AveragePriceBuy);
     ObjectSetInteger(0,"AveragePriceLine",OBJPROP_COLOR,Blue);
     }
    if(ObjectFind(0,"AveragePriceLineSell")==0)
     {
     ObjectDelete(0,"AveragePriceLineSell");
     ObjectCreate(0,"AveragePriceLineSell" ,OBJ_HLINE, 0, 0 ,AveragePriceSell);
     ObjectSetInteger(0,"AveragePriceLine",OBJPROP_COLOR,Blue);
     }
    else
     {
     ObjectCreate(0,"AveragePriceLineSell" ,OBJ_HLINE, 0, 0 ,AveragePriceSell);
     ObjectSetInteger(0,"AveragePriceLine",OBJPROP_COLOR,Blue);
     }
    if ((otype == OP_BUY) && (Drawdown <= DrawdownClosingTakeprofitZero)) 
    tp = NormalizeDouble (AveragePriceBuy + TakeProfitGroupOrder*Point, Digits);
    if ((otype == OP_SELL) && (Drawdown <= DrawdownClosingTakeprofitZero))
    tp = NormalizeDouble (AveragePriceSell - TakeProfitGroupOrder*Point, Digits);
    if ((otype == OP_BUY) && (Drawdown > DrawdownClosingTakeprofitZero)) 
    tp = NormalizeDouble (AveragePriceBuy, Digits);
    if ((otype == OP_SELL) && (Drawdown > DrawdownClosingTakeprofitZero))
    tp = NormalizeDouble (AveragePriceSell, Digits);
    
    for(int i = OrdersTotal()-1; i>=0; i--) 
    {
       if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
       {
           if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype)
           {
               if(OrderModify(OrderTicket(), OrderOpenPrice(), 0, tp, 0))
                  Print("Ордера успешно модифицированы!");
                else Print("Ошибка модификации ордеров!");
           }
       }
    }
}   

Or better prescribe,

if there are no open Sell orders, delete the Sell line

the same for the bai

 
Good evening, how do you reset the status of a graphic button when you click, so that it is not pressed all the time until you click it again?
 
Nerd Trader #:
Good evening, how do you reset the status of a graphic button when you click, so that it is not pressed all the time until you click it again?
ObjectSetInteger(0,name,OBJPROP_STATE,false);
 
MakarFX #:
No, you've got it wrong. I need the state to be reset like for normal buttons after the click is released. How to implement this if mql4 doesn't have mouse button state events: click/release.
Reason: