Lean  $LEAN_TAG$
BaseCommand.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
17 using System;
18 
19 namespace QuantConnect.Commands
20 {
21  /// <summary>
22  /// Base command implementation
23  /// </summary>
24  public abstract class BaseCommand : ICommand
25  {
26  /// <summary>
27  /// Unique command id
28  /// </summary>
29  public string Id { get; set; }
30 
31  /// <summary>
32  /// Runs this command against the specified algorithm instance
33  /// </summary>
34  /// <param name="algorithm">The algorithm to run this command against</param>
35  public abstract CommandResultPacket Run(IAlgorithm algorithm);
36 
37  /// <summary>
38  /// Creats symbol using symbol properties.
39  /// </summary>
40  /// <param name="ticker">The string ticker symbol</param>
41  /// <param name="securityType">The security type of the ticker. If securityType == Option, then a canonical symbol is created</param>
42  /// <param name="market">The market the ticker resides in</param>
43  /// <param name="symbol">The algorithm to run this command against</param>
44  /// <exception cref="ArgumentException">If symbol is null or symbol can't be created with given args</exception>
45  protected Symbol GetSymbol(string ticker, SecurityType securityType, string market, Symbol symbol = null)
46  {
47  if (symbol != null)
48  {
49  // No need to create symbol if alrady exists
50  return symbol;
51  }
52  if (ticker != null && (securityType != null && securityType != SecurityType.Base) && market != null)
53  {
54  return Symbol.Create(ticker, securityType, market);
55  }
56  else
57  {
58  throw new ArgumentException($"BaseCommand.GetSymbol(): {Messages.BaseCommand.MissingValuesToGetSymbol}");
59  }
60  }
61  }
62 }