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

Edit Strategy Dialog and State.Terminated

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

    Edit Strategy Dialog and State.Terminated

    I call Account.Flatten (for the instrument of the Strategy) in OnStateChange when it is State.Terminated

    it works fine for the strategy. When I disable the Strategy, the position is flattened.

    But, I did some debugging and it is also the cause of a bug where my changes when I choose "Edit Strategy" from the Strategies tab are lost. I cannot change any properties of a strategy with Edit Strategy unless I comment out this code.

    So I'm wondering what I need to do to make it safe for Edit Strategy

    My code is :

    else if (State == State.Terminated) {
    // instantiate a list of instruments
    Collection<Cbi.Instrument> instrumentsToCancel = new Collection<Instrument>();

    // add instruments to the collection
    instrumentsToCancel.Add(Instrument);

    // pass the instrument collection to the Flatten() method to be flattened
    Account.Flatten(instrumentsToCancel);
    }

    #2
    Hello NinjaCustomer,

    Thank you for the post.

    While I am not able to see the problem you described this code you have provided is not in the right format to be used from State.Terminated. If I had to guess, I would say you are likely getting an error in the log tab when configuring the strategy, possibly a null object reference, likely from the Account or Instrument objects.

    One problem here is that this code will be called every time State.Terminated is called, this will cause problems because that state is called when you open the strategies menu. This is called for many reasons besides the time you specifically disabled your strategy so you will need some logic to know the strategy was actually started before you try to execute this. I suggest adding a Print into State.Terminated to see when this is called when you do various actions to better understand OnStateChange in this context.

    You could use a bool for this like the following:

    Code:
    private bool wasLoaded;
    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            wasLoaded = false;    
        }
        else if (State == State.DataLoaded)
        {
            wasLoaded = true;
        }
        else if (State == State.Terminated)
        {
            if(wasLoaded){
                Collection<Cbi.Instrument> instrumentsToCancel = new Collection<Instrument>(); 
    
                // add instruments to the collection
                instrumentsToCancel.Add(Instrument); 
    
                // pass the instrument collection to the Flatten() method to be flattened
                Account.Flatten(instrumentsToCancel);
            }
        }
    }
    This makes sure the strategy was enabled. if it was not enabled (like opening the strategy menu) your flatten would not be run. Could you try this on your end and see if your still having trouble as you had described? If you are still having trouble, could you export the script or a sample which shows the problem that we can test together?

    I look forward to being of further assistance. s

    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by WeyldFalcon, 12-10-2020, 06:48 PM
    14 responses
    1,426 views
    0 likes
    Last Post Handclap0241  
    Started by Barry Milan, Today, 10:35 PM
    0 responses
    3 views
    0 likes
    Last Post Barry Milan  
    Started by DJ888, Yesterday, 06:09 PM
    2 responses
    9 views
    0 likes
    Last Post DJ888
    by DJ888
     
    Started by jeronymite, 04-12-2024, 04:26 PM
    3 responses
    40 views
    0 likes
    Last Post jeronymite  
    Started by bill2023, Today, 08:51 AM
    2 responses
    16 views
    0 likes
    Last Post bill2023  
    Working...
    X