Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

sequential conditions to define a strategy

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

    sequential conditions to define a strategy

    I am just starting as a programmer using C# in Ninja Script and was wondering, what is the best way for creating a sequence of conditions that must be verified to.... eg: automatically close an open long order. The logic could be something like this:

    In order to close a long order I want that:
    1) First and foremost a long order must be open
    2) RSI Crossed above 70
    3) RSI Crossed below 70
    4) RSI Crossed above 70.

    For 2) to happen, 1) must have happened first;
    for 3) to happen, 2) must have happened before;
    for 4) to happen, 3) must have happened before;

    Only when the 4 conditions happen the action of closing the order is triggered.

    I've tried to create a logic using bool variables for each step that I activate / deactivate but it doesn't seem to work like I was expecting.
    Is there any more elegant way of doing this?

    SideNote: This is a simplified silly example and not an actually strategy that I tested; All i want is understand the logic of how would be an elegant and effective way of programming it.

    Thanks in advance for all your help,

    #2
    Hi Danilod, thanks for posting.

    You will use boolean variables to do this. The process would be: Calculate, OnBarClose: Check the first condition, if true, set first boolean to true then return; from OnBarUpdate. The code structure would be:

    Code:
    bool flag1, flag2 = false;
    
    protected override void OnBarUpdate()
    {
    if(!flag1 && Some Condition1)
    {
    flag1 = true;
    return;
    }
    if(!flag2 && flag1 && Some Condition2)
    {
    flag2 = true;
    //do something here, or retrun and continue the process further. 
    }
    }
    If the script is running OnEachTick or OnPriceChange, some additional logic would be needed to track the CurrentBar property to make the conditions wait 1 bar after checking the condition.

    Kind regards,
    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your help. And one more question. I read about how
      Code:
      State.SetDefaults
      IsInstantiatedOnEachOptimizationIteration = false;
      could speed up optimization process. Do you think this could have any implication on the result of the code like the one you proposed. I am wondering if the bool variables will be reseted on each iteration.

      Thanks.

      Comment


        #4
        Hi Danilod, thanks for your reply.

        You would need to make sure the booleans are reset back to their original value within State.SetDefault or State.Configure.

        Kind regards,
        -ChrisL
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by GussJ, 03-04-2020, 03:11 PM
        11 responses
        3,221 views
        0 likes
        Last Post xiinteractive  
        Started by andrewtrades, Today, 04:57 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by chbruno, Today, 04:10 PM
        0 responses
        7 views
        0 likes
        Last Post chbruno
        by chbruno
         
        Started by josh18955, 03-25-2023, 11:16 AM
        6 responses
        438 views
        0 likes
        Last Post Delerium  
        Started by FAQtrader, Today, 03:35 PM
        0 responses
        9 views
        0 likes
        Last Post FAQtrader  
        Working...
        X