How Can I Code This?

Questions about MultiCharts .NET and user contributed studies.
peachfuzz
Posts: 7
Joined: 10 Sep 2015

How Can I Code This?

Postby peachfuzz » 10 Sep 2015

I would like to utilize the prebuilt signal in MultiChart .Net (eg. MovAvg2Line cross LE and SE signals). However, it appears that this strategy is limited only to "simple moving averages". The system I want to implement is Simple MA AND Displaced Exponential MA, which in fact are both available as Indicators. Hence I can plot both Simple MA and Displaced EMA on the chart but I can't use them when it comes to signals.

Here's what I would like for the entry/exit signals:

Code: Select all

Buy = SMA(20) crossed above EMA(28, -14)*
Sell = SMA(20) crossed below EMA(28, -14)
*EMA(28, -14) = 28-Period Exponential Moving Average with an Offset (ie. displaced backwards) of 14-periods.

I wish there was a way to choose the value/parameter (as well as the type of MA) for the signals as there is for the indicators. This is by no means a complicated formula and I would think tweaking a line or two of codes would fix the issue but not knowing anything about C# I'm inclined to reach out for any assist.

peachfuzz
Posts: 7
Joined: 10 Sep 2015

Re: How Can I Code This?

Postby peachfuzz » 13 Sep 2015

Nobody here can answer this?

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

Re: How Can I Code This?

Postby JoshM » 14 Sep 2015

It depends on what you want. I can probably code such a strategy for you, but I don't have time to start working on it until the next weekend at best since I have some other scripts to create first. If this is what you want, then drop me a PM later this week and I'll start working on it. :)

If you're looking for an answer, which your last posts suggest, then I'm sorry because I don't see a specific question in your post besides a general 'how can I code this?'. But before someone can answer that question, he/she needs to know what you already know to see what needs to be explained and what not.

peachfuzz
Posts: 7
Joined: 10 Sep 2015

Re: How Can I Code This?

Postby peachfuzz » 14 Sep 2015

Thanks for your response.

There are already built-in scripts for "MA2_Crossover (in strategies)" and "EMA (in indicators)". In the format window of EMA, there's also an option for the displacement offset. So I initially thought if you could just replace the wording designated for the Simple Moving Average, used as default in the MA2_Crossover, for the Exponential Moving Average everything will work out. What I can't get my head around are all the technical jargons. Sounds doable, no?

peachfuzz
Posts: 7
Joined: 10 Sep 2015

Re: How Can I Code This?

Postby peachfuzz » 14 Sep 2015

If you're looking for an answer, which your last posts suggest, then I'm sorry because I don't see a specific question in your post besides a general 'how can I code this?'. But before someone can answer that question, he/she needs to know what you already know to see what needs to be explained and what not.
Here's what I've done so far. I've *modified* the built-in strategy script (MovAvg2Line_Cross_LE). I've added comments where I made the revisions. When it was compiled, however, I got this error: "}expected". Unfortunately, not sure what more can be done to fix. Please advise. Thanks.

Code: Select all

using System;
using PowerLanguage.Function;

namespace PowerLanguage.Strategy
{
public class ExpAvg2Line_Cross_LE : SignalObject
//renamed to ExpAvg2Line_Cross_LE from MovAvg2Line_Cross_LE
{
private XAverage m_FastAverageFC;
//renamed AverageFC (Simple MA??) to XAverage (Exponential MA)
private AverageFC m_SlowAverageFC;
//left as is, for I need Simple MA

private VariableSeries<Double> m_FastAvg;
private VariableSeries<Double> m_SlowAvg;
//left as is

private IPlotObject Plot1;
//added to plot on chart

private IOrderMarket m_MA2CrossLE;
//left as is

public ExpAvg2Line_Cross_LE(object ctx) :
base(ctx)
//renamed to ExpAvg2Line_Cross_LE from MovAvg2Line_Cross_LE
{
SlowLength = 18;
FastLength = 9;
}

private ISeries<double> Price { get; set; }
//added "displace" as the third input
[Input]
public int FastLength { get; set; }

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

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

protected override void Create(){
m_FastAverageFC = new AverageFC(this);
m_SlowAverageFC = new AverageFC(this);
m_FastAvg = new VariableSeries<Double>(this);
m_SlowAvg = new VariableSeries<Double>(this);
m_MA2CrossLE =
OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, "MA2CrossLE", EOrderAction.Buy));
}

