Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Algorithm.CSharp/QuantConnect.Algorithm.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<DebugType>portable</DebugType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.48" />
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.49" />
<PackageReference Include="Accord" Version="3.6.0" />
<PackageReference Include="Accord.Fuzzy" Version="3.6.0" />
<PackageReference Include="Accord.MachineLearning" Version="3.6.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.48" />
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.49" />
<PackageReference Include="Accord" Version="3.6.0" />
<PackageReference Include="Accord.Math" Version="3.6.0" />
<PackageReference Include="Accord.Statistics" Version="3.6.0" />
Expand Down
2 changes: 1 addition & 1 deletion Algorithm.Python/QuantConnect.Algorithm.Python.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<Compile Include="..\Common\Properties\SharedAssemblyInfo.cs" Link="Properties\SharedAssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.48" />
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.49" />
</ItemGroup>
<ItemGroup>
<Content Include="OptionUniverseFilterGreeksShortcutsRegressionAlgorithm.py" />
Expand Down
2 changes: 1 addition & 1 deletion Algorithm/QuantConnect.Algorithm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.48" />
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.49" />
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="NodaTime" Version="3.0.5" />
Expand Down
2 changes: 1 addition & 1 deletion AlgorithmFactory/QuantConnect.AlgorithmFactory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.48" />
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.49" />
<PackageReference Include="NodaTime" Version="3.0.5" />
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 0 additions & 6 deletions Brokerages/BrokerageConcurrentMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ private interface ILock : IDisposable
{
int CurrentWriteCount { get; }

void EnterReadLock();

void ExitReadLock();

bool TryEnterReadLockImmediately();
Expand Down Expand Up @@ -204,8 +202,6 @@ public ReaderWriterLockWrapper()
{
_lock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
}

public void EnterReadLock() => _lock.EnterWriteLock();
public void ExitReadLock() => _lock.ExitWriteLock();
public bool TryEnterReadLockImmediately() => _lock.TryEnterWriteLock(0);
public void EnterWriteLock() => _lock.EnterReadLock();
Expand All @@ -230,8 +226,6 @@ public MonitorWrapper()
_lockObject = new object();
}

public void EnterReadLock() => Monitor.Enter(_lockObject);

public void ExitReadLock() => Monitor.Exit(_lockObject);

public bool TryEnterReadLockImmediately() => Monitor.TryEnter(_lockObject);
Expand Down
5 changes: 2 additions & 3 deletions Common/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4451,10 +4451,9 @@ public static bool IsCustomDataType(Symbol symbol, Type type)
/// <param name="security">Security for which we would like to make a market order</param>
/// <param name="quantity">Quantity of the security we are seeking to trade</param>
/// <param name="time">Time the order was placed</param>
/// <param name="marketOrder">This out parameter will contain the market order constructed</param>
public static CashAmount GetMarketOrderFees(Security security, decimal quantity, DateTime time, out MarketOrder marketOrder)
public static CashAmount GetMarketOrderFees(Security security, decimal quantity, DateTime time)
{
marketOrder = new MarketOrder(security.Symbol, quantity, time);
var marketOrder = new MarketOrder(security.Symbol, quantity, time);
return security.FeeModel.GetOrderFee(new OrderFeeParameters(security, marketOrder)).Value;
}

