Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unable to close a position through an automated strategy in SIM account

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

    Unable to close a position through an automated strategy in SIM account

    Hello,

    I am in a SIM account and would like for an open position to close automatically without my manual intervention, if my unrealized PNL is $25.00 or less. It's my first attempt and I have found following code, that actually compiles but doesn't do anything when PNL is less than or equal to $25.00.

    Here is my strategy code that I am attaching to a chart before I open a position:

    protected override void OnAccountItemUpdate(Cbi.Account account, Cbi.AccountItem accountItem, double value)
    {

    Account a = Account.All.First(t => t.Name == "Sim101");
    double unrealized = Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]);

    if(State == State.Terminated) return;

    if (unrealized <= 25.00)
    {

    if(Position.MarketPosition == MarketPosition.Long)
    {
    ExitLong(Position.Quantity);
    }
    if(Position.MarketPosition == MarketPosition.Short)
    {
    ExitShort(Position.Quantity);
    }
    SetState(State.Terminated); return;

    }
    }

    Is it possible to close an open position via code? I don't put bracket orders and would like to create strategies for automatically taking profit and losses. Thanks

    John​

    #2
    Hello John,

    Thank you for your post.

    It is certainly possible to identify an open position and close it via code. It looks like you are looking to access a position that was opened manually, so your approach of using the Account class is the way to go.

    As for the script compiling successfully but not behaving as expected when you run it, I recommend adding Print statements to understand the behavior of your script. It can help to print the time and any values/variables that are used in your script's conditions and/or actions to see how the logic is being evaluated. For more information about using Prints to debug:


    That being said, it may also be helpful to review the snippets in the help guide, such as the one on the following page for accessing the Positions collection in the Account class:


    For more information about the different methods and properties in the Account class in general, please see the following section of the help guide:


    The AddOn Framework NinjaScript Basic file found on the AddOn development overview page also shows the Account class being used in a script:


    Please let us know if we may be of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    89 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    135 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    119 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    69 views
    0 likes
    Last Post PaulMohn  
    Working...
    X