none
Indicator and plot for a daytrading statergy RRS feed

  • Question

  • I am new to C#

    I trying to build an indicator and put a plot on my trading chart 

    for each bar I want to calculate (Open+High+Low+Close)/4;

    Then plot the return value on my chart.


    which I would like to into the Indicator and then plot the value on the chart 


    This is the ERROR I am getting 

    The type or namespace name 'AvgPrice' does not exist in the namespace 'PowerLanguage.Function' (are you missing an assembly reference?) "AvgPrice" [Indicator] Ln 8

    I just want to be able to run the calculation on each bar and plot the value on the chart

    Thanks in advance 






    using System;
    
    namespace PowerLanguage.Function
    {
        public static class _AvgPrice
        {
            public static double AvgPrice( this IInstrument Bars, int _bb)
            {
                if (Bars.Open[_bb] > 0.0)
                {
                    return (Bars.Open[_bb] + Bars.High[_bb] + Bars.Low[_bb] + Bars.Close[_bb]) / 4;
                }
                return (Bars.High[_bb] + Bars.Low[_bb] + Bars.Close[_bb]) / 3;
            }
        }
    

    using System;
    using System.Drawing;
    
    namespace PowerLanguage.Indicator
    {
        public class PAA : IndicatorObject
        {
            private Function.AvgPrice m_AvgPrice1;
    
            public PAA(object ctx) :
                base(ctx){
                
            }
    
            protected override void Create(){
                m_AvgPrice1 = new Function.AvgPrice(this);
                
                Plot1 =
                    AddPlot(new PlotAttributes("PAA", 0, Color.Cyan,
                                               Color.Empty, 0, 0, true));
                }
    
            protected override void StartCalc(){
                m_AvgPrice1.Price = Bars.Close;
                
            }
    
    
            protected override void CalcBar(){
                m_mypaa.Value = m_AvgPrice1[0];
                Plot1.Set(0, Function._AvgPrice.AvgPrice(Bars,1));
                
    			
                    }
                }
    	}
    


    Wednesday, October 25, 2017 8:05 PM

Answers

  • Hello HICKSUNLIMITED,

    using System;
    namespace PowerLanguage.Function
    {
        public static class _AvgPrice
        {
            public static double AvgPrice( this IInstrument Bars, int _bb)
            {
                if (Bars.Open[_bb] > 0.0)
                {
                    return (Bars.Open[_bb] + Bars.High[_bb] + Bars.Low[_bb] + Bars.Close[_bb]) / 4;
                }
                return (Bars.High[_bb] + Bars.Low[_bb] + Bars.Close[_bb]) / 3;
            }
        }

    For the above code, I would say your code is defining a extension method,For more information about extend method you could see here. For my previous code , it just tell you how to use the extend method.

    private Function.AvgPrice m_AvgPrice1;

    For the above code in your second part, you have defined a class named "_AvgPrice", "AvgPrice" just is a method name and you couldn't use method name to declare a variable. And notice that static class also couldn't be declared as a variable. For more information about "static" keyword you could see here.

    For a c# newer,I suggest you get started from here.

    Sincerely,
    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 2, 2017 11:12 AM
    Moderator
  • // Working Version of needed Indicator

    using System;
    using System.Drawing;
    namespace PowerLanguage.Indicator
    {
    public class PAA_current : IndicatorObject
    {
    public PAA_current(object ctx) : base(ctx){

    }
    IPlotObject Plot1; 
    protected override void Create(){

    Plot1 =
    AddPlot(new PlotAttributes("PAA", 0, Color.Cyan,
    Color.Empty, 0, 0, true));
    }
    protected override void StartCalc(){

    }
    protected override void CalcBar(){
    Plot1.Set(0, Function._AvgPrice.AvgPrice(Bars,0));
    }
    }
    }


    Friday, November 3, 2017 11:40 AM

All replies

  • It would be much easier for those wanting to assist if you edit your post and place any source into code blocks

    And clean up your text, get rid of the codes


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Thursday, October 26, 2017 12:04 AM
    Moderator
  • Per your post I have updated my request
    Thursday, October 26, 2017 4:44 AM
  • Hello HICKSUNLIMITED

    For your first part of code, it defines an extension method of IInstrument type. Under the same namespace could you invoke the method as below.

    //I'm not sure whether IInstrument is class or interface,in this case do I assume it's class
    
                IInstrument instrument = new IInstrument();
    
                instrument.AvgPrice(23);

    >>The type or namespace name 'AvgPrice' does not exist in the namespace 'PowerLanguage.Function' (are you missing an assembly reference?)

    The error message shows that you haven't defined a 'AvgPrice' class in your Function namespace. Please notice that "AvgPrice" is a method rather than class type in your project. If you just want to invoke "AvgPrice" method under PowerLanguage.Indicator namespace, your code should be like this.

    //Because you modify _AvgPrice type as static,you could invoke AvgPrice method by class name.
    
    Function._AvgPrice.AvgPrice(new Function.IInstrument(),23);

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 26, 2017 8:20 AM
    Moderator
  • Hello HICKSUNLIMITED,

    Is there anything updated?

    If you have any issues about the replies , please feel free to contact us. And if you are satisfied with the reply, you could close the thread by marking  as answer, This can be beneficial to other community members reading this thread. Thanks for your contribution to the forum.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, October 29, 2017 5:18 AM
    Moderator
  • Thanks for the answer but I don't know how to implement the needed changes to this Code 

    I will attempt to make the changes 

    Thanks 

    Wednesday, November 1, 2017 11:50 AM
  • Please explain (23) in your statement

    Response 01

    //I'm not sure whether IInstrument is class or interface,in this case do I assume it's class
                IInstrument instrument = new IInstrument();
                instrument.AvgPrice(23);

     are you saying to make the following changes

    {

            public static double AvgPrice( this IInstrument Bars, int _bb)

            {

            To

            public static double AvgPrice IInstrument instrument = new IInstrument();
                instrument.AvgPrice(23);

    Responses 02;

    Are you saying you want me to make this  changes

    protected override void Create(){

                m_AvgPrice1 = new Function.AvgPrice(this);

               

    to

    protected override void Create(){

                Function._AvgPrice.AvgPrice(new Function.IInstrument(),23);

    Please advise 


    Wednesday, November 1, 2017 12:15 PM
  • Hello HICKSUNLIMITED,

    using System;
    namespace PowerLanguage.Function
    {
        public static class _AvgPrice
        {
            public static double AvgPrice( this IInstrument Bars, int _bb)
            {
                if (Bars.Open[_bb] > 0.0)
                {
                    return (Bars.Open[_bb] + Bars.High[_bb] + Bars.Low[_bb] + Bars.Close[_bb]) / 4;
                }
                return (Bars.High[_bb] + Bars.Low[_bb] + Bars.Close[_bb]) / 3;
            }
        }

    For the above code, I would say your code is defining a extension method,For more information about extend method you could see here. For my previous code , it just tell you how to use the extend method.

    private Function.AvgPrice m_AvgPrice1;

    For the above code in your second part, you have defined a class named "_AvgPrice", "AvgPrice" just is a method name and you couldn't use method name to declare a variable. And notice that static class also couldn't be declared as a variable. For more information about "static" keyword you could see here.

    For a c# newer,I suggest you get started from here.

    Sincerely,
    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 2, 2017 11:12 AM
    Moderator
  • // Working Version of needed Indicator

    using System;
    using System.Drawing;
    namespace PowerLanguage.Indicator
    {
    public class PAA_current : IndicatorObject
    {
    public PAA_current(object ctx) : base(ctx){

    }
    IPlotObject Plot1; 
    protected override void Create(){

    Plot1 =
    AddPlot(new PlotAttributes("PAA", 0, Color.Cyan,
    Color.Empty, 0, 0, true));
    }
    protected override void StartCalc(){

    }
    protected override void CalcBar(){
    Plot1.Set(0, Function._AvgPrice.AvgPrice(Bars,0));
    }
    }
    }


    Friday, November 3, 2017 11:40 AM