mql5/Experts/My_EAs/Grids & pyramids/Almacao_LONGS(EURUSD).mq5
2026-10-30 17:13:21 -04:00
//+------------------------------------------------------------------+
//| Almacao_LONGS(EURUSD).mq5 |
//| MathewCole |
//| https://www.ColBekATS |
//+------------------------------------------------------------------+
#property copyright "MathewCole"
#property link "https://www.ColBekATS"
#property version "1.00"
#include <Trade\Trade.mqh>
#include <Controls\Dialog.mqh>
#include <Controls\Button.mqh>
#include <Controls\Label.mqh>
#include <Controls\Edit.mqh>
enum TSL_TYPE{
cont,
snap,
indicator
};
enum Risk_TYPE{
Fixed_Risk,
Martingale
};
enum MARTINGALE_PROG_TYPE{
fixed_profit,
Regressive_Progression,
Custom_input
};
string BUY_TYPE = "_BUY_Trade_",
SELL_TYPE = "_SELL_Trade_";
input group "GENRAL SETTINGS"
input double LOT = 1.0; //Starting lot size
input double Risk_Percent = 1 ; //Risk Percentage total
input double INIT_BALANCE = 0 ; //Inital Balance (0 = current)
input double R2R = 5 ; //minimum Risk to Reward Ratio
input double TARGET_TP = 3 ; //target Profit (dollars)
input double MAX_TP = 100; //MAX TP
// input double Max_SL = 10 ; //MAX Loss (points)
// input double SL_Buff = 10 ; //Stop Loss Buff(points)
input group "Grid Settings"
input double GRID_DISTANCE = 10; // grid distance
input double GRID_dFACTOR = 1.1; // grid distance factor
input double MAX_dGRID = 100; // max grid distance
input double MIN_dGRID = 1 ; // min grid distance
input group "Risk Managment Settings"
input Risk_TYPE Risk_Type = Martingale; // Risk type
input double MAX_LOT = 10 ; // max lot size
// input double Max_DD = 5 ; // max Drawdown (%)
input group "Martingale Numbers"
input MARTINGALE_PROG_TYPE Progression_type = fixed_profit ;
input double MARTINGALE_FACTOR = 2 ; //martingale factor
input int STEP_SIZE = 1 ; // Step size
input int MAX_STEP = 15 ; // maximum number of steps
input group "Trailing Stop Settings"
input bool U_TSl = true; //Use trailing Stop?
input TSL_TYPE TSL_type = snap; //Trailing Stop Type
input double TSl = 10 ; //Trailing Stop (%)
input double TSl_Buff = 20 ; //Trailing Stop buffer(%)
input double HTTP = 0.1; //HTTP ratio crit.
input group "ATR Settings"
input bool U_ATR = false; // USE ATR for stop
input int ATR_PERIOD = 100 ; // ATR Period
input double ATR_FACTOR = 0.5 ; // ATR Factor
input group "------Indicator Settings-------"
input group "Alligator Settings"
input int JAWS_PERIOD = 13;
input int JAWS_SHIFT = 8 ;
input int TEETH_PERIOD = 8 ;
input int TEETH_SHIFT = 5 ;
input int LIPS_PERIOD = 5 ;
input int LIPS_SHIFT = 3 ;
input ENUM_APPLIED_PRICE PRICE = PRICE_MEDIAN;
input group "MACD Settings"
input int SLOW_MACD = 200;
input int FAST_MACD = 100;
input int SIG_LINE = 10 ;
input group "Awsome Oscillator Indicator Settings"
input group "EA Settings"
input ulong magicnumber = 3123410 ; //EA magic number
input ulong slipage = 50000;
CTrade trade ;
MqlRates rates[] ;
double bid, ask ;
int barsTotal = 0 ;
datetime init_time ;
//variable shadows
double lot, risk_percent, r2r, start_balance;
double min_tp , target_tp, max_tp, max_sL, sl_buff, tsl, tsl_buff, http,
grid_distance, grid_factor, max_dgrid, min_dgrid,
martingale_factor, max_lot,
ATR_factor;
int step_size , max_step;
int jaws_period, jaws_shift, teeth_period, teeth_shift, lips_period, lips_shift ,ATR_period;
bool u_tsl, u_atr;
TSL_TYPE tsl_type;
Risk_TYPE risk_type;
//handles
int MACD_handle , alligator_handle , AO_handle, ATR_handle;
double MACD[] , Jaws[], Teeth[], Lips[], AO_buffer[], ATR[] ;
//Tracking
double t_prof, c_profit, g_profit,
MAE_last , MFE_last,
c_step,
last_lot , last_price ;
//Signal vars
bool buy_trend = false , buy_window = false , buy_signal = false;
//Switches
bool has_open = false ;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit(){
if(!MQLInfoInteger(MQL_TESTER)) return INIT_FAILED;
trade.SetExpertMagicNumber(magicnumber);
trade.SetAsyncMode(true);
trade.SetDeviationInPoints(10);
init_time = TimeCurrent();
//vars inititialisation
lot = lot = LOT; risk_percent = Risk_Percent; r2r = R2R ;
target_tp = TARGET_TP ; max_tp = MAX_TP ; min_tp = 0 ;/*sl_buff = SL_Buff; */ tsl = TSl ;tsl_buff = TSl_Buff ; http = HTTP;
grid_distance = GRID_DISTANCE; grid_factor = GRID_dFACTOR; max_dgrid = MAX_dGRID; min_dgrid = MIN_dGRID;
martingale_factor = MARTINGALE_FACTOR; max_lot = MAX_LOT; step_size = STEP_SIZE; max_step = MAX_STEP ;
jaws_period = JAWS_PERIOD ; jaws_shift = JAWS_SHIFT; teeth_period = TEETH_PERIOD; teeth_shift = TEETH_SHIFT ;
lips_period = LIPS_PERIOD; lips_shift = LIPS_SHIFT;
ATR_period = ATR_PERIOD; ATR_factor = ATR_FACTOR ;
u_tsl = U_TSl ;
tsl_type = TSL_type ;
risk_type = Risk_Type ;
max_sL = (INIT_BALANCE == 0)
? AccountInfoDouble(ACCOUNT_BALANCE) * (risk_percent /100)
: INIT_BALANCE * (risk_percent /100) ;
max_sL *= -1;
c_step = 0 ;
//Load_GUI();
//Loading handles
ArraySetAsSeries(MACD , true);
ArraySetAsSeries(Jaws , true);
ArraySetAsSeries(Teeth , true);
ArraySetAsSeries(Lips , true);
ArraySetAsSeries(AO_buffer , true);
ArraySetAsSeries(ATR , true);
ArraySetAsSeries(rates , true);
AO_handle = iAO(_Symbol,PERIOD_CURRENT);
MACD_handle = iMACD(_Symbol,PERIOD_CURRENT,FAST_MACD,SLOW_MACD,SIGNAL_LINE,PRICE_CLOSE);
alligator_handle = iAlligator(_Symbol,PERIOD_CURRENT,JAWS_PERIOD,JAWS_SHIFT,TEETH_PERIOD,TEETH_SHIFT,LIPS_PERIOD,LIPS_SHIFT,MODE_SMMA,PRICE_MEDIAN);
if (ATR_factor)
ATR_handle = iATR(_Symbol,PERIOD_CURRENT,ATR_PERIOD);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick(){
bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
int bars = iBars(_Symbol,PERIOD_CURRENT);
if (barsTotal != bars){
CopyBuffer(MACD_handle,0,0,8,MACD);
CopyBuffer(AO_handle, 0,0,4, AO_buffer);
CopyBuffer(alligator_handle,0,0,3,Jaws);
CopyBuffer(alligator_handle,1,0,4,Teeth);
CopyBuffer(alligator_handle,2,0,4,Lips);
if (u_atr)
CopyBuffer(ATR_handle, 0,0,3,ATR);
CopyRates (_Symbol,PERIOD_CURRENT,0,5,rates);
barsTotal = bars;
}
TRADE_EXECUTIONS();
TRACKERS();
//Comment("Profit Target :::: ", target_tp * c_step, " /n " , " current Profit ::::" , die );
//Comment("BUY STOP LOSS ::::" , max_sL);
//Comment(" min prof ::::" , min_tp);
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//|//////////// ////// ////// ///////////////// SIGNALS /////// ///////////////// ///////////////////////|
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
void SIGNALS(){
BUY_SIGNAL();
//SELL_SIGNAL();
}
bool BUY_TREND(){ // note on smaller MacDs, may want addditional smoothing to for determining increasing/degreasing
if ( AO_buffer[1] > 0 && MACD[1] > 0 && MACD[1] > MACD[2])
return true;
return false;
}
// bool SELL_TREND(){
// if ( AO_buffer[1] < 0 && MACD[1] < 0 && MACD[1] < MACD[2] )
// //Print("Sell trend");
// return true;
// return false;
//}
bool BUY_Window(){
if (BUY_TREND() && rates[1].close > Lips[1] && Lips[1] > Teeth[1] && Teeth[1] > Jaws[1])
return true;
return false;
}
//bool SELL_WINDOW(){
// if (SELL_TREND() && rates[1].close < Lips[1] && Lips[1] < Teeth[1] && Teeth[1] < Jaws[1])
// return true;
// return false;
//}
bool BUY_SIGNAL(){
if (BUY_TREND() && BUY_Window())
return true;
return false;
}
//bool SELL_SIGNAL(){
// if (SELL_TREND() && SELL_WINDOW())
// return true;
// return false;
//}
//+------------------------------------------------------------------+
//| Master Trade Execution Function |
//+------------------------------------------------------------------+
void TRADE_EXECUTIONS(){
Enter_BUY();
//Enter_SELL();
Exit_Buy();
//Exit_SELL();
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//|//////////// ////// ////// ///////////////// TRADE ENTRIES /////// ///////////////// ///////////////////////|
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//+----------------------------------------------------------------------------------------------------------------------------------------------------------------
void Enter_BUY(){
if ((BUY_SIGNAL() || has_open) && Grid_distance_valid()){
//Stop_levels();
Calc_Lot_Size();
if (ExecuteTrade()){
Set_Nums();
UI_Post();
}
}
}
//void Enter_SELL(){
// if ((SELL_SIGNAL() || has_open_sell) && Grid_distance_valid(ORDER_TYPE_SELL) ){
// //Stop_levels();
// Calc_Lot_Size();
// if (ExecuteTrade()){
// Set_Nums();
// UI_Post();
// }
//
// }
//}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//|//////////// ////// ////// ///////////////// TRADE EXITS /////// ///////////////// ///////////////////////|
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
void Exit_Buy(){
//Print ("Has BUY_ open ::::" , has_open );
if ( !(has_open) ) return;
if ( (Hit_Max_SL() || Hit_Max_TP()) ){
if (Close_Grid())
reSet_Nums();
else
Alert("There was an error closing BUY Positions. Please execute manual reset or kill EA if still not closed");
}
}
//void Exit_SELL(){
// if ( !(has_open_sell) ) return;
// if ((Hit_Max_SL() || Hit_Max_TP()) )
// if ( Close_Grid() )
// reSet_Nums();
// else
// Alert("There was an error closing SELL Positions. Please execute manual reset or kill EA");
//}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//|//////////// ////// ////// ///////////////// TRADE FUNCTIONS /////// ///////////////// ///////////////////////|
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Entry Functions |
//+------------------------------------------------------------------+
void Calc_Lot_Size(){
if (PositionsTotal() != 0 ){
if ( has_open)
lot = (LOT * martingale_factor * c_step > max_lot )
? last_lot
: LOT * martingale_factor * c_step ;
lot = NormalizeDouble(lot, 2) ;
//if ( has_open)
//die = (LOT * martingale_factor * c_sell_step > max_lot )
// ? last_sell_lot
// : LOT * martingale_factor * c_sell_step;
}
}
void Calc_Stop_levels(){}
bool ExecuteTrade(){
bool executed = false;
if (trade.Buy(lot,_Symbol,ask, 0,0,BUY_TYPE))
executed = true;
//if (trade.Sell(lot,_Symbol,bid, 0,0,SELL_TYPE))
// executed = true;
return executed;
}
void Set_Nums(){
has_open = true ;
last_lot = lot ;
last_price = ask;
c_step++ ;
}
//+------------------------------------------------------------------+
//| Exit Functions |
//+------------------------------------------------------------------+
bool Close_Grid(){
bool fail_case = false;
for (int i = PositionsTotal() - 1; i >= 0; i--) {
ulong ticket = PositionGetTicket(i);
if (!PositionSelectByTicket(ticket))
continue;
if (PositionGetInteger(POSITION_MAGIC) != magicnumber)
continue;
if (PositionGetString(POSITION_SYMBOL) != _Symbol)
continue;
if (!trade.PositionClose(ticket)) {
Print("Failed to close trade --- Ticket: ", ticket, ", Error: ", GetLastError());
fail_case = true;
} else {
Print("Successfully closed trade--- Ticket: ", ticket);
}
}
if (! fail_case) return true;
return false;
}
void reSet_Nums( ){
has_open = false ;
last_lot = lot = lot = LOT;
MAE_last = MFE_last = c_profit = 0 ;
c_step = 0;
//has_open_sell = false ;
//last_sell_lot = die = lot = LOT;
//MAE_sell = MFE_sell = die = 0 ;
//c_sell_step = 0;
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//|//////////// ////// ////// ///////////////// TRACKERS AND INFO ////////// ///////////////// ///////////////////////|
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
void TRACKERS(){
Current_Profit();
}
void Current_Profit(){
double profit = 0 ;
for (int i = PositionsTotal() - 1; i >= 0; i--) {
ulong ticket = PositionGetTicket(i);
if (!PositionSelectByTicket(ticket))
continue;
if (PositionGetInteger(POSITION_MAGIC) != magicnumber)
continue;
if (PositionGetString(POSITION_SYMBOL) != _Symbol)
continue;
profit += PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP) ; /// add swap and commission prices
}
MFE_last = MathMax(profit,MFE_last);
MAE_last = MathMin(profit,MAE_last);
c_profit = profit;
}
bool Hit_Max_SL(){
if (c_profit < max_sL) Print ("HIT SL");
return c_profit < max_sL;
}
bool Hit_Max_TP(){
if (c_step == 0) return false ;
min_tp = (u_atr)
? MathAbs(max_sL * r2r *(ATR_factor * ATR[1]))
: MathAbs(max_sL * r2r );
bool hit_tp = (c_profit > target_tp *c_step || c_profit > max_tp /*&& c_profit > min_tp*/ ) ;
if (hit_tp) Print ("HIT TP");
return hit_tp;
}
bool Grid_distance_valid (){
if(PositionsTotal() == 0) return true;
bool valid = false;
if (
ask < last_price
&& P2p(ask,last_price) > grid_distance
&& PositionsTotal() < max_step
)
valid = true;
//Comment(P2p(ask,last_price));
return valid;
}
void UI_Post(){}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//|//////////// ////// ////// ///////////////// HELPER FUNCTIONS /////// ///////////////// ///////////////////////|
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
double P2p (double price_1, double price_2){
double difference = MathAbs(price_2 - price_1);;
return difference * 10000 ; // For EURUSD, 1 pip = 0.0001
}
double p2P(double entryPrice, int pips) {
return entryPrice - (pips * 0.0001);
}