Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Wonder if this is possible adding conditions

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

    Wonder if this is possible adding conditions

    Hi,
    I have this idea and don't know how or if it is even possible:
    (The following isn't code but my attempt at writing it down and helping visualise/explain it)
    if
    Close[x]>Close[X+1]
    golong

    example
    x=5
    Close[5]>Close[5+1] && Close[4]>Close[4+1] && Close[3]>Close[3+1] && Close[2]>Close[2+1] && Close[1]>Close[1+1] && Close[0]>Close[0+1]

    maybe?
    if x != 0?
    {
    Close[x]>Close[x+1]
    something with ++?
    }


    It's a long shot, as I said earlier don't know how I would go about it maybe it's fairly simple I dont know, basicly having an optimizable variable but instead of just saying Close[0]>Close[variable] having new conditions with && depending on the variable.

    btw maybe it is clear but I'm fairly new to this 1-2weeks
    Hope the explanation is clear enough, any help is appreciated,
    Carl

    #2
    Hello Cavaro,

    Thank you for your note.

    If you just want to compare close prices of the last x bars, you could make a loop and use a variable to control how many bars are looped over.

    Code:
    // in OnStateChange State.SetDefaults
    LookbackBars = 5;
    
    // OnBarUpdate
    protected override void OnBarUpdate()
    {
    // ensure we have enough bars to lookback
    if (CurrentBar < LookbackBars +1)
    return;
    
    // loop through bars ago indexes and check close prices
    for (int x = LookbackBars; x >= 0; x--)
    {
    // if the close is not greater than the close prior, stop the loop
    if (Close[x] < Close[x+1])
    return;
    // if we get to x = 0 and the loop hasn't been exited, enter long
    if (x == 0)
    {
    EnterLong();
    }
    
    }
    }
    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="LookbackBars", Order=1, GroupName="Parameters")]
    public int LookbackBars
    { get; set; }
    #endregion
    If you wanted to have different conditions for entry based on a variable that can be optimized, you could consider doing something like if (MyVar ==1) else if (MyVar == 2) etc and then putting further conditions dependent on those for entry. You could then optimize MyVar so the strategy would choose different code paths based on MyVar.

    For example

    Code:
    // in State == State.SetDefaults
    MyVar =3;
    
    
    protected override void OnBarUpdate()
    {
    // other code omitted, if you are looking back to prior bars for conditions you will need to do a CurrentBar check first
    
    if (MyVar == 1)
    {
    // conditions and actions for path 1 go here
    }
    else if (MyVar == 2)
    {
    // conditions and actions for path 2 go here
    }
    else if (MyVar == 3)
    {
    // conditions and actions for path 3 go here
    }
    }
    
    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="MyVar", Order=1, GroupName="Parameters")]
    public int MyVar
    { get; set; }
    #endregion
    Please let us know if we may be of further assistance to you.

    Comment


      #3
      Hi Kate,
      That makes a lot of sense, works for me .
      appreciate the help,
      kind regards
      Carl

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      65 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      139 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      75 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X