protected override void StartCalc(){
Price = Bars.Close;
m_FastAverageFC.price = Price;
m_FastAverageFC.length = FastLength;
m_SlowAverageFC.price = Price;
m_SlowAverageFC.length = SlowLength;
}


protected override void CalcBar(){
m_FastAvg.Value = m_FastAverageFC[0];
//the below lines written to add the offset (eg. displace) to Exponential MA
if (((displace >= 0)
|| Bars.CurrentBar > Math.Abs(displace))){
Plot1.Set(displace, m_FastAvg.Value);
if ((displace <= 0)){
if (((PublicFunctions.DoubleGreater(price[0], m_FastAvg.Value) &&
PublicFunctions.DoubleGreater(m_FastAvg.Value, m_FastAvg[1]))
&& PublicFunctions.DoubleLessEquals(m_FastAvg[1], m_FastAvg[2]))){
Alerts.Alert("Indicator turning up");
}
else{
if (((PublicFunctions.DoubleLess(price[0], m_FastAvg.Value) &&
PublicFunctions.DoubleLess(m_FastAvg.Value, m_FastAvg[1]))
&& PublicFunctions.DoubleGreaterEquals(m_FastAvg[1], m_FastAvg[2]))){
Alerts.Alert("Indicator turning down");

m_SlowAvg.Value = m_SlowAverageFC[0];
if (Bars.CurrentBar > 1 && m_FastAvg.CrossesOver(m_SlowAvg, ExecInfo.MaxBarsBack))
{
m_MA2CrossLE.Send();
}
}
}
}

peachfuzz
Posts: 7
Joined: 10 Sep 2015

Re: How Can I Code This?

Postby peachfuzz » 15 Sep 2015

Compiling the code produces too much error for which I do not quite understand. Any insight would be appreciated.

Code: Select all

using System;
using PowerLanguage.Function;

namespace PowerLanguage.Strategy
{
public class ExpAvg2Line_Cross_LE : SignalObject
//renamed to ExpAvg2Line_Cross_LE from MovAvg2Line_Cross_LE
{
private XAverage m_FastAverageFC;
//renamed AverageFC (Simple MA??) to XAverage (Exponential MA)
private AverageFC m_SlowAverageFC;
//left as is, for I need Simple MA

private VariableSeries<Double> m_FastAvg;
private VariableSeries<Double> m_SlowAvg;
//left as is

private IPlotObject Plot1;
//added to plot on chart

private IOrderMarket m_MA2CrossLE;
//left as is

public ExpAvg2Line_Cross_LE(object ctx) :
base(ctx)
//renamed to ExpAvg2Line_Cross_LE from MovAvg2Line_Cross_LE
{
SlowLength = 18;
FastLength = 9;
}

private ISeries<double> Price { get; set; }
//added "displace" as the third input
[Input]
public int FastLength { get; set; }

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

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

protected override void Create(){
m_FastAverageFC = new XAverage(this);
m_SlowAverageFC = new AverageFC(this);
m_FastAvg = new VariableSeries<Double>(this);
m_SlowAvg = new VariableSeries<Double>(this);
Plot1 =
AddPlot(new PlotAttributes("WMAX", 0, Color.Blue,
Color.Cyan, 0, 0, true));
m_MA2CrossLE =
OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, "MA2CrossLE", EOrderAction.Buy));
}

protected override void StartCalc(){
Price = Bars.Close;
m_FastAverageFC.price = Price;
m_FastAverageFC.length = FastLength;
m_SlowAverageFC.price = Price;
m_SlowAverageFC.length = SlowLength;
}


protected override void CalcBar(){
m_FastAvg.Value = m_FastAverageFC[0];
//the below lines added to displace Exponential MA
if (((displace >= 0)
|| Bars.CurrentBar > Math.Abs(displace))){
Plot1.Set(displace, m_FastAvg.Value);
if ((displace <= 0)){
if (((PublicFunctions.DoubleGreater(price[0], m_FastAvg.Value) &&
PublicFunctions.DoubleGreater(m_FastAvg.Value, m_FastAvg[1]))
&& PublicFunctions.DoubleLessEquals(m_FastAvg[1], m_FastAvg[2]))){
Alerts.Alert("Indicator turning up");
}
else{
if (((PublicFunctions.DoubleLess(price[0], m_FastAvg.Value) &&
PublicFunctions.DoubleLess(m_FastAvg.Value, m_FastAvg[1]))
&& PublicFunctions.DoubleGreaterEquals(m_FastAvg[1], m_FastAvg[2]))){
Alerts.Alert("Indicator turning down");
m_SlowAvg.Value = m_SlowAverageFC[0];
if (Bars.CurrentBar > 1 && m_FastAvg.CrossesOver(m_SlowAvg, ExecInfo.MaxBarsBack))
{
m_MA2CrossLE.Send();
}
}
}
}
}
}
}
}

