MT5 Sachin BigBoss Forex Big candle strategy
//+------------------------------------------------------------------+
//| BigBossBigCandle.mq5 |
//| Copyright 2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#include <Trade\Trade.mqh>
CTrade trade;
datetime ExitTime;
input string TradeStartTime = "00:00";
input string TradeStopTime = "23:36";
input bool CandleClosing=false;
input bool useclosingpercentage=false;
input double closingpercentage=80;
input double CandleRange;
input double AveragingStep;
input double NumberOfTrades;
input double Quantity;
input string QtyCalType;
input double QuantityMultiplier;
input double RangePercentage;
input double StoplossStepLevel;
input double TargetRangePercentage;
input double MagicNumber;
input bool USESL;
input bool useRMS=true;
input double maxprofit;
input double maxloss;
double Ltp,quantity, Sl_Val,updated_high,updated_low,perval,fixed_low_sell,fixed_high_buy,range,NextOrderQty;
string InitialTrade= NULL,check= NULL,orderlog=NULL;
bool ActivateSl=false,EAActivated=false,tradingenable=true;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
quantity=Quantity;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
bool rangecondition=false;
double percentagevalue;
double mtm,combined_pnl,low,high,close,diff_to_high,diff_to_low,NextTradeVal,target_val,previous_target_val,open;
double crange,bodysize;
int AveragingStepCount=0;
void OnTick()
{
mtm=AccountInfoDouble(ACCOUNT_PROFIT);
if(useRMS==true)
{
if(mtm>maxprofit && maxprofit>0)
{
tradingenable=false;
close_all_sell_position();
rangecondition=false;
close_all_buy_position();
Alert("Max profit acheived closing all position");
}
if(mtm<maxloss && maxloss<0)
{
tradingenable=false;
close_all_sell_position();
close_all_buy_position();
rangecondition=false;
Alert("Max loss acheived closing all position");
}
}
if(TimeCurrent()>StringToTime(TradeStartTime) && TimeCurrent()<StringToTime(TradeStopTime))
{
EAActivated = true;
}
else
{
EAActivated = false;
}
if(EAActivated == true)
{
check="Trading enabled";
}
if(EAActivated == false)
{
check="Trading Disabled";
}
Ltp=iClose(Symbol(),PERIOD_CURRENT,0);
if(CandleClosing==false &&EAActivated==true)
{
low=iLow(Symbol(),PERIOD_CURRENT,0);
high=iHigh(Symbol(),PERIOD_CURRENT,0);
open=iOpen(Symbol(),PERIOD_CURRENT,0);
close=iClose(Symbol(),PERIOD_CURRENT,0);
range=high-low;
diff_to_high=close - high;
diff_to_high =MathAbs(diff_to_high);
diff_to_low=close - low;
diff_to_low =MathAbs(diff_to_low);
}
if(CandleClosing==true&&EAActivated==true)
{
low=iLow(Symbol(),PERIOD_CURRENT,1);
high=iHigh(Symbol(),PERIOD_CURRENT,1);
close=iClose(Symbol(),PERIOD_CURRENT,1);
open=iOpen(Symbol(),PERIOD_CURRENT,1);
range=high-low;
diff_to_high=close - high;
diff_to_high =MathAbs(diff_to_high);
diff_to_low=close - low;
diff_to_low =MathAbs(diff_to_low);
}
if(useclosingpercentage==true&& CandleClosing==true&&EAActivated==true)
{
if(iClose(Symbol(),PERIOD_CURRENT,1) > iOpen(Symbol(),PERIOD_CURRENT,1))
{
crange=0;
bodysize=0;
percentagevalue=0;
crange=iHigh(Symbol(),PERIOD_CURRENT,1)-iLow(Symbol(),PERIOD_CURRENT,1);
bodysize=iClose(Symbol(),PERIOD_CURRENT,1)-iOpen(Symbol(),PERIOD_CURRENT,1);
percentagevalue= crange*closingpercentage*0.01;
if(rangecondition==false&&bodysize>=percentagevalue&&crange>CandleRange)
{
rangecondition=true;
}
}
if(iClose(Symbol(),PERIOD_CURRENT,1)<iOpen(Symbol(),PERIOD_CURRENT,1))
{
crange=0;
bodysize=0;
percentagevalue=0;
crange=iHigh(Symbol(),PERIOD_CURRENT,1)-iLow(Symbol(),PERIOD_CURRENT,1);
bodysize=iOpen(Symbol(),PERIOD_CURRENT,1)-iClose(Symbol(),PERIOD_CURRENT,1);
percentagevalue= crange*closingpercentage*0.01;
if(rangecondition==false&&bodysize>=percentagevalue&&crange>CandleRange)
{
rangecondition=true;
}
}
}
if(useclosingpercentage==false)
{
rangecondition=true;
}
Comment("Candle Range: "+range+"\n\nbodysize: "+bodysize+"\n\npercentagevalue: "+percentagevalue+"\n\nTotal running profit: "+mtm+"\n\ntradingenable: "+tradingenable+"\n\nInitialTrade: "+InitialTrade+"\n\nAveragingStepCount: "+AveragingStepCount+"\n\ntarget_val: "+target_val);
if(
rangecondition==true&&
tradingenable==true&&
range >= CandleRange &&
InitialTrade == NULL &&
diff_to_high < diff_to_low && EAActivated == true&&ExitTime != iTime(Symbol(),PERIOD_CURRENT,0)
)
{
InitialTrade = "SHORT";
AveragingStepCount = AveragingStepCount + 1;
NextTradeVal = close + AveragingStep;
if(CandleClosing==true)
{
NextTradeVal = high + AveragingStep;
}
if(QtyCalType=="MUL")
{ NextOrderQty = Quantity * QuantityMultiplier;}
if(QtyCalType=="ADD")
{ NextOrderQty =Quantity +QuantityMultiplier;}
updated_high = high;
fixed_low_sell = low;
previous_target_val = updated_high;
perval= updated_high- fixed_low_sell;
Alert("perval: "+perval+" updated_high: "+updated_high+" updated_low: "+updated_low);
if(StoplossStepLevel>0)
{
Sl_Val=StoplossStepLevel*AveragingStep;
Sl_Val=SymbolInfoDouble(Symbol(),SYMBOL_BID)+Sl_Val;
}
target_val = perval * TargetRangePercentage * 0.01;
target_val = updated_high - target_val;
orderlog = "Initial Short order executed @ "+Symbol()+"for lotsize="+quantity+",Step = "+AveragingStepCount+",Target1 = "+target_val+",Sl_Val= "+Sl_Val;
Alert(orderlog);
trade.Sell(quantity,Symbol(),NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_BID),_Digits),0,0,"Sell backtest order");
}
if(rangecondition==true&&
tradingenable==true&&
range >= CandleRange &&
InitialTrade == NULL &&
diff_to_high > diff_to_low&&EAActivated == true&&ExitTime != iTime(Symbol(),PERIOD_CURRENT,0)
)
{
InitialTrade = "BUY";
AveragingStepCount = AveragingStepCount + 1;
NextTradeVal = close - AveragingStep;
if(CandleClosing==true)
{
NextTradeVal = low - AveragingStep;
}
if(QtyCalType=="MUL")
{ NextOrderQty = quantity * QuantityMultiplier;}
if(QtyCalType=="ADD")
{ NextOrderQty =quantity +QuantityMultiplier;}
if(StoplossStepLevel>0)
{
Sl_Val=StoplossStepLevel*AveragingStep;
Sl_Val=SymbolInfoDouble(Symbol(),SYMBOL_ASK)-Sl_Val;
}
updated_low= low;
fixed_high_buy = high;
previous_target_val = updated_low;
perval=fixed_high_buy - updated_low;
Alert("perval: "+perval+" updated_high: "+updated_high+" updated_low: "+updated_low);
target_val= perval * TargetRangePercentage * 0.01;
target_val = updated_low+ target_val;
orderlog = "Initial Buy order executed @ "+Symbol()+"for lotsize="+quantity+",Step = "+AveragingStepCount+",Target1 = "+target_val+",Sl_Val= "+Sl_Val;
Alert(orderlog);
trade.Buy(quantity,Symbol(),NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_ASK),_Digits),0,0,"Buy backtest order");
}
if(
tradingenable==true&&
InitialTrade == "BUY" &&
close <= NextTradeVal &&
NextTradeVal > 0 &&
AveragingStepCount < NumberOfTrades&&ExitTime != iTime(Symbol(),PERIOD_CURRENT,0)
)
{
AveragingStepCount = AveragingStepCount + 1;
NextTradeVal = close - AveragingStep;
if(CandleClosing==true)
{
NextTradeVal = low - AveragingStep;
}
quantity= NextOrderQty;
if(QtyCalType == "MUL")
{ NextOrderQty = quantity * QuantityMultiplier;}
if(QtyCalType == "ADD")
{ NextOrderQty= quantity + QuantityMultiplier;}
updated_low = low;
previous_target_val= updated_low;
perval = fixed_high_buy - updated_low;
Alert("perval: "+perval+" updated_high: "+updated_high+" updated_low: "+updated_low);
target_val = perval* TargetRangePercentage* 0.01;
target_val = updated_low +target_val;
if(AveragingStepCount == NumberOfTrades)
{
ActivateSl = false;
// Sl_Val = close - AveragingStep;
}
orderlog = "Buy order executed @ "+Symbol()+"for lotsize="+quantity+",Step = "+AveragingStepCount+",Target1 = "+target_val;
Alert(orderlog);
trade.Buy(quantity,Symbol(),NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_ASK),_Digits),0,0,"Buy backtest order");
}
if(
tradingenable==true&&InitialTrade == "SHORT" &&
close >= NextTradeVal &&
NextTradeVal > 0 &&
AveragingStepCount < NumberOfTrades&&ExitTime != iTime(Symbol(),PERIOD_CURRENT,0)
)
{
AveragingStepCount = AveragingStepCount + 1;
NextTradeVal = close + AveragingStep;
if(CandleClosing==true)
{
NextTradeVal = high + AveragingStep;
}
quantity=NextOrderQty;
if(QtyCalType== "MUL")
{NextOrderQty = quantity * QuantityMultiplier;}
if(QtyCalType== "ADD")
{ NextOrderQty =quantity +QuantityMultiplier;}
updated_high= high;
previous_target_val = updated_high;
perval= updated_high- fixed_low_sell;
Alert("perval: "+perval+" updated_high: "+updated_high+" updated_low: "+updated_low);
target_val = perval* TargetRangePercentage * 0.01;
target_val = updated_high-target_val;
if(AveragingStepCount == NumberOfTrades)
{
ActivateSl = false;
// Sl_Val = close + AveragingStep;
}
orderlog = "Short order executed @ "+Symbol()+"for lotsize="+quantity+",Step = "+AveragingStepCount+",Target1 = "+target_val;
Alert(orderlog);
trade.Sell(quantity,Symbol(),NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_BID),_Digits),0,0,"Sell backtest order");
}
///---# target calculation
if(InitialTrade == "SHORT")
{
updated_high = takeprofit_calculation(high,InitialTrade,
previous_target_val);
previous_target_val = updated_high;
perval = updated_high- fixed_low_sell;
// Alert("perval: "+perval+" updated_high: "+updated_high+" updated_low: "+updated_low);
target_val = perval * TargetRangePercentage * 0.01;
target_val = updated_high - target_val;
}
if(InitialTrade== "BUY")
{
updated_low = takeprofit_calculation(low, InitialTrade,
previous_target_val);
previous_target_val = updated_low;
perval = fixed_high_buy - updated_low;
// Alert("perval: "+perval+" updated_high: "+updated_high+" updated_low: "+updated_low);
target_val = perval * TargetRangePercentage * 0.01;
target_val= updated_low +target_val;
}
// target and stoploss execution
if(
InitialTrade == "SHORT" &&
Ltp >=Sl_Val &&
Sl_Val > 0 &&USESL==true
)
{
rangecondition=false;
orderlog = "Stoploss Executed For Short Trade All Position Exited @ "+Symbol()+"@ price"+ close+", high:"+updated_high+", low="+ fixed_low_sell;
Alert(orderlog);
InitialTrade = NULL;
target_val = 0;
Sl_Val = 0;
quantity = Quantity;
ExitTime = iTime(Symbol(),PERIOD_CURRENT,0);
AveragingStepCount = 0;
updated_low = 0;
fixed_high_buy = 0;
updated_high = 0;
fixed_low_sell = 0;
close_sell_position();
}
if(
InitialTrade == "SHORT" &&
Ltp <=target_val &&
target_val > 0
)
{
rangecondition=false;
orderlog = "Target Executed For Short Trade All Position Exited @ "+Symbol()+"@ price"+ close+", high:"+updated_high+", low="+ fixed_low_sell;
Alert(orderlog);
InitialTrade = NULL;
target_val = 0;
Sl_Val = 0;
quantity = Quantity;
ExitTime = iTime(Symbol(),PERIOD_CURRENT,0);
AveragingStepCount = 0;
updated_low = 0;
fixed_high_buy = 0;
updated_high = 0;
fixed_low_sell = 0;
close_sell_position();
}
if(
InitialTrade== "BUY" &&
Ltp <= Sl_Val &&
Sl_Val > 0&&USESL==true
)
{
rangecondition=false;
InitialTrade = NULL;
target_val = 0;
Sl_Val = 0;
orderlog = "Stoploss Executed For Buy Trade All Position Exited @ "+Symbol()+"@ price"+ close+", high:"+updated_high+", low="+ fixed_low_sell;
Alert(orderlog);
ExitTime = iTime(Symbol(),PERIOD_CURRENT,0);
AveragingStepCount= 0;
quantity = Quantity;
updated_low = 0;
fixed_high_buy= 0;
updated_high= 0;
fixed_low_sell = 0;
close_buy_position();
}
if(
InitialTrade== "BUY" &&
Ltp >= target_val &&
target_val > 0
)
{
InitialTrade = NULL;
target_val = 0;
Sl_Val = 0;
rangecondition=false;
orderlog = "Target Executed For Buy Trade All Position Exited @ "+Symbol()+"@ price"+ close+", high:"+updated_high+", low="+ fixed_low_sell;
Alert(orderlog);
ExitTime = iTime(Symbol(),PERIOD_CURRENT,0);
AveragingStepCount= 0;
quantity = Quantity;
updated_low = 0;
fixed_high_buy= 0;
updated_high= 0;
fixed_low_sell = 0;
close_buy_position();
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double takeprofit_calculation(double updated_price, string tradetype,double previous_target_value)
{
if(tradetype == "SHORT")
{
if(updated_price >= previous_target_value)
{previous_target_value = updated_price;}
}
if(tradetype == "BUY")
{
if(updated_price <= previous_target_value)
{
previous_target_value = updated_price;
return previous_target_value;
}
}
return previous_target_value;
}
//+------------------------------------------------------------------+
void close_sell_position()
{
for(int i=PositionsTotal()-1; i>=0; i--)
{
string currencypair=PositionGetSymbol(i);
int position_direction=PositionGetInteger(POSITION_TYPE);
if(currencypair==Symbol() && position_direction == POSITION_TYPE_SELL)
{
int 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);
if(currencypair==Symbol() && position_direction == POSITION_TYPE_BUY)
{
int ticket=PositionGetTicket(i);
trade.PositionClose(ticket);
}
}
}
//+------------------------------------------------------------------+
void close_all_sell_position()
{
for(int i=PositionsTotal()-1; i>=0; i--)
{
string currencypair=PositionGetSymbol(i);
int position_direction=PositionGetInteger(POSITION_TYPE);
{
int ticket=PositionGetTicket(i);
trade.PositionClose(ticket);
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void close_all_buy_position()
{
for(int i=PositionsTotal()-1; i>=0; i--)
{
string currencypair=PositionGetSymbol(i);
int position_direction=PositionGetInteger(POSITION_TYPE);
{
int ticket=PositionGetTicket(i);
trade.PositionClose(ticket);
}
}
}
//+------------------------------------------------------------------+
Comments
Post a Comment