Dont allow to change timeframe while EA is attached to a chart.

Back to topics list To post a new topic, please log in or register
avatar
83
BeLikeWater 2016.08.17 19:47
 

I have an EA that only works on H1 timeframe, is there a way to:


- Automatically change timeframe to H1 when EA is attached succesfully to a chart?

- Disable the abiility to change timeframe while the EA is attached to a chart.


Thank you very much!



avatar
1235
FMIC 2016.08.17 23:47 #
 
BeLikeWater: I have an EA that only works on H1 timeframe, is there a way to:

- Automatically change timeframe to H1 when EA is attached succesfully to a chart?
- Disable the abiility to change timeframe while the EA is attached to a chart.

Under normal MQL, the answer is NO to both questions, although with some external WinAPI tricks you could accomplish it, but it would not be very practical to implement!
EDIT: I guess I was wrong, as WHRoeder has pointed out below!

However, you can alter your code so that the EA always uses H1 data irrespective of the time-frame of the chart!

You can accomplish this by either using the ArrayCopyRates() function, in order to have a TimeSeries array of the H1 data, or by using the various functions such as iOpen(), iHigh(), iLow(), iClose(), iTime() with a time-frame selection argument.


avatar
13010
WHRoeder 2016.08.19 17:20 #
 
BeLikeWater:

- Automatically change timeframe to H1 when EA is attached succesfully to a chart?

- Disable the abiility to change timeframe while the EA is attached to a chart.

  1. Yes ChartSetSymbolPeriod - Chart Operations - MQL4 Reference
  2. Can't disable it but you can remember the previous and change back.
    NOT TESTED
       #define  THIS_SYMBOL ""                   ///< Indicates current `_Symbol`.
    enum Uninitialization_Reason{
       UNINIT_PROGRAM,      /**< Expert Advisor terminated its operation by calling
                             * the ExpertRemove() function */
       UNINIT_REMOVE,       ///< Program has been deleted from the chart
       UNINIT_RECOMPILE,    ///< Program has been recompiled
       UNINIT_CHARTCHANGE,  ///< Symbol or chart period has been changed
       UNINIT_CHARTCLOSE,   ///< Chart has been closed
       UNINIT_PARAMETERS,   ///< Input parameters have been changed by a user
       UNINIT_ACCOUNT,      /**< Another account has been activated or reconnection
                             * to the trade server has occurred due to changes in
                             * the account settings.                              */
       UNINIT_TEMPLATE,     ///< A new template has been applied
       UNINIT_INITFAILED,   /**< This value means that OnInit() handler has returned
                             * a nonzero value.                                   */
       UNINIT_CLOSE         /**< Terminal has been closed*/  };
    string            as_string(Uninitialization_Reason reason){
       string UNINIT_XXXX = EnumToString(reason);
       return StringSubstr(UNINIT_XXXX, 7);
    }
    int      Core::on_init(string v){
       if(mPreviousSymbol != THIS_SYMBOL){             // Init due to chart change,
                                                       // WITH open orders.
          if(mPreviousSymbol != _Symbol){              // Symbol changed: UNDO!
             Alert(StringFormat("Chart change to %s with ticket open on %s",
                               _Symbol, mPreviousSymbol) );
             ChartSetSymbolPeriod(0, mPreviousSymbol, _Period);
             return   INIT_SUCCEEDED;
          }
          mPreviousSymbol = THIS_SYMBOL;               // Period changed: no problem
       }
       :
    }
    void     Core::on_deinit(Uninitialization_Reason reason){
       switch(reason){
       case  UNINIT_RECOMPILE:
       case  UNINIT_PARAMETERS:
       case  UNINIT_TEMPLATE:
          break;   // Benign, will return to the same chart.
       case  UNINIT_CHARTCHANGE:
                   // Benign, will return to the same symbol, different period.
                   // Bad, will return to a different symbol, same period!
          if(mPreviousSymbol == THIS_SYMBOL)  // Not already bad case undoing.
             mPreviousSymbol = _Symbol;       // Remember for possible bad.
          break;                              // No alert for benign case.
       default:
          Alert(StringFormat("Uninitialize (%s) with open tickets on %s",
                             as_string(reason), _Symbol));
          break;
       }  // switch
    
    NOT TESTED




Back to topics list  

To add comments, please log in or register