[ARCHIVE] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 3. - page 280

 
sergeev:

How is the iMA calculated?


90
Eugene1 20.10.2011 16:34

Is there anywhere I can find how iMA function is calculated (or is it classified)?

Especially I am interested in MODE_LWMA.

I want to look and tweak to my liking, but something googling the source code did not work

https://docs.mql4.com/ru/indicators/iMA

double iMA(string symbol, int timeframe, int period, int ma_shift, int ma_method, int applied_price, int shift)
Calculation of moving average.
You need a formula, so Google can easily find it too.
 
snail09:

https://docs.mql4.com/ru/indicators/iMA

double iMA(string symbol, int timeframe, int period, int ma_shift, int ma_method, int applied_price, int shift)
Calculation of moving average.
You need a formula, Google can easily find it too.
Or don't you understand the term "linear-weighted"?

 

Где-нибудь можно найти как вычисляется функция iMA (или она засекреченная)

Особеннно меня интересует MODE_LWMA.

Хочу посмотреть и подправить на свой вкус, но что-то нагуглить исходники не получилось


//+------------------------------------------------------------------+
//|                                        Custom Moving Average.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- indicator parameters
extern int MA_Period=13;
extern int MA_Shift=0;
extern int MA_Method=0;
//---- indicator buffers
double ExtMapBuffer[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   int    draw_begin;
   string short_name;
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexShift(0,MA_Shift);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   if(MA_Period<2) MA_Period=13;
   draw_begin=MA_Period-1;
//---- indicator short name
   switch(MA_Method)
     {
      case 1 : short_name="EMA(";  draw_begin=0; break;
      case 2 : short_name="SMMA("; break;
      case 3 : short_name="LWMA("; break;
      default :
         MA_Method=0;
         short_name="SMA(";
     }
   IndicatorShortName(short_name+MA_Period+")");
   SetIndexDrawBegin(0,draw_begin);
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer);
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   if(Bars<=MA_Period) return(0);
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
//----
   switch(MA_Method)
     {
      case 0 : sma();  break;
      case 1 : ema();  break;
      case 2 : smma(); break;
      case 3 : lwma();
     }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+
//| Simple Moving Average                                            |
//+------------------------------------------------------------------+
void sma()
  {
   double sum=0;
   int    i,pos=Bars-ExtCountedBars-1;
//---- initial accumulation
   if(pos<MA_Period) pos=MA_Period;
   for(i=1;i<MA_Period;i++,pos--)
      sum+=Close[pos];
//---- main calculation loop
   while(pos>=0)
     {
      sum+=Close[pos];
      ExtMapBuffer[pos]=sum/MA_Period;
           sum-=Close[pos+MA_Period-1];
           pos--;
     }
//---- zero initial bars
   if(ExtCountedBars<1)
      for(i=1;i<MA_Period;i++) ExtMapBuffer[Bars-i]=0;
  }
//+------------------------------------------------------------------+
//| Exponential Moving Average                                       |
//+------------------------------------------------------------------+
void ema()
  {
   double pr=2.0/(MA_Period+1);
   int    pos=Bars-2;
   if(ExtCountedBars>2) pos=Bars-ExtCountedBars-1;
//---- main calculation loop
   while(pos>=0)
     {
      if(pos==Bars-2) ExtMapBuffer[pos+1]=Close[pos+1];
      ExtMapBuffer[pos]=Close[pos]*pr+ExtMapBuffer[pos+1]*(1-pr);
           pos--;
     }
  }
//+------------------------------------------------------------------+
//| Smoothed Moving Average                                          |
//+------------------------------------------------------------------+
void smma()
  {
   double sum=0;
   int    i,k,pos=Bars-ExtCountedBars+1;
//---- main calculation loop
   pos=Bars-MA_Period;
   if(pos>Bars-ExtCountedBars) pos=Bars-ExtCountedBars;
   while(pos>=0)
     {
      if(pos==Bars-MA_Period)
        {
         //---- initial accumulation
         for(i=0,k=pos;i<MA_Period;i++,k++)
           {
            sum+=Close[k];
            //---- zero initial bars
            ExtMapBuffer[k]=0;
           }
        }
      else sum=ExtMapBuffer[pos+1]*(MA_Period-1)+Close[pos];
      ExtMapBuffer[pos]=sum/MA_Period;
           pos--;
     }
  }