Expand Down
2 changes: 1 addition & 1 deletion Common/Orders/OrderSizing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static decimal GetUnorderedQuantity(IAlgorithm algorithm, IPortfolioTarge
// Adjust the order quantity taking into account the fee's
if (accountForFees && security.Symbol.SecurityType == SecurityType.Crypto && quantity > 0)
{
var orderFee = Extensions.GetMarketOrderFees(security, quantity, algorithm.UtcTime, out _);
var orderFee = Extensions.GetMarketOrderFees(security, quantity, algorithm.UtcTime);
var baseCurrency = ((Crypto)security).BaseCurrency.Symbol;
if (baseCurrency == orderFee.Currency)
{
Expand Down
2 changes: 1 addition & 1 deletion Common/QuantConnect.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<Message Text="SelectedOptimization $(SelectedOptimization)" Importance="high" />
</Target>
<ItemGroup>
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.48" />
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.49" />
<PackageReference Include="CloneExtensions" Version="1.3.0" />
<PackageReference Include="fasterflect" Version="3.0.0" />
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
Expand Down
20 changes: 11 additions & 9 deletions Common/Securities/SecurityHolding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/

using System;
using QuantConnect.Orders;
using QuantConnect.Orders.Fees;
using QuantConnect.Algorithm.Framework.Portfolio;

namespace QuantConnect.Securities
Expand Down Expand Up @@ -478,22 +476,26 @@ public virtual ConvertibleCashAmount GetQuantityValue(decimal quantity, decimal
/// <remarks>Does not use the transaction model for market fills but should.</remarks>
public virtual decimal TotalCloseProfit(bool includeFees = true, decimal? exitPrice = null, decimal? entryPrice = null, decimal? quantity = null)
{
var quantityToUse = quantity ?? Quantity;
if (quantityToUse == 0)
var quantityToUse = Quantity;
if (quantity.HasValue)
{
quantityToUse = quantity.Value;
}
else if (!_invested)
{
return 0;
}

// this is in the account currency
var orderFee = Extensions.GetMarketOrderFees(_security, -quantityToUse, _security.LocalTime.ConvertToUtc(_security.Exchange.TimeZone), out var marketOrder);

var feesInAccountCurrency = 0m;
if (includeFees)
{
feesInAccountCurrency = _currencyConverter.ConvertToAccountCurrency(orderFee).Amount;
// this is in the account currency
var liquidationFees = Extensions.GetMarketOrderFees(_security, -quantityToUse, _security.LocalTime.ConvertToUtc(_security.Exchange.TimeZone));
feesInAccountCurrency = _currencyConverter.ConvertToAccountCurrency(liquidationFees).Amount;
}

var price = marketOrder.Direction == OrderDirection.Sell ? _security.BidPrice : _security.AskPrice;
// if we are long, we would need to sell against the bid
var price = IsLong ? _security.BidPrice : _security.AskPrice;
if (price == 0)
{
// Bid/Ask prices can both be equal to 0. This usually happens when we request our holdings from
Expand Down
2 changes: 1 addition & 1 deletion Engine/QuantConnect.Lean.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<Message Text="SelectedOptimization $(SelectedOptimization)" Importance="high" />
</Target>
<ItemGroup>
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.48" />
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.49" />
<PackageReference Include="fasterflect" Version="3.0.0" />
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
Expand Down
52 changes: 13 additions & 39 deletions Engine/Results/BacktestingResultHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,15 +400,11 @@ protected void SendFinalResult()
/// <param name="algorithm">Algorithm we're working on.</param>
/// <param name="startingPortfolioValue">Algorithm starting capital for statistics calculations</param>
/// <remarks>While setting the algorithm the backtest result handler.</remarks>
public virtual void SetAlgorithm(IAlgorithm algorithm, decimal startingPortfolioValue)
public override void SetAlgorithm(IAlgorithm algorithm, decimal startingPortfolioValue)
{
Algorithm = algorithm;
base.SetAlgorithm(algorithm, startingPortfolioValue);
Algorithm.SetStatisticsService(this);
State["Name"] = Algorithm.Name;
StartingPortfolioValue = startingPortfolioValue;
DailyPortfolioValue = StartingPortfolioValue;
CumulativeMaxPortfolioValue = StartingPortfolioValue;
AlgorithmCurrencySymbol = Currencies.GetCurrencySymbol(Algorithm.AccountCurrency);
_capacityEstimate = new CapacityEstimate(Algorithm);
_progressMonitor = new BacktestProgressMonitor(Algorithm.TimeKeeper, Algorithm.EndDate);

Expand All @@ -418,21 +414,7 @@ public virtual void SetAlgorithm(IAlgorithm algorithm, decimal startingPortfolio
ResamplePeriod = TimeSpan.FromMinutes(resampleMinutes);
Log.Trace("BacktestingResultHandler(): Sample Period Set: " + resampleMinutes.ToStringInvariant("00.00"));

//Set the security / market types.
var types = new List<SecurityType>();
foreach (var kvp in Algorithm.Securities)
{
var security = kvp.Value;

if (!types.Contains(security.Type)) types.Add(security.Type);
}
SecurityType(types);

ConfigureConsoleTextWriter(algorithm);

// Wire algorithm name and tags updates
algorithm.NameUpdated += (sender, name) => AlgorithmNameUpdated(name);
algorithm.TagsUpdated += (sender, tags) => AlgorithmTagsUpdated(tags);
}

/// <summary>
Expand Down Expand Up @@ -496,18 +478,6 @@ protected override void AddToLogStore(string message)
base.AddToLogStore(messageToLog);
}

/// <summary>
/// Send list of security asset types the algorithm uses to browser.
/// </summary>
public virtual void SecurityType(List<SecurityType> types)
{
var packet = new SecurityTypesPacket
{
Types = types
};
Messages.Enqueue(packet);
}

/// <summary>
/// Send an error message back to the browser highlighted in red with a stacktrace.
/// </summary>
Expand Down Expand Up @@ -743,6 +713,11 @@ public virtual void ProcessSynchronousEvents(bool forceProcess = false)
{
if (Algorithm == null) return;

var time = Algorithm.UtcTime;

// Check to see if we should update stored portfolio values
UpdatePortfolioValues(time, forceProcess);

_capacityEstimate.UpdateMarketCapacity(forceProcess);

// Invalidate the processed days count so it gets recalculated
Expand All @@ -751,7 +726,6 @@ public virtual void ProcessSynchronousEvents(bool forceProcess = false)
// Update the equity bar
UpdateAlgorithmEquity();

var time = Algorithm.UtcTime;
if (time > _nextSample || forceProcess)
{
//Set next sample time: 4000 samples per backtest
Expand All @@ -762,14 +736,14 @@ public virtual void ProcessSynchronousEvents(bool forceProcess = false)

//Also add the user samples / plots to the result handler tracking:
SampleRange(Algorithm.GetChartUpdates());
}

ProcessAlgorithmLogs();
ProcessAlgorithmLogs();

//Set the running statistics:
foreach (var pair in Algorithm.RuntimeStatistics)
{
RuntimeStatistic(pair.Key, pair.Value);
//Set the running statistics:
foreach (var pair in Algorithm.RuntimeStatistics)
{
RuntimeStatistic(pair.Key, pair.Value);
}
}
}

Expand Down
Loading
Loading