4.4 Output, Alerts and Expert Commentary

From MultiCharts
Revision as of 18:12, 29 April 2013 by Roman MultiCharts (talk | contribs) (Created page with "==Output== To open Output Window in PLEditor .Net select View and click Output Window. Output Window can be accessed from any study during any calculation stage. It can be us...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Output

To open Output Window in PLEditor .Net select View and click Output Window. Output Window can be accessed from any study during any calculation stage. It can be used for initial script debugging through logging the actions and printing the logs to Output window.

Example:

using System;

namespace PowerLanguage.Indicator
{

    public class ___test : IndicatorObject
    {

        public ___test(object ctx) : base(ctx) { }
        private IPlotObject Plot1;

        IVar<int> m_myPlotVal;

        protected override void Create()
        {
            Output.Clear();
            Output.WriteLine("___test Construct.");

            Plot1 = AddPlot(new PlotAttributes("Close"));
            m_myPlotVal = CreateSimpleVar<int>();

            Output.WriteLine("___test Construct finished.");
        }

        protected override void StartCalc()
        {
            Output.WriteLine("___test Init.");

            m_myPlotVal.Value = 0;

            Output.WriteLine("___test Init finished.");
        }

        protected override void CalcBar()
        {
            try
            {
                int pointsHL = (int)((Bars.HighValue - Bars.LowValue) / Bars.Point);
                int pointsCO = (int)((Bars.CloseValue - Bars.OpenValue) / Bars.Point);
                m_myPlotVal.Value = pointsHL / pointsCO;
                Plot1.Set(0, m_myPlotVal.Value);
            }
            catch (Exception exc)
            {
                Output.WriteLine("___test !!! Attention. Exception on bar {0}, message: {1}", Bars.CurrentBar, exc.Message);
                Plot1.Set(0, -1);
            }
        }
    }
}

The following text can appear in the Output window as a result of such script calculation:

___test Construct.
___test Construct finished.
___test Init.
___test Init finished.
___test !!! Attention. Exception on bar 358, message: Attempted to divide by zero.
___test !!! Attention. Exception on bar 484, message: Attempted to divide by zero.
___test !!! Attention. Exception on bar 569, message: Attempted to divide by zero.

Write() and WriteLine() methods accept the formatted line, the difference between them is that the WriteLine() method adds a line ending symbol.


Alerts

Alert is the means by which a user is notified about some important event occurring on a data series by a text message, sound and e-mail. Studies can access alerts through the Alerts property:

public interface IAlert
{
	bool CheckAlertLastBar { get; } // true – if it is the last bar now
	bool Enabled { get; } //true – if alerts are on
	void Alert(); //to generate Alert with an empty text
	void Alert(string format, params object[] _args);//generate Alert with a message. 
	void Cancel();//finish Alert generation on this bar. 
}

To enable/disable strategy alerts, right-click on the chart to see the shortcut menu, click Properties, select the Alerts tab and check/uncheck the Enable Alerts check box.

To enable/disable Indicator alerts, right-click on the chart to see the shortcut menu, click Format Indicators, select the Indicator and click Format.., then select the Alerts tab and check/uncheck the Enable Alerts check box.

The list of all generated alerts can be seen on the Alerts tab of the Orders and Position Tracker:

OPT.png

Example:

Let’s configure the indicator to send an alert every time when the Open of the new bar is higher than the High or lower than the Low of the previous bar.

Here is the script:

public class AlertOHOL : IndicatorObject {
	public AlertOHOL(object _ctx):base(_ctx){}
	private IPlotObject plotUpH;
	private IPlotObject plotUpL;
	private IPlotObject plotDnH;
	private IPlotObject plotDnL;
	protected override void Create() {
		plotUpH = AddPlot(new PlotAttributes("", EPlotShapes.BarHigh, Color.Red));
		plotUpL = AddPlot(new PlotAttributes("", EPlotShapes.BarLow, Color.Red));
		plotDnH = AddPlot(new PlotAttributes("", EPlotShapes.BarHigh, Color.Blue));
		plotDnL = AddPlot(new PlotAttributes("", EPlotShapes.BarLow, Color.Blue));
	}
	
	protected override void CalcBar(){
		if (Bars.Open[0] > Bars.High[1])
		{
			plotUpH.Set(Bars.High[0]);
			plotUpL.Set(Bars.Low[0]);
			Alerts.Alert("Open highest High");		}
		if (Bars.Open[0] < Bars.Low[1])
		{
			plotDnH.Set(Bars.High[0]);
			plotDnL.Set(Bars.Low[0]);
			Alerts.Alert("Open lowest Low");
		}
	}
}

Then, compile the indicator and apply it to the chart to see the result:

Result.png


Expert Commentary

Let’s add comments to the previous sample indicator:

protected override void CalcBar(){
	if (Bars.Open[0] > Bars.High[1])
	{
		plotUpH.Set(Bars.High[0]);
		plotUpL.Set(Bars.Low[0]);
		Alerts.Alert("Open highest High");
		ExpertCommentary.WriteLine("Open highest High");
	}
	if (Bars.Open[0] < Bars.Low[1])
	{
		plotDnH.Set(Bars.High[0]);
		plotDnL.Set(Bars.Low[0]);
		Alerts.Alert("Open lowest Low");
		ExpertCommentary.WriteLine("Open lowest Low");
	}
	if (ExpertCommentary.AtCommentaryBar)
	{
		ExpertCommentary.WriteLine("You are here!");
	}
}

Then, let’s compile this indicator and apply it to the chart. Enable Expert Commentary (in the main menu of MultiCharts .NET select View and click Expert Commentary) and click on the necessary bar:

Expertcomm.png