using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using QuantConnect.Brokerages;
namespace QuantConnect.API
{
///
/// Helper class to put BaseLiveAlgorithmSettings in proper format.
///
public class LiveAlgorithmApiSettingsWrapper
{
///
/// Constructor for LiveAlgorithmApiSettingsWrapper
///
/// Id of project from QuantConnect
/// Id of compilation of project from QuantConnect
/// Server type to run live Algorithm
/// Live Algorithm Settings for a specific brokerage
/// The version identifier
public LiveAlgorithmApiSettingsWrapper(int projectId, string compileId, string serverType, BaseLiveAlgorithmSettings settings, string version = "-1")
{
VersionId = version;
ProjectId = projectId;
CompileId = compileId;
ServerType = serverType;
Brokerage = settings;
}
///
/// -1 is master
///
[JsonProperty(PropertyName = "versionId")]
public string VersionId { get; set; }
///
/// Project id for the live instance
///
[JsonProperty(PropertyName = "projectId")]
public int ProjectId { get; private set; }
///
/// Compile Id for the live algorithm
///
[JsonProperty(PropertyName = "compileId")]
public string CompileId { get; private set; }
///
/// Type of server being used to run live algorithm
///
[JsonProperty(PropertyName = "serverType")]
public string ServerType { get; private set; }
///
/// The API expects the settings as part of a brokerage object
///
[JsonProperty(PropertyName = "brokerage")]
public BaseLiveAlgorithmSettings Brokerage { get; private set; }
}
///
/// Base class for settings that must be configured per Brokerage to create new algorithms via the API.
///
public class BaseLiveAlgorithmSettings
{
///
/// Constructor used by FXCM
///
/// Username associated with brokerage
/// Password associated with brokerage
/// 'live'/'paper'
/// Account id for brokerage
public BaseLiveAlgorithmSettings(string user,
string password,
BrokerageEnvironment environment,
string account)
{
User = user;
Password = password;
Environment = environment;
Account = account;
}
///
/// Constructor used by Interactive Brokers
///
/// Username associated with brokerage
/// Password associated with brokerage
public BaseLiveAlgorithmSettings(string user,
string password)
{
Password = password;
User = user;
}
///
/// The constructor used by Oanda
///
/// 'live'/'paper'
/// Account id for brokerage
public BaseLiveAlgorithmSettings(BrokerageEnvironment environment,
string account)
{
User = "";
Password = "";
Environment = environment;
Account = account;
}
///
/// The constructor used by Tradier
///
/// Account id for brokerage
public BaseLiveAlgorithmSettings(string account)
{
User = "";
Password = "";
Account = account;
}
///
/// 'Interactive' / 'FXCM' / 'Oanda' / 'Tradier' /'PaperTrading'
///
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
///
/// Username associated with brokerage
///
[JsonProperty(PropertyName = "user")]
public string User { get; private set; }
///
/// Password associated with brokerage
///
[JsonProperty(PropertyName = "password")]
public string Password { get; private set; }
///
/// 'live'/'paper'
///
[JsonProperty(PropertyName = "environment")]
public BrokerageEnvironment Environment { get; set; }
///
/// Account of the associated brokerage
///
[JsonProperty(PropertyName = "account")]
public string Account { get; set; }
}
///
/// Default live algorithm settings
///
public class DefaultLiveAlgorithmSettings : BaseLiveAlgorithmSettings
{
///
/// Constructor for default algorithms
///
/// Username associated with brokerage
/// Password associated with brokerage
/// 'live'/'paper'
/// Account id for brokerage
public DefaultLiveAlgorithmSettings(string user,
string password,
BrokerageEnvironment environment,
string account)
: base(user, password, environment, account)
{
Id = BrokerageName.QuantConnectBrokerage.ToString();
}
}
///
/// Algorithm setting for trading with FXCM
///
public class FXCMLiveAlgorithmSettings : BaseLiveAlgorithmSettings
{
///
/// Contructor for live trading with FXCM
///
/// Username associated with brokerage
/// Password associated with brokerage
/// 'live'/'paper'
/// Account id for brokerage
public FXCMLiveAlgorithmSettings(string user,
string password,
BrokerageEnvironment environment,
string account)
: base(user, password, environment, account)
{
Id = BrokerageName.FxcmBrokerage.ToString();
}
}
///
/// Live algorithm settings for trading with Interactive Brokers
///
public class InteractiveBrokersLiveAlgorithmSettings : BaseLiveAlgorithmSettings
{
///
/// Contructor for live trading with IB.
///
/// Username associated with brokerage
/// Password of assciate brokerage
/// Account id for brokerage
public InteractiveBrokersLiveAlgorithmSettings(string user,
string password,
string account)
: base(user, password)
{
Account = account;
Environment = Account.Substring(0, 2) == "DU" ? BrokerageEnvironment.Paper : BrokerageEnvironment.Live;
Id = BrokerageName.InteractiveBrokersBrokerage.ToString();
}
}
///
/// Live algorithm settings for trading with Oanda
///
public class OandaLiveAlgorithmSettings : BaseLiveAlgorithmSettings
{
///
/// Contructor for live trading with Oanda.
///
/// Access Token (specific for Oanda Brokerage)
/// 'live'/'paper'
/// Account id for brokerage
public OandaLiveAlgorithmSettings(string accessToken,
BrokerageEnvironment environment,
string account)
: base(environment, account)
{
AccessToken = accessToken;
// The DateIssued parameter is required by the Api, but not required to trade.
// This should be fixed on the Api side.
DateIssued = "1";
Id = BrokerageName.OandaBrokerage.ToString();
}
///
/// Access token for Oanda
///
[JsonProperty(PropertyName = "accessToken")]
public string AccessToken { get; private set; }
///
/// Date token was issued
///
[JsonProperty(PropertyName = "dateIssued")]
public string DateIssued { get; private set; }
}
///
/// Live algorithm settings for trading with Tradier
///
public class TradierLiveAlgorithmSettings : BaseLiveAlgorithmSettings
{
///
/// Contructor for live trading with Tradier.
///
///
/// Specific for live trading with Tradier. See Tradier account for more details.
/// Specific for live trading with Tradier. See Tradier account for more details.
/// Account id for brokerage
public TradierLiveAlgorithmSettings(string accessToken,
string dateIssued,
string refreshToken,
string account)
: base(account)
{
Environment = BrokerageEnvironment.Live;
AccessToken = accessToken;
DateIssued = dateIssued;
RefreshToken = refreshToken;
Lifetime = "86399";
Id = BrokerageName.TradierBrokerage.ToString();
}
///
/// Access token for tradier brokerage
///
[JsonProperty(PropertyName = "accessToken")]
public string AccessToken { get; private set; }
///
/// Property specific to Tradier account. See tradier account for more details.
///
[JsonProperty(PropertyName = "dateIssued")]
public string DateIssued { get; private set; }
///
/// Property specific to Tradier account. See tradier account for more details.
///
[JsonProperty(PropertyName = "refreshToken")]
public string RefreshToken { get; private set; }
///
/// Property specific to Tradier account. See tradier account for more details.
///
[JsonProperty(PropertyName = "lifetime")]
public string Lifetime { get; private set; }
}
}