Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
14.149%
Drawdown
7.700%
Expectancy
0
Net Profit
0%
Sharpe Ratio
1.314
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.001
Beta
0.773
Annual Standard Deviation
0.105
Annual Variance
0.011
Information Ratio
-0.669
Tracking Error
0.063
Treynor Ratio
0.179
Total Fees
$1.00
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 5% below of SPY's price, 
    *   and every 5 days update it until its filled 
    */ 
    public class QCULimitOrders : QCAlgorithm
    {
        //Access for the order we'll place
        OrderTicket _limitOrder;
        string _symbol = "SPY";
        decimal _price = 0;
        int _rebalancePeriod = 5;
        DateTime _updatedDate;
        
        //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.Daily);
        }

        //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) 
        {
            _price = data[_symbol].Close;
            
            //Create the first order
            if (_limitOrder == null) 
            {
                //Set our first order to less than SPY's price:
                var quantity =(int)(Portfolio.Cash/_price);
                _limitOrder = LimitOrder(_symbol, quantity, (_price * 0.95m));
                Debug("Created limit order with " + _symbol + " Price: " + _price.ToString("C") + " id: " + _limitOrder.OrderId);
            }
            
            //Update the limit price once per week:
            if (_updatedDate.Date < Time.Date && !Portfolio.Invested)
            {
                _updatedDate = Time.Date.AddDays(_rebalancePeriod); 
                
                var newLimitPrice = _price*0.95m;
                var currentLimitPrice = _limitOrder.Get(OrderField.LimitPrice);
                if (newLimitPrice > currentLimitPrice)
                {
                    //Submit the request to update our limit order
                    _limitOrder.Update(new UpdateOrderFields{LimitPrice = newLimitPrice});
                }
            }
        }
        
        /*
        *   For our plotting we can show the limit price and MSFT to check if its hit.
        */
        public override void OnEndOfDay()
        {
            Plot("Limit Plot", _symbol, _price);
            if (_limitOrder.Status != OrderStatus.Filled)
            {
                Plot("Limit Plot", "Limit", _limitOrder.Get(OrderField.LimitPrice));
            }
        }
    }
}