/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 nodeId, BaseLiveAlgorithmSettings settings, string version = "-1")
{
VersionId = version;
ProjectId = projectId;
CompileId = compileId;
NodeId = nodeId;
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; }
///
/// Id of the node being used to run live algorithm
///
[JsonProperty(PropertyName = "nodeId")]
public string NodeId { 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;
}
///
/// The constructor used by Bitfinex
///
public BaseLiveAlgorithmSettings()
{
User = "";
Password = "";
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.ToStringInvariant();
}
}
///
/// 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.ToStringInvariant();
}
}
///
/// 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.ToStringInvariant();
}
}
///
/// 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.ToStringInvariant();
}
///
/// 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; }
}
///
/// Live algorithm settings for trading with Bitfinex
///
public class BitfinexLiveAlgorithmSettings : BaseLiveAlgorithmSettings
{
///
/// Constructor for live trading with Bitfinex
///
/// Api key to Bitfinex account
/// Secret Api key to Bitfinex account
public BitfinexLiveAlgorithmSettings(string key, string secret)
{
Environment = BrokerageEnvironment.Live;
Id = "BitfinexBrokerage"; // BrokerageName.Bitfinex.ToString() returns "Bitfinex"
Key = key;
Secret = secret;
}
///
/// Property specific to Bitfinex account. API Key
///
[JsonProperty(PropertyName = "key")]
public string Key { get; private set; }
///
/// Property specific to Bitfinex account. API Secret Key
///
[JsonProperty(PropertyName = "secret")]
public string Secret { get; private set; }
}
///
/// Live algorithm settings for trading with Alpaca
///
public class AlpacaLiveAlgorithmSettings : BaseLiveAlgorithmSettings
{
///
/// Constructor for live trading with Alpaca
///
/// Api key to Alpaca account
/// Secret Api key to Alpaca account
/// Environment to launch the algorithm in
public AlpacaLiveAlgorithmSettings(string key, string secret, BrokerageEnvironment environment)
{
Environment = environment;
Id = "AlpacaBrokerage"; //BrokerageName.Alpaca.ToString(); returns "Alpaca"
Key = key;
Secret = secret;
}
///
/// Property specific to Alpaca account. API Key
///
[JsonProperty(PropertyName = "key")]
public string Key { get; private set; }
///
/// Property specific to Alpaca account. API Secret Key
///
[JsonProperty(PropertyName = "secret")]
public string Secret { get; private set; }
}
///
/// Live algorithm settings for trading with GDAX (Coinbase)
///
public class GDAXLiveAlgorithmSettings : BaseLiveAlgorithmSettings
{
///
/// Constructor for live trading with GDAX (Coinbase)
///
/// Api key to GDAX account
/// Secret Api key to GDAX account
/// Passphrase to this API key
public GDAXLiveAlgorithmSettings(string key, string secret, string passphrase)
{
Environment = BrokerageEnvironment.Live;
Id = "GDAXBrokerage"; //BrokerageName.GDAX.ToString(); returns "GDAX"
Key = key;
Secret = secret;
Passphrase = passphrase;
}
///
/// Property specific to GDAX account. API Key
///
[JsonProperty(PropertyName = "key")]
public string Key { get; private set; }
///
/// Property specific to GDAX account. API Secret Key
///
[JsonProperty(PropertyName = "secret")]
public string Secret { get; private set; }
///
/// Property specific to GDAX account. API Passphrase
///
[JsonProperty(PropertyName = "passphrase")]
public string Passphrase { get; private set; }
}
}