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

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    81 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    149 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    79 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    52 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    59 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X