Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

how to update a variable !!

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

    how to update a variable !!

    Hello,
    I created a method to calculate a range. The value of this range is used to calculate the Exit.
    The problem is that the value of the Range does not remain stationary, but varies with each candle. How can I do to make it stay fixed?
    Example:

    # Region OrderRouting //Region to manage the position Long
    private void ManagerOrder ()

    if (Position.MarketPosition == MarketPosition.Long)
    {
    if ( High [0]> Position.AvgPrice + RangeLong () ) the value of the Range is not that calculated when EnterLong, changes each candle
    }




    private double RangeLong () //method to calculate the Range

    return (MAX (HIGH, 5) [0]-MIN (Low, 10) [5]);




    protected override void OnBarUpdate ()
    RangeLong ();
    if (Low [0] <MAX (High, 5) [0] - RangeLong ())
    EnterLong



    If, instead, I try to do this:

    RangeLong () = VariableM
    the value of variableM becomes equal to the value of the Range, remains as the initial default (1).
    How can I do?
    Thanks.
    Hello.
    Roberto

    #2
    Hello Roberto,

    You are working with programming language so there is not just one answer here.

    One approach is using a bool flag to force the calculation only once. You set the bool flag to the opposite of what you check for.

    In your code, it will most likely be simpler to move away from the custom method and capture the value you want after certain conditions.

    Code:
     
    if (Position.MarketPosition == MarketPosition.Long && callOnce)
    {
    double myRangeLong = (MAX (HIGH, 5) [0] - MIN (Low, 10) [5]);
    callOnce = false;
    }
     
    if ( High [0]> Position.AvgPrice + myRangeLong)
    //exit here
     
    if (Position.MarketPosition == MarketPosition.Flat) //resets your bool flag when flat
    callOnce = true;
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      thanks RyanM,
      now I try, I'll let you know.
      Ciao.
      Roberto

      Comment


        #4
        Hello RyanM,
        I am attaching the code where I made the changes, the value is always the default (1). and for the profit target for the management of Exit not updated.
        If you can help me I would be grateful because they are blocked by two weeks on this point.
        Thanks.
        Ciao.
        Roberto
        Attached Files

        Comment


          #5
          Roberto, we could unfortunately not code or debug this for you - if you look to get this custom coded for you a certified NinjaScript consultant would be a potential route to choose -

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          601 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          347 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          103 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          559 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          558 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X