private void ConnectClick(object sender, RoutedEventArgs e)
{
if (Path.Text.IsEmpty())
{
MessageBox.Show(this, "Путь не выбран.");
return;
}
var storageRegistry = new StorageRegistry
{
// изменяем путь, используемый по умолчанию
DefaultDrive = new LocalMarketDataDrive(Path.Text)
};
var secCode = "RIU4";
var board = ExchangeBoard.GetOrCreateBoard("Forts");
var security = new Security
{
Id = "RIU4@FORTS", // по идентификатору инструмента будет искаться папка с историческими маркет данными
Code = secCode,
PriceStep = 10,
StepPrice = 2,
MinPrice = 10,
MaxPrice = 1000000,
MarginBuy = 10000, // задаем ГО
MarginSell = 10000,
Board = ExchangeBoard.Forts,
};
var portfolio = new Portfolio
{
Name = "TEST",
BeginValue = 500000m,
CurrentValue = 500000m,
Board = ExchangeBoard.GetBoard(instruments.First().strategyParams.exchange),
};
realTimeTradeEmulation = new HistoryEmulationConnector(new List<Security>() { security }, new List<Portfolio>() { portfolio } , storageRegistry)
{
MarketEmulator =
{
Settings =
{
UseCandlesTimeFrame = System.TimeSpan.Zero,
MatchOnTouch = true,
}
},
StorageRegistry = storageRegistry,
};
realTimeTradeEmulation.NewSecurities += securities =>
{
if (securities.All(s => s != security))
return;
realTimeTradeEmulation.RegisterSecurity(security);
realTimeTradeEmulation.RegisterTrades(security);
realTimeTradeEmulation.RegisterPortfolio(portfolio);
realTimeTradeEmulation.RegisterMarketDepth(security);
realTimeTradeEmulation.RegisterMarketDepth(new TrendMarketDepthGenerator(realTimeTradeEmulation.GetSecurityId(security))
{
Interval = TimeSpan.FromMilliseconds(30),
MaxAsksDepth = 10,
MaxBidsDepth = 10,
UseTradeVolume = true,
MaxVolume = 100,
MinSpreadStepCount = 2, // минимальный генерируемый спред - 2 минимальных шага цены
MaxSpreadStepCount = 2, // не генерировать спрэд между лучшим бид и аск больше чем 5 минимальных шагов цены - нужно чтобы при генерации из свечей не получалось слишком широкого спреда.
MaxPriceStepCount = 2 // максимальное количество шагов между ценами,
});
};
realTimeTradeEmulation.NewPortfolios += myPortfolios => this.GuiAsync(() => _portfolios.Portfolios.AddRange(myPortfolios));
realTimeTradeEmulation.NewStopOrders += stopOrders => this.GuiAsync(() => _orders.Orders.AddRange(stopOrders));
realTimeTradeEmulation.NewOrders += orders => this.GuiAsync(() => _orders.Orders.AddRange(orders));
realTimeTradeEmulation.NewMyTrades += trades => this.GuiAsync(() =>
{
instruments.ForEach(elem =>
{
if (elem.isFormed)
{
var currentElemTrades = trades.Where(t => elem.strategy.Orders.Any(o => o == t.Order));
this.GuiAsync(() => _trades.Trades.AddRange(currentElemTrades));
currentElemTrades.ToList().ForEach(strategyTrade =>
{
var tradeTime = elem.strategy.lastWorkedCandle == null ? strategyTrade.Order.Time : elem.strategy.lastWorkedCandle.OpenTime;
this.GuiAsync(() => elem.chart.ProcessValues(tradeTime, new Dictionary<IChartElement, object> { { elem.tradesIndicator, strategyTrade } }));
});
}
});
});
realTimeTradeEmulation.StateChanged += (oldState, newState) =>
{
if (realTimeTradeEmulation.State == EmulationStates.Stopped)
{
this.GuiAsync(() =>
{
if (realTimeTradeEmulation.IsFinished)
MessageBox.Show("Закончено");
else
MessageBox.Show("Отменено");
});
}
else if (realTimeTradeEmulation.State == EmulationStates.Started)
{
realtimeCandleManager.Processing += (s, candle) =>
{
if (candle.State == CandleStates.Finished)
_buffer.Add(candle);
};
realtimeCandleManager.Start(instruments.First().series);
instruments.First().strategy.Start();
}
};
realTimeTradeEmulation.Connect();
realTimeTradeEmulation.StartExport();
realtimeCandleManager = new CandleManager(realTimeTradeEmulation);
CreateStrategyFromInstrument(security, instruments.First().strategyParams, instruments.First(), portfolio);
realTimeTradeEmulation.Start(From, To);
}