Files
quantconnect--lean/Engine/StateCheck.cs
T
snugs 4de2ec4070 Make Engine an instance
Updated all interfaces to accept dependencies via Initialize methods
2015-06-18 13:50:11 -04:00

89 lines
3.2 KiB
C#

/*
* 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.Threading;
using QuantConnect.Interfaces;
using QuantConnect.Lean.Engine.Results;
using QuantConnect.Logging;
namespace QuantConnect.Lean.Engine
{
/// <summary>
/// Algorithm status monitor reads the central command directive for this algorithm/backtest. When it detects
/// the backtest has been deleted or cancelled the backtest is aborted.
/// </summary>
public class StateCheck
{
/// DB Ping Class
public class Ping
{
// set to true to break while loop in Run()
private static volatile bool _exitTriggered;
private readonly AlgorithmManager _algorithmManager;
private readonly IApi _api;
private readonly IResultHandler _resultHandler;
public Ping(AlgorithmManager algorithmManager, IApi api, IResultHandler resultHandler)
{
_algorithmManager = algorithmManager;
_api = api;
_resultHandler = resultHandler;
}
/// DB Ping Run Method:
public void Run()
{
while (!_exitTriggered)
{
try
{
Thread.Sleep(500);
if (_algorithmManager.AlgorithmId != "" && _algorithmManager.QuitState == false)
{
//Get the state from the central server:
var state = _api.GetAlgorithmStatus(_algorithmManager.AlgorithmId);
//Set state via get/set method:
_algorithmManager.SetStatus(state.Status);
//Set which chart the user is look at, so we can reduce excess messaging (e.g. trading 100 symbols, only send 1).
_resultHandler.SetChartSubscription(state.ChartSubscription);
}
}
catch (ThreadAbortException)
{
return;
}
catch (Exception err)
{
Log.Error("StateCheck.Run(): Error in state check: " + err.Message);
}
}
}
/// <summary>
/// Send an exit signal to the thread
/// </summary>
public static void Exit()
{
_exitTriggered = true;
}
}
}
}