Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Extract info from Risk Template to a strategy

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

    Extract info from Risk Template to a strategy

    Hey,

    I want to extract info from RIsk template (Everything, like initial margin, maintenance margin, max order, etc.) to a strategy and use that info first for backtest and optimization. How can I do that? I searched help guide, but no formulas were provided.
    Attached Files

    #2
    Hello UltraNIX,

    You could use the following undocumented code to find a template by name and then iterate over the master instruments in the given template.

    Code:
    Risk myRiskTemplate = Risk.Get("templatename");
    
    foreach(KeyValuePair<MasterInstrument, InstrumentRisk> template in myRiskTemplate.ByMasterInstrument)
    {
        //The key is a Master Instrument object and the Value is the risk template for the given master instrument
        Print(template.Key.Name + " " + template.Value.InitialMargin);
    }

    You can use the NinjaScript editors intelleprompt to explore the template.Value object, that is a InstrumentRisk object type which is not documented.

    Comment


      #3
      Can I specify directly the instument name, like ES, MES or MNQ without looping?

      Comment


        #4
        Hello UltraNIX,

        Because the key is an object and not a string it would be difficult to find a dictionary item without looping in this case. You could use any C# LINQ methods to target looping logic which may be less code but still requires looping. the linq would honestly be less easy to read than a simple if condition in this situation.

        This loop would break when it found the instrument and has error checking after the loop, this would be a simple way to get the template and not iterate every instrument. .

        Code:
        Risk myRiskTemplate = Risk.Get("");
        InstrumentRisk riskTemplate = null;
        foreach (KeyValuePair<MasterInstrument, InstrumentRisk> template in myRiskTemplate.ByMasterInstrument)
        {
            var masterInstrument = template.Key;
           if (masterInstrument.Name == "ES")
           {
              riskTemplate = template.Value;
              break;
           }
        }
        
        if (riskTemplate != null)
        {
           Print(riskTemplate.MaintenanceMargin);
        }

        Comment


          #5
          UltraNIX I have found something like this works.
          Code:
                  if (myAccount.Risk == null)
                  {
                      Print("No Risk Template for account");
                  }
                  else
                  if (!(myAccount.Risk.ByMasterInstrument.ContainsKey(myInstInstrument.MasterInstrument))
                  {
                      Print("Risk Template does not contain instrument");
                  }
                  else
                  {
                      InstrumentRisk InstRisk = null;
                      if (!(myAccount.Risk.ByMasterInstrument.TryGetValue(myInstInstrument.MasterInstrument,out InstRisk)))
                      {
                          Print("Could not obtain information from Risk Template");
                      }
                      else
                      {
                          double buyIntradayMargin = InstRisk.BuyIntradayMargin;   // For example
                      }
                  }
          Hope it helps.

          Thanks.
          Multi-Dimensional Managed Trading
          jeronymite
          NinjaTrader Ecosystem Vendor - Mizpah Software

          Comment


            #6
            Originally posted by NinjaTrader_Jesse View Post
            Hello UltraNIX,

            Because the key is an object and not a string it would be difficult to find a dictionary item without looping in this case. You could use any C# LINQ methods to target looping logic which may be less code but still requires looping. the linq would honestly be less easy to read than a simple if condition in this situation.

            This loop would break when it found the instrument and has error checking after the loop, this would be a simple way to get the template and not iterate every instrument. .

            Code:
            Risk myRiskTemplate = Risk.Get("");
            InstrumentRisk riskTemplate = null;
            foreach (KeyValuePair<MasterInstrument, InstrumentRisk> template in myRiskTemplate.ByMasterInstrument)
            {
            var masterInstrument = template.Key;
            if (masterInstrument.Name == "ES")
            {
            riskTemplate = template.Value;
            break;
            }
            }
            
            if (riskTemplate != null)
            {
            Print(riskTemplate.MaintenanceMargin);
            }
            I suppose it should be run once, like if (CurrentBar == 0), because there should be no need to loop on every bar OnBarUpdate, right? Or should I write a method like GetRiskTemplate(); and call it within if (CurrentBar == 0)?

            Comment


              #7
              UltraNIX It should only need to be executed once. You could do it like you have suggested, or in one of the State changes perhaps, possibly anything from State.DataLoaded onwards. Perhaps State.Transition? Once you have the InstrumentRisk object, you can, if needed, access it at any time.

              Thanks.
              Multi-Dimensional Managed Trading
              jeronymite
              NinjaTrader Ecosystem Vendor - Mizpah Software

              Comment


                #8
                Originally posted by jeronymite View Post
                UltraNIX It should only need to be executed once. You could do it like you have suggested, or in one of the State changes perhaps, possibly anything from State.DataLoaded onwards. Perhaps State.Transition? Once you have the InstrumentRisk object, you can, if needed, access it at any time.

                Thanks.
                That is a good idea, I would wait for NinjaTrader guys to confirm it for usage it in Sate.DataLoaded or elsewhere.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                633 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                364 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                105 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                567 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                568 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X