random trade every hour

 
Hi, where i can download random trader ea mt5 ( set TP , SL and Lots) and trade every hour (h1)
 
Michal Klis:
Hi, where i can download random trader ea mt5 ( set TP , SL and Lots) and trade every hour (h1)

The easiest way to code such a thing is to call the open time of every H1 bar that is stored in MT5. Here's some code from another thread that trades every bar regardless of chart timeframe, so just be sure to run it on an H1 chart.

Forum on trading, automated trading systems and testing trading strategies

How to start with MQL5

Vladimir Karputov, 2020.04.09 08:44

Open position every new candle.

First we define a new bar.

If this is a new bar - open a BUY position with a minimum volume.


//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- we work only at the time of the birth of new bar
   datetime time_0=iTime(m_symbol.Name(),Period(),0);
   if(time_0==m_prev_bars)
      return;
   m_prev_bars=time_0;
//---
   m_trade.Buy(m_symbol.LotsMin());
  }


Full code:

//+------------------------------------------------------------------+
//|                               Open position every new candle.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.000"
/*
   barabashkakvn Trading engine 3.116
*/
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
//---
CTrade         m_trade;                      // object of CTrade class
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
//--- input parameters
input ulong    InpDeviation         = 10;          // Deviation, in points (1.00045-1.00055=10 points)
input ulong    InpMagic             = 201;         // Magic number
//---
datetime m_prev_bars                = 0;        // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return(INIT_FAILED);
     }
//---
   m_trade.SetExpertMagicNumber(InpMagic);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
   m_trade.SetDeviationInPoints(InpDeviation);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- we work only at the time of the birth of new bar
   datetime time_0=iTime(m_symbol.Name(),Period(),0);
   if(time_0==m_prev_bars)
      return;
   m_prev_bars=time_0;
//---
   m_trade.Buy(m_symbol.LotsMin());
  }
//+------------------------------------------------------------------+

Also, you'll need to add your SL and TP to:

   m_trade.Buy(m_symbol.LotsMin())
 
Ryan L Johnson #:

The easiest way to code such a thing is to call the open time of every H1 bar that is stored in MT5. Here's some code from another thread that trades every bar regardless of chart timeframe, so just be sure to run it on an H1 chart.


Also, you'll need to add your SL and TP to:

ok, If i want 10 pips TP and 1 pip SL, and 0.1 lots what should i write ? best regards
 
Michal Klis #:
ok, If i want 10 pips TP and 1 pip SL, and 0.1 lots what should i write ? best regards
For 0.1 lots, just replace:
m_symbol.LotsMin()

with 0.1, and use this code for SL and TP (a 1 pip SL is 10 points, and a 10 pip TP is 100 points):