Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How do i get current market position in OnStateChange?

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

    How do i get current market position in OnStateChange?

    In OnStateChange I am trying to print the current market position like this:

    PHP Code:
    } else if (State == State.DataLoaded) {
        if (Position == null) {
            Print(" Position is null");
        } else {
            Print(" Position is "+Position);
        }
        Print(" market position is ");
        Print(Position.MarketPosition);
    } 
    
    This is telling me that Position is null and it throws an error:

    Strategy 'SimpleTestStrategy': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.

    So how can I get the initial position of a strategy? Do I just always assume it is Flat?

    #2
    Hello westofpluto, thanks for writing in.

    You would need to check this property in either State.Historical or State.Transition:

    Add these the If-Else chain in OnStateChanged:

    Code:
    else if(State == State.Transition)
    {
        // if(Position != null)
        // {
            // Print(Position.MarketPosition); // Transistion from historical to real time. 
        // }
    }
    else if(State == State.Historical)
    {
        if(Position != null)
        {
            Print(Position.MarketPosition); //Start of the historical data
        }
    }
    Please let me know if I can assist any further.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    80 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    40 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    63 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    63 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    54 views
    0 likes
    Last Post CarlTrading  
    Working...
    X