Overall Statistics
//Copyright HardingSoftware.com, granted to the public domain.
//Use entirely at your own risk.
namespace QuantConnect.Algorithm.CSharp
{
    public class M : QCAlgorithm
    {
        string ticker = "IMGN";
		int holdCount= 5;
		int count=0;
		decimal lowerRatio=0.985m;
		decimal upperRatio=1.025m;
		OrderTicket orderTicket;
		decimal leverage=.999m;
		decimal increment=0.01m;
        public override void Initialize()
        {
            SetStartDate(2019, 7, 20);  //Set Start Date
           // SetEndDate(2019, 8, 8);    //Set End Date
            SetCash(8500);             //Set Strategy Cash

            AddEquity(ticker, Resolution.Hour);
            SetBenchmark(ticker);
            
            Securities[ticker].FeeModel = new ConstantFeeModel(0.1m);
        }
        
        public override void OnData(Slice data)
        {
        	count++;
        	if (count<holdCount)
        	{
        		return;
        	}
        	count=0;
        	
        	if (data[ticker]==null || data.ContainsKey(ticker)==false)
        	{
        		return;
        	}
        	
        	decimal price=data[ticker].Close;
            if (!Portfolio.Invested)
            {
            	if (orderTicket!=null)
            	{
            		orderTicket.Cancel();
            	}
            	//int quantity=(int)(leverage*Portfolio.Cash/price);
            	int quantity=(int)(leverage*7000m/price);
            	decimal buyPrice=FloorToIncrement(lowerRatio*price,increment);
				orderTicket = LimitOrder(ticker, quantity, buyPrice);
            }
            else
            {
            	if (orderTicket!=null)
            	{
            		orderTicket.Cancel();
            	}
            	int quantity=(int)Portfolio[ticker].Quantity;
            	decimal sellPrice=CeilingToIncrement(upperRatio*price,increment);
				orderTicket = LimitOrder(ticker, -quantity, sellPrice);
            }
        }
        
        public decimal RoundToIncrement(decimal roundMe, decimal increment)
        {
        	return (decimal)(Math.Round(roundMe/increment)*increment);
        }
        
        public decimal FloorToIncrement(decimal roundMe, decimal increment)
        {
        	return (decimal)(Math.Floor(roundMe/increment)*increment);
        }
        
        public decimal CeilingToIncrement(decimal roundMe, decimal increment)
        {
        	return (decimal)(Math.Ceiling(roundMe/increment)*increment);
        }
   } 
}