chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 13:02:50 +08:00
commit 0fc60fdcb1
5008 changed files with 910633 additions and 0 deletions
+278
View File
@@ -0,0 +1,278 @@
/*
* 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 QuantConnect.Interfaces;
using QuantConnect.Logging;
using QuantConnect.Notifications;
using QuantConnect.Packets;
namespace QuantConnect.Messaging
{
/// <summary>
/// Desktop implementation of messaging system for Lean Engine
/// </summary>
public class EventMessagingHandler : IMessagingHandler
{
private AlgorithmNodePacket _job;
private volatile bool _loaded;
private Queue<Packet> _queue;
/// <summary>
/// Gets or sets whether this messaging handler has any current subscribers.
/// When set to false, messages won't be sent.
/// </summary>
public bool HasSubscribers
{
get;
set;
}
/// <summary>
/// Initialize the Messaging System Plugin.
/// </summary>
/// <param name="initializeParameters">The parameters required for initialization</param>
public void Initialize(MessagingHandlerInitializeParameters initializeParameters)
{
_queue = new Queue<Packet>();
ConsumerReadyEvent += () => { _loaded = true; };
}
/// <summary>
/// Set Loaded to true
/// </summary>
public void LoadingComplete()
{
_loaded = true;
}
/// <summary>
/// Set the user communication channel
/// </summary>
/// <param name="job"></param>
public void SetAuthentication(AlgorithmNodePacket job)
{
_job = job;
}
#pragma warning disable 1591
public delegate void DebugEventRaised(DebugPacket packet);
public event DebugEventRaised DebugEvent;
public delegate void SystemDebugEventRaised(SystemDebugPacket packet);
#pragma warning disable 0067 // SystemDebugEvent is not used currently; ignore the warning
public event SystemDebugEventRaised SystemDebugEvent;
#pragma warning restore 0067
public delegate void LogEventRaised(LogPacket packet);
public event LogEventRaised LogEvent;
public delegate void RuntimeErrorEventRaised(RuntimeErrorPacket packet);
public event RuntimeErrorEventRaised RuntimeErrorEvent;
public delegate void HandledErrorEventRaised(HandledErrorPacket packet);
public event HandledErrorEventRaised HandledErrorEvent;
public delegate void BacktestResultEventRaised(BacktestResultPacket packet);
public event BacktestResultEventRaised BacktestResultEvent;
public delegate void ConsumerReadyEventRaised();
public event ConsumerReadyEventRaised ConsumerReadyEvent;
#pragma warning restore 1591
/// <summary>
/// Send any message with a base type of Packet.
/// </summary>
public void Send(Packet packet)
{
//Until we're loaded queue it up
if (!_loaded)
{
_queue.Enqueue(packet);
return;
}
//Catch up if this is the first time
while (_queue.TryDequeue(out var item))
{
ProcessPacket(item);
}
//Finally process this new packet
ProcessPacket(packet);
}
/// <summary>
/// Send any notification with a base type of Notification.
/// </summary>
/// <param name="notification">The notification to be sent.</param>
public void SendNotification(Notification notification)
{
if (!notification.CanSend())
{
Log.Error("Messaging.SendNotification(): Send not implemented for notification of type: " + notification.GetType().Name);
return;
}
notification.Send();
}
/// <summary>
/// Send any message with a base type of Packet that has been enqueued.
/// </summary>
public void SendEnqueuedPackets()
{
while (_loaded && _queue.TryDequeue(out var item))
{
ProcessPacket(item);
}
}
/// <summary>
/// Packet processing implementation
/// </summary>
private void ProcessPacket(Packet packet)
{
//Packets we handled in the UX.
switch (packet.Type)
{
case PacketType.Debug:
var debug = (DebugPacket)packet;
OnDebugEvent(debug);
break;
case PacketType.SystemDebug:
var systemDebug = (SystemDebugPacket)packet;
OnSystemDebugEvent(systemDebug);
break;
case PacketType.Log:
var log = (LogPacket)packet;
OnLogEvent(log);
break;
case PacketType.RuntimeError:
var runtime = (RuntimeErrorPacket)packet;
OnRuntimeErrorEvent(runtime);
break;
case PacketType.HandledError:
var handled = (HandledErrorPacket)packet;
OnHandledErrorEvent(handled);
break;
case PacketType.BacktestResult:
var result = (BacktestResultPacket)packet;
OnBacktestResultEvent(result);
break;
}
}
/// <summary>
/// Raise a debug event safely
/// </summary>
protected virtual void OnDebugEvent(DebugPacket packet)
{
var handler = DebugEvent;
if (handler != null)
{
handler(packet);
}
}
/// <summary>
/// Raise a system debug event safely
/// </summary>
protected virtual void OnSystemDebugEvent(SystemDebugPacket packet)
{
var handler = DebugEvent;
if (handler != null)
{
handler(packet);
}
}
/// <summary>
/// Handler for consumer ready code.
/// </summary>
public virtual void OnConsumerReadyEvent()
{
var handler = ConsumerReadyEvent;
if (handler != null)
{
handler();
}
}
/// <summary>
/// Raise a log event safely
/// </summary>
protected virtual void OnLogEvent(LogPacket packet)
{
var handler = LogEvent;
if (handler != null)
{
handler(packet);
}
}
/// <summary>
/// Raise a handled error event safely
/// </summary>
protected virtual void OnHandledErrorEvent(HandledErrorPacket packet)
{
var handler = HandledErrorEvent;
if (handler != null)
{
handler(packet);
}
}
/// <summary>
/// Raise runtime error safely
/// </summary>
protected virtual void OnRuntimeErrorEvent(RuntimeErrorPacket packet)
{
var handler = RuntimeErrorEvent;
if (handler != null)
{
handler(packet);
}
}
/// <summary>
/// Raise a backtest result event safely.
/// </summary>
protected virtual void OnBacktestResultEvent(BacktestResultPacket packet)
{
var handler = BacktestResultEvent;
if (handler != null)
{
handler(packet);
}
}
/// <summary>
/// Dispose of any resources
/// </summary>
public virtual void Dispose()
{
}
}
}
+130
View File
@@ -0,0 +1,130 @@
/*
* 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.Logging;
using QuantConnect.Notifications;
using QuantConnect.Packets;
using QuantConnect.Util;
using System;
using System.Linq;
namespace QuantConnect.Messaging
{
/// <summary>
/// Local/desktop implementation of messaging system for Lean Engine.
/// </summary>
public class Messaging : IMessagingHandler
{
/// <summary>
/// This implementation ignores the <seealso cref="HasSubscribers"/> flag and
/// instead will always write to the log.
/// </summary>
public bool HasSubscribers
{
get;
set;
}
/// <summary>
/// Initialize the messaging system
/// </summary>
/// <param name="initializeParameters">The parameters required for initialization</param>
public void Initialize(MessagingHandlerInitializeParameters initializeParameters)
{
//
}
/// <summary>
/// Set the messaging channel
/// </summary>
public virtual void SetAuthentication(AlgorithmNodePacket job)
{
}
/// <summary>
/// Send a generic base packet without processing
/// </summary>
public virtual void Send(Packet packet)
{
switch (packet.Type)
{
case PacketType.Debug:
var debug = (DebugPacket)packet;
Log.Trace("Debug: " + debug.Message);
break;
case PacketType.SystemDebug:
var systemDebug = (SystemDebugPacket)packet;
Log.Trace("Debug: " + systemDebug.Message);
break;
case PacketType.Log:
var log = (LogPacket)packet;
Log.Trace("Log: " + log.Message);
break;
case PacketType.RuntimeError:
var runtime = (RuntimeErrorPacket)packet;
var rstack = (!string.IsNullOrEmpty(runtime.StackTrace) ? (Environment.NewLine + " " + runtime.StackTrace) : string.Empty);
Log.Error(runtime.Message + rstack);
break;
case PacketType.HandledError:
var handled = (HandledErrorPacket)packet;
var hstack = (!string.IsNullOrEmpty(handled.StackTrace) ? (Environment.NewLine + " " + handled.StackTrace) : string.Empty);
Log.Error(handled.Message + hstack);
break;
case PacketType.AlphaResult:
break;
case PacketType.BacktestResult:
var result = (BacktestResultPacket)packet;
if (result.Progress == 1)
{
var orderHash = result.Results.Orders.GetHash();
result.Results.Statistics.Add("OrderListHash", orderHash);
var statisticsStr = $"{Environment.NewLine}" +
$"{string.Join(Environment.NewLine, result.Results.Statistics.Select(x => $"STATISTICS:: {x.Key} {x.Value}"))}";
Log.Trace(statisticsStr);
}
break;
}
}
/// <summary>
/// Send any notification with a base type of Notification.
/// </summary>
public void SendNotification(Notification notification)
{
if (!notification.CanSend())
{
Log.Error("Messaging.SendNotification(): Send not implemented for notification of type: " + notification.GetType().Name);
return;
}
notification.Send();
}
/// <summary>
/// Dispose of any resources
/// </summary>
public void Dispose()
{
}
}
}
+17
View File
@@ -0,0 +1,17 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("QuantConnect.Messaging")]
[assembly: AssemblyProduct("QuantConnect.Messaging")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("ebbf7771-8d17-488e-86a6-b0f33f69612d")]
+49
View File
@@ -0,0 +1,49 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<RootNamespace>QuantConnect.Messaging</RootNamespace>
<AssemblyName>QuantConnect.Messaging</AssemblyName>
<TargetFramework>net10.0</TargetFramework>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<OutputPath>bin\$(Configuration)\</OutputPath>
<DocumentationFile>bin\$(Configuration)\QuantConnect.Messaging.xml</DocumentationFile>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Description>QuantConnect LEAN Engine: Messaging Project - The external messaging system implementation</Description>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<NoWarn>CA1062</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>full</DebugType>
<Optimize>$(SelectedOptimization)</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<Target Name="Print" BeforeTargets="Build">
<Message Text="SelectedOptimization $(SelectedOptimization)" Importance="high" />
</Target>
<ItemGroup>
<PackageReference Include="NetMQ" Version="4.0.1.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Common\Properties\SharedAssemblyInfo.cs" Link="Properties\SharedAssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Common\QuantConnect.csproj" />
<ProjectReference Include="..\Configuration\QuantConnect.Configuration.csproj" />
<ProjectReference Include="..\Logging\QuantConnect.Logging.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
</Project>
+131
View File
@@ -0,0 +1,131 @@
/*
* 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;
using System.Net;
using System.Net.Sockets;
using Newtonsoft.Json;
using QuantConnect.Configuration;
using QuantConnect.Interfaces;
using QuantConnect.Logging;
using QuantConnect.Notifications;
using QuantConnect.Packets;
using NetMQ;
using NetMQ.Sockets;
using QuantConnect.Orders.Serialization;
namespace QuantConnect.Messaging
{
/// <summary>
/// Message handler that sends messages over tcp using NetMQ.
/// </summary>
public class StreamingMessageHandler : IMessagingHandler
{
private string _port;
private PushSocket _server;
private AlgorithmNodePacket _job;
private OrderEventJsonConverter _orderEventJsonConverter;
/// <summary>
/// Gets or sets whether this messaging handler has any current subscribers.
/// This is not used in this message handler. Messages are sent via tcp as they arrive
/// </summary>
public bool HasSubscribers { get; set; }
/// <summary>
/// Initialize the messaging system
/// </summary>
/// <param name="initializeParameters">The parameters required for initialization</param>
public void Initialize(MessagingHandlerInitializeParameters initializeParameters)
{
_port = Config.Get("desktop-http-port");
CheckPort();
_server = new PushSocket("@tcp://*:" + _port);
}
/// <summary>
/// Set the user communication channel
/// </summary>
/// <param name="job"></param>
public void SetAuthentication(AlgorithmNodePacket job)
{
_job = job;
_orderEventJsonConverter = new OrderEventJsonConverter(job.AlgorithmId);
Transmit(_job);
}
/// <summary>
/// Send any notification with a base type of Notification.
/// </summary>
/// <param name="notification">The notification to be sent.</param>
public void SendNotification(Notification notification)
{
if (!notification.CanSend())
{
Log.Error("Messaging.SendNotification(): Send not implemented for notification of type: " + notification.GetType().Name);
return;
}
notification.Send();
}
/// <summary>
/// Send all types of packets
/// </summary>
public void Send(Packet packet)
{
Transmit(packet);
}
/// <summary>
/// Send a message to the _server using ZeroMQ
/// </summary>
/// <param name="packet">Packet to transmit</param>
public void Transmit(Packet packet)
{
var payload = JsonConvert.SerializeObject(packet, _orderEventJsonConverter);
var message = new NetMQMessage();
message.Append(payload);
_server.SendMultipartMessage(message);
}
/// <summary>
/// Check if port to be used by the desktop application is available.
/// </summary>
private void CheckPort()
{
try
{
TcpListener tcpListener = new TcpListener(IPAddress.Any, _port.ToInt32());
tcpListener.Start();
tcpListener.Stop();
}
catch
{
throw new Exception("The port configured in config.json is either being used or blocked by a firewall." +
"Please choose a new port or open the port in the firewall.");
}
}
/// <summary>
/// Dispose any resources used before destruction
/// </summary>
public void Dispose()
{
}
}
}