/*
* 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 QuantConnect.Interfaces;
using QuantConnect.Util;
using System;
using System.Collections.Generic;
using System.Net.Http;
namespace QuantConnect.Algorithm.Framework.Portfolio.SignalExports
{
///
/// Base class to send signals to different 3rd party API's
///
public abstract class BaseSignalExport : ISignalExportTarget
{
///
/// Lazy initialization of HttpClient to be used to sent signals to different 3rd party API's
///
private Lazy _lazyClient = new Lazy();
///
/// List of all SecurityTypes present in LEAN
///
private HashSet _defaultAllowedSecurityTypes = new HashSet
{
SecurityType.Equity,
SecurityType.Forex,
SecurityType.Option,
SecurityType.Future,
SecurityType.FutureOption,
SecurityType.Crypto,
SecurityType.CryptoFuture,
SecurityType.Cfd,
SecurityType.IndexOption,
};
///
/// The name of this signal export
///
protected abstract string Name { get; }
///
/// Property to access a HttpClient
///
protected HttpClient HttpClient => _lazyClient.Value;
///
/// Default hashset of allowed Security types
///
protected virtual HashSet AllowedSecurityTypes
{
get => _defaultAllowedSecurityTypes;
}
///
/// Sends positions to different 3rd party API's
///
/// Holdings the user have defined to be sent to certain 3rd party API and the algorithm being ran
/// True if the positions were sent correctly and the 3rd party API sent no errors. False, otherwise
public virtual bool Send(SignalExportTargetParameters parameters)
{
if (parameters.Targets.Count == 0)
{
parameters.Algorithm.Debug("Portfolio target is empty");
return false;
}
return VerifyTargets(parameters);
}
///
/// Verifies the security type of every holding in the given list is allowed
///
/// Holdings the user have defined to be sent to certain 3rd party API and the algorithm being ran
/// True if all the targets were allowed, false otherwise
private bool VerifyTargets(SignalExportTargetParameters parameters)
{
foreach (var signal in parameters.Targets)
{
if (!AllowedSecurityTypes.Contains(signal.Symbol.SecurityType))
{
parameters.Algorithm.Debug($"{signal.Symbol.SecurityType} security type is not supported by {Name}. Allowed security types: [{string.Join(",", AllowedSecurityTypes)}]");
return false;
}
}
return true;
}
///
/// If created, dispose of HttpClient we used for the requests to the different 3rd party API's
///
public void Dispose()
{
if (_lazyClient.IsValueCreated)
{
_lazyClient.Value.DisposeSafely();
}
}
}
}