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

 

Hello, I'm a new coder who is learning by myself so this might be a noob question but please bear with me...

What I want to do ===> create a custom indicator that has some hotkeys (for example by pressing Key "A" or "CTRL+A") that will execute a script or some other function.

Question =====> If I use #include Virtualkeys.mqh + WinUser.mqh and use OnChartEvent Keydown + switch case to execute this ( i am using already #defined keys in Virtualkeys.mqh for the case);

                             1. Will this hotkey work across platforms (windows/MAC OS/Linux)? ( I think Winuser imports user32.dll, not sure if MAC OS supports that)

                             2. Will this hotkey work across Keyboard layouts/regional settings (US QWERTY/US DVORAK)?

                             3.If it does not, Is there a way to make it happen? (I've been searching for a way to get a key as a input and set that into the code but haven't still found a way to do that)

                             4.Also if there is a way to get a key as a user input and use that key for the switch, please enlighten me guys.

Thanks in advance, and my apologies if this is a frequently asked noob question....
 
@Thamira Wijerathne #:

Hello, I'm a new coder who is learning by myself so this might be a noob question but please bear with me...

What I want to do ===> create a custom indicator that has some hotkeys (for example by pressing Key "A" or "CTRL+A") that will execute a script or some other function.

Question =====> If I use #include Virtualkeys.mqh + WinUser.mqh and use OnChartEvent Keydown + switch case to execute this ( i am using already #defined keys in Virtualkeys.mqh for the case);

                             1. Will this hotkey work across platforms (windows/MAC OS/Linux)? ( I think Winuser imports user32.dll, not sure if MAC OS supports that)

                             2. Will this hotkey work across Keyboard layouts/regional settings (US QWERTY/US DVORAK)?

                             3.If it does not, Is there a way to make it happen? (I've been searching for a way to get a key as a input and set that into the code but haven't still found a way to do that)

                             4.Also if there is a way to get a key as a user input and use that key for the switch, please enlighten me guys.

Thanks in advance, and my apologies if this is a frequently asked noob question....
  1. MetaTrader is a Windows application and will run in an emulated or virtualised environment under Mac/Linux, so probably most key sequences will be emulated as the same.
    There is no need to use "user.dll". The OnChartEvent() function can handle keyboard events.
  2. No, the key sequences will NOT be the same across different layouts.
  3. There are some keys that will remain the same over most of the regional layouts but not all. The best way to handle this is to allow the user to customise and choose their own sequences.
  4. Yes! Simply keep a running state in a variable and each time the key is pressed, you toggle that boolean state.
 
Fernando Carreiro #:
  1. MetaTrader is a Windows application and will run in an emulated or virtualised environment under Mac/Linux, so probably most key sequences will be emulated as the same.
    There is no need to use "user.dll". The OnChartEvent() function can handle keyboard events.
  2. No, the key sequences will NOT be the same across different layouts.
  3. There are some keys that will remain the same over most of the regional layouts but not all. The best way to handle this is to allow the user to customise and choose their own sequences.
  4. Yes! Simply keep a running state in a variable and each time the key is pressed, you toggle that boolean state.

Thanks for the reply. Really helped me clear that out.

It certainly seems easier to just let the user customize it, so will just do that :)

 
sibifid #: What am I doing wrong?
  1.     double stopLossPrice = (orderType == OP_BUY) ? openPrice - StopLossPips * Point : openPrice + StopLossPips * Point;
    

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerablf=bugy at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  2. void OnTick()
    {
        if (IsNewTradingDay() && IsTradingTime())

    Your condition is true for one minute. You will open one order per tick that minute.

  3. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles

  4. Your code
        if (serverHour == TradeHour && serverMinute == TradeMinute)
        {
            return true;
        }
    
        return false;
    Simplified
        return  serverHour == TradeHour && serverMinute == TradeMinute;
 
hellow am new to mql5 and ive been trying to code an indicator where three or more volume indicator ticks rise or fall consecutively the candles above change colour to green or red and rest grey

.am expiriensing some errors and i would like to know whre am wrong

#property indicator_chart_window
#property indicator_buffers 6
#property indicator_plots   1
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  Green,Red,Gray
#property indicator_width1  3
#property indicator_label1  "Open;High;Low;Close"

//--- indicator buffers
double ExtOBuffer[];
double ExtHBuffer[];
double ExtLBuffer[];
double ExtCBuffer[];
double ExtColorBuffer[];
double ExtVolumesBuffer[];

void OnInit()
{
    //--- indicator buffers mapping
    SetIndexBuffer(0, ExtOBuffer, INDICATOR_DATA);
    SetIndexBuffer(1, ExtHBuffer, INDICATOR_DATA);
    SetIndexBuffer(2, ExtLBuffer, INDICATOR_DATA);
    SetIndexBuffer(3, ExtCBuffer, INDICATOR_DATA);
    SetIndexBuffer(4, ExtVolumesBuffer, INDICATOR_DATA);
    SetIndexBuffer(5, ExtColorBuffer, INDICATOR_COLOR_INDEX);


}

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[])
{
    if (rates_total < 2)
        return (0);
  
    int pos = prev_calculated - 1;
   
    if (pos < 1)
    {
        ExtVolumesBuffer[0] = 0;
        pos = 1;
    }
  
    if (ExtVolumesBuffer[0] == 0)
        CalculateVolume(pos, rates_total, tick_volume);
    else
        CalculateVolume(pos, rates_total, volume);

    return (rates_total);
}

