Need help with this Multichart .Net code

Discussion in 'App Development' started by schizo, Sep 17, 2015.

  1. schizo

    schizo

    I'm just getting started with MultiCharts .Net. C# is already driving me up the wall. Be that as it may, the following code is a simple strategy that utilizes the crossover of two exponential moving averages. I think I got the codes down correct but it won't compile. Both the code and the error message is pasted below. Any pointer would be much appreciated.

    Code:
    using System;
    using PowerLanguage.Function;
    
    namespace PowerLanguage.Strategy
    {
        public class xMaxOver : SignalObject
        {
            private XAverage FastAverage;
    
            private XAverage SlowAverage;
    
            private VariableSeries<Double> FastAvg;
    
            private VariableSeries<Double> SlowAvg;
    
            private IOrderMarket EMA_Cross;
    
            public xMaxOver(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; }
    
            protected override void Create(){
                FastAverage = new XAverage(this);
                SlowAverage = new XAverage(this);
                FastAvg = new VariableSeries<Double>(this);
                SlowAvg = new VariableSeries<Double>(this);
                EMA_Cross =
                    OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, "Long", EOrderAction.Buy));
            }
    
            protected override void StartCalc(){
                Price = Bars.Close;
    
    //next 4 lines are error-prone
                FastAverage.price = Price;
                FastAverage.length = FastLength;
                SlowAverage.price = Price;
                SlowAverage.length = SlowLength;
             }
    
    
            protected override void CalcBar(){
                FastAvg.Value = FastAverage[0];
                SlowAvg.Value = SlowAverage[0];
                if (Bars.CurrentBar > 1 && FastAvg.CrossesOver(SlowAvg, ExecInfo.MaxBarsBack))
                {
                    EMA_Cross.Send();
                }
            }
        }
    }
     
  2. jharmon

    jharmon

    I don't use MultiCharts.Net, but maybe you're missing a reference file (or using an old version of one)
     
  3. Those error messages mean that the XAverage class has no public member called either "price" or "length".

    Your code snippet doesn't show the XAverage class, nor am I familiar with MultiCharts.

    However, an often used convention is that private member names start with a lowercase letter, whereas public members use an UPPERCASE letter.

    So the first thing I would try is ...
    Code:
    FastAverage.Price = Price;
    FastAverage.Length = FastLength;
    SlowAverage.Price = Price;
    SlowAverage.Length = SlowLength;
    
    If you are coding in VisualStudio (or if MultiChart's development environment incorporates Intellisense), if you delete say 'FastAverage.price = Price;', and then write 'FastAverage.' (i.e. with the dot at the end!) in its place, a list of valid options should pop up where you will also get clues as to what is allowed...
     
    Last edited: Sep 17, 2015
    nth likes this.
  4. nth

    nth

    this
     
  5. schizo

    schizo

    Thanks for the replies, folks. I modified the code as suggested but one type of error still stubbornly rears its ugly head. Any idea how this could this solved? I look forward to further enlightenment. For the time being, here's the revised code with the list of errors:


    Code:
    using System;
    using System.Drawing;
    using PowerLanguage.Function;
    
    namespace PowerLanguage.Strategy
    {
        [SameAsSymbol(true)]
        public class Exponential_Mov_Avg : SignalObject
        {
            private XAverage FastAverage;
            private XAverage SLowAverage;
         
            private VariableSeries<Double> FastAvg;
            private VariableSeries<Double> SLowAvg;
         
            private IOrderMarket MA2CrossLE;
            private IOrderMarket MA2CrossSE;
    
            public Exponential_Mov_Avg(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 XAverage(this);
                FastAvg = new VariableSeries<Double>(this);
                SLowAvg = new VariableSeries<Double>(this);
                MA2CrossLE =
                    OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, "Long", EOrderAction.Buy));
                MA2CrossSE =
                    OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, "Short", EOrderAction.Sell));
            }
    
         
            protected override void StartCalc(){
                Price = Bars.Close;
                FastAverage.Price = Price;
                FastAverage.Length = FastLength;
                 SLowAverage.Price = Price;
                SLowAverage.Length = SLowLength;
            }
    
    
            protected override void CalcBar(){
                FastAvg.Value = FastAverage[0];
                SLowAvg.Value = SLowAverage[0];
                if (Bars.CurrentBar > 1 && FastAvg.CrossesOver(SLowAvg, ExecInfo.MaxBarsBack)){
                    MA2CrossLE.Send();
                }
                else{
                    if (Bars.CurrentBar < 1 && FastAvg.CrossesUnder(SLowAvg, ExecInfo.MaxBarsBack)){
                        MA2CrossSE.Send();
                    }
                }
            }
        }
    }
     
  6. On the other side of those equations you altered you are using 'Price' (which appears to mean something else somewhere else ...), whereas you should be using 'price' ...

    so try ...

    Code:
    protected override void StartCalc(){
    price = Bars.Close;
    FastAverage.Price = price;
    FastAverage.Length = FastLength;
    SLowAverage.Price = price;
    SLowAverage.Length = SLowLength;
    }
    
     
  7. schizo

    schizo

    Thanks, bro. You're a saving grace. That set me on the right course. It finally compiled after only a very minor tweak. (At the top, I had to capitalize P, so it reads "Price=Bars.Close". ;)
     
  8. schizo

    schizo

    Okay, having finally coded the EMA, it doesn't look at all like the EMA from another platform that I use. However, their Weighted Moving Average looks pretty similar. So I got down to writing the script, but I realize it ain't so easy. One reason is that WMA doesn't seem to have the function like EMA (eg. 'XAverage'). Can anyone take a stab at this and enlighten this noob?

    From the code below, I am trying to extract the few lines that I need to substitute in my EMA script. But, again, there's no function that I can use as a replacement in the script, or so it seems.

    Code:
    //WEIGHTED MOVING AVERAGE
    
    using System;
    using System.Drawing;
    using PowerLanguage.Function;
    
    namespace PowerLanguage.Indicator
    {
        [SameAsSymbol(true)]
        public class Mov_Avg_Weighted : IndicatorObject
        {
            private VariableSeries<Double> m_avgwtd;
    
            private IPlotObject Plot1;
    
            public Mov_Avg_Weighted(object ctx) :
                base(ctx){
                length = 6;
            }
    
            private ISeries<double> price { get; set; }
    
            [Input]
            public int length { get; set; }
    
            [Input]
            public int displace { get; set; }
    
            protected override void Create(){
                m_avgwtd = new VariableSeries<Double>(this);
                Plot1 =
                    AddPlot(new PlotAttributes("AvgWtd", 0, Color.Yellow,
                                               Color.Empty, 0, 0, true));
            }
    
            protected override void StartCalc(){
                price = Bars.Close;
            }
    
    
            protected override void CalcBar(){
                if (((displace >= 0)
                     || Bars.CurrentBar > Math.Abs(displace))){
                         m_avgwtd.Value = price.WAverageCustom(length);
                    Plot1.Set(displace, m_avgwtd.Value);
                    if ((displace <= 0)){
                        if (((PublicFunctions.DoubleGreater(price[0], m_avgwtd.Value) &&
                              PublicFunctions.DoubleGreater(m_avgwtd.Value, m_avgwtd[1]))
                             && PublicFunctions.DoubleLessEquals(m_avgwtd[1], m_avgwtd[2]))){
                            Alerts.Alert("Indicator turning up");
                        }
                        else{
                            if (((PublicFunctions.DoubleLess(price[0], m_avgwtd.Value) &&
                                  PublicFunctions.DoubleLess(m_avgwtd.Value, m_avgwtd[1]))
                                 && PublicFunctions.DoubleGreaterEquals(m_avgwtd[1], m_avgwtd[2]))){
                                Alerts.Alert("Indicator turning down");
                            }
                        }
                    }
                }
            }
        }
    }
     
  9. nth

    nth

    #10     Sep 20, 2015