// this section loads the list of instruments to select from
NinjaTrader.Cbi.InstrumentList listOfInstruments = NinjaTrader.Cbi.InstrumentList.GetObject("Default" ); // method accesses the 'Default' list in the Instrument Manager
List<string> AvailableInstruments = new List<string>();
foreach (Instrument instrument in listOfInstruments.Instruments) //only include first 2 characters ignoring instruments that begin with '^'
{
if (instrument.FullName.Substring(0,1) != "^")
{
addme = instrument.FullName.Substring(0,2);
if (!AvailableInstruments.Contains(addme)) AvailableInstruments.Add(addme);
}
}
ArrayAvailableInstruments = AvailableInstruments.ToArray();
if (theListOfInstruments == null) defaultInst = Instrument.MasterInstrument.Name; // no instrument selected so default to current charts instrument
else defaultInst = theListOfInstruments;
// this section selects the most recent xpiry contract for the instrument selected
if (defaultInst != Instrument.MasterInstrument.Name) // if instrument from list, select the most recent expiry
{
NinjaTrader.Cbi.InstrumentList list = NinjaTrader.Cbi.InstrumentList.GetObject("Default" ); // method accesses the 'Default' list in the Instrument Manager
List<Instrument> myList=new List<Instrument>();
foreach (Instrument myInstrument in list.Instruments){myList.Add(myInstrument);}
var result=from row in myList where row.FullName.Contains(defaultInst) orderby row.Expiry descending select row.FullName;
bool contractFound = false;
if (result!=null){if (result.Count()>0) {contractFound=true;}}
List<string> selectedInstruments = result.ToList<string>();
if (contractFound)
{
selected_contract = selectedInstruments[0];
}
else
{
selected_contract = "";
runProgram = false;
}
}

Comment