Code: Select all

[b]ERRORS:[/b]

The name 'AddPlot' does not exist in the current context (Ln 49)
The name 'Color' does not exist in the current context (Ln 49)
The name 'Color' does not exist in the current context (Ln 50)
'PowerLanguage.Function.XAverage' does not contain a definition for 'price' and no extension method 'price' accepting a first argument of type 'PowerLanguage.Function.XAverage' could be found (are you missing a using directive or an assembly reference?) (Ln 57)
'PowerLanguage.Function.XAverage' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'PowerLanguage.Function.XAverage' could be found (are you missing a using directive or an assembly reference?) (Ln 58)
The name 'price' does not exist in the current context (Ln 71)
The name 'price' does not exist in the current context (Ln 77)

peachfuzz
Posts: 7
Joined: 10 Sep 2015

Re: How Can I Code This?

Postby peachfuzz » 15 Sep 2015

Okay, I've managed to eliminate all but 2 errors and this one eludes me. Please shine some light on this noob.

Code: Select all

Error Messages

'PowerLanguage.Function.XAverage' does not contain a definition for 'price' and no extension method 'price' accepting a first argument of type 'PowerLanguage.Function.XAverage' could be found (are you missing a using directive or an assembly reference?) (Ln 57)
'PowerLanguage.Function.XAverage' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'PowerLanguage.Function.XAverage' could be found (are you missing a using directive or an assembly reference?) (Ln 58)

Code: Select all

using System;
using PowerLanguage.Function;

namespace PowerLanguage.Strategy
{
public class ExpAvg2Line_Cross : SignalObject
//renamed to ExpAvg2Line_Cross from MovAvg2Line_Cross_LE
{
private XAverage m_FastAverageFC;
//renamed AverageFC (Simple MA??) to XAverage (Exponential MA)
private AverageFC m_SlowAverageFC;
//left as is, for I need Simple MA

private VariableSeries<Double> m_FastAvg;
private VariableSeries<Double> m_SlowAvg;
//left as is

private IOrderMarket m_MA2CrossLE;
private IOrderMarket m_MA2CrossSE;
//left as is

public ExpAvg2Line_Cross(object ctx) :
base(ctx)
//renamed to ExpAvg2Line_Cross_LE from MovAvg2Line_Cross_LE
{
SlowLength = 18;
FastLength = 9;
}

private ISeries<double> Price { get; set; }

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

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

[Input]
public int displace { get; set; }
//added "displace" as the third input


protected override void Create(){
m_FastAverageFC = new XAverage(this);
m_SlowAverageFC = new AverageFC(this);
m_FastAvg = new VariableSeries<Double>(this);
m_SlowAvg = new VariableSeries<Double>(this);
m_MA2CrossLE =
OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, "MA2CrossLE", EOrderAction.Buy));
m_MA2CrossSE =
OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, "MA2CrossSE", EOrderAction.Sell));
}


