Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
namespace QuantConnect.Algorithm.CSharp
{

    public class BasicTemplateAlgorithm : QCAlgorithm
    {
        private Symbol spy = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA);
        private HeikinAshi heikinHour;
        private HeikinAshi heikinDay;
        decimal spread = .0001m; 
        decimal price;
		OrderTicket lastOrderTicket = null;     
		decimal prezzOrdineAltoDelGiorno = 0;   
		bool condizioneDiSicurezza = true;

        
        
        public override void Initialize()
        {
            SetStartDate(2018, 10, 07);  //Set Start Date
            SetEndDate(DateTime.Now.Date.AddDays(-1)); 
            SetCash(100000);             //Set Strategy Cash

            AddEquity("SPY", Resolution.Hour);
            var eurusd = AddForex("EURUSD", Resolution.Hour, Market.Oanda);
            eurusd.SetDataNormalizationMode(DataNormalizationMode.Raw);
            heikinDay = HeikinAshi("EURUSD", Resolution.Daily);
            heikinHour = HeikinAshi("EURUSD", Resolution.Hour);

        }

        public override void OnData(Slice data)             
        {
        	var tickets = Transactions.GetOpenOrderTickets().ToList();
			var orders = Transactions.GetOpenOrders();
			var holdings_eurusd = Portfolio["EURUSD"].Quantity;
			
			if(heikinHour.IsReady) {
				Log(heikinHour.Close);
			}
			
			apriOrdineTest();			
			//controllaTicket(); 			      
        	
        }
        
        // Override the base class event handler for order events
        /*public override void OnOrderEvent(OrderEvent orderEvent)
        {
        	var lastOrder = Transactions.getOrderById(lastOrderId);
        	var order = Transactions.GetOrderById(orderEvent.OrderId);
        	Console.WriteLine("{0}: {1}: {2}", Time, order.Type, orderEvent);
        	
        }*/
        

        // funzione di prova che apre l'ordine /---------------------
        public void apriOrdineTest()
        {
				if(heikinHour.IsReady) {	// THIS ONE causes Runtime Error: Object reference not set to an instance of an object
					Log(heikinHour.Close);
					var heikinValue = heikinHour;	
				} 
				/*else 
				{
		        	if(condizioneDiSicurezza == true){
		        		if(lastOrderTicket == null) {lastOrderTicket = StopMarketOrder("EURUSD", 1, Decimal.Add(heikinHour.High, spread));} 
		        		// Runtime Error: Object reference not set to an instance of an object
		        		if(Transactions.GetOrderById(lastOrderTicket).Status == OrderStatus.Filled && Transactions.GetOrderById(lastOrderTicket).Price < heikinHour.High) {
		           			prezzOrdineAltoDelGiorno = Transactions.GetOrderById(lastOrderTicket).Price;
		        			lastOrderTicket = StopMarketOrder("EURUSD", 1, Decimal.Add(heikinHour.High, spread)); 
		        		}		        		
		        	} 
		        	else // condizione sicurezza
		        	{
		        		Log("Non ci sono le condizioni di sicurezza per procedere");
		        	}
	        	}*/
        	
        	/*if (lastOrderTicket == null){ // THIS ONE GOES
        	
				if(!heikinHour.IsReady) {	
					var heikinValue = heikinHour;	
				} 	
				else // heikin hour is ready
				{
					//if(){}
					Debug("lanciato apriOrdineTest()");
					lastOrderTicket = StopMarketOrder("EURUSD", 1, Decimal.Add(heikinHour.High, spread));
					Debug("lastOrderTicket = " + lastOrderTicket);
				}
        		
        	}*/
			
        } 
        
        public bool ordinePrecedente(){
			int lastOrder_id = Transactions.LastOrderId;
			var lastOrder_status = Transactions.GetOrderById(lastOrder_id).Status;
			var lastOrder_time = Transactions.GetOrderById(lastOrder_id).Time;
			//var lastOrder_ticket = Transactions.GetOrderById(lastOrder_id);

			Debug("lastOrder_status " + lastOrder_status.ToString() +" lastOrder_time " + lastOrder_time);
			
			/*if(lastOrder_status ==){return true;}
			else
			{return false;}*/
			
			return true;
			
			//Values: New, Submitted, PartiallyFilled, Filled, Canceled, None, Invalid, CancelPending
			
        }
        
        public void controllaTicket(){ //---------------------
        	decimal peggiorePerdita = 0;

        	var tickets = Transactions
        	.GetOrderTickets(x => x.Status == OrderStatus.Filled);
        	DateTime ticketTime = Time;
        	
			int worstOrderId = 0; 
			
            Debug("Average price of " + Portfolio["EURUSD"].Quantity + " fills at average " + Portfolio["EURUSD"].AveragePrice);

            foreach (var ticket in tickets){
            	var symbol = ticket.Symbol;
            	var fillQuantity = ticket.QuantityFilled;
            	var fillPrice = ticket.AverageFillPrice;
            	ticketTime = ticket.Time;
            	
            	if (fillPrice - price > peggiorePerdita){ 
            		peggiorePerdita = fillPrice - price; 
            		worstOrderId = ticket.OrderId;
            	}
            }                
        }              
    }
}