[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 485

 
At SL, the EA should open an order of the same type and at TP of the opposite type!
But for some reason the EA often opens an order of the wrong type!



//+------------------------------------------------------------------+
//|                                                      Kogalym.mq4 |
//|                                                               TO |
//|                             http://forex-tradexperts-to.narod.ru |
//+------------------------------------------------------------------+
#property copyright "TO"
#property link      "http://forex-tradexperts-to.narod.ru"

//--------------------------------------------------------------------
extern int  stoploss    = 50,
            takeprofit  = 50;
extern double      Lot=1;
extern double Martin_Koef = 1.6;
//--------------------------------------------------------------------
int init()
{
   OrderSend(Symbol(),OP_SELL,Lot,Bid,3,NormalizeDouble(Ask + stoploss*Point,Digits),
                        NormalizeDouble(Bid - takeprofit*Point,Digits)," ",777,Blue);
}
//--------------------------------------------------------------------
int start()
{   for (int i=0; i<OrdersTotal(); i++){   
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true){
         if (OrderSymbol()==Symbol()){
         Lot = OrderLots();return;}}}
            if (Lot==0) return;
       
  if(OrderType() == OP_SELL)
   if(OrderProfit()>0) OrderSend(Symbol(),OP_BUY , getLot(),Ask,3,NormalizeDouble(Bid - stoploss*Point,Digits),
                                    NormalizeDouble(Ask + takeprofit*Point,Digits)," ",777,Blue); 
 else  OrderSend(Symbol(),OP_SELL,getLot(),Bid,3,NormalizeDouble(Ask + stoploss*Point,Digits),
                                   NormalizeDouble(Bid - takeprofit*Point,Digits)," ",777,Blue);

  if(OrderType() == OP_BUY) 
   if(OrderProfit()>0)OrderSend(Symbol(),OP_SELL,getLot(),Bid,3,NormalizeDouble(Ask + stoploss*Point,Digits),
        NormalizeDouble(Bid - takeprofit*Point,Digits)," ",777,Blue);
else  OrderSend(Symbol(),OP_BUY ,getLot(),Ask,3,NormalizeDouble(Bid - stoploss*Point,Digits),
                                    NormalizeDouble(Ask + takeprofit*Point,Digits)," ",777,Blue);
  
                                     
       
   return(0);
}
//-----------------------------------------------------------------

double getLot()
{
   if(OrdersHistoryTotal()==0)return(1);
   // ищем самый последний закрытый ордер
   datetime time=0;
   int i;
   int ticket=-1;
   for(i=OrdersHistoryTotal()-1;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
      {
         if(OrderSymbol()==Symbol())
         {
            if(OrderCloseTime()>time)
            {
               time=OrderCloseTime();
               ticket=OrderTicket();
            }
         }
      }
   }
   if(OrderTicket()!=ticket)OrderSelect(ticket,SELECT_BY_TICKET);
   if(OrderProfit()<=0) return(NormalizeDouble(OrderLots()*Martin_Koef,2));
   if(OrderProfit()>0)return(1);
   //-----
}
Please, tell me where there is a mistake!
 
Kogalym писал(а) >>
At SL the EA should open an order of the same type and at TP of the opposite type!
But for some reason the EA often opens the order of the wrong type!



Can you tell me where the mistake is?

if(OrderType() == OP_SELL)
   if(OrderProfit()>0)
This is what selection is for.
 
Who knows if this indicator is suitable for an EA, I found one strategy where it is applied by the author, can it be written in the EA and how to do it, for example, can you show me an example. I have found the histogram is green and if the line is below the histogram, it gives a buy signal. The same for sell.
 
PapaYozh >>:


какова размерность массива x10 ?

10?
Тогда надо так переписать:

и не понятно, что именно вы хотите получить в массивах MAX и MIN

Let me explain again :)

There is an array that contains all values of MACD columns.

We need to take from all those values a dozen of values (in order) and look for the maximum value in them, and respectively draw it in the indicator, and so on to the end.

Here is the code once again:

for(i=0; i<50; i=i+10) //общий цикл для всех значений МАКД, шаг 10
       {
         int k = i;
         for(k=i; k<i+10; k++) // цикл внутри десятки значений, шаг 1
             {       
              x10[k] = MACD[k];// массив десяток
             }
         int IndexMAX = ArrayMaximum(x10); // найден индекс максимального значения внутри десятки 
         int IndexMIN = ArrayMinimum(x10); // --//--   
         MAX[k] = MACD[IndexMAX]; // Рисуем найденное максимальное значение внутри десятки на k баре
         MIN[k] = MACD[IndexMIN];
       }

