protected override void OnCalculate() { //Set Autopilot this.IsAutomated = this.Autopilot; //Check if peridocity is valid for this script if (!this._Example_Indicator_SMA_CrossOver_Advanced.DatafeedPeriodicityIsValid(Bars.TimeFrame)) { Log(this.DisplayName + ": Periodicity of your data feed is suboptimal for this indicator!", InfoLogLevel.AlertLog); return; } //Lets call the calculate method and save the result with the trade action ResultValue_Example_Indicator_SMA_CrossOver_Advanced returnvalue = this._Example_Indicator_SMA_CrossOver_Advanced.calculate(this.InSeries, this.FastSma, this.SlowSma, this.IsLongEnabled, this.IsShortEnabled); //If the calculate method was not finished we need to stop and show an alert message to the user. if (returnvalue.ErrorOccured) { Log(this.DisplayName + ": A problem has occured during the calculation method!", InfoLogLevel.AlertLog); return; } //Entry if (returnvalue.Entry.HasValue) { switch (returnvalue.Entry) { case OrderAction.Buy: this.DoEnterLong(); break; case OrderAction.SellShort: this.DoEnterShort(); break; } } //Exit if (returnvalue.Exit.HasValue) { switch (returnvalue.Exit) { case OrderAction.BuyToCover: this.DoExitShort(); break; case OrderAction.Sell: this.DoExitLong(); break; } } }
/// <summary> /// In this method we do all the work and return the object with all data like the OrderActions. /// This method can be called from any other script like strategies, indicators or conditions. /// </summary> /// <param name="data"></param> /// <returns></returns> public ResultValue_Example_Indicator_SMA_CrossOver_Advanced calculate(IDataSeries data, int fastsma, int slowsma, bool islongenabled, bool isshortenabled) { //Create a return object ResultValue_Example_Indicator_SMA_CrossOver_Advanced returnvalue = new ResultValue_Example_Indicator_SMA_CrossOver_Advanced(); //try catch block with all calculations try { //Calculate SMA and set the data into the result object returnvalue.Fast = SMA(data, fastsma)[0]; returnvalue.Slow = SMA(data, slowsma)[0]; returnvalue.Price = data.Last(); /* * CrossAbove: We create buy (entry) signal for the long position and BuyToCover (exit) for the short position. * CrossBelow: We create sell (exit) signal for the long positon and SellShort (entry) for the short position. */ if (CrossAbove(SMA(data, fastsma), SMA(data, slowsma), 0) == true) { if (islongenabled) { returnvalue.Entry = OrderAction.Buy; } if (isshortenabled) { returnvalue.Exit = OrderAction.BuyToCover; } } else if (CrossBelow(SMA(data, fastsma), SMA(data, slowsma), 0) == true) { if (islongenabled) { returnvalue.Exit = OrderAction.Sell; } if (isshortenabled) { returnvalue.Entry = OrderAction.SellShort; } } } catch (Exception) { //If this method is called via a strategy or a condition we need to log the error. returnvalue.ErrorOccured = true; } //return the result object return(returnvalue); }
protected override void OnCalculate() { //Check if peridocity is valid for this script if (!this._Example_Indicator_SMA_CrossOver_Advanced.DatafeedPeriodicityIsValid(Bars.TimeFrame)) { Log(this.DisplayName + ": Periodicity of your data feed is suboptimal for this indicator!", InfoLogLevel.AlertLog); return; } //Lets call the calculate method and save the result with the trade action ResultValue_Example_Indicator_SMA_CrossOver_Advanced returnvalue = this._Example_Indicator_SMA_CrossOver_Advanced.calculate(this.InSeries, this.FastSma, this.SlowSma, this.IsLongEnabled, this.IsShortEnabled); //If the calculate method was not finished we need to stop and show an alert message to the user. if (returnvalue.ErrorOccured) { Log(this.DisplayName + ": A problem has occured during the calculation method!", InfoLogLevel.AlertLog); return; } //Entry if (returnvalue.Entry.HasValue) { switch (returnvalue.Entry) { case OrderAction.Buy: //Long Signal Occurred.Set(1); Entry.Set(1); break; case OrderAction.SellShort: //Short Signal Occurred.Set(-1); Entry.Set(-1); break; } } else { //No Signal Occurred.Set(0); Entry.Set(0); } //Set the drawing style, if the user has changed it. PlotColors[0][0] = this.Plot0Color; Plots[0].PenStyle = this.Dash0Style; Plots[0].Pen.Width = this.Plot0Width; }
/// <summary> /// Called on each update of the bar. /// </summary> protected override void OnCalculate() { //Check if peridocity is valid for this script if (!DatafeedPeriodicityIsValid(Bars.TimeFrame)) { Log(this.DisplayName + ": Periodicity of your data feed is suboptimal for this indicator!", InfoLogLevel.AlertLog); return; } //Lets call the calculate method and save the result with the trade action ResultValue_Example_Indicator_SMA_CrossOver_Advanced returnvalue = this.calculate(this.InSeries, this.FastSma, this.SlowSma, this.IsLongEnabled, this.IsShortEnabled); //If the calculate method was not finished we need to stop and show an alert message to the user. if (returnvalue.ErrorOccured) { Log(this.DisplayName + ": A problem has occured during the calculation method!", InfoLogLevel.AlertLog); return; } //Set the curve data for the chart drawing this.Indicator_Curve_Fast.Set(returnvalue.Fast); this.Indicator_Curve_Slow.Set(returnvalue.Slow); //Entry if (returnvalue.Entry.HasValue) { switch (returnvalue.Entry) { case OrderAction.Buy: AddChartDot("ArrowLong_Entry" + Bars[0].Time.Ticks, true, Bars[0].Time, Bars[0].Open, Color.Orange); break; case OrderAction.SellShort: AddChartDiamond("ArrowShort_Entry" + Bars[0].Time.Ticks, true, Bars[0].Time, Bars[0].Open, Color.Orange); break; } } ////Exit //if (returnvalue.Exit.HasValue) //{ // switch (returnvalue.Exit) // { // case OrderAction.BuyToCover: // DrawDiamond("ArrowShort_Exit" + Bars[0].Time.Ticks, true, Bars[0].Time, Bars[0].Open, Color.Orange); // break; // case OrderAction.Sell: // DrawDot("ArrowLong_Exit" + Bars[0].Time.Ticks, true, Bars[0].Time, Bars[0].Open, Color.Orange); // break; // } //} //Set the drawing style, if the user has changed it. PlotColors[0][0] = this.Plot0Color; Plots[0].PenStyle = this.Dash0Style; Plots[0].Pen.Width = this.Plot0Width; PlotColors[1][0] = this.Plot1Color; Plots[1].PenStyle = this.Dash0Style; Plots[1].Pen.Width = this.Plot0Width; }