[WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you. - page 239

 
dmmikl86 >> :

Dear forum users!

I wrote an indicator and the problem is that I can't draw moving average (Buf_2), here is the code:

Help me to understand what's the problem?

And why would it be drawn if you don't enter the loop after //average and i<0 otherwise you won't come out of the previous loop

 i++; // поставьте здесь увеличение на один и всё заработает или присвойте i=0; напрямую ноль
//====================================================================
// average
int n=0;
   while( i>=0) 
      {  
       //----
       while( n<= average){
         Buf_2[ i]= Buf_1[ i- n];
         n++;
         }
       
       i--;
       //----
      }

//----
   return(0);
  }
//+------------------------------------------------------------------+
 

Good afternoon)


Sorry for the intrusion, I have already asked this question, but do not get the answer( Anyway, during the week, nothing has moved on


Can you tell me how to write a function placing a pending order?


What should I put instead of Bid or Ask? I need a pending order at High of one of the candlesticks. I get this value and how should I enter it into the function? This price may not be in the trade flow. I wonder if you could put out the code for putting a pending order and, more importantly, where to get its setting price from!

 

For example (example of setting a buy stop) :

(it is not at all necessary for the set price to be in the quote stream in order to set a pending order.

you may dance with the current price or take any price, as long as you adhere to the stop level from CURRENT PRICE TO THE STOP PRICE)

( 'EURUSD - Trends, Forecasts & Consequences' )

extern int      Magic = 77777;
extern int      StopLoss=400;
extern int      TakeProfit=120;
extern double   Lots=0.1;

static int prevtime = 0;
//-- Подключаемые модули --
#include <stderror.mqh>
#include <stdlib.mqh>


//=======================================================
int start()
{
if(Time[0] == prevtime)   return(0);//ждём появления нового бара
   prevtime = Time[0];//если появился новый бар , включаемся
   

//-----------------------------------------------------------------------
if ( NumberOfOrders(NULL,OP_BUYSTOP, Magic)<1 )  {//если нет открытых бай- поз.
if   ( NumberOfPositions(NULL,OP_BUY, Magic)<1)  {//если нет ордеров байстоп
double priceBUY=iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,50,0));//-задаем цену
// установки  ордера по максимуму из посл. 50-ти свечей
double SL=0;double TP=0;//выставляем стопы
if( StopLoss>0)   SL= priceBUY-Point* StopLoss;
if( TakeProfit>0) TP= priceBUY+Point* TakeProfit;  
//ставим ордер 
ticket=OrderSend(Symbol(),OP_BUYSTOP, Lots, price,5, SL, TP,"байстоп", Magic,0,Blue);
if( ticket < 0) { Print("Ошибка открытия ордера BUY #", GetLastError()); 
               Sleep(10000);   return (0); }
}}
//--------------------------------------------------------------------------

Аналогично ставится селл-стоп


//----------------------------------------------------------------------------
return (0);
 //-----------------Конец функции int start()----
}//int start()

//--------ставим пользовательские функции -------------

Instead of

double priceBUY=..... ....

enter your formula

 

I am writing an indicator, the essence of which is as follows: if a candle is bullish, the indicator line is up, bearish down. well, volume is added to all this (so that each candle has a volume weight). And the second line is a moving average of the current line

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 Red

extern int average=14;
//---- buffers
double Buf_1[], Buf_2[];
double sum_b;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0, Buf_1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1, Buf_2);
   
   sum_b=0.0;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    i, 
          Counted_bars=IndicatorCounted();

int n=0;
//----
//====================================================================
// график тела с объемом