As I see it, it's not drawn because the index of array with tens doesn't coincide with the index of this value in the main array with MAKD values.

In other words, when counter reaches, say, 30 in main loop, then in subcycle will go 30-31-32...39 (let's assume, maximal of them will be 34 value), function Arraymaximum(x10) will find it, but will write in IndexMAX exactly ordinal number in HIS (x10) array, i.e. actually true value will be 34, but it will write some other one (from 0 to 9, because it's an array of tens). I can't figure out what to do...

 
Noterday писал(а) >>

Let me explain again :)

There is an array that contains all values of MACD columns.

We need to take from all those values a dozen of values (in order) and look for the maximum value in them, and respectively draw it in the indicator, and so on to the end.

Here is the code once again:

As I see it, it's not drawn because the index of array with tens doesn't coincide with the index of this value in the main array with MAKD values.

In other words, when counter reaches, say, 30 in main loop, then in subcycle will go 30-31-32...39 (let's assume, maximal of them will be 34 value), function Arraymaximum(x10) will find it, but will write in IndexMAX exactly ordinal number in HIS (x10) array, i.e. actually true value will be 34, but it will write some other one (from 0 to 9, because it's an array of tens). I can't figure out what to do...


Show all the code for the indicator
 
Vinin >>:


Весь код индикатора покажи
//+------------------------------------------------------------------+
//|                                                  Custom MACD.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                                        Alexander |
//+------------------------------------------------------------------+
#property  copyright "none"
#property  link      "none"
//---- indicator settings
#property  indicator_separate_window
#property indicator_buffers 4

#property indicator_color1 MediumPurple
#property indicator_color2 MediumPurple
#property indicator_color3 Green
#property indicator_color4 Red

#property indicator_width1 1
#property indicator_width2 1

//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double     MACD[];
double     SMACD[];
double     MAX[];
double     MIN[];
double     x10[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(5);
   
   SetIndexBuffer(0,MACD);
   SetIndexBuffer(1,SMACD);
   SetIndexBuffer(2,MAX);
   SetIndexBuffer(3,MIN);
   SetIndexBuffer(4,x10);
   
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_LINE);
   
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,119);
   
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,119);
   
   SetIndexStyle(4,DRAW_NONE);
   
   SetIndexDrawBegin(1,SMACD);
   IndicatorDigits(Digits+2);

   IndicatorShortName("MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");
   SetIndexLabel(0,"MACD");
   SetIndexLabel(1,"Signal");

   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();

   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   for(int i=0; i<limit; i++)
      MACD[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);

   for(i=0; i<limit; i++)
      SMACD[i]=iMAOnArray(MACD,Bars,SignalSMA,0,MODE_SMA,i);
      
      
   for(i=0; i<limit; i=i+10)
       {
         int k = i;
         for(k=i; k<i+10; k++)
             {       
              x10[k] = MACD[k];// массив десяток
             }
         int IndexMAX = ArrayMaximum(x10);
         int IndexMIN = ArrayMinimum(x10);    
         MAX[k] = MACD[IndexMAX];
         MIN[k] = MACD[IndexMIN];
       }
   return(0);
  }
//+------------------------------------------------------------------+

int deinit()
{   
return(0);
}
 
Noterday писал(а) >>

Let me explain again :)

I think you meant to write like this:

for(i=0; i<limit; i+=10) //общий цикл для всех значений МАКД, шаг 10
       {
         int k;
         for(k=0; k<10; k++) // цикл внутри десятки значений, шаг 1
             {       
              x10[k] = MACD[i+k];// массив десяток
             }
         int IndexMAX = ArrayMaximum(x10); // найден индекс максимального значения внутри десятки 
         int IndexMIN = ArrayMinimum(x10); // --//--   
         MAX[k] = x10[IndexMAX]; // Рисуем найденное максимальное значение внутри десятки на k баре
         MIN[k] = x10[IndexMIN];
       }
 
Noterday писал(а) >>


What's your goal?
Well, what do you want to get. Breaking it down into tens is pointless. They move around in time all the time.

 
PapaYozh >>:

мне кажется, Вы хотели написать вот так:

Doesn't draw :((.

s.w. k=0 I finished it :)

 
Vinin >>:


А какую цель преследуешь?
Ну, что хочешь получить. Разбивать на десятки бесмысленно. Они все время передвигаются во времени.

Yes the aim is literal...I want the maximum to be displayed amongst every 10 bars...

Reason: