script help  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
darob
Posts: 207
Joined: 20 Nov 2014
Has thanked: 57 times
Been thanked: 32 times

script help

Postby darob » 22 Nov 2014

Hi, I don't know the first thing about programming. I have this script based on an example I found online. As it is, an alert is fired after every bar where the condition is met, but I'd like it to act only on fresh conditions--after the first bar where the condition is met I'd like the count to start over regardless of whether consecutive bars meet the condition. (Hope I haven't lost you!) Any ideas appreciated. Thanks a lot.
HLC 7 bars.wsp
(576.8 KiB) Downloaded 592 times

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: script help

Postby JoshM » 24 Nov 2014

Can you place the code of this script so we can take a look at it?

darob
Posts: 207
Joined: 20 Nov 2014
Has thanked: 57 times
Been thanked: 32 times

Re: script help

Postby darob » 25 Nov 2014

I've attached the file. Do you want a screen shot? Actually I've been thinking about the question and it's almost like I'm trying to reinvent the moving average so maybe don't worry about it. Thanks for the reply.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: script help

Postby JoshM » 26 Nov 2014

The file attached is a workspace file, what you'll get when saving a workspace. A workspace file does contain the charts and indicators used (including their settings), but does not answer what "this script based on an example I found online" is. It can also not be used to answer how to add a condition where the count is start over regardless of whether consecutive bars meet the condition.

For those things, the script either needs to be exported in the PowerLanguage .NET Editor or (much preferred) the script's contents placed here on the forum between the [ code ] and [ /code ] tags. Such a script will not need to contain proprietary contents, but will need to contain enough code so that we can replicate the behaviour that is giving you the issue.

Or is your question about 'how to configure a chart or indicator'? Then a workspace file helps, but then I'm somewhat confused because the topic is titled "script help" and your initial post suggests (to me) that you want to change how the script works.

darob
Posts: 207
Joined: 20 Nov 2014
Has thanked: 57 times
Been thanked: 32 times

Re: script help

Postby darob » 26 Nov 2014

Got it (I think) thanks for your patience. Cheers.

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
[SameAsSymbol(true)]
[UpdateOnEveryTick(false)]
public class Three_Bar_HLC : IndicatorObject
{
public Three_Bar_HLC(object _ctx) : base(_ctx) { }

protected override void CalcBar()
{
// Return if the current bar is not the last bar on the chart
if (!Bars.LastBarOnChart)
return;

double threeBarHigh = Bars.High.Highest(3, 1);
double threeBarLow = Bars.Low.Lowest(3, 1);

if (Bars.Close[0] < threeBarLow)
{
Alerts.Alert("The current bar close ({0}) is below the three bar low ({1}).",
Bars.Close[0], threeBarLow);
}
else if (Bars.Close[0] > threeBarHigh)
{
Alerts.Alert("The current bar close ({0}) is above the three bar high ({1}).",
Bars.Close[0], threeBarHigh);
}
}
}
}

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: script help

Postby JoshM » 29 Nov 2014

As it is, an alert is fired after every bar where the condition is met, but I'd like it to act only on fresh conditions--after the first bar where the condition is met I'd like the count to start over regardless of whether consecutive bars meet the condition.
Got it (I think) thanks for your patience. Cheers.
Yeah you got it, that's it. :)

I've made a few changes to the code:

- Set `UpdateOnEveryTick` to 'true' so that alerts can also be triggered intra-bar.

- Added two variables (`barNumHigh` and `barNumLow`) that keep track of the bar number on which the alert occurred.

- Added a condition to both alerts that ensures they are only triggered when they happen on another bar than the previous alert. At most, you'll now receive one 'higher' alert and one 'lower' alert per bar.

Is this what you meant? If I misunderstood, let me know.

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
[SameAsSymbol(true), UpdateOnEveryTick(true)]
public class Three_Bar_HLC : IndicatorObject
{
public Three_Bar_HLC(object _ctx) : base(_ctx) { }

private int barNumHigh, barNumLow;

protected override void CalcBar()
{
if (!Bars.LastBarOnChart)
return;

double threeBarHigh = Bars.High.Highest(3, 1);
double threeBarLow = Bars.Low.Lowest(3, 1);

if (Bars.Close[0] < threeBarLow && Bars.CurrentBar != barNumLow)
{
Alerts.Alert("The current bar close ({0}) is below the three bar low ({1}).",
Bars.Close[0], threeBarLow);

barNumLow = Bars.CurrentBar;
}
else if (Bars.Close[0] > threeBarHigh && Bars.CurrentBar != barNumHigh)
{
Alerts.Alert("The current bar close ({0}) is above the three bar high ({1}).",
Bars.Close[0], threeBarHigh);

barNumHigh = Bars.CurrentBar;
}
}
}
}

darob
Posts: 207
Joined: 20 Nov 2014
Has thanked: 57 times
Been thanked: 32 times

Re: script help

Postby darob » 30 Nov 2014

Hi Josh, I think I misled you with my confusing description of my goal, starting with a study title that only makes sense in my mind :)

I'd like to fire an alert when price closes past fresh consolidations and not on the close of every subsequent trending candle. I've been filtering the alert visually but thought it would be great to get the alert to filter itself. One way I thought of was to combine it with a moving average and only have an alert when the H/L close also closes across the MA. At this point it occurred to me I'm just re-inventing the moving average crossover.

