Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
NaN
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
NaN
Tracking Error
NaN
Treynor Ratio
NaN
Total Fees
$0.00
namespace QuantConnect 
{   
    /*
    *   QuantConnect University: Consolidators - Creating custom timespan events.
    *
    *   Consolidators are tools inside of LEAN to merge data into single bars. They are 
    *   very flexible and can generate events which trigger from any timespan.
    */
    public class ConsolidatorAlgorithm : QCAlgorithm
    {
        int _stopMarketOrderId = 0;
        StopMarketOrder _stopMarketOrder;

        public SimpleMovingAverage SMA_Fast;
        public SimpleMovingAverage SMA_Slow;
        
        string symbol = "EURGBP";

        public override void Initialize()
        {
            SetStartDate(2009, 1, 1);         
            SetEndDate(2014, 1, 1); 
            SetCash(25000);
            
            AddSecurity(SecurityType.Forex, symbol, Resolution.Minute);
            

            
            // define our SMAs
            SMA_Fast = SMA(symbol, 60, Resolution.Minute);
            SMA_Slow = SMA(symbol, 600, Resolution.Minute);
        }


        bool crossLong = false;
        bool crossShort = false;
        decimal breakoutLong = new decimal();
        decimal breakoutShort = new decimal();
        int quantity = 1000;

        /// <summary>
        /// This is our event handler for our daily trade bar defined above in Initialize(). So each time the consolidator
        /// produces a new daily bar, this function will be called automatically. The 'sender' parameter will be the
        /// instance of the IDataConsolidator that invoked the event, but you'll almost never need that!
        /// </summary>
        
         //Create the first order
            
            
        public void OnData(TradeBars data) 
        {
            if (!SMA_Slow.IsReady) return;
            
            Log("Current position is: " + Portfolio[symbol].Quantity);
            
        //first order set stopmMarket order @ high of tradebar 
           if (_stopMarketOrderId == 0)
           {
                _stopMarketOrderId = StopMarketOrder(symbol, quantity, data[symbol].High + .0001m);
                _stopMarketOrder = (StopMarketOrder)Transactions.GetOrderById(_stopMarketOrderId);
           }
           
        //if fast moving average > slow, we are bullish
            if (SMA_Fast > SMA_Slow)
            {
        //reset crossShort variable so next time we cross back to bearish we know we have just crossed
                crossShort = false;
        //if we are short, liquidate short position
                if (Portfolio[symbol].Quantity < 0)
                {
                    Log("Liqudiating" + Portfolio[symbol].Quantity + " of " + symbol);
                    Order(symbol, -Portfolio[symbol].Quantity);
                }
        //if first trade bar going from bearish to bullish, update stopMarket order to buy at market when price is at         
                if (crossLong == false)
               {
        // set crossLong variable to true so that this block will only execute on the first bar going from bearish to bullish           
                   crossLong = true;
                   Log("Going to go long if price is above " + data[symbol].High + " + .0001");
                   _stopMarketOrder.StopPrice = data[symbol].High + .0001m;
                   _stopMarketOrder.Quantity = quantity;
                   Log("Setting stop limit order at:" + data[symbol].High + " + .0001");
                   Transactions.UpdateOrder(_stopMarketOrder);
               } else if (crossLong == true)
               {
                  Log("Current position is " + Portfolio[symbol].Quantity + " and current StopMarket order is " + _stopMarketOrder.Quantity + " @ price:" + _stopMarketOrder.StopPrice);
               }
            }   
          
         // case if bearish      
           if (SMA_Fast < SMA_Slow)
           {
        //reset bullish counter
               crossLong = false;
        //liquidate an long positions       
                if (Portfolio[symbol].Quantity > 0)
                {
                    Log("Liqudiating " + Portfolio[symbol].Quantity + " of " + symbol);
                    Order(symbol,-Portfolio[symbol].Quantity);    
                }
         //if first trade bar after going from bullish to bearish       
                if (crossShort == false)
               {
        //change counter variable so this code will not execute until next time we cross bearish
                   crossShort = true;
                   Log("Going to go short if price is below " + data[symbol].Low + " - .0001");
                   _stopMarketOrder.StopPrice = data[symbol].Low - .0001m;
                   _stopMarketOrder.Quantity = -quantity;                   
                   Log("Setting stop limit order short at:" + data[symbol].Low + " - .0001");
                   Transactions.UpdateOrder(_stopMarketOrder);
               } else if (crossShort == true)
               {
                  Log("Current position is " + Portfolio[symbol].Quantity + " and current StopMarket order is " + _stopMarketOrder.Quantity + " @ price:" + _stopMarketOrder.StopPrice);
               }
           }
        }
    }
}