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 NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    58 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    133 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    73 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    50 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X