Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1322

 
Good afternoon, there are a lot of indicator buffers of the same type, which can be calculated in the loop, but how to make an array of indicator buffers? I have tried it through the structure, and everything was fine, SetIndexBuffer() worked without errors, but when OnCalculate() comes, the array sizes don't change and remain 0. Please advise how to organize calculation of indicator buffers in the loop, perhaps by names, such as: "buff_0", "buff_1", etc., but how to do this I don't know(( Otherwise, it's a very long sheet comes out(
 
SanAlex:

I guess that's what you had in mind - it seems to have worked.

Thanks !

Everything is correct, butnot exactly what I had in mind!!! The order is placed along the trend (according to the indicator) and closed at take and as soon as it closes - a new order is opened in the same direction (along the trend), but this is only one part of my idea.

The second part is that an order which was not closed by Takei when the trend reverses (when the indicator gives a signal of a change in the price direction) should not be closed (which you have done).

In short, the two parts should work as a pair.

 
Alexey Viktorov:

Yesterday I downloaded this miracle to watch... Suddenly I had no internet. After a thunderstorm I had technical works for the rest of the day. So I decided to rewrite it in MQL5 out of idleness and published it here.

So every cloud has a silver lining.........

Thank you very much!

It may come in handy, if my idea comes true!

 
VANDER:
Tried it through the page and it seemed to work fine

Isn't that how it works?

int size=100;

struct All
   {
   double buff[];
   } all[];

int OnInit()
   {
   IndicatorBuffers(size);
   ArrayResize(all,size);
   for(int i=0; i<size; i++)
      {
      SetIndexBuffer(i,all[i].buff);
      SetIndexStyle(i,DRAW_LINE);
      }
   return(INIT_SUCCEEDED);
   }

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[])
   {
   for(int i=0; i<size; i++)
      {
      all[i].buff[shift]=i;
      }
   return(rates_total);
   }
 

Sprut 185:

and now you have to apply Martingale to it, i.e. open non-trend orders with a certain step, multiplier and take averaging.

In short, the two parts should work in a pair.

You mean the next position should be opened with double lot 2-4-8-16 on BUY and opposite 2-4-8-16 on SELL?

I have such a function - built it myself somehow.

//+------------------------------------------------------------------+
//| ENUM_LOT_RISK                                                    |
//+------------------------------------------------------------------+
enum LotMax
  {
   Lot=0,   // Lots
   Lotx2=1, // Lots*2
   Risk=2,  // Risk
  };
//+------------------------------------------------------------------+
input LotMax InpLotRisk              = Risk;         // : Lots,- Lots*2,- Risk
input double MaximumRisk             = 0.02;         // : Maximum Risk in percentage
input double DecreaseFactor          = 3;            // : Descrease factor
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double TradeSizeOptimized(void)
  {
   double price=0.0;
   double margin=0.0;
//--- select lot size
   if(!SymbolInfoDouble(_Symbol,SYMBOL_ASK,price))
      return(0.0);
   if(!OrderCalcMargin(ORDER_TYPE_BUY,_Symbol,1.0,price,margin))
      return(0.0);
   if(margin<=0.0)
      return(0.0);
   double lot=NormalizeDouble(AccountInfoDouble(ACCOUNT_MARGIN_FREE)*MaximumRisk/margin,2);
//--- calculate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      //--- select history for access
      HistorySelect(0,TimeCurrent());
      //---
      int    orders=HistoryDealsTotal();  // total history deals
      int    losses=0;                    // number of losses orders without a break
      for(int i=orders-1; i>=0; i--)
        {
         ulong ticket=HistoryDealGetTicket(i);
         if(ticket==0)
           {
            Print("HistoryDealGetTicket failed, no trade history");
            break;
           }
         //--- check symbol
         if(HistoryDealGetString(ticket,DEAL_SYMBOL)!=_Symbol)
            continue;
         //--- check Expert Magic number
         if(HistoryDealGetInteger(ticket,DEAL_MAGIC)!=UNO_MAGIC)
            continue;
         //--- check profit
         double profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
         if(profit>0.0)
            break;
         if(profit<0.0)
            losses++;
        }
      //---
      if(losses>1)
         lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//--- normalize and check limits
   double stepvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
   lot=stepvol*NormalizeDouble(lot/stepvol,0);
   double minvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);
   if(lot<minvol)
      lot=minvol;
   double maxvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);
   if(lot>maxvol)
      lot=maxvol;