//+------------------------------------------------------------------+
//| Linear Weighted Moving Average                                   |
//+------------------------------------------------------------------+
void lwma()
  {
   double sum=0.0,lsum=0.0;
   double price;
   int    i,weight=0,pos=Bars-ExtCountedBars-1;
//---- initial accumulation
   if(pos<MA_Period) pos=MA_Period;
   for(i=1;i<=MA_Period;i++,pos--)
     {
      price=Close[pos];
      sum+=price*i;
      lsum+=price;
      weight+=i;
     }
//---- main calculation loop
   pos++;
   i=pos+MA_Period;
   while(pos>=0)
     {
      ExtMapBuffer[pos]=sum/weight;
      if(pos==0) break;
      pos--;
      i--;
      price=Close[pos];
      sum=sum-lsum+price*MA_Period;
      lsum-=Close[i];
      lsum+=price;
     }
//---- zero initial bars
   if(ExtCountedBars<1)
      for(i=1;i<MA_Period;i++) ExtMapBuffer[Bars-i]=0;
  }
//+------------------------------------------------------------------+
 

Good afternoon!

As the first experience I decided to implement the following algorithm: we can expect a sharp market movement up or down in some time after the Bollinger lines converge into a narrow corridor. In my Expert Advisor, I analyze the state of Bollinger lines and when they are getting very close(Delta pips), we put a pending sell order in the lower direction (at stepOpen pips below the lower line), hoping that the market will suddenly go in that direction. If the market went in another direction, we simply delete this order.

extern double Delta=800.0;              // Ширина канала, которую мы считаем достаточно узкой чтобы ожидать скоро серьезного движения в одну из сторон
extern double StepOpen=150.0;           // Отступ от линии Боллинджера  для открытия отложенного ордера
extern double TP=350.0;                 // Take Profit
extern double SL=400.0;                 // Stop Loss

int start()
  {
   double T1=iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_UPPER,0);  // верхняя линия Боллинджера
   double T2=iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_LOWER,0); // нижняя линия Боллинджера
   if ((Ask>T1+StepOpen*Point)&&OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==true) OrderDelete(0); // если рынок пошел вверх – то удаляем отложенный ордер
   if (OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==true) return(0); // если есть ранее открытый ордер – прекращаем выполнение текущей итерации советника
   if (T1-T2<Delta*Point) {               // если линии Боллинджера сблизились ближе чем на Delta пунктов начинаем операцию открытия ордера
      double PriceOpen=NormalizeDouble(T2-StepOpen*Point,5);  // вычисляем цену открытия
      double StopLoss=NormalizeDouble(T2-StepOpen*Point+SL*Point,5); // вычисляем StopLoss
      double TakeProfit=NormalizeDouble(T2-StepOpen*Point-TP*Point,5); // вычисляем TakeProfit
      OrderSend(Symbol(),OP_SELL,0.1,PriceOpen,5,StopLoss,TakeProfit,0,0,0,Green); //  открываем ордер на продажу
            }
   return(0);
  }
The algorithm works unstable, sometimes opens two orders in a short period of time, constantly gives errors OrderSend Error 130 and OrderSend Error 138, and removing open orders in case the market moves in the other direction does not work at all.

Please, advise us! Thank you!

 
Vinin:


Victor Ivanovich!

So much to leaf through, why didn't you give a link? You put in the code... What for?

 
snail09:
int start()
{
static datetime PrevTime=0; //penultimate bar open time

//Additional checks

if (PrevTime==0) PrevTime=Time[0]; // at first start the current bar is skipped
if (Time[0]<=PrevTime) return(0); // check the new bar open time

//--Your basic code

PrevTime=Time[0]; // Remember the time of the zero bar opening

return(0);
}

If you have a bar open setup, here's the template, it works.

Could you please write this function in the EA yourself? In the picture you can see 3 orders opened on one bar. I need only one. I can't fix the code.

//+------------------------------------------------------------------+
int start()
//+------------------------------------------------------------------+
{
//--- Разрешение на открытие позиций - каждую свечу
if(candle_time != Time[0])
{
candle_time = Time[0];
new_bar_buy = true;
new_bar_sell = true;
}
if (Bars < bars_count){Print("Мало данных для работы"); return(0);}

//--- Получение значений индикатора iMA
ma10 = iMA ( Symbol(), Period(), MA1_period, MA1_shift, MA1_method, MA1_price, bars_shift);
ma20 = iMA ( Symbol(), Period(), MA2_period, MA2_shift, MA2_method, MA2_price, bars_shift);
ma11 = iMA ( Symbol(), Period(), MA1_period, MA1_shift, MA1_method, MA1_price, bars_shift+1);
ma21 = iMA ( Symbol(), Period(), MA2_period, MA2_shift, MA2_method, MA2_price, bars_shift+1);

if (Revers)
{
if(rsi>50 && rsi<70)//--- включена инверсия
{
//--- Условие для продажи
if(ma11 < ma21 && ma10 > ma20 && ma30 > ma10 && new_bar_sell)
{
sell_open=true;
new_bar_sell = false;
}
}
if(rsi>30 && rsi<50)//--- включена инверсия
{
//--- Условие для продажи
if(ma11 > ma21 && ma10 < ma20 && ma30 < ma10 && new_bar_buy)
{
buy_open=true;
new_bar_buy = false;
}
}
}
//--- выставление рыночных ордеров
if(IsTradeAllowed())
{
Trade_BUY();
Trade_SELL();
}
return(0);
//+------------------------------------------------------------------+
void Trade_BUY()
//+------------------------------------------------------------------+
{

for ( int i = 0; i < OrdersTotal(); i++ )
{
if ( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) )
{
if ((OrderSymbol() != Symbol()) || (OrderMagicNumber() != Magic)) continue; //не наш ордер
if ( OrderType() == OP_BUY) // если ордер OP_BUY
{
min_level = MarketInfo( Symbol(), MODE_STOPLEVEL );
//--- проверка на работу трейлинг стопа
if (trailing_stop !=0.0 && Ask > NormalizeDouble(OrderStopLoss() + (stop_loss_buy + MathMax(min_level, trailing_stop)) * Point, Digits) && OrderProfit() > 0)
{

if ( OrderModify ( OrderTicket(), OrderOpenPrice(),
NormalizeDouble (OrderStopLoss() + MathMax(min_level, trailing_stop) * Point, Digits),
OrderTakeProfit(), 0, CLR_NONE))
{
Print("Сработал трейлинг стоп для Buy");
}
else
{
Print ("Ошибка модификации ордера #", GetLastError());
}

RefreshRates();
Sleep(3000);
} //end modify

}// end BUY
}//end OrderSelect
else
{
Print( "OrderSelect ошибка #", GetLastError() );
Sleep(3000);
RefreshRates();
return;
}
}// end for
//--- Открываем ордер если есть сигнал
while (buy_open)
{
//--- нормализация лота
lot = Normalize_Lot(Lots);
//--- проверка на наличие свободной маржи
if( AccountFreeMarginCheck(Symbol(),OP_BUY, lot) <= 0 || GetLastError() == 134 )
{
Print("Недостаточно свободной маржи для открытия ордера");
buy_open = false;
break;
}
min_level = MarketInfo( Symbol(), MODE_STOPLEVEL );
if ( OrderSend( Symbol(), OP_BUY, lot, NormalizeDouble(Ask, Digits), slippage,
NormalizeDouble(Bid - MathMax(stop_loss_buy, min_level) * Point, Digits),
NormalizeDouble(Bid + MathMax(take_profit_buy,min_level) * Point, Digits),
DoubleToStr(Magic, 0), Magic, 0, Blue) > 0 )
{
PlaySound("Wait.wav");
buy_open = false; // ордер открыт
break;
}
else
{
int Error = GetLastError(); // Не получилось :(
switch(Error) // Преодолимые ошибки
{
case 138:Alert("Ошибка: ",Error," Новые цены.");
RefreshRates();
continue;
case 135:Alert("Ошибка: ",Error," Цена изменилась.");
RefreshRates();
continue;
case 136:Alert("Нет цен.");
while(RefreshRates() == false)
Sleep(500);
continue;
case 146:Alert("Подсистема торговли занята.");
Sleep(500);
RefreshRates();
continue;
default: Alert("Возникла ошибка ", Error," Выход из подпрограммы."); // Другие варианты ошибок
}// end switch
}// end else
break;
}// end while
}// end Trade_BUY
//+------------------------------------------------------------------+
void Trade_SELL()
//+------------------------------------------------------------------+
{

for ( int i = 0; i < OrdersTotal(); i++ )
{
if ( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) )
{
if ((OrderSymbol() != Symbol()) || (OrderMagicNumber() != Magic)) continue; //не наш ордер
if ( OrderType() == OP_SELL) //если ордер OP_SELL
{
min_level = MarketInfo( Symbol(), MODE_STOPLEVEL );

//--- проверка на работу трейлинг стопа
if (trailing_stop !=0.0 && Bid < NormalizeDouble(OrderStopLoss() - (stop_loss_sell + MathMax(min_level, trailing_stop)) * Point, Digits)&&OrderProfit() > 0)
{
if ( OrderModify ( OrderTicket(), OrderOpenPrice(),
NormalizeDouble (OrderStopLoss() - MathMax(min_level, trailing_stop) * Point, Digits),
OrderTakeProfit(), 0, CLR_NONE))
{
Print("Сработал трейлинг стоп для Sell");
}
else
{
Print ( "Ошибка модификации ордера #", GetLastError());
}

RefreshRates();
Sleep(3000);
}
}// end BUY
}//end OrderSelect
else
{
Print( "OrderSelect ошибка #", GetLastError() );
Sleep(3000);
RefreshRates();
return;
}
}// end for
//--- Открываем ордер если есть сигнал
while (sell_open)
{

//--- нормализация лота
lot = Normalize_Lot(Lots);
//--- проверка на наличие свободной маржи
if( AccountFreeMarginCheck(Symbol(),OP_SELL, lot) <= 0 || GetLastError() == 134 )
{
Print("Недостаточно свободной маржи для открытия ордера");
sell_open = false;
break;
}
min_level = MarketInfo( Symbol(), MODE_STOPLEVEL );
if ( OrderSend( Symbol(), OP_SELL, lot, NormalizeDouble(Bid, Digits), slippage,
NormalizeDouble(Ask + MathMax(stop_loss_sell, min_level) * Point, Digits),
NormalizeDouble(Ask - MathMax(take_profit_sell, min_level) * Point, Digits),
DoubleToStr(Magic, 0), Magic, 0, Blue) > 0 )
{
PlaySound("Wait.wav");
sell_open = false; // ордер открыт
break;
}
else
{
int Error = GetLastError(); // Не получилось :(
switch(Error) // Преодолимые ошибки
{
case 138:Alert("Ошибка: ",Error," Новые цены.");
RefreshRates();
continue;
case 135:Alert("Ошибка: ",Error," Цена изменилась.");
RefreshRates();
continue;
case 136:Alert("Нет цен.");
while(RefreshRates() == false)
Sleep(500);
continue;
case 146:Alert("Подсистема торговли занята.");
Sleep(500);
RefreshRates();
continue;
default: Alert("Возникла ошибка ", Error," Выход из подпрограммы."); // Другие варианты ошибок
}// end switch
}// end else
break;
}// end while
}// end Trade_SELL


