Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Variable Declaration?

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

    Variable Declaration?

    I would like to only access a variable if market position is flat. When I code the following I get the error message "Embedded statement cannot be a declaration or labeled statement."

    protected override void OnBarUpdate()
    {
    if(Position.MarketPosition==MarketPosition.Flat)
    double valuex = EMA(20);

    //other random code//
    }

    Any ideas how to fix?
    Thanks
    GT

    #2
    double valuex = EMA(20)[0];
    RayNinjaTrader Customer Service

    Comment


      #3
      OK that was pretty dumb by me. Thanks for the quick fix. But what about declaring a variable with an equation? i.e.

      protected override void OnBarUpdate()
      {
      //various entry logic

      if(Position.MarketPosition==MarketPosition.Long)
      double LongExit = ((Position.AvgPrice - MIN(Low, 20)[BarsSinceEntry)*3))+Position.AvgPrice;
      SetProfitTarget(CalculationMode.Price, LongExit);
      }

      1) I am receiving the same error message listed in the earlier post. Any thoughts?
      2) Do I need to use OnPositionUpdate for the profit target to begin working immediately upon entry or does the Position.MarketPosition==MarketPosition.Long accomplish that?
      Thanks.

      GT

      Comment


        #4
        double LongExit = ((Position.AvgPrice - MIN(Low, 20)[BarsSinceEntry]*3))+Position.AvgPrice;
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Two obvious typos in a row. Sorry and thanks. I was still getting the same error message until I "fixed" the code by adding brackets (shown below) but now the profit target always keeps the value from the initial long position on the chart which a lot of the time is way under the market. So when it enters a long position it exits on the same bar way under the actual market.

          if(Position.MarketPosition==MarketPosition.Long)
          {
          double LongExit = ((Position.AvgPrice - MIN(Low,20)[BarsSinceEntry()] * 3)) + Position.AvgPrice;
          SetProfitTarget(CalculationMode.Price, LongExit);
          }

          How do I reset/recalculate the profit target when I enter a new long position? Thanks.

          GT

          Comment


            #6
            You could add at the top of OnBarUpdate() something like:

            Code:
            if (Position.MarketPosition == MarketPosition.Flat)
                SetProfitTarget(CalculationMode.Ticks, someReasonableOffsetValue);
            Then once you have entered your position, call the method again and set the specific price you want.
            RayNinjaTrader Customer Service

            Comment


              #7
              Thanks again for the quick reply. When I try putting the code in the top of the OnBarUpdate() section, once the position is entered the profit target never recalculates with the code under if(Position.MarketPosition==Market.Position==Long) . I also took the variable LongExit out and added the calculation code into the SetProfitTarget line. If I leave the suggested code out it calculates correctly for only the first position. If I leave it in it never recalculates. Any more ideas on how to get this to recalculated for each new position? Code now looks like this:

              if(Position.MarketPosition==MarketPosition.Long)
              SetProfitTarget(CalculationMode.Price, ((Position.AvgPrice - MIN(Low,20)[BarsSinceEntry()] * 3)) + Position.AvgPrice) ;
              Thanks again,
              GT

              Comment


                #8
                You likely will have to debug your code. Here is a reference sample that illustrates the basic concepts.

                RayNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                576 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                334 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                553 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                551 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X