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

 
WindUP:
Thank you, but could we have at least one objective example? A view from above, so to speak. I'm a complete novice and I've been on the subject for two days :-)

here's probably something similar

//+------------------------------------------------------------------+
//|                                                    maxLimits.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property script_show_inputs
#include <Trade\Trade.mqh>
//---
CTrade m_trade; // trading object
//---
sinput uint   maxLimits      = 1;    // Кол-во Позиции Открыть в одну сторону
sinput double InpLots        = 0.01; // Lots
//---
sinput int    Inpcopiedrates = 2;    // какой бар
sinput double Inplow         = 100;  // отступить от low
//---
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   const string symbol=NULL;
   double price=0.0;
   const double tp=0.0;
   const string comment="";
//--- check symbol
   string symbol_name=(symbol==NULL) ? Symbol() : symbol;
//--- check price
   if(price==0.0)
      price=SymbolInfoDouble(symbol_name,SYMBOL_ASK);
//---
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int copied=CopyRates(Symbol(),0,0,10,rates);
   double sl=rates[Inpcopiedrates].low-Inplow/100;
//---
   for(uint x=0; x<maxLimits; x++)
     {
      //--- open position
      //if(m_trade.Buy(InpLots,symbol_name,price,sl,tp,comment))
      if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,InpLots,price,sl,tp))
         printf("Position by %s to be opened",Symbol());
      else
        {
         printf("Error opening BUY position by %s : '%s'",Symbol(),m_trade.ResultComment());
         printf("Open parameters : price=%f,SL=%f,TP=%f",price,sl,tp);
        }
     }
  }
//+------------------------------------------------------------------+
 
SanAlex:

I'm trying to learn too - I think I'm getting somewhere.

Hi. Sorry, I see in the screenshot that you succeeded, but I did not see where in this code the function of copying data of previous candle [1] is located ?




Can you please tell me MQL5 programming courses for dummies, from simple to complex, where and with what is better to start, to get more results and less time to waste? I would be grateful :-)

 
WindUP:

Hi. Sorry, I see in the screenshot that you succeeded, but here I did not see where in this code is the function of copying data of the previous candle [1] ?




How can I tell you MQL5 programming courses for dummies, from simple to complex, where and with what is better to start, to get more results and less time to waste? I would be grateful :-)

Imagine that a friend approaches you and asks to "teach you how to work in the Internet". Is it difficult to imagine what to teach? It is just as difficult to teach programming. There is a task - there is a solution. But just like that... you can learn any language, the basics are the same for all of them. Programming is not so much writing code as setting a problem, writing an algorithm for its solution and only then writing code. And the final stage, debugging and checking the result.

In SanAlrx's code, I don't understand why we have to make such a stock of copied data if we have a specific task to get OHLC for the first candlestick only.

int copied=CopyRates(Symbol(),0,0,10,rates);

and why copy the values of the current, just opened candle. But... this is his decision and his cockroaches.

 
WindUP:

Hi. Sorry, I see in the screenshot that you succeeded, but here I did not see where in this code there is a function to copy data of the previous candle [1] ?




How can I tell you MQL5 programming courses for dummies, from simple to complex, where and with what is better to start, to get more results and less time to waste? I would be grateful :-)

i myself am a student - for instance, i look at the documentationhttps://www.mql5.com/ru/docs/series/copyrates , there is an example of a script, i save it on my PC and start reading it and working out the details by gut feeling.

and this function - by gut feeling - is what I found.

//---
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int copied=CopyRates(Symbol(),0,0,10,rates);
   double sl=rates[Inpcopiedrates].low-Inplow/100;

It is for BUY "SL".

To SELL It should be like this

   double sl=rates[Inpcopiedrates].high+Inphigh/100;

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

I just put a number here - it would be possible to buy up to 10 bars. But you don't need it - you need 1 (one).

   int copied=CopyRates(Symbol(),0,0,10,rates);
Документация по MQL5: Доступ к таймсериям и индикаторам / CopyRates
Документация по MQL5: Доступ к таймсериям и индикаторам / CopyRates
  • www.mql5.com
CopyRates - Доступ к таймсериям и индикаторам - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
WindUP:

Hi. Sorry, I see in the screenshot that you succeeded, but here I did not see where in this code is the function of copying data of the previous candle [1] ?




How can I tell you MQL5 programming courses for dummies, from simple to complex, where and with what is better to start, to get more results and less time to waste? I would be grateful :-)

here's a good example - just went to the page and here's a better script to tell you how it works.https://www.mql5.com/ru/docs/constants/objectconstants/enum_anchorpoint

Документация по MQL5: Константы, перечисления и структуры / Константы объектов / Способы привязки объектов
Документация по MQL5: Константы, перечисления и структуры / Константы объектов / Способы привязки объектов
  • www.mql5.com