//+------------------------------------------------------------------+
double Normalize_Lot(double lot)// Проверка на допустимое значение лота
//+------------------------------------------------------------------+
{
double lot_min = MarketInfo(Symbol(), MODE_MINLOT);
double lot_max = MarketInfo(Symbol(), MODE_MAXLOT);
double lot_step = MarketInfo(Symbol(), MODE_LOTSTEP);
if ( lot <= lot_min ) lot = lot_min; // минимальный
else if ( lot >= lot_max ) lot = lot_max; // максимальный
else lot = MathFloor( lot / lot_step ) * lot_step ; // округление до ближайшего меньшего
return(lot);
}

//+------------------------------------------------------------------+
int MathMax4(int i1,int i2,int i3,int i4)// Возврат максимального значения
//+------------------------------------------------------------------+
{
int imax=i1;
if(i2>imax)imax=i2;
if(i3>imax)imax=i3;
if(i4>imax)imax=i4;
return(imax);
}








 

Hi all ))))

Help in the adviser to limit opening of orders to 1 per day, no matter how many times it will pass an entry point...

I am trying to write something like this, it checks history of last closed order before opening, and if it matches then don't open...

for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
{
if(OrderCloseTime()==Day()
break;

}

this is not working, please help the lamochinka ))))

 

order on one bar site:mql4.com

https://www.mql5.com/ru/forum/102366/page3

 
Good afternoon, dear forum users.
Need help in changing the EA, ie leave everything as is, but change the action (buy/sell) to (sell/buy), my tests showed that the uptrend is sell, and the downtrend buy. a simple change in the code BUY on SELL me nothing, or something is missing, or .... Anyway,i need help
Thanks in advance.
Files:
 
rusa:








All right, I will. Just for the weekend, can you wait? (business trip).
Reason: