mql5/Experts/My_EAs/Grids & pyramids/Almacao.mq5
2026-10-30 17:16:21 -04:00
584 라인
23 KiB
MQL5
//+------------------------------------------------------------------+
//| Almacao.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 Max_TP = 3 ; //MAX target Profit (dollars)
// 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 = 0.90;
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 int 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, buy_lot, sell_lot, risk_percent, r2r, start_balance;
double min_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, c_buy_profit , c_sell_profit,
MAE_last , MFE_last , MAE_buy, MAE_sell, MFE_buy, MFE_sell,
c_buy_step, c_sell_step, last_buy_lot, last_sell_lot,
last_buy_price , last_sell_price;
int trade_type = 999999;
//Signal vars
bool buy_trend = false , sell_trend = false,
buy_window = false , sell_window = false,
buy_signal = false , sell_signal = false;
//Switches
bool has_open_buy = false , has_open_sell = 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 = buy_lot =sell_lot = LOT; risk_percent = Risk_Percent; r2r = R2R ;
max_Tp = min_tp = Max_TP ; /*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_buy_step = c_sell_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 :::: ", max_Tp * c_buy_step, " /n " , " current Profit ::::" , c_buy_profit );
//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_buy) && Grid_distance_valid(ORDER_TYPE_BUY)){
trade_type = ORDER_TYPE_BUY;
//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) ){
trade_type = 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_buy );
if ( !(has_open_buy) ) return;
if ( (Hit_Max_SL(ORDER_TYPE_BUY) || Hit_Max_TP(ORDER_TYPE_BUY)) ){
if (Close_Grid(ORDER_TYPE_BUY))
reSet_Nums(ORDER_TYPE_BUY);
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(ORDER_TYPE_SELL) || Hit_Max_TP(ORDER_TYPE_SELL)) )
if ( Close_Grid(ORDER_TYPE_SELL) )
reSet_Nums(ORDER_TYPE_SELL);
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_buy)
buy_lot = (LOT * martingale_factor * c_buy_step > max_lot )
? last_buy_lot
: LOT * martingale_factor * c_buy_step ;
if ( has_open_sell)
sell_lot = (LOT * martingale_factor * c_sell_step > max_lot )
? last_sell_lot
: LOT * martingale_factor * c_sell_step ;
}
lot = NormalizeDouble(lot, 2) ;
}
void Calc_Stop_levels(){}
bool ExecuteTrade(){
bool executed = false;
if (trade_type == ORDER_TYPE_BUY)
if (trade.Buy(buy_lot,_Symbol,ask, 0,0,BUY_TYPE))
executed = true;
if (trade_type == ORDER_TYPE_SELL)
if (trade.Sell(sell_lot,_Symbol,bid, 0,0,SELL_TYPE))
executed = true;
return executed ;
}
void Set_Nums(){
if (trade_type == ORDER_TYPE_BUY){
has_open_buy = true ;
last_buy_lot = buy_lot ;
last_buy_price = ask;
c_buy_step++ ;
} else if (trade_type == ORDER_TYPE_SELL){
has_open_sell = true ;
last_sell_lot = sell_lot ;
last_sell_price = bid;
c_sell_step++ ;
}
}
//+------------------------------------------------------------------+
//| Exit Functions |
//+------------------------------------------------------------------+
bool Close_Grid(ENUM_ORDER_TYPE type){
bool fail_case = false;
string comment = (type == ORDER_TYPE_BUY)
? BUY_TYPE
: SELL_TYPE ;
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 (PositionGetString(POSITION_COMMENT)!= comment)
continue ;
if (!trade.PositionClose(ticket)) {
Print("Failed to closed" , comment," Ticket: ", ticket, ", Error: ", GetLastError());
fail_case = true;
} else {
Print("Successfully closed" , comment," Ticket: ", ticket);
}
}
if (! fail_case) return true;
return false;
}
void reSet_Nums( ENUM_ORDER_TYPE type){
if (type == ORDER_TYPE_BUY){
has_open_buy = false ;
last_buy_lot = buy_lot = lot = LOT;
MAE_buy = MFE_buy = c_buy_profit = 0 ;
c_buy_step = 0;
} else if
(type == ORDER_TYPE_SELL)
{
has_open_sell = false ;
last_sell_lot = sell_lot = lot = LOT;
MAE_sell = MFE_sell = c_sell_profit = 0 ;
c_sell_step = 0;
}
}
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//|//////////// ////// ////// ///////////////// TRACKERS AND INFO ////////// ///////////////// ///////////////////////|
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
void TRACKERS(){
Current_Profit();
}
void Current_Profit(){
double profit = 0, buy_profit = 0, sell_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
if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
buy_profit += PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP);
else
sell_profit += PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP);
}
MFE_last = MathMax(profit,MFE_last);
MAE_last = MathMin(profit,MAE_last);
c_profit = profit;
MFE_buy = MathMax(buy_profit,MFE_buy);
MAE_buy = MathMin(buy_profit,MAE_buy);
MFE_sell = MathMax(sell_profit,MFE_sell);
MAE_sell = MathMin(sell_profit,MAE_sell);
c_buy_profit = buy_profit ;
c_sell_profit = sell_profit;
}
bool Hit_Max_SL( ENUM_ORDER_TYPE type){
bool hit_sl = (type == ORDER_TYPE_BUY)
? (c_buy_profit < max_sL )
: (c_sell_profit < max_sL) ;
//Print("-::::::::::::::::::::::::::::::::::::::::::::::::-") ;
//Print (" c_buy_profit ::::::" ,c_buy_profit ) ;
//Print (" max_sL ::::::" ,max_sL ) ;
//Print (" c_buy_profit < max_sL ::::::" , c_buy_profit < max_sL ) ;
if (hit_sl) Print ("HIT SL");
return hit_sl;
}
bool Hit_Max_TP( ENUM_ORDER_TYPE type ){
if (c_buy_step == 0 && type == ORDER_TYPE_BUY) return false ;
if (c_sell_step == 0 && type == ORDER_TYPE_SELL) return false ;
min_tp = (u_atr)
? MathAbs(max_sL * r2r *(ATR_factor * ATR[1]))
: MathAbs(max_sL * r2r );
bool hit_tp = (type == ORDER_TYPE_BUY)
? (c_buy_profit > max_Tp *c_buy_step /*&& c_buy_profit > min_tp */ )
: (c_sell_profit > max_Tp *c_sell_step /*&& c_buy_profit > min_tp */) ;
//Print("-::::::::::::::::::::::::::::::::::::::::::::::::-") ;
//Print (" c_buy_profit ::::::" ,c_buy_profit ) ;
//Print (" c_buy_step ::::::" ,c_buy_step ) ;
//Print (" max_Tp *c_buy_step ::::::" ,max_Tp *c_buy_step ) ;
//Print (" c_buy_profit . max_Tp *c_buy_step ::::::" , c_buy_profit > max_Tp *c_buy_step ) ;
if (hit_tp) Print ("HIT TP");
return hit_tp; /// Add ATR , add Alerts/ Prints for type of exit
}
bool Grid_distance_valid ( ENUM_ORDER_TYPE type){
if(PositionsTotal() == 0) return true;
bool valid = false;
if (type == ORDER_TYPE_BUY){
if (
ask < last_buy_price
&& P2p(ask,last_buy_price) > grid_distance
&& PositionsTotal() < max_step
)
valid = true;
}
if (type == ORDER_TYPE_SELL){
if (
bid > last_sell_price
&& P2p(bid,last_sell_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) {
double price = (trade_type == ORDER_TYPE_BUY )
? entryPrice + (pips * 0.0001)
: entryPrice - (pips * 0.0001) ; // Assuming EURUSD
return price;
}