Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
-0.02%
Compounding Annual Return
-0.023%
Drawdown
0.000%
Expectancy
-1
Net Profit
-0.016%
Sharpe Ratio
-1.033
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-2.232
Tracking Error
0.1
Treynor Ratio
0.404
Total Fees
$4.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 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
        int _limitOrderId = 0;
        LimitOrder _limitOrder;
        string _symbol = "GBPUSD";
        decimal _price = 0;
        public SimpleMovingAverage SMA_Fast;
        public SimpleMovingAverage SMA_Slow;
        string direction = "bullish";
        bool crossover = false;
        int quantity = 1000;
        
        //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.Forex, _symbol, Resolution.Minute);
            
            SMA_Fast = SMA(_symbol, 60, Resolution.Minute);
            SMA_Slow = SMA(_symbol, 600, 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[_symbol].Close;
            
            //Create the first order
            if (_limitOrderId == 0) 
            {
                //Set our first order to less than MS's price: 
                _limitOrderId = LimitOrder(_symbol, (int)(Portfolio.Cash/_price), (_price * 0.95m));
                _limitOrder = (LimitOrder)Transactions.GetOrderById(_limitOrderId); 
                Debug("Created first limit order with " + _symbol + " Price: " + _price.ToString("C") + " id: " + _limitOrderId);
            }
            
            if(!SMA_Slow.IsReady) return;
            
            
            if ((SMA_Fast > SMA_Slow && direction == "bearish") || (SMA_Fast < SMA_Slow && direction == "bullish"))
            {
                crossover = true;
                Log("it is a crossover");
            }
            else 
            {
                Log("not a crossover; direction from last time is " + direction + " and SMA_Fast is " + SMA_Fast + " while SMA_Slow is " + SMA_Slow);
            } 
            
            if (SMA_Fast > SMA_Slow)
            {
                direction = "bullish";
            }
            else
            {
                direction = "bearish";
            }
            
            //Update the limit price once per week:
            if (crossover == true)
            {
            // liquidate current position on crossover    
               Order(_symbol, -Portfolio[_symbol].Quantity);
            
            // update limit order based on whether bearish or bullish   
                if (direction == "bullish")
                {
                   _limitOrder.Price = data[_symbol].High + .0001m;
                   _limitOrder.Quantity = quantity;
                   Transactions.UpdateOrder(_limitOrder);
                }
                else if (direction == "bearish")
                {
                   _limitOrder.Price = data[_symbol].Low - .0001m;
                   _limitOrder.Quantity = -quantity;
                   Transactions.UpdateOrder(_limitOrder);
                }
                else
                {
                    Log("some kind of error");
                }
            }
        }
        
        /*
        *   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);
            Plot("Limit Plot", "Limit", _limitOrder.LimitPrice);
        }
    }
}