/*
* 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 System.Collections.Generic;
using System.Linq;
using QuantConnect.Orders;
namespace QuantConnect.Securities.Positions
{
///
/// Defines the parameters for
///
public class HasSufficientPositionGroupBuyingPowerForOrderParameters
{
///
/// The orders associated with this request
///
public List Orders { get; }
///
/// Gets the position group representing the holdings changes contemplated by the order
///
public IPositionGroup PositionGroup { get; }
///
/// Gets the algorithm's portfolio manager
///
public SecurityPortfolioManager Portfolio { get; }
///
/// Initializes a new instance of the class
///
/// The algorithm's portfolio manager
/// The position group
/// The orders
public HasSufficientPositionGroupBuyingPowerForOrderParameters(
SecurityPortfolioManager portfolio,
IPositionGroup positionGroup,
List orders
)
{
Orders = orders;
Portfolio = portfolio;
PositionGroup = positionGroup;
}
///
/// This may be called for non-combo type orders where the position group is guaranteed to have exactly one position
///
public static implicit operator HasSufficientBuyingPowerForOrderParameters(
HasSufficientPositionGroupBuyingPowerForOrderParameters parameters
)
{
var position = parameters.PositionGroup.Single();
var security = parameters.Portfolio.Securities[position.Symbol];
return new HasSufficientBuyingPowerForOrderParameters(parameters.Portfolio, security, parameters.Orders.Single());
}
///
/// Creates a new result indicating that there is sufficient buying power for the contemplated order
///
public HasSufficientBuyingPowerForOrderResult Sufficient()
{
return new HasSufficientBuyingPowerForOrderResult(true);
}
///
/// Creates a new result indicating that there is insufficient buying power for the contemplated order
///
public HasSufficientBuyingPowerForOrderResult Insufficient(string reason)
{
return new HasSufficientBuyingPowerForOrderResult(false, reason);
}
///
/// Creates a new result indicating that there was an error
///
public HasSufficientBuyingPowerForOrderResult Error(string reason)
{
return new HasSufficientBuyingPowerForOrderResult(false, reason);
}
}
}