Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Referring to MFE in automated trading startegy

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    #16
    Originally posted by pandyav View Post
    You're awesome. Thank you.

    Now the last thing that I want to do is to display the HighestGain in form of a bar below the chart. Do you know what kind of code I can use to display simple bar chart for HighestGain calculated per your code?
    Technically, NT does not allow Strategies to communicate with Indicators, but it is all just C#, so conceptually, you just need the strategy to update a double value in the indicator, and use that value to set the Plot. You would perform the update through the publicly exposed properties of the double, using the setter.

    A variation on the theme is actually available in the Samples subForum. ref: http://www.ninjatrader.com/support/f...ead.php?t=6651
    Last edited by koganam; 01-02-2014, 05:12 PM.

    Comment


      #17
      Hello pandyav,

      If you wanted to change the Plot Style you may do something like the following:

      StrategyPlot(0).Plots[0].PlotStyle = NinjaTrader.Gui.Chart.PlotStyle.Bar;
      JCNinjaTrader Customer Service

      Comment


        #18
        Originally posted by NinjaTrader_JC View Post
        Hello pandyav,

        If you wanted to change the Plot Style you may do something like the following:

        StrategyPlot(0).Plots[0].PlotStyle = NinjaTrader.Gui.Chart.PlotStyle.Bar;
        It is a Strategy: there is no Plot to change.

        Comment


          #19
          Hello koganam,

          It is changing the Plot Style of the StrategyPlot Indicator just like changing the color. The reason why I referenced it like that is because since PlotStyle is a Indicator Object you cannot simply use PlotStyle.Bar to reference this is all.
          JCNinjaTrader Customer Service

          Comment


            #20
            Originally posted by NinjaTrader_JC View Post
            Hello koganam,

            It is changing the Plot Style of the StrategyPlot Indicator just like changing the color. The reason why I referenced it like that is because since PlotStyle is a Indicator Object you cannot simply use PlotStyle.Bar to reference this is all.
            Oops. Did not see that you were referencing StrategyPlot in the first place. Mea culpa.

            Comment


              #21
              Hi Koganam,
              I took the original code you had provided to identify the max profit of a trade, but I keep receiving the compiling errors.

              Here is what I have under OnBarUpdate:

              if (Position.MarketPosition == MarketPosition.Long)
              {
              double CurrentBarHighestGain = High[0] - Position.AvgPrice;
              }

              if (Position.MarketPosition == MarketPosition.Short)
              {
              double CurrentBarHighestGain = Position.AvgPrice-Low[0];
              }
              double HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);

              For some reason, I received the following errors:
              - Use of unassigned local variable 'HighestGain'
              - The name 'CurrentBarHighestGain' does not exist in the current context

              Can you please help to understand what could be wrong? I have not made any changes except trying to calculate HighestGain for both long and short sides.

              Any ideas what could be wrong here?

              Very much appreciate your help Koganam.

              Comment


                #22
                Originally posted by pandyav View Post
                Hi Koganam,
                I took the original code you had provided to identify the max profit of a trade, but I keep receiving the compiling errors.

                Here is what I have under OnBarUpdate:

                if (Position.MarketPosition == MarketPosition.Long)
                {
                double CurrentBarHighestGain = High[0] - Position.AvgPrice;
                }

                if (Position.MarketPosition == MarketPosition.Short)
                {
                double CurrentBarHighestGain = Position.AvgPrice-Low[0];
                }
                double HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);

                For some reason, I received the following errors:
                - Use of unassigned local variable 'HighestGain'
                - The name 'CurrentBarHighestGain' does not exist in the current context

                Can you please help to understand what could be wrong? I have not made any changes except trying to calculate HighestGain for both long and short sides.

                Any ideas what could be wrong here?

                Very much appreciate your help Koganam.
                Quite to the contrary, you have made multiple changes that are causing your issues.

                HighestGain was defined as a class variable. You have changed it to a local variable that you tried to use before you initialized it. If it is to survive each tick update, it must be declared as a class variable. Moreover, the very act of declaring it as a class variable, would have initialized it, yet I specifically initialized it for redundancy.

                Your CurrentBarHighestGain is in each case defined as a local variable, delimited by your scope braces. You can do what you are trying to do, but in that case declare and initialize the variable before and outside any of the blocks that will use it, and just assign a value in your position filter blocks. As the positions are mutually exclusive, you can use the same variable for both sides, just as you have used it now for both sides. You just have to declare it with proper scope.

                OTOH, if you really prefer to use your current scheme, where CurrentBarHighestGain is a local variable, then you want to calculate HighestGain in the same block. HighestGain will still have to be declared with class scope.

                Comment


                  #23
                  Thanks Koganam,
                  Trying my best to follow your advise here. This is what I have now:

                  Variables:
                  private double HighestGain = 0;
                  OnBarUpdate:

                  if (Position.MarketPosition == MarketPosition.Long)
                  {
                  double CurrentBarHighestGain = High[0] - Position.AvgPrice;
                  double HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);
                  }

                  if (Position.MarketPosition == MarketPosition.Short)
                  {
                  double CurrentBarHighestGain = Position.AvgPrice-Low[0];
                  double HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);
                  }

                  I get the following error message: Use of unassigned local variable 'HighestGain'

                  At least I was able to remove one of the prior error messages. This time I've put the calculation of HighestGain within the same block as CurrentBarHighestGain. Just now sure what else is wrong?

                  Any thoughts?

                  Comment


                    #24
                    Originally posted by pandyav View Post
                    Thanks Koganam,
                    Trying my best to follow your advise here. This is what I have now:

                    Variables:
                    private double HighestGain = 0;
                    OnBarUpdate:

                    if (Position.MarketPosition == MarketPosition.Long)
                    {
                    double CurrentBarHighestGain = High[0] - Position.AvgPrice;
                    double HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);
                    }

                    if (Position.MarketPosition == MarketPosition.Short)
                    {
                    double CurrentBarHighestGain = Position.AvgPrice-Low[0];
                    double HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);
                    }

                    I get the following error message: Use of unassigned local variable 'HighestGain'

                    At least I was able to remove one of the prior error messages. This time I've put the calculation of HighestGain within the same block as CurrentBarHighestGain. Just now sure what else is wrong?

                    Any thoughts?
                    You want to use the class variable for holding the HighestGain, as it must survive calls to the event handler. You are again trying to declare it as local. Remove the attempt to declare it again as local: remove double from in front of it.

                    Comment


                      #25
                      So I was able to compile the code by removing double. The next task I took on was to plot the values of HighestGain in a form of a bar by using the sample example link you forwarded earlier.

                      This is what I have
                      Initialize:

                      Add(StrategyPlot(0));
                      Add(StrategyPlot(1));

                      // Set the color for the indicator plots
                      StrategyPlot(0).Plots[0].Pen.Color = Color.Blue;
                      StrategyPlot(1).Plots[0].Pen.Color = Color.YellowGreen;
                      StrategyPlot(0).Plots[0].PlotStyle = PlotStyle.Bar;
                      StrategyPlot(1).Plots[0].PlotStyle = PlotStyle.Bar;
                      StrategyPlot(0).Plots[0].Pen.Width = 4;
                      StrategyPlot(1).Plots[0].Pen.Width = 4;

                      // Set the panel which the plots will be placed on. 1 = price panel, 2 = panel under the price panel, etc.
                      StrategyPlot(0).PanelUI = 2;
                      StrategyPlot(1).PanelUI = 2;

                      OnBarUpdate
                      StrategyPlot(0).Value.Set(HighestGain);
                      StrategyPlot(1).Value.Set(HighestGain);

                      As you can see in the attached chart, all I get is a single value of HighestGain and it does not change as the position moves in my favor. Should I be placing StrategyPlot(0) and (1) in some different fashion?

                      What am I missing here? Thank you again for all your help so far.
                      Attached Files

                      Comment


                        #26
                        Originally posted by pandyav View Post
                        So I was able to compile the code by removing double. The next task I took on was to plot the values of HighestGain in a form of a bar by using the sample example link you forwarded earlier.

                        This is what I have
                        Initialize:

                        Add(StrategyPlot(0));
                        Add(StrategyPlot(1));

                        // Set the color for the indicator plots
                        StrategyPlot(0).Plots[0].Pen.Color = Color.Blue;
                        StrategyPlot(1).Plots[0].Pen.Color = Color.YellowGreen;
                        StrategyPlot(0).Plots[0].PlotStyle = PlotStyle.Bar;
                        StrategyPlot(1).Plots[0].PlotStyle = PlotStyle.Bar;
                        StrategyPlot(0).Plots[0].Pen.Width = 4;
                        StrategyPlot(1).Plots[0].Pen.Width = 4;

                        // Set the panel which the plots will be placed on. 1 = price panel, 2 = panel under the price panel, etc.
                        StrategyPlot(0).PanelUI = 2;
                        StrategyPlot(1).PanelUI = 2;

                        OnBarUpdate
                        StrategyPlot(0).Value.Set(HighestGain);
                        StrategyPlot(1).Value.Set(HighestGain);

                        As you can see in the attached chart, all I get is a single value of HighestGain and it does not change as the position moves in my favor. Should I be placing StrategyPlot(0) and (1) in some different fashion?

                        What am I missing here? Thank you again for all your help so far.
                        Without knowing exactly where you placed your code, it would not be possible to determine the exact cause of the issue here. You will want to Print() out the values that you are trying to use, in order to be sure that the code is doing what you want it to do. Print() will send output to the Output Window.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by owensd, 04-21-2024, 11:34 PM
                        9 responses
                        34 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by trilliantrader, 04-10-2024, 09:33 PM
                        7 responses
                        25 views
                        0 likes
                        Last Post NinjaTrader_BrandonH  
                        Started by traderqz, Today, 12:06 AM
                        5 responses
                        11 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by Mongo, Today, 11:05 AM
                        2 responses
                        10 views
                        0 likes
                        Last Post Mongo
                        by Mongo
                         
                        Started by guillembm, Today, 11:25 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post guillembm  
                        Working...
                        X