2026-05-28 13:03:25 -05:00
//+------------------------------------------------------------------+
//| ObTest.mq5 |
//| Copyright 2025, Niquel Mendoza. |
//| https://www.mql5.com/es/users/nique_372/news |
//+------------------------------------------------------------------+
# property copyright " Copyright 2025, Niquel Mendoza. "
# property link " https://www.mql5.com/es/users/nique_372/news "
# property version " 1.00 "
# property strict
# property tester_indicator " OrderBlockIndPart2.ex5 "
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
// For ict we will not define the define CONDITIONAL_PARTIAL_EXECUTE_WHEN_THE_ARE_POSITIONS, this is because
// we need the swings to be detected at all times.
2026-05-28 13:25:21 -05:00
# define CONDITIONALPARTIAL_CONDITION_SWING_SWEEP_MQH
2026-10-29 22:33:07 -05:00
# include "..\\..\\..\\PosMgmt\\BE\\Breakeven.mqh"
2026-05-28 13:25:21 -05:00
# include "..\\..\\..\\PosMgmt\\ConditionalPartial\\Main.mqh"
2026-10-29 22:33:07 -05:00
# include "..\\..\\..\\Account\\RM\\RiskManagement\\Main.mqh"
# include "..\\..\\..\\Account\\RM\\RiskManagement\\LPMods\\Examples.mqh"
2026-10-25 09:14:49 -05:00
# include <TSN\\Std\\Memory.mqh>
2026-10-29 22:33:07 -05:00
# include <TSN\\Std\\Basics\\Try.mqh>
2026-05-28 13:03:25 -05:00
2026-09-13 08:47:52 -05:00
2026-05-28 13:03:25 -05:00
//+------------------------------------------------------------------+
//| Inputs |
//+------------------------------------------------------------------+
sinput group " -------| General |------- "
input ulong InpMagic = 545244 ; //Magic number
2026-10-30 09:52:05 -05:00
//---
2026-10-29 22:33:07 -05:00
# include "..\\..\\Indicator\\EaCommon.mqh"
2026-05-28 13:03:25 -05:00
sinput group " "
sinput group " -------| Account Status |------- "
input ENUM_VERBOSE_LOG_LEVEL InpLogLevelAccountStatus = VERBOSE_LOG_LEVEL_ERROR_ONLY ; //(Account Status|Ticket Mangement) log level:
sinput group " "
sinput group " -------| Risk Management |------- "
2026-09-13 08:47:52 -05:00
input TSN : : ENUM_LOTE_TYPE InpRmLoteType = TSN : : Dinamico ; //Lote Type:
2026-05-28 13:03:25 -05:00
input double InpRmLote = 0.1 ; //Lot size (only for fixed lot)
2026-09-13 08:47:52 -05:00
input TSN : : ENUM_MODE_RISK_MANAGEMENT InpRmRiskMode = TSN : : risk_mode_personal_account ; //type of risk management mode
2026-05-28 13:03:25 -05:00
input bool InpRmUpdateDailyLossRiskModeProp = true ; //Update the MDL, if the risk-management type is propfirm?
2026-09-13 08:47:52 -05:00
input TSN : : ENUM_GET_LOT InpRmGetMode = TSN : : GET_LOT_BY_STOPLOSS_AND_RISK_PER_OPERATION ; //How to get the lot
2026-05-28 13:03:25 -05:00
input double InpRmPropFirmBalance = 0 ; //If risk mode is Prop Firm FTMO, then put your ftmo account balance
input ENUM_VERBOSE_LOG_LEVEL InpRmLogLevel = VERBOSE_LOG_LEVEL_ERROR_ONLY ; //Risk Management log level:
sinput group " - ML/Maximum loss/Maximum loss - "
input double InpRmPercentageOrMoneyMl = 0 ; //percentage or money (0 => not used ML)
2026-09-13 08:47:52 -05:00
input TSN : : ENUM_RISK_CALCULATION_MODE InpRmModeCalculationMl = TSN : : percentage ; //Mode calculation Max Loss
input TSN : : ENUM_APPLIED_PERCENTAGES InpRmAppliedPercentagesMl = TSN : : Balance ; //ML percentage applies to:
2026-05-28 13:03:25 -05:00
sinput group " - MWL/Maximum weekly loss/Maximum weekly loss - "
input double InpRmPercentageOrMoneyMwl = 0 ; //percentage or money (0 => not used MWL)
2026-09-13 08:47:52 -05:00
input TSN : : ENUM_RISK_CALCULATION_MODE InpRmModeCalculationMwl = TSN : : percentage ; //Mode calculation Max weekly Loss
input TSN : : ENUM_APPLIED_PERCENTAGES InpRmAppliedPercentagesMwl = TSN : : Balance ; //MWL percentage applies to:
2026-05-28 13:03:25 -05:00
sinput group " - MDL/Maximum daily loss/Maximum daily loss - "
input double InpRmPercentageOrMoneyMdl = 3.0 ; //percentage or money (0 => not used MDL)
2026-09-13 08:47:52 -05:00
input TSN : : ENUM_RISK_CALCULATION_MODE InpRmModeCalculationMdl = TSN : : percentage ; //Mode calculation Max daily loss
input TSN : : ENUM_APPLIED_PERCENTAGES InpRmAppliedPercentagesMdl = TSN : : Balance ; //MDL percentage applies to:
2026-05-28 13:03:25 -05:00
sinput group " - GMLPO/Gross maximum loss per operation/Percentage to risk per operation - "
2026-09-13 08:47:52 -05:00
input TSN : : ENUM_OF_DYNAMIC_MODES_OF_GMLPO InpRmModeGmlpo = TSN : : NO_DYNAMIC_GMLPO ; //Select GMLPO mode:
2026-05-28 13:03:25 -05:00
input double InpRmPercentageOrMoneyGmlpo = 2.0 ; //percentage or money (0 => not used GMLPO)
2026-09-13 08:47:52 -05:00
input TSN : : ENUM_RISK_CALCULATION_MODE InpRmModeCalculationGmlpo = TSN : : percentage ; //Mode calculation Max Loss per operation
input TSN : : ENUM_APPLIED_PERCENTAGES InpRmAppliedPercentagesGmlpo = TSN : : Balance ; //GMPLO percentage applies to:
2026-05-28 13:03:25 -05:00
sinput group " -- Optional GMLPO settings, Dynamic GMLPO "
sinput group " --- Full customizable dynamic GMLPO "
input string InpRmNote1 = " subtracted from your total balance to establish a threshold. " ; //This parameter determines a specific percentage that will be
input string InpRmStrPercentagesToBeReviewed = " 15,30,50 " ; //percentages separated by commas.
input string InpRmNote2 = " a new risk level will be triggered on your future trades: " ; //When the current balance (equity) falls below this threshold
input string InpRmStrPercentagesToApply = " 10,20,25 " ; //percentages separated by commas.
input string InpRmNote3 = " 0 in both parameters => do not use dynamic risk in gmlpo " ; //Note:
sinput group " --- Fixed dynamic GMLPO with parameters "
sinput group " - 1 - "
input string InpRmNote11 = " subtracted from your total balance to establish a threshold. " ; //This parameter determines a specific percentage that will be
input double InpRmBalancePercentageToActivateTheRisk1 = 2.0 ; //percentage 1 that will be exceeded to modify the risk separated by commas
input string InpRmNote21 = " a new risk level will be triggered on your future trades: " ; //When the current balance (equity) falls below this threshold
input double InpRmPercentageToBeModified1 = 1.0 ; //new percentage 1 to which the gmlpo is modified
sinput group " - 2 - "
input double InpRmBalancePercentageToActivateTheRisk2 = 5.0 ; //percentage 2 that will be exceeded to modify the risk separated by commas
input double InpRmPercentageToBeModified2 = 0.7 ; //new percentage 2 to which the gmlpo is modified
sinput group " - 3 - "
input double InpRmBalancePercentageToActivateTheRisk3 = 7.0 ; //percentage 3 that will be exceeded to modify the risk separated by commas
input double InpRmPercentageToBeModified3 = 0.5 ; //new percentage 3 to which the gmlpo is modified
sinput group " - 4 - "
input double InpRmBalancePercentageToActivateTheRisk4 = 9.0 ; //percentage 4 that will be exceeded to modify the risk separated by commas
input double InpRmPercentageToBeModified4 = 0.33 ; //new percentage 4 1 to which the gmlpo is modified
sinput group " -- MDP/Maximum daily profit/Maximum daily profit -- "
input bool InpRmMdpIsStrict = true ; //MDP is strict?
input double InpRmPercentageOrMoneyMdp = 11.0 ; //percentage or money (0 => not used MDP)
2026-09-13 08:47:52 -05:00
input TSN : : ENUM_RISK_CALCULATION_MODE InpRmModeCalculationMdp = TSN : : percentage ; //Mode calculation Max Daily Profit
input TSN : : ENUM_APPLIED_PERCENTAGES InpRmAppliedPercentagesMdp = TSN : : Balance ; //MDP percentage applies to:
2026-05-28 13:03:25 -05:00
sinput group " "
sinput group " -------| Risk Modifier 1 |------- "
input bool InpRmod1Enable = false ; // Enables dynamic risk adjustment (Booster)
input double InpRmod1Step = 2.0 ; // Increment applied to risk each time the condition is met
input double InpRmod1Start = 2.0 ; // Profit percentage from which risk adjustment begins
2026-09-13 08:47:52 -05:00
input TSN : : ENUM_MULTIPLIER_METHOD_DR InpRmod1Method = TSN : : DR_EXPONECIAL ; // Type of progression used to increase risk
2026-05-28 13:03:25 -05:00
sinput group " "
sinput group " -------| Risk modifier 2 |------- "
input bool InpRmod2Enable = false ; //Activate risk modifier 2
input string InpRmod2StrPercentagesToApply = " 6, 8, 10, 20, 25 " ; //Percentages to apply
sinput group " "
sinput group " -------| Risk modifier 3 by Kevin |------- "
input bool InpRmod3Enable = false ; //Activate risk modifier 3
input double InpRmod3Constant = 8.0 ; //Function constant
sinput group " "
sinput group " -------| Session |------- "
input char InpSessionStartHour = 1 ; // Start hour to operate (0-23)
input char InpSessionStartMinute = 0 ; // Start minute to operate (0-59)
input char InpSessionEndHour = 23 ; // End hour to operate (1-23)
input char InpSessionEndMinute = 0 ; // End minute to operate (0-59)
sinput group " "
sinput group " -------| Breakeven |------- "
input bool InpBeUse = true ; // Enable Breakeven logic
2026-09-13 08:47:52 -05:00
input TSN : : ENUM_BREAKEVEN_TYPE InpBeType = TSN : : BREAKEVEN_TYPE_RR ; // Calculation method (RR, ATR, or fixed points)
2026-05-28 13:03:25 -05:00
input ENUM_VERBOSE_LOG_LEVEL InpBeLogLevel = VERBOSE_LOG_LEVEL_ERROR_ONLY ; // Break Even log level:
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
sinput group " --- Breakeven based on Risk/Reward (RR) --- "
input string InpBeRrAdv = " Requires a configured Stop Loss. " ; // Warning: Stop Loss is required to calculate RR
input double InpBeRrRatio = 1.0 ; // Risk/Reward ratio to activate Breakeven (e.g. 1.0)
2026-09-13 08:47:52 -05:00
input TSN : : ENUM_TYPE_EXTRA_BE_BY_RRR InpBeRrTypeExtra = TSN : : EXTRA_BE_RRR_BY_ATR ; // Method to adjust Breakeven price (ATR or points)
2026-05-28 13:03:25 -05:00
input double InpBeRrExtraPointsOrAtrMultiplier = 1.0 ; // Adjustment value: ATR multiplier (atr 14 period) if method = ATR, or fixed points if method = Points
sinput group " --- Breakeven based solely on ATR --- "
input double InpBeAtrMultiplier = 2.0 ; // ATR multiplier to trigger Breakeven (atr 14 period)
input double InpBeAtrMultiplierExtra = 1.0 ; // Additional multiplier for precise Breakeven adjustment (atr 14 period)
sinput group " --- Breakeven based on fixed points --- "
input int InpBeFixedPointsToPutBe = 200 ; // Minimum distance (in points) to trigger Breakeven
input int InpBeFixedPointsExtra = 100 ; // Extra points added when Breakeven is triggered
sinput group " "
sinput group " -------| Partial Closures by Condition |------- "
input ENUM_VERBOSE_LOG_LEVEL InpPartialsLogLevel = VERBOSE_LOG_LEVEL_ALL ; // Partial Closures log level
input bool InpPartialsIsEnable = false ; // Enable partials ?
input string InpPartialsVolumeToClosePercentage = " 0 " ; // Volume to close separate by ","
input color InpPartialsSwingHighColor = clrBlue ; // Swing high color
input color InpPartialsSwingLowColor = clrRed ; // Swing low color
input int InpPartialsSwingBarsLeft = 4 ; // Bars left for swing detection
input int InpPartialsSwingBarRight = 4 ; // Bars right for swing detection
2026-05-28 13:25:21 -05:00
input double InpPartialsMinDistanceInAtrMul = 1.0 ; // Min distance between partials in atr14 mult
2026-05-28 13:03:25 -05:00
//+------------------------------------------------------------------+
//| Global variables |
//+------------------------------------------------------------------+
2026-09-13 08:47:52 -05:00
TSN : : CRiskManagemet * risk ;
2026-10-29 22:33:07 -05:00
TSN : : CBreakEvenBase * g_breakeven ;
2026-11-02 15:25:42 -05:00
TSN : : CConditionalPartials g_partials ;
2026-09-13 08:47:52 -05:00
TSN : : CAtr * g_atr ;
2026-05-28 13:03:25 -05:00
CTrade tradep ;
//--- Handles
2026-10-29 22:33:07 -05:00
int order_block_indicator_handle = INVALID_HANDLE ;
int hanlde_ma = INVALID_HANDLE ;
2026-05-28 13:03:25 -05:00
//--- Global buffers
double tp1 [ ] ;
double tp2 [ ] ;
double sl1 [ ] ;
double sl2 [ ] ;
//--- Session
datetime start_sesion ;
datetime end_sesion ;
//--- General
2026-10-29 22:33:07 -05:00
TSN : : CBarControlerFastMinors_D1 bar_d1 ;
2026-09-13 08:47:52 -05:00
TSN : : CBarControlerFastW1 bar_w1 ;
TSN : : CBarControlerFastMN1 bar_mn1 ;
TSN : : CBarControlerFast bar_curr ( PERIOD_CURRENT ) ;
TSN : : CDynamicRisk riesgo_c ( InpRmod1Step , InpRmod1Start , TSN : : LP_GMLPO , InpRmod1Method ) ;
TSN : : CModifierDynamicRisk risk_modificator ( InpRmod2StrPercentagesToApply ) ;
TSN : : CMathDynamicRisk risk_modificator_kevin ( InpRmod3Constant ) ;
2026-05-28 13:03:25 -05:00
//---
2026-11-02 10:55:13 -05:00
uint g_trade_flags = 0 ;
2026-10-29 22:33:07 -05:00
TSN : : CLossProfitManager g_loss_profit_manager ; // LossProfitManager
2026-10-25 09:14:49 -05:00
TSN : : CAtrOptimizedZero atr_ultra_optimized ( _Period , _Symbol , 14 ) ;
2026-05-28 13:03:25 -05:00
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit ( )
{
2026-10-29 22:33:07 -05:00
//---
string _what = NULL ;
TSNSTD_TRY_INI
2026-05-28 13:03:25 -05:00
{
2026-10-29 22:33:07 -05:00
//--- We set the breakeven values so its use is allowed
if ( InpBeUse )
{
TSN : : CBreakEvenCreator be ;
be . SetBeByAtr ( InpBeAtrMultiplier , InpBeAtrMultiplierExtra , GetPointer ( atr_ultra_optimized ) ) ;
be . SetBeByFixedPoints ( InpBeFixedPointsToPutBe , InpBeFixedPointsExtra ) ;
be . SetBeByRR ( InpBeRrRatio , InpBeRrTypeExtra , InpBeRrExtraPointsOrAtrMultiplier , GetPointer ( atr_ultra_optimized ) ) ;
TSN : : BreakEvenCreatorRes be_res = be . Create ( InpBeType , _Symbol , InpMagic ) ;
2026-11-05 17:49:32 -05:00
TSNSTD_TRY_EF ( be_res . ok , true ,
2026-11-06 10:40:44 -05:00
TSN : : CErrorTrace : : FormatTrace ( _what ) )
2026-10-29 22:33:07 -05:00
g_breakeven = be_res . ptr ;
g_breakeven . AddLogFlags ( InpBeLogLevel ) ;
CAutoCleaner : : AddPtr ( g_breakeven ) ;
}
2026-05-28 13:03:25 -05:00
2026-10-29 22:33:07 -05:00
//--- Partials
if ( InpPartialsIsEnable )
{
// Atr creation
g_atr = new TSN : : CAtr ( ) ;
g_atr . Create ( _Period , _Symbol , 14 , true , true ) ;
g_atr . SetAsSeries ( true ) ;
CAutoCleaner : : AddPtr ( g_atr ) ; // Added for auto-cleanup
// Initial log flags
g_partials . AddLogFlags ( InpPartialsLogLevel ) ;
// Create condition
TSN : : CConditionalPartialsSwingSweep * sweep_condition = new TSN : : CConditionalPartialsSwingSweep ( ) ; // True since it will be deleted by partials
// Swing condition config
TSN : : ConditionalPartialSwingSweepConfig config_sweep ;
config_sweep . swing_bars_left = InpPartialsSwingBarsLeft ;
config_sweep . swing_bars_right = InpPartialsSwingBarRight ;
config_sweep . swing_high_color = InpPartialsSwingHighColor ;
config_sweep . swing_low_color = InpPartialsSwingLowColor ;
2026-11-06 10:40:44 -05:00
TSNSTD_TRY_EF ( sweep_condition .Init ( config_sweep ) , true ,
TSN : : CErrorTrace : : FormatTrace ( _what ) ; delete sweep_condition )
2026-10-29 22:33:07 -05:00
// Condition config
TSN : : ConditionalPartialConfig config ;
config . condition = sweep_condition ;
config . min_distance_to_close_pos = TSN : : CreateDiffptr ( TSN : : MODE_DIFF_BY_ATR , _Symbol , g_atr , 0 , InpPartialsMinDistanceInAtrMul ) ;
config . str_percentage_volume_to_close = InpPartialsVolumeToClosePercentage ;
config . magic_number = InpMagic ;
// Init partials
2026-11-02 15:25:42 -05:00
TSNSTD_TRY_EF ( g_partials .Init ( config ) , true ,
2026-11-06 10:40:44 -05:00
TSN : : CErrorTrace : : FormatTrace ( _what ) )
2026-10-29 22:33:07 -05:00
}
2026-05-28 13:03:25 -05:00
2026-10-29 22:33:07 -05:00
//--- Indicators
: : ResetLastError ( ) ;
// Create ob handle
TSNSTD_TRY_N_EF ( ( order_block_indicator_handle = CreateIndicatorObHandle ( ) ) , INVALID_HANDLE ,
StringFormat ( " The ob indicator is not available latest error: _LastError=%d " , _LastError ) )
2026-05-28 13:03:25 -05:00
2026-10-29 22:33:07 -05:00
TSNSTD_TRY_EF ( TESTER_FLAG | | ChartIndicatorAdd ( 0 , 0 , order_block_indicator_handle ) , true ,
StringFormat ( " Error in add ob: _LastError=%d " , _LastError ) )
2026-05-28 13:03:25 -05:00
2026-10-29 22:33:07 -05:00
// Create ma handle
TSNSTD_TRY_N_EF ( ( hanlde_ma = iMA ( _Symbol , InpTimeframeOrderBlock , 30 , 0 , MODE_EMA , PRICE_CLOSE ) ) , INVALID_HANDLE ,
StringFormat ( " The ema indicator is not available latest error: _LastError=%d " , _LastError ) )
2026-05-28 13:03:25 -05:00
2026-10-29 22:33:07 -05:00
TSNSTD_TRY_EF ( TESTER_FLAG | | ChartIndicatorAdd ( 0 , 0 , hanlde_ma ) , true ,
StringFormat ( " Error in add ma: _LastError=%d " , _LastError ) )
2026-05-28 13:03:25 -05:00
2026-10-29 22:33:07 -05:00
//--- Trade
tradep . SetExpertMagicNumber ( InpMagic ) ;
2026-05-28 13:03:25 -05:00
2026-10-29 22:33:07 -05:00
//--- Risk management
TSN : : CRiskManagementCreator manager ;
manager . SetPropirm ( InpRmPropFirmBalance ) ;
2026-05-28 13:03:25 -05:00
2026-10-29 22:33:07 -05:00
//---
TSN : : MQLArtRMCreatorRes result = manager . GetRiskPointer ( InpRmRiskMode , InpMagic , InpRmGetMode ) ;
2026-11-05 17:49:32 -05:00
TSNSTD_TRY_EF ( result . ok , true ,
2026-11-06 10:40:44 -05:00
TSN : : CErrorTrace : : FormatTrace ( _what ) )
2026-05-28 13:03:25 -05:00
2026-10-29 22:33:07 -05:00
//---
risk = result . ptr ;
risk . AddLogFlags ( InpRmLogLevel ) ;
risk . SetLossProfitManager ( g_loss_profit_manager ) ;
CAutoCleaner : : AddPtr ( risk ) ;
2026-09-13 08:47:52 -05:00
2026-10-29 22:33:07 -05:00
// We set the parameters
string to_apply = InpRmStrPercentagesToApply , to_modfied = InpRmStrPercentagesToBeReviewed ;
2026-09-13 08:47:52 -05:00
2026-10-29 22:33:07 -05:00
if ( InpRmModeGmlpo = = TSN : : DYNAMIC_GMLPO_FIXED_PARAMETERS )
TSN : : CRmFunctions : : SetDynamicUsingFixedParameters ( InpRmBalancePercentageToActivateTheRisk1 , InpRmBalancePercentageToActivateTheRisk2 , InpRmBalancePercentageToActivateTheRisk3
, InpRmBalancePercentageToActivateTheRisk4 , InpRmPercentageToBeModified1 , InpRmPercentageToBeModified2 , InpRmPercentageToBeModified3 , InpRmPercentageToBeModified4
, to_modfied , to_apply ) ;
2026-05-28 13:03:25 -05:00
2026-10-29 22:33:07 -05:00
//--- Risk management
TSNSTD_TRY_INI_S // nota: genera bool _err implicito
{
TSNSTD_TRY_SE ( risk . AddLP ( InpRmPercentageOrMoneyMdl , InpRmAppliedPercentagesMdl , InpRmModeCalculationMdl , TSN : : LP_MDL , true ) , true ) ;
TSNSTD_TRY_SE ( risk . AddLP ( InpRmPercentageOrMoneyGmlpo , InpRmAppliedPercentagesGmlpo , InpRmModeCalculationGmlpo , TSN : : LP_GMLPO , true ,
( InpRmModeGmlpo ! = TSN : : NO_DYNAMIC_GMLPO ) , to_modfied , to_apply ) , true ) ;
TSNSTD_TRY_SE ( risk . AddLP ( InpRmPercentageOrMoneyMl , InpRmAppliedPercentagesMl , InpRmModeCalculationMl , TSN : : LP_ML , true ) , true ) ;
TSNSTD_TRY_SE ( risk . AddLP ( InpRmPercentageOrMoneyMwl , InpRmAppliedPercentagesMwl , InpRmModeCalculationMwl , TSN : : LP_MWL , true ) , true ) ;
TSNSTD_TRY_SE ( risk . AddLP ( InpRmPercentageOrMoneyMdp , InpRmAppliedPercentagesMdp , InpRmModeCalculationMdp , TSN : : LP_MDP , InpRmMdpIsStrict ) , true ) ;
risk . EndAddProfitLoss ( ) ; // Execute this every time we finish setting the maximum losses and profits
// We add modifiers
if ( InpRmod1Enable )
TSNSTD_TRY_SE ( risk . AddModificator ( riesgo_c ) , true ) ;
if ( InpRmod2Enable )
TSNSTD_TRY_SE ( risk . AddModificator ( risk_modificator ) , true ) ;
if ( InpRmod3Enable )
TSNSTD_TRY_SE ( risk . AddModificator ( risk_modificator_kevin ) , true ) ;
}
TSNSTD_TRY_END ;
2026-05-28 13:03:25 -05:00
2026-10-29 22:33:07 -05:00
//---
2026-11-06 10:40:44 -05:00
TSNSTD_TRY_N_EF ( _err , true , TSN : : CErrorTrace : : FormatTrace ( _what ) )
2026-10-29 22:33:07 -05:00
}
TSNSTD_TRY_END ;
2026-05-28 13:03:25 -05:00
2026-10-29 22:33:07 -05:00
//---
if ( _what ! = NULL ) // error final
{
FastLog ( FUNCION_ACTUAL , FATAL_ERROR_TEXT , _what ) ;
return INIT_FAILED ;
}
2026-05-28 13:03:25 -05:00
//--- We initialize the account
2026-09-13 08:47:52 -05:00
TSN : : account_status . AddLogFlags ( InpLogLevelAccountStatus ) ;
TSN : : account_status . OnInitEvent ( ) ;
2026-05-28 13:03:25 -05:00
2026-05-28 13:25:21 -05:00
//---
2026-09-13 08:47:52 -05:00
CAutoCleaner : : AddFunction ( TSN : : CAccountStatus_Deinit ) ;
2026-05-28 13:25:21 -05:00
CAutoCleaner : : AddFunction ( CBasicEvents : : Deinit ) ;
2026-05-28 13:03:25 -05:00
//--- We configure the arrays in series
ArraySetAsSeries ( tp1 , true ) ;
ArraySetAsSeries ( tp2 , true ) ;
ArraySetAsSeries ( sl1 , true ) ;
ArraySetAsSeries ( sl2 , true ) ;
return ( INIT_SUCCEEDED ) ;
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit ( const int reason )
{
2026-10-29 22:33:07 -05:00
//---
if ( ! TESTER_FLAG )
{
ChartIndicatorDelete ( 0 , 0 , ChartIndicatorName ( 0 , 0 , GetMovingAverageIndex ( ) ) ) ;
ChartIndicatorDelete ( 0 , 0 , " Order Block Indicator " ) ;
}
2026-05-28 13:03:25 -05:00
if ( hanlde_ma ! = INVALID_HANDLE )
IndicatorRelease ( hanlde_ma ) ;
if ( order_block_indicator_handle ! = INVALID_HANDLE )
IndicatorRelease ( order_block_indicator_handle ) ;
2026-05-28 13:25:21 -05:00
//---
CAutoCleaner : : Deinit ( reason ) ;
2026-05-28 13:03:25 -05:00
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick ( )
{
//--- General
const datetime time_curr = TimeCurrent ( ) ;
//--- Code that runs every new day
if ( bar_d1 . IsNewBar ( time_curr ) )
{
2026-11-02 10:55:13 -05:00
//---
2026-05-28 13:25:21 -05:00
CBasicEvents : : OnNewDay ( time_curr ) ;
2026-11-02 10:55:13 -05:00
TSNMQLART_U_BEVENTS_MAN_REM_F ( g_trade_flags , BASICEVENT_REG_FLAG_ON_NEW_DAY ) ;
2026-05-28 13:03:25 -05:00
2026-11-02 10:55:13 -05:00
//---
2026-05-28 13:03:25 -05:00
if ( bar_w1 . IsNewBar ( time_curr ) )
2026-11-02 10:55:13 -05:00
{
2026-05-28 13:25:21 -05:00
CBasicEvents : : OnNewWeek ( time_curr ) ;
2026-11-02 10:55:13 -05:00
TSNMQLART_U_BEVENTS_MAN_REM_F ( g_trade_flags , BASICEVENT_REG_FLAG_ON_NEW_WEEK ) ;
}
2026-05-28 13:03:25 -05:00
2026-11-02 10:55:13 -05:00
//---
2026-05-28 13:03:25 -05:00
if ( bar_mn1 . IsNewBar ( time_curr ) )
2026-11-02 10:55:13 -05:00
{
2026-05-28 13:25:21 -05:00
CBasicEvents : : OnNewMonth ( time_curr ) ;
2026-11-02 10:55:13 -05:00
// no uso tradeflags dado que no hay parametros para un Monthly.. pero si lo hubiera:
//TSNMQLART_U_BEVENTS_MAN_REM_F(g_trade_flags, BASICEVENT_REG_FLAG_ON_NEW_MON);
}
2026-05-28 13:03:25 -05:00
}
2026-05-28 13:25:21 -05:00
//--- Checking maximum losses and profits
2026-09-13 08:47:52 -05:00
if ( TSN : : account_status_positions_open )
2026-05-28 13:25:21 -05:00
{
2026-11-01 21:34:23 -05:00
# ifdef MQLARTICLES_RM_MULTI_EA_MODE
2026-09-13 08:47:52 -05:00
risk . CalcProfit ( ) ;
2026-11-01 21:34:23 -05:00
# else // MQLARTICLES_RM_MULTI_EA_MODE
ACCOUNTSTATUS_ONEVERYTICK
2026-09-13 08:47:52 -05:00
# endif // MQLARTICLES_RM_MULTI_EA_MODE
2026-11-02 10:55:13 -05:00
if ( TSNMQLART_U_BEVENTS_MAN_TRADE_NOW ( g_trade_flags ) )
2026-05-28 13:25:21 -05:00
{
if ( g_loss_profit_manager . MaxLossIsSuperated ( ) )
{
const int type = g_loss_profit_manager . GetLastLossSuperatedType ( ) ;
2026-11-02 10:55:13 -05:00
if ( type < TSN : : LP_ML )
{
risk . CloseAllPositions ( ) ;
// añadimos segun corresponda
TSNMQLART_U_BEVENTS_MAN_ADD_F ( g_trade_flags , TSNMQLART_RM_LP_M_BEVENTS_F_LOSS ( type ) ) ;
Print ( TSN : : g_mqarticles_lp_str_names [ type ] , " is superated now " ) ;
}
else // Manejo de las demas perdidas (aqui tambien entra pico, como no lo tenemos "agregado" no importa, pero si estuviera habria que manejar un caso especial)
2026-05-28 13:25:21 -05:00
{
2026-09-13 08:47:52 -05:00
if ( InpRmRiskMode = = TSN : : risk_mode_propfirm_dynamic_daiy_loss )
2026-05-28 13:25:21 -05:00
Print ( " The expert advisor lost the funding test " ) ;
else
Print ( " Maximum loss exceeded now " ) ;
//---
2026-11-02 10:55:13 -05:00
risk . CloseAllPositions ( ) ;
2026-05-28 13:25:21 -05:00
Remover ( ) ;
}
}
else
if ( g_loss_profit_manager . MaxProfitIsSuperated ( ) )
{
2026-11-02 10:55:13 -05:00
// NOTA: no se comprueba el tipo dado que ahora mismo solo hay un profit agregado (MDP).
// Si hubieran mas se haria algo similar a la rama de losses.
2026-05-28 13:25:21 -05:00
risk . CloseAllPositions ( ) ;
2026-11-02 10:55:13 -05:00
TSNMQLART_U_BEVENTS_MAN_ADD_F ( g_trade_flags , BASICEVENT_REG_FLAG_ON_NEW_DAY ) ;
2026-05-28 13:25:21 -05:00
Print ( " Excellent Maximum daily profit achieved " ) ;
}
}
}
2026-05-28 13:03:25 -05:00
//--- Check Session
if ( time_curr > end_sesion )
{
start_sesion = HoraYMinutoADatetime ( InpSessionStartHour , InpSessionStartMinute , time_curr ) ;
end_sesion = HoraYMinutoADatetime ( InpSessionEndHour , InpSessionEndMinute , time_curr ) ;
if ( start_sesion > end_sesion )
end_sesion = end_sesion + 86400 ;
}
//--- Breakeven
if ( InpBeUse )
2026-10-29 22:33:07 -05:00
g_breakeven . BreakEven ( ) ;
2026-05-28 13:03:25 -05:00
//--- bar controlation
const bool new_bar = bar_curr . IsNewBar ( time_curr ) ;
//--- Partials
if ( InpPartialsIsEnable & & new_bar )
{
g_atr . CopyData ( 0 , 1 ) ; // Copy atr data
g_partials . Execute ( time_curr ) ; // Execution in newbar
}
2026-11-02 10:55:13 -05:00
//--- Si hay al menos una bandera activa no se puede operar
if ( TSNMQLART_U_BEVENTS_MAN_NO_TRADE_NOW ( g_trade_flags ) )
2026-05-28 13:03:25 -05:00
return ;
//--- Strategy
if ( new_bar )
{
if ( time_curr > start_sesion & & time_curr < end_sesion )
{
if ( risk . GetPositionsTotal ( ) = = 0 )
{
2026-10-29 22:33:07 -05:00
CopyBuffer ( order_block_indicator_handle , 2 , 0 , 1 , tp1 ) ;
CopyBuffer ( order_block_indicator_handle , 3 , 0 , 1 , tp2 ) ;
CopyBuffer ( order_block_indicator_handle , 4 , 0 , 1 , sl1 ) ;
CopyBuffer ( order_block_indicator_handle , 5 , 0 , 1 , sl2 ) ;
2026-05-28 13:03:25 -05:00
if ( tp1 [ 0 ] > 0 & & tp2 [ 0 ] > 0 & & sl1 [ 0 ] > 0 & & sl2 [ 0 ] > 0 )
{
if ( tp2 [ 0 ] > sl2 [ 0 ] ) // buy orders
{
2026-10-29 22:33:07 -05:00
const double ASK = ( SymbolInfoDouble ( _Symbol , SYMBOL_ASK ) ) ;
risk . SetStopLoss ( _Symbol , ASK - sl1 [ 0 ] ) ;
2026-09-13 08:47:52 -05:00
const double lot = ( InpRmLoteType = = TSN : : Dinamico ? risk . GetLote ( ORDER_TYPE_BUY , ASK , 0 , 0 ) : InpRmLote ) ;
2026-05-28 13:03:25 -05:00
tradep . Buy ( lot , _Symbol , ASK , sl1 [ 0 ] , tp2 [ 0 ] , " Order Block EA Buy " ) ;
}
else
if ( sl2 [ 0 ] > tp2 [ 0 ] ) // sell orders
{
2026-10-29 22:33:07 -05:00
const double BID = ( SymbolInfoDouble ( _Symbol , SYMBOL_BID ) ) ;
risk . SetStopLoss ( _Symbol , sl1 [ 0 ] - BID ) ;
2026-09-13 08:47:52 -05:00
const double lot = ( InpRmLoteType = = TSN : : Dinamico ? risk . GetLote ( ORDER_TYPE_SELL , BID , 0 , 0 ) : InpRmLote ) ;
2026-05-28 13:03:25 -05:00
tradep . Sell ( lot , _Symbol , BID , sl1 [ 0 ] , tp2 [ 0 ] , " Order Block EA Sell " ) ;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| TradeTransaction function |
//+------------------------------------------------------------------+
void OnTradeTransaction ( const MqlTradeTransaction & trans ,
const MqlTradeRequest & request ,
const MqlTradeResult & result )
{
2026-09-13 08:47:52 -05:00
TSN : : account_status . OnTradeTransactionEvent ( trans ) ;
2026-05-28 13:03:25 -05:00
}
//+------------------------------------------------------------------+