Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
-4.017%
Drawdown
15.900%
Expectancy
0
Net Profit
-4.007%
Sharpe Ratio
-0.088
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.035
Beta
1.116
Annual Standard Deviation
0.186
Annual Variance
0.035
Information Ratio
-0.176
Tracking Error
0.186
Treynor Ratio
-0.015
Total Fees
$1.19
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(2000, 1, 1);
            SetEndDate(2000, 12, 31); 
            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});
                }
            }
        }
        
        // Print the order event information to the console.
        public override void OnOrderEvent(OrderEvent orderEvent)
        {
            var order = Transactions.GetOrderById(orderEvent.OrderId);
            Console.WriteLine("{0}: {1}: {2}", Time, order.Type, orderEvent);
        }

        
        /*
        *   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));
            }
        }
    }
}