export class Main extends Indicator { // create and export a class named Main - a child class (inheriting from the property) of the Indicator predefined class, which in turn is a child of the mScript predefined class. // constructor creates and initializes the objects necessary for the indicator to work constructor () { // "In the constructor, the super keyword is used as a function that calls the parent constructor. It must be called before the first call to the this keyword in the body of the constructor." super (); // The indicator data is displayed in a separate window? no, by default; here could be the string this.setProperty (Property.SeparateWindow, false); // here it was possible to set the display name of the indicator this.setProperty (Property.ShortName, "ATR_Channels"); or this.setProperty (Property.ShortName, "ATR Channels"); // mtrader7.com/docs/Terminal/mScript/JavaScript/v1/mScript/setProperty // Add user preference requests to the indicator initialization dialog box mtrader7.com/docs/Terminal/mScript/JavaScript/v1/mScript/addInput this.addInput ("PeriodsATR", InputType.int, 18); // ATR indicator period (Average true Range,
// mtrader7.com/ru/docs/Terminal/mScript/JavaScript/v1/Indicators/ATR this.addInput ("MA_Periods", InputType.int, 49); // Moving Average Period
this.addInput ("MA_type", InputType.int, Averaging.LinearWeighted); // type of moving average used
// channel width this.addInput ("Mult_Factor1", InputType.float, 1.6); // channel multiplication factor 1
this.addInput ("Mult_Factor2", InputType.float, 3.2); // channel 2 multiplier
this.addInput ("Mult_Factor3", InputType.float, 4.8); // channel 3 multiplier
this.addInput ("PriceType", InputType.PriceType, PriceType.Bid); // type of used price (PriceType) - Bid or Ask
this.buffers = {// create the necessary buffers for work // mtrader7.com/docs/Terminal/mScript/JavaScript/v1/mScript/addBuffer // moving average line buffer MA_Buffer0: this.addBuffer (), // channel line buffers Ch1up_Buffer1: this.addBuffer (), Ch1dn_Buffer2: this.addBuffer (), Ch2up_Buffer3: this.addBuffer (), Ch2dn_Buffer4: this.addBuffer (), Ch3up_Buffer5: this.addBuffer (), Ch3dn_Buffer6: this.addBuffer (), }; } // end of constructor method onInit () {// Initialization of the indicator, executed immediately after loading by the client terminal let {PeriodsATR, MA_Periods, Mult_Factor1, Mult_Factor2, Mult_Factor3} = this.getInputs (), // get user-defined data and enter their values in the corresponding variables, // valid only within the given program block drawBegin = Math.max (PeriodsATR, MA_Periods); // calculate the number of the bar from which the indicator lines begin to be drawn: the largest value from PeriodsATR, MA_Periods; // developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Math/max // initialization of indicator value buffers this.buffers.MA_Buffer0 .setShape (Shape.Line) // set the type of chart - line .setColor (Color.Green) // default line color; in the dialog box, you can select another
.setDrawBegin (drawBegin) // set the bar number (counted from the beginning of the data), from which the indicator line starts drawing // mtrader7.com/en/docs/Terminal/mScript/JavaScript/v1/Buffer/setDrawBegin ; this.buffers.Ch1up_Buffer1 .setShape (Shape.Line) .setColor (Color.DeepSkyBlue) .setDrawBegin (drawBegin) .setLabel ("ATRd (" + PeriodsATR + "," + Mult_Factor1 + ")") // name of the menu item in the indicator launch dialog box - a line composed of the default values of the variables specified in the constructor, // framed by appropriate text ; // similarly - for other buffers this.buffers.Ch1dn_Buffer2 .setShape (Shape.Line) .setColor (Color.DeepSkyBlue) .setDrawBegin (drawBegin) .setLabel ("ATRd (" + PeriodsATR + "," + Mult_Factor1 + ")") ; // Buffer settings 1-2 are responsible for two blue parallel lines of the indicator
this.buffers.Ch2up_Buffer3 .setShape (Shape.Line) .setColor (Color.Blue) .setDrawBegin (drawBegin) .setLabel ("ATRd (" + PeriodsATR + "," + Mult_Factor2 + ")") ; this.buffers.Ch2dn_Buffer4 .setShape (Shape.Line) .setColor (Color.Blue) .setDrawBegin (drawBegin) .setLabel ("ATRd (" + PeriodsATR + "," + Mult_Factor2 + ")") ; // Buffer settings 3-4 are responsible for two blue parallel indicator lines
this.buffers.Ch3up_Buffer5 .setShape (Shape.Line) .setColor (Color.Red) .setDrawBegin (drawBegin) .setLabel ("ATRd (" + PeriodsATR + "," + Mult_Factor3 + ")") ; this.buffers.Ch3dn_Buffer6 .setShape (Shape.Line) .setColor (Color.Red) .setDrawBegin (drawBegin) .setLabel ("ATRd (" + PeriodsATR + "," + Mult_Factor3 + ")") ; // Buffer settings 5-6 are responsible for two red parallel indicator lines } // end of the onInit function
async onUpdate () {// onUpdate is a standard function that is executed every time a new tick is received by the symbol for which the indicator is calculated const {PeriodsATR, MA_Periods, MA_type, Mult_Factor1, Mult_Factor2, Mult_Factor3, PriceType} = this.getInputs (), // get user-defined data and enter their values in the corresponding constants, {MA_Buffer0, Ch1up_Buffer1, Ch1dn_Buffer2, Ch2up_Buffer3, Ch2dn_Buffer4, Ch3up_Buffer5, Ch3dn_Buffer6} = this.buffers, // get accumulated buffer data {// request High, Low, Close data from the terminal for all bars and wait for the filling of the corresponding arrays // mtrader7.com/ru/docs/Terminal/mScript/JavaScript/v1/Bar/load High Low Close } = await Bar.load ([// await is a program execution is suspended until all data is received Bar.Mode.High, Bar.Mode.Low, Bar.Mode.Close, ], PriceType), barSize = Close.length; // barSize = the number of elements in the array Close / * Here it was possible to add "protection against the fool": MA_type is a type of moving average should be 0, 1, 2 or 3, and the user is allowed to enter any other value, including negative * / let maList, atrList, atr, ma; // let is a declaration of variables valid only within the given program block if (barSize <= Math.max (PeriodsATR, MA_Periods)) {// if the number of bars is not enough for calculations return // interrupt function execution, return to waiting for a new tick } // request (with appropriate parameters: symbol, timeframe, moving period, offset, type of moving, type of applied price // mtrader7.com/ru/docs/Terminal/mScript/JavaScript/v1/AppliedPrice, type of price) // array of moving average values // mtrader7.com/ru/docs/Terminal/mScript/JavaScript/v1/Indicators/MA maList = await Indicators.MA(Current.Symbol, Current.TimeFrame, MA_Periods, 0, MA_type, AppliedPrice.Typical, PriceType); // wait for all the data // request (with appropriate parameters: symbol, timeframe, ATR period, price type) an array of ATR indicator values // mtrader7.com/ru/docs/Terminal/mScript/JavaScript/v1/Indicators/ATR atrList = await Indicators.ATR(Current.Symbol, Current.TimeFrame, PeriodsATR, PriceType); // дождаться всех данных for (let i = 0; i