MQL5: Sachin Range Algo
//+------------------------------------------------------------------+
//| Range Hedge.mq5 |
//| Copyright 2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
CTrade trade;
datetime lastTradeExitTime = 0;
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
bool Positional=false;
input string TradeStartTime = "00:00";
input int NumberOfCandle=4;
input int MagicNumber=12345;
input double Lotsize=1;
enum stmode
{
HighLow=1,
Body=2,
};
string stgmodes;
input ENUM_TIMEFRAMES Timeframe=0;
input stmode StrategyMode = HighLow;
input double TargetMul=2;
input double Martangle=2;
string Trade=NULL;
bool runonce =false, EAActivated = false,BUY=false,SELL=false;
double ltp,ask,bid,tgt,lot;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
lot=Lotsize;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
double RangeHigh,RangeLow,rangelength,target;
void CalculateRangeBody(int lookbackPeriod)
{
RangeHigh = iClose(Symbol(),Timeframe,0); // Initialize with the current low
RangeLow = iOpen(Symbol(),Timeframe,0); // Initialize with the current high
for(int i = 0; i < lookbackPeriod; i++)
{
if(iHigh(Symbol(),Timeframe,i) > RangeHigh)
RangeHigh = iClose(Symbol(),Timeframe,i); // Update the highest high
if(iLow(Symbol(),Timeframe,i) < RangeLow)
RangeLow = iOpen(Symbol(),Timeframe,i); // Update the lowest low
}
Print("RangeHigh: ", RangeHigh, " RangeLow: ", RangeLow);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CalculateRangeHighlow(int lookbackPeriod)
{
RangeHigh = iHigh(Symbol(),Timeframe,0); // Initialize with the current low
RangeLow = iLow(Symbol(),Timeframe,0); // Initialize with the current high
for(int i = 0; i < lookbackPeriod; i++)
{
if(iHigh(Symbol(),Timeframe,i) > RangeHigh)
RangeHigh = iHigh(Symbol(),Timeframe,i); // Update the highest high
if(iLow(Symbol(),Timeframe,i) < RangeLow)
RangeLow = iLow(Symbol(),Timeframe,i); // Update the lowest low
}
Print("RangeHigh: ", RangeHigh, " RangeLow: ", RangeLow);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
ltp=iClose(Symbol(),PERIOD_CURRENT,0);
ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
Comment("\n\nRangeHigh: "+RangeHigh+"\n\nRangeLow: "+RangeLow+"\n\nEAActivated: "+EAActivated+"\n\nrunonce: "+runonce);
if(StrategyMode==HighLow)
{
stgmodes="HighLow";
}
else
if(StrategyMode==Body)
{ stgmodes="Body";}
if(Positional==false)
{
datetime current_time = TimeCurrent();
datetime start_time = StringToTime(TradeStartTime);
if(current_time >= start_time)
{
MqlDateTime current_time_struct, last_exit_struct;
TimeToStruct(current_time, current_time_struct);
TimeToStruct(lastTradeExitTime, last_exit_struct);
datetime today_start_time = StringToTime(TimeToString(current_time, TIME_DATE) + " " + TradeStartTime);
// Check if lastTradeExitTime is initialized and compare with reference time
// Ensure trading only starts after today's 12 PM (reference time)
if(current_time < today_start_time)
{
// Skip trading if current time is before today's reference time
return;
}
// Check if lastTradeExitTime is initialized and if the trade exited today after start_time
if(lastTradeExitTime != 0 && current_time_struct.day == last_exit_struct.day && lastTradeExitTime > start_time)
{
return;
}
EAActivated = true;
}
}
if(runonce==false &&EAActivated == true)
{
runonce=true;
if(stgmodes=="HighLow")
{
CalculateRangeHighlow(NumberOfCandle);
rangelength=RangeHigh-RangeLow;
target=rangelength*TargetMul;
CreateBlueLine(RangeHigh);
CreateRedLine(RangeLow);
}
if(stgmodes=="Body")
{
CalculateRangeBody(NumberOfCandle);
rangelength=RangeHigh-RangeLow;
target=rangelength*TargetMul;
CreateBlueLine(RangeHigh);
CreateRedLine(RangeLow);
}
}
if(runonce==true&&EAActivated==true)
{
if(ask>=RangeHigh&&RangeHigh>0&&BUY==false)
{
BUY=true;
SELL=false;
tgt=ask+target;
Trade="BUY";
Alert("Buy @ "+Symbol());
trade.SetExpertMagicNumber(MagicNumber);
trade.Buy(lot,Symbol(),NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_ASK),_Digits),0,tgt,"Programetix Range hedge Buy");
lot=lot*Martangle;
}
if(ltp<=RangeLow&&RangeLow>0&&SELL==false)
{
BUY=false;
SELL=true;
tgt=ltp-target;
Trade="SHORT";
Alert("Sell @ "+Symbol());
trade.SetExpertMagicNumber(MagicNumber);
trade.Sell(lot,Symbol(),NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_BID),_Digits),0,tgt," Programetix Range hedge Sell");
lot=lot*Martangle;
}
if(Trade=="BUY")
{
if(ltp>=tgt)
{
Alert("TARGET EXECUTED BUY @ "+Symbol());
close_sell_position();
lastTradeExitTime = TimeCurrent();
runonce = false;
EAActivated=false;
ResetState();
Trade=NULL;
}
}
if(Trade=="SHORT")
{
if(ask<=tgt)
{
Alert("TARGET EXECUTED SHORT @ "+Symbol());
close_buy_position();
lastTradeExitTime = TimeCurrent();
EAActivated=false;
runonce = false;
ResetState();
Trade=NULL;
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void ResetState()
{
BUY=false;
SELL=false;
runonce = false;
}
//+------------------------------------------------------------------+
void close_sell_position()
{
for(int i=PositionsTotal()-1; i>=0; i--)
{
string currencypair=PositionGetSymbol(i);
int position_direction=PositionGetInteger(POSITION_TYPE);
int posmagic=PositionGetInteger(POSITION_MAGIC);
if(currencypair==Symbol() && position_direction == POSITION_TYPE_SELL&& int(posmagic) == int(MagicNumber))
{
ulong ticket=PositionGetTicket(i);
trade.PositionClose(ticket);
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void close_buy_position()
{
for(int i=PositionsTotal()-1; i>=0; i--)
{
string currencypair=PositionGetSymbol(i);
int position_direction=PositionGetInteger(POSITION_TYPE);
int posmagic=PositionGetInteger(POSITION_MAGIC);
if(currencypair==Symbol() && position_direction == POSITION_TYPE_BUY && int(posmagic) == int(MagicNumber)
)
{
ulong ticket=PositionGetTicket(i);
trade.PositionClose(ticket);
}
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void CreateBlueLine(double price)
{
string lineName = "DAYHIGH";
if(ObjectFind(0, lineName) != -1)
{
ObjectDelete(0, lineName);
}
if(ObjectCreate(0, lineName, OBJ_HLINE, 0, 0, price))
{
ObjectSetInteger(0, lineName, OBJPROP_COLOR, clrRed);
ObjectSetInteger(0, lineName, OBJPROP_WIDTH, 2);
ObjectSetInteger(0, lineName, OBJPROP_STYLE, STYLE_SOLID);
}
else
{
Print("Error creating blue horizontal line at price ", price);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CreateRedLine(double price)
{
string lineName = "DAYLOW";
if(ObjectFind(0, lineName) != -1)
{
ObjectDelete(0, lineName);
}
if(ObjectCreate(0, lineName, OBJ_HLINE, 0, 0, price))
{
ObjectSetInteger(0, lineName, OBJPROP_COLOR, clrLawnGreen);
ObjectSetInteger(0, lineName, OBJPROP_WIDTH, 2);
ObjectSetInteger(0, lineName, OBJPROP_STYLE, STYLE_SOLID);
}
else
{
Print("Error creating red horizontal line at price ", price);
}
}
//+------------------------------------------------------------------+
Comments
Post a Comment