i=Bars- Counted_bars-1;           // Индекс первого непосчитанного
   while( i>=0) 
      {  
       //----
       if (Close[ i]>Open[ i]) {
         sum_b = sum_b + (Close[ i]-Open[ i])*Volume[ i];
         Buf_1[ i]= sum_b;
         }
         
       if (Close[ i]<Open[ i]) {
         sum_b = sum_b - (Open[ i]-Close[ i])*Volume[ i];
         Buf_1[ i]= sum_b;
         }
         
       if (NormalizeDouble(Close[ i],Digits)==NormalizeDouble(Open[ i],Digits)) {
         Buf_1[ i] = Buf_1[ i-1];
         }
       
       // average  
       while( n<= average){
         Buf_2[ i]= Buf_1[ i- n];
         n++;
         }

       i--;
       //----
      }
//====================================================================


//----
   return(0);
  }
//+------------------------------------------------------------------+
problems: the main line draws peaks when a doji is formed, and the moving average is not drawn?
 
dmmikl86 >> :

I am writing an indicator, the essence of which is as follows: if a candle is bullish, the indicator line is up, bearish down. well, volume is added to all this (so that each candle has a volume weight). And the second line is the moving average of the current line

problems: the main line draws peaks during doji formation and the moving average is not drawn?


The block of ifs can be replaced with a more efficient

if (Close[ i]>Open[ i]) {
         sum_b = sum_b + (Close[ i]-Open[ i])*Volume[ i];
         Buf_1[ i]= sum_b;
}
else if (Close[ i]<Open[ i]) {
         sum_b = sum_b - (Open[ i]-Close[ i])*Volume[ i];
         Buf_1[ i]= sum_b;
}
else {  //       if (NormalizeDouble(Close[i],Digits)==NormalizeDouble(Open[i],Digits)) {
         Buf_1[ i] = Buf_1[ i + 1];
}

[i + 1] - since the index arrays are numbered from right to left. Now the candlesticks with zero bodies will be processed correctly.

With the middle line buf_2 from the code it is not clearat all what the author wanted.


 
OneDepo >> :

The if-value block can be replaced with a more efficient one

[i + 1] - because index arrays are numbered from right to left. Now the candlesticks with zero body will be processed correctly.

With the middle line buf_2 from the code it is not clearat all what the author wanted.


wanted it to be a moving average with a period of 14, based on the value of the indicator

 
dmmikl86 >> :

wanted it to be a moving average with a period of 14, plotted against the value of the indicator

Then it goes like this:

//+------------------------------------------------------------------+
//|                                                     dmmikl86.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 Red

extern int BarsCount = 1000;
extern int average=14;

//---- buffers
double Buf_1[], Buf_2[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
    if ( average < 1)
        average = 1;

    //---- indicators
    SetIndexStyle(0,DRAW_LINE);
    SetIndexBuffer(0, Buf_1);
    SetIndexStyle(1,DRAW_LINE);
    SetIndexBuffer(1, Buf_2);
    //----
    return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
    int i, n;

    //====================================================================
    // график тела с объемом
    double s, sum_b = 0;

    int limit = BarsCount;
    if ( limit > Bars)
        limit = Bars;

    for ( i = limit; 0 <= i; i--) {

        if (Close[ i]>Open[ i]) {
            sum_b += (Close[ i]-Open[ i])*Volume[ i];
            Buf_1[ i] = sum_b;
        }
        else if (Close[ i]<Open[ i]) {
            sum_b -= (Open[ i]-Close[ i])*Volume[ i];
            Buf_1[ i] = sum_b;
        }
        else {
            Buf_1[ i] = Buf_1[ i + 1];
        }

        // SMA calculation
        if ( i + average < limit) {
            s = 0;
            for ( n = 0; n < average; n++) {
                s += Buf_1[ i+ n];
            }
            Buf_2[ i] = s/ average;
        }

    }

    return(0);
  }
//+------------------------------------------------------------------+
 
how do I make a trade once per bar and not open any more orders on this bar if there is already a trade on this bar?
 
Just look at this thread.
 
Vinin >> :
Just look at this thread.

Or read task 27 in Kovalev's textbook. There are code examples and a detailed explanation of how it works.

Reason: