MQL5 OOP: When syntax starts to feel like Python (Clean & Modular)

 

Hi everyone,

The more I dive into OOP, the more I realize that my MQL5 syntax is starting to look and feel like Python in terms of concepts (readability, method chaining, clarity).

One thing is for sure: MQL5 is much more powerful when structured this way. We move away from 'spaghetti code' toward something truly modular and rock-solid.

Here is an extract of my OnInit() . OOP is definitely worth the effort, but you have to put the work in.

Happy coding!


int OnInit() {
  int    i_result     = INT_MIN;
  string s_log_result = NULL;

  // Mapping inputs to central preferences
  i_center.prefs().allow_long      ( gu_allow_longs);
  i_center.prefs().allow_short     ( gu_allow_shorts);
  i_center.prefs().asset_type      ( gu_asset_type);
  i_center.prefs().ea_comment      ( gu_ea_comment);
  i_center.prefs().ea_name         ( gu_ea_name);
  i_center.prefs().lot_size        ( gu_lot_size);
  i_center.prefs().magic           ( gu_magic_number);
  i_center.prefs().max_non_be      ( gu_max_non_be);
  i_center.prefs().max_slipp       ( gu_max_slippage);
  i_center.prefs().order_time      ( gu_order_time);
  i_center.prefs().retry_tempo_sec ( gu_retry_tempo_sec);
  i_center.prefs().risk            ( gu_risk_per_trade);
  i_center.prefs().set_asyn        ( gu_set_async_mode);
  i_center.prefs().symbol          ( _Symbol);

  // Dependency injection
  i_center.ranges().init_center      ( & i_center);
  i_center.ranges().init_contener    ( & i_center, i_center.prefs_dev());
  i_center.ranges().init_preferences ( i_center.prefs());
  i_center.ranges().init_requisite   ( i_center.requisites());
  i_center.ranges().init_status      ( i_center.sys_state());

  if ( i_center.ranges().set_symbol( _Symbol) != STATUS_OK) {
    return INIT_FAILED;
  }

  // Trading environment setup
  i_result = i_trads.Init_Trading_Settings( i_center.prefs().ea_name(),
             i_center.prefs().magic());
  if ( i_result < 0) {
    i_msg_fract.error(
      StringFormat( "Trade settings initialization failed: %d", i_result),
      __FUNCTION__, ERR_TECH_FAILED_INITIALIZE);
    return INIT_FAILED;
  }

  // Initialize trading environment with center preferences
  i_result = i_trads.Init_Trading_Environment(
               i_center.prefs().symbol(),
               i_center.prefs().asset_type(), i_center.prefs().risk(),
               i_center.prefs().lot_size(), i_center.prefs().max_slipp(),
               i_center.prefs().order_time(), i_center.prefs().allow_long(),
               i_center.prefs().allow_short(), i_center.prefs().set_asyn());

  if ( i_result < ZERO_ERROR_THRESHOLD) {
    i_msg_fract.error(
      StringFormat( "Trading environment setup failed: %d", ERR_FUNC_GENERIC),
      function_class( __FUNCTION__), ERR_FUNC_GENERIC);
    return INIT_FAILED;
  }

  // Validate internal preference state
  i_result = i_center.prefs().check_is_valid();
  if ( i_result < ZERO_ERROR_THRESHOLD) {
    i_msg_fract.error( "Internal preference validation failed",
                       function_class( __FUNCTION__), ERR_FUNC_GENERIC);
    return INIT_FAILED;
  }

  // Initialize range components, indicators and setups
  i_result = i_center.ranges().init_ranges();
  if ( i_result < ZERO_ERROR_THRESHOLD) {
    i_msg_fract.error( "Range initialization failure",
                       function_class( __FUNCTION__), ERR_FUNC_GENERIC);
    return INIT_FAILED;
  }

  return INIT_SUCCEEDED;
}