using System;
using System.Linq;
using StockSharp.Algo;
using StockSharp.Algo.Strategies;
using StockSharp.Algo.Strategies.Quoting;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace Run_strategy_in_real_trading
{
public class MQStrikeStrategy : Strategy
{
protected override void OnStarted()
{
Connector.MarketTimeChanged += Connector_MarketTimeChanged;
Connector_MarketTimeChanged(new TimeSpan());
base.OnStarted();
}
private MarketQuotingStrategy _strategyBuy;
private MarketQuotingStrategy _strategySell;
private void Connector_MarketTimeChanged(TimeSpan obj)
{
if (_strategyBuy != null && _strategyBuy.ProcessState != ProcessStates.Stopped) return;
if (_strategySell != null && _strategySell.ProcessState != ProcessStates.Stopped) return;
if (Position == 0)
{
_strategyBuy = new MarketQuotingStrategy(Sides.Buy, 1)
{
Name = "Buy " + Connector.CurrentTime,
Volume = 1,
PriceType = MarketPriceTypes.Following,
IsSupportAtomicReRegister = false
};
ChildStrategies.Add(_strategyBuy);
_strategySell = new MarketQuotingStrategy(Sides.Sell, 1)
{
Name = "Sell " + Connector.CurrentTime,
Volume = 1,
PriceType = MarketPriceTypes.Following,
IsSupportAtomicReRegister = false
};
ChildStrategies.Add(_strategySell);
}
else
if (Math.Abs(Position) == 1)
{
ChildStrategies.ToList().ForEach(s => s.Stop());
if (Position > 0)
{
var orderSell = new Order
{
Type = OrderTypes.Limit,
Direction = Sides.Sell,
Portfolio = Portfolio,
Security = Security,
Volume = Math.Abs(Position),
Price = Security.BestBid.Price,
};
RegisterOrder(orderSell);
}
else
if (Position < 0)
{
var orderBuy = new Order
{
Type = OrderTypes.Limit,
Direction = Sides.Buy,
Portfolio = Portfolio,
Security = Security,
Volume = Math.Abs(Position),
Price = Security.BestAsk.Price,
};
RegisterOrder(orderBuy);
}
}
}
}
}