using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using StockSharp.Algo;
using StockSharp.Algo.Strategies;
using StockSharp.Algo.Candles;
using StockSharp.Algo.Testing;
using StockSharp.BusinessEntities;
using StockSharp.Logging;
namespace OrderTest
{
class OpenCancelOrderStrategy:Strategy
{
CandleManager _candleManager; //Менеджер свечек
CandleSeries _series; //Серия однотиковых свечек
protected override void OnStarted()
{
//Начало
_candleManager = new CandleManager(this.Connector);
_series = new CandleSeries(typeof(TickCandle), this.Security, 1);
Connector.RegisterMarketDepth(this.Security);
_series
.WhenCandlesFinished() //Когда закончится тик
.Do(ProcessCandle) //Вызывать процедуру
.Apply(this);
_candleManager.Start(_series);
base.OnStarted();
}
protected override void OnStopped()
{
_candleManager.Stop(_series);
_series.Dispose();
_candleManager.Dispose();
base.OnStopped();
}
private int IsOrder = 0;
private void ProcessCandle(Candle candle)
{
if (IsOrder < 1)
{
if (Security.BestAsk == null)
return;
IsOrder++;
//var order = this.BuyAtMarket(this.Volume);
var price = Security.BestAsk.Price;
var order = this.CreateOrder(StockSharp.Messages.Sides.Buy, price, Volume);
order
.WhenRegistered()
.Do(_OrderRegistered)
.Apply(this);
order
.WhenRegisterFailed()
.Do(_OrderFailed)
.Apply(this);
order
.WhenCanceled()
.Do(_OrderCanceled)
.Apply(this);
order
.WhenCancelFailed()
.Do(_OrderCanceledFailed)
.Apply(this);
RegisterOrder(order);
var msg = String.Format("Заявка UserOrderID={0}; ID={1}; TrasID={2}; IsMatched={3}; IsMatchedEmpty={4}; IsMatchedPartially={5}; Vol={6}; Bal={7}; State={8}", order.UserOrderId, order.Id, order.TransactionId, order.IsMatched(), order.IsMatchedEmpty(), order.IsMatchedPartially(), order.Volume, order.Balance, order.State);
this.AddInfoLog(msg);
CancelOrder(order);
msg = "Заявку отменили";
this.AddInfoLog(msg);
}
}
private void _OrderFailed(OrderFail Error)
{
var msg = String.Format("[OnOrderFailed]: Ошибка при регистрации заявки: {0}; ThreadID: {1}; Ошибка: {2}; IsMatched={3}; IsMatchedEmpty={4}; IsMatchedPartially={5}; Vol={6}; Bal={7}; State={8}", Error.Order.Id, Thread.CurrentThread.ManagedThreadId, Error.Error, Error.Order.IsMatched(), Error.Order.IsMatchedEmpty(), Error.Order.IsMatchedPartially(), Error.Order.Volume, Error.Order.Balance, Error.Order.State);
this.AddInfoLog(msg);
}
private void _OrderRegistered(Order order)
{
var msg = String.Format("[OnOrderRegistered1]: Заявка зарегистрирована: {0}; ThreadID: {1}; IsMatched={2}; IsMatchedEmpty={3}; IsMatchedPartially={4}; Vol={5}; Bal={6}; State={7}", order.Id, Thread.CurrentThread.ManagedThreadId, order.IsMatched(), order.IsMatchedEmpty(), order.IsMatchedPartially(), order.Volume, order.Balance, order.State);
this.AddInfoLog(msg);
}
private void _OrderCanceled(Order order)
{
var msg = String.Format("[_OrderCanceled]: Заявка успешно отменена: {0}; ThreadID: {1}; IsMatched={2}; IsMatchedEmpty={3}; IsMatchedPartially={4}; Vol={5}; Bal={6}; State={7}", order.Id, Thread.CurrentThread.ManagedThreadId, order.IsMatched(), order.IsMatchedEmpty(), order.IsMatchedPartially(), order.Volume, order.Balance, order.State);
this.AddInfoLog(msg);
}
private void _OrderCanceledFailed(OrderFail Error)
{
var msg = String.Format("[_OrderCanceledFailed]: Ошибка при отмене заявки: {0}; ThreadID: {1}; Ошибка: {2}; IsMatched={3}; IsMatchedEmpty={4}; IsMatchedPartially={5}; Vol={6}; Bal={7}; State={8}", Error.Order.Id, Thread.CurrentThread.ManagedThreadId, Error.Error, Error.Order.IsMatched(), Error.Order.IsMatchedEmpty(), Error.Order.IsMatchedPartially(), Error.Order.Volume, Error.Order.Balance, Error.Order.State);
this.AddInfoLog(msg);
}
}
}