Overall Statistics
Total Trades
159
Average Win
2.00%
Average Loss
-1.15%
Compounding Annual Return
14.594%
Drawdown
9.300%
Expectancy
0.250
Net Profit
24.754%
Sharpe Ratio
1.297
Loss Rate
54%
Win Rate
46%
Profit-Loss Ratio
1.74
Alpha
0.004
Beta
0.977
Annual Standard Deviation
0.109
Annual Variance
0.012
Information Ratio
0.026
Tracking Error
0.017
Treynor Ratio
0.145
Total Fees
$159.00
namespace Quantconnect
{
public class Beginning : QCAlgorithm
    {
        string _symbol = "SPY";
        WilliamsPercentR _willr;
        const decimal StopLossPercent = 0.01m ;
        const decimal take_profit = 0.02m ;
        decimal _price;
        // decimal five_perc = 0.05m;
    	int loss = 0 ;
    	private OrderTicket CurrentOrder;
    	private OrderTicket StopLoss;
		private OrderTicket TakeProfit;
    	
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize()
        {
            //Initialize
            SetStartDate(2016, 1, 1);         
            SetEndDate(2017, 8, 15); 
            SetCash(25000);
            
            //Add as many securities as you like. All the data will be passed into the event handler:
            AddSecurity(SecurityType.Equity, _symbol, Resolution.Minute);
            // var fiveMinuteConsolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(5));
            
            _willr = WILR(_symbol, 9, Resolution.Minute);
            // RegisterIndicator(_symbol, _willr , fiveMinuteConsolidator);
            
			// SubscriptionManager.AddConsolidator(_symbol, fiveMinuteConsolidator);	
			// fiveMinuteConsolidator.DataConsolidated += FiveMinuteHandler;
            //Set up Indicators:

        }

        public void OnData(TradeBars data) 
        {   
            if (!_willr.IsReady) return;
            
            _price = data[_symbol].Close;
            
            if (!Portfolio.HoldStock && (_willr < -80 )) 
            { 
                // int quantity = ((int)Math.Floor((Portfolio.Cash / _price)*five_perc));
				int quantity = (int)Math.Floor((Portfolio.Cash / _price));
                // int quantity =1 ;
				
				
                //Order function places trades: enter the string symbol and the quantity you want:
                	CurrentOrder = Order(_symbol,  quantity);
            	 	StopLoss = StopMarketOrder(_symbol, -quantity, _price * (1m - StopLossPercent));
					TakeProfit = LimitOrder(_symbol, -quantity, _price * (1m + take_profit));
                //Debug sends messages to the user console: "Time" is the algorithm time keeper object 
                	Debug("Purchased SPY on " + CurrentOrder ) ;
            		Debug("Stop loss " + StopLoss) ;
            		Debug("Take Profit " + TakeProfit) ;
            		
            }
			if (Portfolio.HoldStock)
			
            	if (_price < StopLoss)
            	{
            		loss++;
            		Debug("Stop Loss Hit "  + Time.ToShortDateString());
            		string log_loss = loss.ToString();
            		Log(log_loss);
            		
            	}
				else if (_price >= TakeProfit)
            	{
            		loss = 0;
            		// Debug("Take Profit Hit "  + Time.ToShortDateString());
            	}
		}


        
	public override void OnEndOfDay() 
        {
            if (!_willr.IsReady) return;            
            Plot("WilliamsPercentR", _willr);
        }
	public override void OnOrderEvent(OrderEvent orderEvent)
		{
			// Ignore OrderEvents that are not closed
			if (!orderEvent.Status.IsClosed())
			{
				return;
			}
			
			// Defensive check
			if (TakeProfit == null || StopLoss == null)
			{
				return;
			}
			
			var filledOrderId = orderEvent.OrderId;

			// If the ProfitTarget order was filled, close the StopLoss order
			if (TakeProfit.OrderId == filledOrderId)
			{
				StopLoss.Cancel();
			}

			// If the StopLoss order was filled, close the ProfitTarget
			if (StopLoss.OrderId == filledOrderId)
			{
				TakeProfit.Cancel();
			}
		}
    }
}