//--- return trading volume
   return(lot);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double OptimizedBuy(void)
  {
   double PROFIT_BUY=0.00;
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions
     {
      string   position_GetSymbol=PositionGetSymbol(i); // GetSymbol позиции
      if(position_GetSymbol==m_symbol.Name())
        {
         if(m_position.PositionType()==POSITION_TYPE_BUY)
           {
            PROFIT_BUY=PROFIT_BUY+m_position.Select(Symbol());
           }
        }
     }
   double Lots=MaximumRisk;
   double ab=PROFIT_BUY;
   switch(InpLotRisk)
     {
      case Lot:
         Lots=MaximumRisk;
         break;
      case Lotx2:
         if(ab>0 && ab<=1)
            Lots=MaximumRisk*2;
         if(ab>1 && ab<=2)
            Lots=MaximumRisk*4;
         if(ab>2 && ab<=3)
            Lots=MaximumRisk*8;
         if(ab>3)
            Lots=TradeSizeOptimized();
         break;
      case Risk:
         Lots=TradeSizeOptimized();
         break;
     }
   return(Lots);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double OptimizedSell(void)
  {
   double PROFIT_SELL=0.00;
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions
     {
      string   position_GetSymbol=PositionGetSymbol(i); // GetSymbol позиции
      if(position_GetSymbol==m_symbol.Name())
        {
         if(m_position.PositionType()==POSITION_TYPE_SELL)
           {
            PROFIT_SELL=PROFIT_SELL+m_position.Select(Symbol());
           }
        }
     }
   double Lots=MaximumRisk;
   double ab=PROFIT_SELL;
   switch(InpLotRisk)
     {
      case Lot:
         Lots=MaximumRisk;
         break;
      case Lotx2:
         if(ab>0 && ab<=1)
            Lots=MaximumRisk*2;
         if(ab>1 && ab<=2)
            Lots=MaximumRisk*4;
         if(ab>2 && ab<=3)
            Lots=MaximumRisk*8;
         if(ab>3)
            Lots=TradeSizeOptimized();
         break;
      case Risk:
         Lots=TradeSizeOptimized();
         break;
     }
   return(Lots);
  }
//+------------------------------------------------------------------+

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

You need to replace in the position opening - here instead of InpLots with OptimizedBuy() and OptimizedSell()

      if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,OptimizedBuy(),price,0.0,0.0))
      if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,OptimizedSell(),price,0.0,0.0))
 
SanAlex:

You mean the next position will be opened with double lot 2-4-8-16 in BUY and opposite 2-4-8-16 in SELL?

I have such a function - built it myself somehow.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

You need to replace it in the position opening with OptimizedBuy() and OptimizedSell() instead of InpLots()

but for this function, it would be better to have this function - it would close at profit (in currency)

input double InpTProfit              = 40000;        // : Take Profit --> (In currency the amount)
input double InpSLoss                = 1000000;      // : Stop Loss --> (In currency the amount)
//+------------------------------------------------------------------+
//| ProfitOnTick closing                                             |
//+------------------------------------------------------------------+
void ProfitOnTick(void)
  {
//---
   double PROFIT_BUY=0.00;
   double PROFIT_SELL=0.00;
   int total=PositionsTotal();
   for(int i=total-1; i>=0; i--)
     {
      string   position_GetSymbol=PositionGetSymbol(i);
      if(position_GetSymbol==m_symbol.Name())
        {
         if(m_position.PositionType()==POSITION_TYPE_BUY)
           {
            PROFIT_BUY=PROFIT_BUY+PositionGetDouble(POSITION_PROFIT);
           }
         else
           {
            PROFIT_SELL=PROFIT_SELL+PositionGetDouble(POSITION_PROFIT);
           }
        }
      if(PROFIT_BUY<-InpSLoss || PROFIT_BUY>=InpTProfit)
        {
         CheckForCloseBuy();
        }
      if(PROFIT_SELL<-InpSLoss || PROFIT_SELL>=InpTProfit)
        {
         CheckForCloseSell();
        }
     }
  }
//+------------------------------------------------------------------+
 
SanAlex:

But for this function it would be desirable to have this function, which would close on profit (in currency)

Let me try to explain the meaning of my idea and operation of the EA I am creating more clearly.

You need a simple Martin - for example AutoProfit 3, in which you only need to add that the opening of orders was based on indicator signals (according to the trend), but not as in AutoProfit - as the cards fall on ......... and that's all.

With roughly the same output parameters as in the attached file.
Files:
pbx0dcw.jpg  201 kb
 
Sprut 185:

Let me try to explain more clearly the meaning of my idea and the work of the EA being created.

You need a simple Martin - for example AutoProfit 3, in which you only need to add that the opening of orders was on the signal of the indicator (the trend), rather than as in AutoProfit - as the card lies ......... and that's all.

So, it opens on a signal from the indicator - but if a signal appears opposite, it will open in the other direction. And if that position is not closed on profit, it will hang until it closes on profit. If the signal flips to the other side and closes that open position, it does not work for you.

 
SanAlex:

If the signal turns the other way, it will open in the other direction, and if that position has not closed on profit, it will hang there until it closes on profit. If a signal to the other side turned over and closed that open position, it will not suit you.

Right .......... and if that position has not closed on profit, it will stay there until it closes on profit.

But the position that was not closed on profit(for example, in buy) should remain and go down to a certain step where another order (in buy) is opened with the multiplied volume and the average time loss, etc., until the trend reverses, but the opening of positions in the track will not stop.

That's why I wrote that 2 blocks should work simultaneously - Martin and by indicator.

If I didn't explain something well - I'm ready to demonstrate on Skype and show you how it should look like.

 
Sprut 185:

Everything is correct .......... and if that position has not closed on profit, it will stay there until it closes on profit.

But the position that did not close on profit(for example, in buy) should remain and go down to a certain step, where another order would open (in buy) with the multiplied volume and the average timing would go down, etc., until the trend reverses, but the opening of positions in the trade would not stop.

That's why I wrote that 2 blocks should work simultaneously - Martin and by indicator.

If something is not well explained - I'm ready to demonstrate on Skype and show you how it should look like.

Martin must be activated only when the indicator signal is opposite or not dependent on it?

Example: I opened a Buy position according to the indicator. The price has moved down by the set distance, and the indicator already shows Sell. Should the Buy position be opened?