Overall Statistics
using System;
using System.Collections;
using System.Collections.Generic; 
using QuantConnect.Securities;  
using QuantConnect.Models;   

namespace QuantConnect 
{   
    /*
    *   QuantConnect University - Creating and Updating Limit Orders 
    *
    *   This algorithm walks through an example of creating and updating a limit order.
    *   The orders are stored in the Transactions Manager inside the algorithm.
    *   
    *   For the demonstration we will place a buy limit order for 1 SD below of microsoft's price, 
    *   and every day update it until its filled 
    */ 
    public class QCULimitOrders : QCAlgorithm
    {
        //Access for the order we'll place
        Order _limitOrder;
        string _symbol = "MSFT";
        decimal _price = 0;
        int _rebalancePeriod = 20;
        DateTime _updatedDate;
        LimitedQueue<decimal> _queue = new LimitedQueue<decimal>(30);
        
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize()
        {
            SetStartDate(2013, 1, 1);
            SetEndDate(DateTime.Now.Date.AddDays(-1)); 
            SetCash(25000);
            AddSecurity(SecurityType.Equity, _symbol, Resolution.Minute);
        }

        //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
        public void OnData(TradeBars data) 
        {   
            //if (data.Contains("MSFT"))
            _price = data["MSFT"].Close;
            
            //Create the first order
            if (_limitOrder == null) 
            {
                //Set our first order to half of microsoft's price Jan-2013
                _limitOrder = new Order(_symbol, (int)(Portfolio.Cash/_price), OrderType.Limit, Time, (_price * 0.95m), "Our customized limit order");
                
                Debug("Creating first limit order: " + _symbol + " Price: " + _price.ToString("C"));
                
                var id = Transactions.AddOrder(_limitOrder);
                _limitOrder.Id = id;
            }
            
            //Update the limit price once per week:
            if (_updatedDate.Date < Time.Date)
            {
                _updatedDate = Time.Date.AddDays(_rebalancePeriod); 
                
                _limitOrder.Price = (_price * 0.95m);
                
                Transactions.UpdateOrder(_limitOrder);
            }
        }
        
        /*
        *   For our plotting we can show the limit price and MSFT to check if its hit.
        */
        public override void OnEndOfDay()
        {
            Plot("Limit Plot", "MSFT", _price);
            Plot("Limit Plot", "Limit", _limitOrder.Price);
            _queue.Enqueue(_price);
        }
    }
}