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.
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Extract info from Risk Template to a strategy
Collapse
X
-
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.Tags: None
-
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.
-
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
-
UltraNIX I have found something like this works.
Hope it helps.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 } }
Thanks.
- Likes 1
Comment
-
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)?Originally posted by NinjaTrader_Jesse View PostHello 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
-
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.
- Likes 1
Comment
-
That is a good idea, I would wait for NinjaTrader guys to confirm it for usage it in Sate.DataLoaded or elsewhere.Originally posted by jeronymite View PostUltraNIX 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.
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
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
364 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
105 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
567 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
568 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment