• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 EnveousColnago, Yesterday, 05:43 PM
    7 responses
    34 views
    0 likes
    Last Post Lancer
    by Lancer
     
    Started by PeterGolz, 01-17-2020, 10:42 AM
    11 responses
    72 views
    0 likes
    Last Post reubenlasky  
    Started by utilizator, Today, 11:36 AM
    0 responses
    4 views
    0 likes
    Last Post utilizator  
    Started by EnveousColnago, Today, 07:24 AM
    1 response
    11 views
    0 likes
    Last Post NinjaTrader_ShawnB  
    Started by tornadoatc, Today, 10:31 AM
    0 responses
    4 views
    0 likes
    Last Post tornadoatc  
    Working...
    X