protected override void StartCalc(){
Price = Bars.Close;
m_FastAverageFC.price = Price;
m_FastAverageFC.length = FastLength;
m_SlowAverageFC.price = Price;
m_SlowAverageFC.length = SlowLength;
}


protected override void CalcBar(){
m_FastAvg.Value = m_FastAverageFC[0];
//the below lines added to displace Exponential MA
if (((displace >= 0) || Bars.CurrentBar > Math.Abs(displace))){
if ((displace <= 0)){
if (Bars.CurrentBar > 1 && m_FastAvg.CrossesOver(m_SlowAvg, ExecInfo.MaxBarsBack)){
m_MA2CrossLE.Send();
}
else{
if (Bars.CurrentBar < 1 && m_FastAvg.CrossesUnder(m_SlowAvg, ExecInfo.MaxBarsBack)){
m_MA2CrossSE.Send();
}
}
}
}

}
}
}

christephan
Posts: 17
Joined: 04 Dec 2014
Has thanked: 2 times
Been thanked: 3 times

Re: How Can I Code This?

Postby christephan » 15 Sep 2015

There is a pre-built function called "XAverage". I think that's the EMA. You could have a look.

peachfuzz
Posts: 7
Joined: 10 Sep 2015

Re: How Can I Code This?

Postby peachfuzz » 15 Sep 2015

Thanks for the replies. Here's what I've managed to write thus far. However, I get an error on lines 50 and 51 relating to XAverage. Not sure what is wrong since XAverage, as far as I can tell, is a legit function, no? Anyway, any advice would be appreciated.

Code: Select all

using System;
using PowerLanguage.Function;

namespace PowerLanguage.Strategy
{
public class MA_Cross : SignalObject
{
private XAverage FastAverage;
private AverageFC SlowAverage;

private VariableSeries<Double> FastAvg;
private VariableSeries<Double> SlowAvg;

private IOrderMarket MA2CrossLE;
private IOrderMarket MA2CrossSE;

public MA_Cross(object ctx) :
base(ctx)
{
SlowLength = 18;
FastLength = 9;
}

private ISeries<double> Price { get; set; }

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

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

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


protected override void Create(){
FastAverage = new XAverage(this);
SlowAverage = new AverageFC(this);
FastAvg = new VariableSeries<Double>(this);
SlowAvg = new VariableSeries<Double>(this);
MA2CrossLE =
OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, "MA2CrossLE", EOrderAction.Buy));
MA2CrossSE =
OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, "MA2CrossSE", EOrderAction.Sell));
}


protected override void StartCalc(){
Price = Bars.High;
//When compiling, error shows up for the next two lines
FastAverage.price = Price;
FastAverage.length = FastLength;
SlowAverage.price = Price;
SlowAverage.length = SlowLength;
}


protected override void CalcBar(){
FastAvg.Value = FastAverage[0];
if (((displace >= 0) || Bars.CurrentBar > Math.Abs(displace))){
if ((displace <= 0)){
if (Bars.CurrentBar > 1 && FastAvg.CrossesOver(SlowAvg, ExecInfo.MaxBarsBack)){
MA2CrossLE.Send();
}
else{
if (Bars.CurrentBar < 1 && FastAvg.CrossesUnder(SlowAvg, ExecInfo.MaxBarsBack)){
MA2CrossSE.Send();
}
}
}
}

}
}
}


Return to “MultiCharts .NET”