LearnMQL5/CandleTest2.mq5

156 lines
7.7 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 15:03:27 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| TestCandles.mq5 |
//| Copyright <EFBFBD> 2020 MhFx7, All Rights Reserved |
//| https://www.mql5.com/en/users/mhfx7 |
//+------------------------------------------------------------------+
#property copyright "Copyright <00> 2020 MhFx7, All Rights Reserved"
#property link "https://www.mql5.com/en/users/mhfx7"
#property version "1.00"
//---
#include <Trade\Trade.mqh> CTrade trade;
//---
static ulong MagicNumber=123;
//---
input double MinRange=25;//Minumum Range
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(IsNewBar() && !IsPosition())
{
//---
double open=iOpen(_Symbol,PERIOD_M1,1);
//---
double close=iClose(_Symbol,PERIOD_M1,1);
//---
if(close>open)
{
//---
double range=MathRound((close-open)/_Point);
//---
if(range>=MinRange)
{
//---
double ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
//---
trade.SetExpertMagicNumber(MagicNumber);
//---
trade.Buy(1,_Symbol,ask,ask-range*_Point,ask+range*_Point);
}
}
//---
if(close<open)
{
//---
double range=MathRound((open-close)/_Point);
//---
if(range>=MinRange)
{
//---
double bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
//---
trade.SetExpertMagicNumber(MagicNumber);
//---
trade.Sell(1,_Symbol,bid,bid+range*_Point,bid-range*_Point);
}
}
}
//---
}
//+------------------------------------------------------------------+
//| IsPosition |
//+------------------------------------------------------------------+
bool IsPosition()
{
//---
bool result=false;
//---
int position_total=PositionsTotal();
//---
for(int i=0; i<position_total; i++)
{
//---
ulong ticket=PositionGetTicket(i);
//---
if(!PositionSelectByTicket(ticket))
continue;
//---
if(PositionGetString(POSITION_SYMBOL)!=_Symbol)
continue;
//---
if(PositionGetInteger(POSITION_MAGIC)!=MagicNumber)
continue;
//---
result=true;
//---
break;
}
//---
return(result);
}
//+------------------------------------------------------------------+
//| IsNewBar |
//+------------------------------------------------------------------+
bool IsNewBar()
{
//---
static datetime last_time=0;
//---
datetime time_now=iTime(_Symbol,PERIOD_M1,0);
//---
if(time_now!=last_time)
{
//---
last_time=time_now;
//---
return(true);
}
else
return(false);
//---
}
//+------------------------------------------------------------------+