Really appreciate your interest and your time, but I don't want to be wasting it!

Cheers, Dave

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: script help

Postby JoshM » 01 Dec 2014

I'd like to fire an alert when price closes past fresh consolidations and not on the close of every subsequent trending candle. I've been filtering the alert visually but thought it would be great to get the alert to filter itself. One way I thought of was to combine it with a moving average and only have an alert when the H/L close also closes across the MA. At this point it occurred to me I'm just re-inventing the moving average crossover.
Oh I see, I think I get you now. :]

I've updated the code to add an input (`ConsolidationLength`) that can be used to set the number of bars after which a new alert should be triggered as a filter.

When `ConsolidationLength` is set to 0, there is no filter and alerts can happen on consecutive bars:
Image

When the `ConsolidationLength` is set to 3, alerts in the same direction are only triggered with at least more than 3 bars in between them, like this:

Image

Is this what you meant?

====================================

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
[SameAsSymbol(true), UpdateOnEveryTick(true)]
public class Example_Three_Bar_HLC : IndicatorObject
{
public Example_Three_Bar_HLC(object _ctx) : base(_ctx) { }

private int barNumHigh, barNumLow;
private IArrowObject arrowUp, arrowDown;

public enum ShowArrows
{
True,
False
}

[Input]
public int ConsolidationLength { get; set; }

[Input]
public ShowArrows DisplayArrows { get; set; }

[Input]
public int ArrowSize { get; set; }

[Input]
public Color DownArrowColour { get; set; }

[Input]
public Color UpArrowColor { get; set; }

protected override void Create()
{
ConsolidationLength = 3;
DisplayArrows = ShowArrows.True;
ArrowSize = 3;
DownArrowColour = Color.Firebrick;
UpArrowColor = Color.ForestGreen;
}

protected override void CalcBar()
{
double threeBarHigh = Bars.High.Highest(3, 1);
double threeBarLow = Bars.Low.Lowest(3, 1);

if (Bars.Close[0] < threeBarLow && Bars.CurrentBar > barNumLow)
{
if (Alerts.CheckAlertLastBar)
Alerts.Alert("The current bar close ({0}) is below the three bar low ({1}).",
Bars.Close[0], threeBarLow);

barNumLow = Bars.CurrentBar + ConsolidationLength;

if (DisplayArrows == ShowArrows.True)
{
arrowDown = DrwArrow.Create(ArrowDownLocation(), true);
arrowDown.Color = DownArrowColour;
arrowDown.Size = ArrowSize;
}
}
else if (Bars.Close[0] > threeBarHigh && Bars.CurrentBar > barNumHigh)
{
if (Alerts.CheckAlertLastBar)
Alerts.Alert("The current bar close ({0}) is above the three bar high ({1}).",
Bars.Close[0], threeBarHigh);

barNumHigh = Bars.CurrentBar + ConsolidationLength;

if (DisplayArrows == ShowArrows.True)
{
arrowUp = DrwArrow.Create(ArrowUpLocation(), false);
arrowUp.Color = UpArrowColor;
arrowUp.Size = ArrowSize;
}
}
}

private ChartPoint ArrowDownLocation()
{
return new ChartPoint(Bars.Time[0], Bars.Close[0] + (Bars.High[0] - Bars.Low[0]));
}

private ChartPoint ArrowUpLocation()
{
return new ChartPoint(Bars.Time[0], Bars.Close[0] - (Bars.High[0] - Bars.Low[0]));
}
}
}
Attachments
exampleThreeBarHLC.pln
(1.53 KiB) Downloaded 822 times
scr.01-12-2014 09.28.42.png
(9 KiB) Downloaded 2291 times
scr.01-12-2014 09.28.16.png
(9.27 KiB) Downloaded 2359 times

darob
Posts: 207
Joined: 20 Nov 2014
Has thanked: 57 times
Been thanked: 32 times

Re: script help

Postby darob » 01 Dec 2014

Wow, this looks exactly like what I was looking for. I'll give it a test drive and report back. Many thanks.

darob
Posts: 207
Joined: 20 Nov 2014
Has thanked: 57 times
Been thanked: 32 times

Re: script help

Postby darob » 07 Apr 2016

Hi, I've applied ConsolidationLength to another script and when placed on the chart it appears as expected initially, but when an input value is changed or the underlying is switched the study disappears and won't reload. I came back to this Example_Three_Bar_HLC and notice the same thing. Any idea why?

Many thanks

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: script help  [SOLVED]

Postby Henry MultiСharts » 08 Apr 2016

Hi, I've applied ConsolidationLength to another script and when placed on the chart it appears as expected initially, but when an input value is changed or the underlying is switched the study disappears and won't reload. I came back to this Example_Three_Bar_HLC and notice the same thing. Any idea why?

Many thanks
Hello darob,

You need to add the following code to the Example_Three_Bar_HLC:
protected override void StartCalc()
{
barNumLow = 0;
barNumHigh = 0;
}

darob
Posts: 207
Joined: 20 Nov 2014
Has thanked: 57 times
Been thanked: 32 times

Re: script help

Postby darob » 08 Apr 2016

Excellent, thanks Henry.


Return to “MultiCharts .NET”