void CalculateVolume(const int pos, const int rates_total, const long &volume[])
{
    ExtVolumesBuffer[0] = (double)volume[0];

 
    for (int i = pos; i < rates_total && !IsStopped(); i++)
    {
        ExtOBuffer[i] = open[i];
        ExtHBuffer[i] = high[i];
        ExtLBuffer[i] = low[i];
        ExtCBuffer[i] = close[i];
        //--- set color for candle
        ExtColorBuffer[i] = 2.0; // set gray Color
        //--- check for Green Zone and set Color Green
        if (ExtVolumesBuffer[i] > ExtVolumesBuffer[i - 1] && ExtVolumesBuffer[i - 1] > ExtVolumesBuffer[i - 2])
            ExtColorBuffer[i] = 0.0;
        //--- check for Red Zone and set Color Red
        if (ExtVolumesBuffer[i] < ExtVolumesBuffer[i - 1] && ExtVolumesBuffer[i - 1] < ExtVolumesBuffer[i - 2])
            ExtColorBuffer[i] = 1.0;
    }
}
Volumes - Volume Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
  • www.metatrader5.com
For the Forex market, Volumes is the indicator of the number of price changes within each period of a selected timeframe. For stock symbols this is...
 
heres the code
//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_plots   1
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  Green,Red,Gray
#property indicator_width1  3
#property indicator_label1  "Open;High;Low;Close"
//--- indicator buffers
double ExtOBuffer[];
double ExtHBuffer[];
double ExtLBuffer[];
double ExtCBuffer[];
double ExtColorBuffer[];
double ExtVolumesBuffer[];

// Declare ExtVolumesHandle at the global scope
int ExtVolumesHandle;
#define DATA_LIMIT 38
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtOBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtHBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,ExtLBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,ExtCBuffer,INDICATOR_DATA);
   SetIndexBuffer(4,ExtColorBuffer,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(5,ExtVolumesBuffer,INDICATOR_DATA);
   
//---
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- sets first bar from what index will be drawn
   IndicatorSetString(INDICATOR_SHORTNAME,"Volumes");
//--- don't show indicator data in DataWindow
   PlotIndexSetInteger(0,PLOT_SHOW_DATA,false);
//--- sets first candle from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,DATA_LIMIT);
//--- get handles
   //--- get handles
ExtVolumesHandle = iVolumes(_Symbol, _Period, VOLUME_TICK);


   }
//+------------------------------------------------------------------+
//| Trade zone by Bill Williams                                      |
//+------------------------------------------------------------------+
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[])
  {
   if(rates_total<DATA_LIMIT)
      return(0);

//--- we can copy not all data
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0)
      to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0)
         to_copy++;
     }

//--- get AO buffer
   if(IsStopped()) // checking for stop flag
      return(0);
   if(CopyBuffer(ExtVolumesHandle,0,0,to_copy,ExtVolumesBuffer)<=0)
     {
      Print("Getting iVolumes is failed! Error ",GetLastError());
      return(0);
     }
//--- set first bar from what calculation will start
   int start;
   if(prev_calculated<DATA_LIMIT)
      start=DATA_LIMIT;
   else
      start=prev_calculated-1;
//--- the main loop of calculations
   for(int i=start; i<rates_total && !IsStopped(); i++)
     {
      ExtOBuffer[i]=open[i];
      ExtHBuffer[i]=high[i];
      ExtLBuffer[i]=low[i];
      ExtCBuffer[i]=close[i];
      //--- set color for candle
      ExtColorBuffer[i]=2.0;  // set gray Color
      //--- check for Green Zone and set Color Green
      if(ExtVolumesBuffer[i]>ExtVolumesBuffer[i-1] && ExtVolumesBuffer[i-1]>ExtVolumesBuffer[i-2])
         ExtColorBuffer[i]=0.0;
      //--- check for Red Zone and set Color Red
      if(ExtVolumesBuffer[i]<ExtVolumesBuffer[i-1] && ExtVolumesBuffer[i-1]<ExtVolumesBuffer[i-2])
         ExtColorBuffer[i]=1.0;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

Hi All. I am trying to get a very simple EA to place a set of pending long entries either side of current chart symbol price.

Can anyone look at the MT4 attached code and tell me why the orders aren't being placed

TIA

Files:
 
@law.tout #: Hi All. I am trying to get a very simple EA to place a set of pending long entries either side of current chart symbol price. Can anyone look at the MT4 attached code and tell me why the orders aren't being placed

Please have a look at both the Experts and Journal log and report the error messages that are reported.

Remember that we cannot see your computer nor read your mind, so you need to provide as much relevant information as possible.

No one can help you if you don't explain in detail what is happening on your end.

 
Fernando Carreiro #:

Please have a look at both the Experts and Journal log and report the error messages that are reported.

Remember that we cannot see your computer nor read your mind, so you need to provide as much relevant information as possible.

No one can help you if you don't explain in detail what is happening on your end.

apologies.  here is a screenshot from the journal of the tester


 
@law.tout #: apologies.  here is a screenshot from the journal of the tester

You have only shown one of the logs instead of both.

Add more debug prints to your code to print out the parameter values, because error code 3 is ...

3

ERR_INVALID_TRADE_PARAMETERS

Invalid trade parameters

Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
Reason: