Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Account Size - Safety Measure

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

    Account Size - Safety Measure

    I'm looking to put a stop-gap safety net on my strategy. Something that will determine my account status, like if the account has lost 10% or something, flatten all trades (and make a martini). Could you point me in the right direction? Also, AccountSize() I see in the docs, but no info. Maybe that's what I need?

    #2
    imported post

    We actually do not support account level properties in the NinjaScript layer at this time, however, I can see the value in what you ask. The code sample belows dives into our framework layer. This should do the trick but double check in testing on your live account. You will have to self calculate what the % loss value is against a user defined account cash balance. Mostbroker API's do not provide cash balance so its safer to enter the starting cash balance of your account when running the strategy. What I have provided below is conceptual.

    Declare in variables section:

    private bool safetyStop = false;

    In OnBarUpdate()

    // If flat and safety stop is hit, stop process strategy logic
    if (safetyStop && Position.MarketPosition == MarketPosition.Flat)
    return;

    // In real-time only and the safety stop not yet hit
    if (!Historical && !safetyStop)
    {
    // Check the account realized PnL and if the max loss exceeded
    if (((AccountItem) Account.GetAccountItem(Cbi.AccountItemType.Realize dProftLoss, Cbi.Currency.Unknown)).Value < -100);
    {
    // Close any open position
    safetyStop = true;
    ExitLong();
    ExitShort();
    }
    }
    Ray


    RayNinjaTrader Customer Service

    Comment


      #3
      imported post

      Cool, understood will check it out. Thanks. -> So, what is that AccountSize() I see in the docs?

      Comment


        #4
        imported post

        That sets a user defined account balance. This can be used to dynamically generate position size. See the Help Guide for more granularity.

        Ray
        RayNinjaTrader Customer Service

        Comment


          #5
          imported post

          I ammended the sample code below to just exit the OnBarUpdate() method as a suggested approach to prevent your strategy from running when account threshholds are breached.

          Ray
          RayNinjaTrader Customer Service

          Comment


            #6
            Does this work in NinjaTrader 6.5 and was this an example from inside a NinjaScript? I am wondering why I could not use a similar method of ((AccountItem) Account.GetAccountItem(Cbi.AccountItemType.BuyingP ower, Cbi.Currency.Unknown)).Value to get BuyingPower in the same way?

            Comment


              #7
              It likely works in 6.5 but as this thread stated, this is in our framework layer and is actually outside the scope of what we provide support for.
              RayNinjaTrader Customer Service

              Comment


                #8
                @Ray, thanks for the reply. I understand this is not formally supported, however this information would be very useful to a number of people on these forums (just from the searches I have done) and it seems you have had to answer this inquiry many times. So if there was an unsupported way (via the Framework) to obtain this information that may work (but still of course not be formally supported) I think there are many that would appreciate it. I will reply to this thread if I am able to make it work myself, as of yet, no luck

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by jclose, Today, 09:37 PM
                0 responses
                4 views
                0 likes
                Last Post jclose
                by jclose
                 
                Started by WeyldFalcon, 08-07-2020, 06:13 AM
                10 responses
                1,413 views
                0 likes
                Last Post Traderontheroad  
                Started by firefoxforum12, Today, 08:53 PM
                0 responses
                9 views
                0 likes
                Last Post firefoxforum12  
                Started by stafe, Today, 08:34 PM
                0 responses
                10 views
                0 likes
                Last Post stafe
                by stafe
                 
                Started by sastrades, 01-31-2024, 10:19 PM
                11 responses
                169 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Working...
                X