using System;
using System.Windows;
using System.IO;
using Ecng.Serialization;
using StockSharp.Algo;
using StockSharp.Algo.Candles;
using StockSharp.BusinessEntities;
using StockSharp.Configuration;
using StockSharp.Messages;
using StockSharp.Xaml.Charting;
namespace Getting_realtime_candles
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow
{
private CandleSeries _candleSeries;
private ChartCandleElement _candleElement;
private readonly Connector _connector = new Connector();
private const string _connectorFile = "ConnectorFile";
public MainWindow()
{
InitializeComponent();
if (File.Exists(_connectorFile))
{
_connector.Load(new XmlSerializer().Deserialize(_connectorFile));
}
CandleSettingsEditor.Settings = new CandleSeries
{
CandleType = typeof(TimeFrameCandle),
Arg = TimeSpan.FromMinutes(5),
};
}
private void Setting_Click(object sender, RoutedEventArgs e)
{
if (_connector.Configure(this))
{
new XmlSerializer().Serialize(_connector.Save(), _connectorFile);
}
}
private void Connect_Click(object sender, RoutedEventArgs e)
{
SecurityPicker.SecurityProvider = _connector;
_connector.Connect();
}
private void SecurityPicker_SecuritySelected(Security security)
{
if (security == null) return;
var _candleSeries = new CandleSeries(CandleSettingsEditor.Settings.CandleType, security, CandleSettingsEditor.Settings.Arg)
{
BuildCandlesMode = BuildCandlesModes.LoadAndBuild
};
if (_candleSeries != null) _connector.UnSubscribeCandles(_candleSeries);
//var _candleSeries = new CandleSeries(typeof(VolumeCandle), security, 10000m)
//var _candleSeries = new CandleSeries(typeof(TickCandle), security, 1000)
//var _candleSeries = new CandleSeries(typeof(RangeCandle), security, new Unit(0.1m))
//var _candleSeries = new CandleSeries(typeof(RenkoCandle), security, new Unit(0.1m))
//var _candleSeries = new CandleSeries(typeof(PnFCandle), security, new PnFArg() { BoxSize = 0.1m, ReversalAmount =1})
_candleSeries =
new CandleSeries(CandleSettingsEditor.Settings.CandleType, security, CandleSettingsEditor.Settings.Arg)
{
BuildCandlesMode = BuildCandlesModes.LoadAndBuild,
BuildCandlesFrom = MarketDataTypes.Trades,
IsCalcVolumeProfile = true
};
//-----------------Chart--------------------------------
Chart.ClearAreas();
var area = new ChartArea();
_candleElement = new ChartCandleElement();
Chart.AddArea(area);
_candleElement = new ChartCandleElement();
Chart.AddElement(area, _candleElement, _candleSeries);
_connector.SubscribeCandles(_candleSeries, DateTime.Today.Subtract(TimeSpan.FromDays(3)), DateTime.Now);
_connector.CandleSeriesProcessing += Connector_CandleSeriesProcessing;
}
private void Connector_CandleSeriesProcessing(CandleSeries candleSeries, Candle candle)
{
Chart.Draw(_candleElement, candle);
}
}
}