Overall Statistics
//  The Goal of this strategy is simply to learn the mechanics of QuantConnect
//  price targets, and limit orders.


using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;

namespace QuantConnect.Algorithm.CSharp
{
    public class LearningLimits : QCAlgorithm
    {

        decimal limitPct = 0.99999m;
        
  		bool OrderSubmitted = false;
  		bool ExitOrderSubmitted = false;
  		bool StopOrderSubmitted = false;
  		
  		
  		string ticker = "BTCUSD";
        decimal ProfitPct = .05m;
        decimal LossPct = -.02m;
        
        OrderTicket StoplimitOrderTicket;  // The debug says these are not being used in the strategy.
	 	OrderTicket limitOrderTicket;	// The debug says these are not being used in the strategy.
		OrderTicket StoplosslimitOrderTicket;	// The debug says these are not being used in the strategy.


        public override void Initialize()
        {
            SetStartDate(2017,12,01);  // Set Start Date
            SetEndDate(2018,02,15);    // Set End Date
            SetCash(22000);          // Set Strategy Cash

            AddCrypto(ticker, Resolution.Minute);


        }

            public override void OnData(Slice data)
            {
 

            if (!Portfolio.Invested && limitOrderTicket == null)  
            {
            	decimal LimitPrice = Decimal.Multiply(Securities[ticker].Price, limitPct);
            	
            	decimal LimitPrice2 = decimal.Round(LimitPrice, 2);
            
                OrderTicket limitOrderTicket = LimitOrder(ticker, 1, LimitPrice2);

                Log($"We set our Limit Order at: {LimitPrice2} while the price is: {Securities[ticker].Price}");
            }


            if (Portfolio.Invested && Portfolio[ticker].UnrealizedProfitPercent > ProfitPct && StoplimitOrderTicket == null)  
            {
            	
            	decimal LockedPrice = Portfolio[ticker].Price;
            	decimal ExitLimitPrice = Decimal.Multiply(LockedPrice,.99999m);
            	
            	OrderTicket StoplimitOrderTicket = StopLimitOrder(ticker, -1, LockedPrice, ExitLimitPrice);
            	
            	Log($"Profit - Limit Order Trigger: {LockedPrice} & Limit Order Price at: {ExitLimitPrice}");
            }
            
            if (Portfolio.Invested && Portfolio[ticker].UnrealizedProfitPercent < LossPct && StoplosslimitOrderTicket == null)
            {
            	decimal LossLockedPrice = Portfolio[ticker].Price;
            	decimal LossLockedLimit = Portfolio[ticker].Price + Decimal.Multiply(LossPct, Portfolio[ticker].Price);
            	
            	
            	OrderTicket StoplosslimitOrderTicket = StopLimitOrder(ticker, -1, LossLockedPrice, LossLockedLimit);
            	
            	Log($"STOP LOSS trigger at: {LossLockedPrice} with Loss Limit price at: {LossLockedLimit}");
            }
           
        }
    }
}