本文整理汇总了C#中IAlgorithm.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# IAlgorithm.GetType方法的具体用法?C# IAlgorithm.GetType怎么用?C# IAlgorithm.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IAlgorithm
的用法示例。
在下文中一共展示了IAlgorithm.GetType方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Algorithm
public Algorithm(IAlgorithm algorithm)
{
date = DateTime.Now;
Name = algorithm.Name;
Type = algorithm.GetType().FullName;
_processingSteps = new List<ProcessingStep>();
foreach (IProcessingStep processingStep in algorithm.ProcessingSteps)
{
_processingSteps.Add(processingStep.BuildParameterList());
}
}
示例2: Run
/********************************************************
* CLASS METHODS
*********************************************************/
/// <summary>
/// Launch the algorithm manager to run this strategy
/// </summary>
/// <param name="job">Algorithm job</param>
/// <param name="algorithm">Algorithm instance</param>
/// <param name="feed">Datafeed object</param>
/// <param name="transactions">Transaction manager object</param>
/// <param name="results">Result handler object</param>
/// <param name="setup">Setup handler object</param>
/// <param name="realtime">Realtime processing object</param>
/// <remarks>Modify with caution</remarks>
public static void Run(AlgorithmNodePacket job, IAlgorithm algorithm, IDataFeed feed, ITransactionHandler transactions, IResultHandler results, ISetupHandler setup, IRealTimeHandler realtime)
{
//Initialize:
var backwardsCompatibilityMode = false;
var tradebarsType = typeof (TradeBars);
var ticksType = typeof(Ticks);
var startingPerformance = setup.StartingCapital;
var backtestMode = (job.Type == PacketType.BacktestNode);
var methodInvokers = new Dictionary<Type, MethodInvoker>();
//Initialize Properties:
_frontier = setup.StartingDate;
_runtimeError = null;
_algorithmId = job.AlgorithmId;
_algorithmState = AlgorithmStatus.Running;
_previousTime = setup.StartingDate.Date;
//Create the method accessors to push generic types into algorithm: Find all OnData events:
//Algorithm 1.0 Data Accessors.
//If the users defined these methods, add them in manually. This allows keeping backwards compatibility to algorithm 1.0.
var oldTradeBarsMethodInfo = (algorithm.GetType()).GetMethod("OnTradeBar", new[] { typeof(Dictionary<string, TradeBar>) });
var oldTicksMethodInfo = (algorithm.GetType()).GetMethod("OnTick", new[] { typeof(Dictionary<string, List<Tick>>) });
//Algorithm 2.0 Data Generics Accessors.
//New hidden access to tradebars with custom type.
var newTradeBarsMethodInfo = (algorithm.GetType()).GetMethod("OnData", new[] { tradebarsType });
var newTicksMethodInfo = (algorithm.GetType()).GetMethod("OnData", new[] { ticksType });
if (newTradeBarsMethodInfo == null && newTicksMethodInfo == null)
{
backwardsCompatibilityMode = true;
if (oldTradeBarsMethodInfo != null) methodInvokers.Add(tradebarsType, oldTradeBarsMethodInfo.DelegateForCallMethod());
if (oldTradeBarsMethodInfo != null) methodInvokers.Add(ticksType, oldTicksMethodInfo.DelegateForCallMethod());
}
else
{
backwardsCompatibilityMode = false;
if (newTradeBarsMethodInfo != null) methodInvokers.Add(tradebarsType, newTradeBarsMethodInfo.DelegateForCallMethod());
if (newTicksMethodInfo != null) methodInvokers.Add(ticksType, newTicksMethodInfo.DelegateForCallMethod());
}
//Go through the subscription types and create invokers to trigger the event handlers for each custom type:
foreach (var config in feed.Subscriptions)
{
//If type is a tradebar, combine tradebars and ticks into unified array:
if (config.Type.Name != "TradeBar" && config.Type.Name != "Tick")
{
//Get the matching method for this event handler - e.g. public void OnData(Quandl data) { .. }
var genericMethod = (algorithm.GetType()).GetMethod("OnData", new[] { config.Type });
//Is we already have this Type-handler then don't add it to invokers again.
if (methodInvokers.ContainsKey(config.Type)) continue;
//If we couldnt find the event handler, let the user know we can't fire that event.
if (genericMethod == null)
{
_runtimeError = new Exception("Data event handler not found, please create a function matching this template: public void OnData(" + config.Type.Name + " data) { }");
_algorithmState = AlgorithmStatus.RuntimeError;
return;
}
methodInvokers.Add(config.Type, genericMethod.DelegateForCallMethod());
}
}
//Loop over the queues: get a data collection, then pass them all into relevent methods in the algorithm.
Log.Debug("AlgorithmManager.Run(): Algorithm initialized, launching time loop.");
foreach (var newData in DataStream.GetData(feed, setup.StartingDate))
{
//Check this backtest is still running:
if (_algorithmState != AlgorithmStatus.Running) break;
//Go over each time stamp we've collected, pass it into the algorithm in order:
foreach (var time in newData.Keys)
{
//Set the time frontier:
_frontier = time;
//Execute with TimeLimit Monitor:
if (Isolator.IsCancellationRequested) return;
//Refresh the realtime event monitor:
realtime.SetTime(time);
//Fire EOD if the time packet we just processed is greater
if (backtestMode && _previousTime.Date != time.Date)
//.........这里部分代码省略.........
示例3: TryCreateILAlgorithm
/// <summary>
/// Create a generic IL algorithm
/// </summary>
/// <param name="assemblyPath"></param>
/// <param name="algorithmInstance"></param>
/// <param name="errorMessage"></param>
/// <returns></returns>
private bool TryCreateILAlgorithm(string assemblyPath, out IAlgorithm algorithmInstance, out string errorMessage)
{
errorMessage = "";
algorithmInstance = null;
try
{
byte[] debugInformationBytes = null;
// if the assembly is located in the base directory then don't bother loading the pdbs
// manually, they'll be loaded automatically by the .NET runtime.
if (new FileInfo(assemblyPath).DirectoryName == AppDomain.CurrentDomain.BaseDirectory)
{
// see if the pdb exists
var mdbFilename = assemblyPath + ".mdb";
var pdbFilename = assemblyPath.Substring(0, assemblyPath.Length - 4) + ".pdb";
if (File.Exists(pdbFilename))
{
debugInformationBytes = File.ReadAllBytes(pdbFilename);
}
// see if the mdb exists
if (File.Exists(mdbFilename))
{
debugInformationBytes = File.ReadAllBytes(mdbFilename);
}
}
//Load the assembly:
Assembly assembly;
if (debugInformationBytes == null)
{
Log.Trace("Loader.TryCreateILAlgorithm(): Loading only the algorithm assembly");
assembly = Assembly.LoadFrom(assemblyPath);
}
else
{
Log.Trace("Loader.TryCreateILAlgorithm(): Loading debug information with algorithm");
var assemblyBytes = File.ReadAllBytes(assemblyPath);
assembly = Assembly.Load(assemblyBytes, debugInformationBytes);
}
if (assembly == null)
{
errorMessage = "Assembly is null.";
Log.Error("Loader.TryCreateILAlgorithm(): Assembly is null");
return false;
}
//Get the list of extention classes in the library:
var types = GetExtendedTypeNames(assembly);
Log.Debug("Loader.TryCreateILAlgorithm(): Assembly types: " + string.Join(",", types));
//No extensions, nothing to load.
if (types.Count == 0)
{
errorMessage = "Algorithm type was not found.";
Log.Error("Loader.TryCreateILAlgorithm(): Types array empty, no algorithm type found.");
return false;
}
if (types.Count > 1)
{
// reshuffle type[0] to the resolved typename
types[0] = _multipleTypeNameResolverFunction.Invoke(types);
if (string.IsNullOrEmpty(types[0]))
{
errorMessage = "Unable to resolve multiple algorithm types to a single type.";
Log.Error("Loader.TryCreateILAlgorithm(): Failed resolving multiple algorithm types to a single type.");
return false;
}
}
//Load the assembly into this AppDomain:
algorithmInstance = (IAlgorithm)assembly.CreateInstance(types[0], true);
if (algorithmInstance != null)
{
Log.Trace("Loader.TryCreateILAlgorithm(): Loaded " + algorithmInstance.GetType().Name);
}
}
catch (ReflectionTypeLoadException err)
{
Log.Error("Loader.TryCreateILAlgorithm(1): " + err.LoaderExceptions[0]);
if (err.InnerException != null) errorMessage = err.InnerException.Message;
}
catch (Exception err)
{
Log.Error("Loader.TryCreateILAlgorithm(2): " + err.Message);
if (err.InnerException != null) errorMessage = err.InnerException.Message;
}
return true;
}
示例4: Setup
/// <summary>
/// Setup the algorithm cash, dates and portfolio as desired.
/// </summary>
/// <param name="algorithm">Existing algorithm instance</param>
/// <param name="brokerage">New brokerage instance</param>
/// <param name="baseJob">Backtesting job</param>
/// <param name="resultHandler">The configured result handler</param>
/// <param name="transactionHandler">The configuration transaction handler</param>
/// <param name="realTimeHandler">The configured real time handler</param>
/// <returns>Boolean true on successfully setting up the console.</returns>
public bool Setup(IAlgorithm algorithm, IBrokerage brokerage, AlgorithmNodePacket baseJob, IResultHandler resultHandler, ITransactionHandler transactionHandler, IRealTimeHandler realTimeHandler)
{
var initializeComplete = false;
try
{
//Set common variables for console programs:
if (baseJob.Type == PacketType.BacktestNode)
{
var backtestJob = baseJob as BacktestNodePacket;
algorithm.SetMaximumOrders(int.MaxValue);
// set our parameters
algorithm.SetParameters(baseJob.Parameters);
algorithm.SetLiveMode(false);
//Set the source impl for the event scheduling
algorithm.Schedule.SetEventSchedule(realTimeHandler);
//Setup Base Algorithm:
algorithm.Initialize();
//Set the time frontier of the algorithm
algorithm.SetDateTime(algorithm.StartDate.ConvertToUtc(algorithm.TimeZone));
//Construct the backtest job packet:
backtestJob.PeriodStart = algorithm.StartDate;
backtestJob.PeriodFinish = algorithm.EndDate;
backtestJob.BacktestId = algorithm.GetType().Name;
backtestJob.Type = PacketType.BacktestNode;
backtestJob.UserId = baseJob.UserId;
backtestJob.Channel = baseJob.Channel;
//Backtest Specific Parameters:
StartingDate = backtestJob.PeriodStart;
StartingPortfolioValue = algorithm.Portfolio.Cash;
}
else
{
throw new Exception("The ConsoleSetupHandler is for backtests only. Use the BrokerageSetupHandler.");
}
}
catch (Exception err)
{
Log.Error(err);
Errors.Add("Failed to initialize algorithm: Initialize(): " + err.Message);
}
if (Errors.Count == 0)
{
initializeComplete = true;
}
algorithm.Transactions.SetOrderProcessor(transactionHandler);
algorithm.PostInitialize();
return initializeComplete;
}
示例5: IsInstanceOfType
bool IsInstanceOfType(Type type, IAlgorithm algorithm) {
Type algorithmType = algorithm.GetType();
Type[] interfaces = algorithmType.GetInterfaces();
foreach (Type iface in interfaces) {
if (iface == type) {
return true;
}
}
while (algorithmType != typeof(System.Object)) {
if (type == algorithmType) {
return true;
}
algorithmType = algorithmType.BaseType;
}
return false;
}