Способы привязки объектов - Константы объектов - Константы, перечисления и структуры - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
SanAlex:

I myself am learning - for example, I went to the documentationhttps://www.mql5.com/ru/docs/series/copyrates , there is a sample script, I save it on my computer and start reading and exploring it by groping for details.

and this function - by gut feeling - is what I found.

It is for BUY "SL".

To SELL It should be like this

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

I just put a number here - it would be possible to buy up to 10 bars. But you do not need it - you need 1 (one).

This opens and sets the SL for BUY and SELL

//+------------------------------------------------------------------+
//|                                                    maxLimits.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property script_show_inputs
#include <Trade\Trade.mqh>
//---
CTrade m_trade; // trading object
//---
sinput uint   maxLimits      = 1;    // Кол-во Позиции Открыть в одну сторону
sinput double InpLots        = 0.01; // Lots
//---
sinput double Inplowhigh     = 100;  // отступить low high
//---
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   OnStartBuy();
   OnStartSell();
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStartBuy()
  {
//---
   const string symbol=NULL;
   double price=0.0;
   const double tp=0.0;
   const string comment="";
//--- check symbol
   string symbol_name=(symbol==NULL) ? Symbol() : symbol;
//--- check price
   if(price==0.0)
      price=SymbolInfoDouble(symbol_name,SYMBOL_ASK);
//---
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int copied=CopyRates(Symbol(),0,0,2,rates);
   if(copied<=0)
     {
      Print("Не удалось получить исторические данные. Error = ",GetLastError());
      return;
     }
   double sl=rates[1].low-Inplowhigh/100;
//---
   for(uint x=0; x<maxLimits; x++)
     {
      //--- open position
      //if(m_trade.Buy(InpLots,symbol_name,price,sl,tp,comment))
      if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,InpLots,price,sl,tp))
         printf("Position by %s to be opened",Symbol());
      else
        {
         printf("Error opening BUY position by %s : '%s'",Symbol(),m_trade.ResultComment());
         printf("Open parameters : price=%f,SL=%f,TP=%f",price,sl,tp);
        }
     }
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStartSell()
  {
//---
   const string symbol=NULL;
   double price=0.0;
   const double tp=0.0;
   const string comment="";
//--- check symbol
   string symbol_name=(symbol==NULL) ? Symbol() : symbol;
//--- check price
   if(price==0.0)
      price=SymbolInfoDouble(symbol_name,SYMBOL_BID);
//---
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int copied=CopyRates(Symbol(),0,0,2,rates);
   if(copied<=0)
     {
      Print("Не удалось получить исторические данные. Error = ",GetLastError());
      return;
     }
   double sl=rates[1].high+Inplowhigh/100;
//---
   for(uint x=0; x<maxLimits; x++)
     {
      //--- open position
      //if(m_trade.Buy(InpLots,symbol_name,price,sl,tp,comment))
      if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,InpLots,price,sl,tp))
         printf("Position by %s to be opened",Symbol());
      else
        {
         printf("Error opening SELL position by %s : '%s'",Symbol(),m_trade.ResultComment());
         printf("Open parameters : price=%f,SL=%f,TP=%f",price,sl,tp);
        }
     }
  }
//+------------------------------------------------------------------+

Photo by

 

Hello.

Could you give me a hint, I'm in over my head.

I prescribe indicator through iCustom and faced with a problem - indicator has a text menu with a choice, it opens and there are several items prescribed, well, I think it's clear)

How can I add it in menu?

extern int HIT = 0; does not work, stops displaying, leaving no parameter displayed.

Perhaps through ENUM , there what to choose, or maybe something else?

Thanks in advance.

Files:
 
Сергей:

Hello.

Could you give me a hint, I'm in over my head.

I prescribe indicator through iCustom and faced with a problem - indicator has a text menu with a choice, it opens and there are several items prescribed, well, I think it's clear)

How can I add it in menu?

extern int HIT = 0; does not work, stops displaying, leaving no parameter displayed.

Perhaps through ENUM , which one to choose, or maybe something else?

Thanks in advance.

Put an empty string "" or a space " "

 

So I wanted to prescribe it somehow so I could change the settings.

I have a different item for each pair and how do I select it?

And without that menu is not relevant, and the screenshot is just an example of a similar one.

 
Сергей:

So I wanted to prescribe it somehow so I could change the settings.

I have a different item for each pair and how do I select it?

And without that menu is not relevant, and the screenshot is just an example of a similar one.

Sorry for inattention. The indicator has an enumeration. And in the Expert Advisor, you have to specify the name of this enumeration as the variable type in the input parameters.

Although, I haven't tried it... and perhaps we will have to repeat this enumeration in the EA, or just set the ENUM_BASE_CORNER variable type

Reason: