NexusFi: Find Your Edge


Home Menu

 





Get Excel Data into Ninja Trader


Discussion in Traders Hideout

Updated
      Top Posters
    1. looks_one LionAgainst with 2 posts (0 thanks)
    2. looks_two Quick Summary with 1 posts (0 thanks)
    3. looks_3 rleplae with 1 posts (1 thanks)
    4. looks_4 addchild with 1 posts (0 thanks)
    1. trending_up 1,245 views
    2. thumb_up 1 thanks given
    3. group 3 followers
    1. forum 4 posts
    2. attach_file 0 attachments




 
Search this Thread

Get Excel Data into Ninja Trader

  #1 (permalink)
LionAgainst
Munich
 
Posts: 2 since Feb 2019
Thanks Given: 1
Thanks Received: 0

Hi,
i have an oscilator in excel based on cot data that saves a value depending on the date.
My mission is to access from NinjaTrader that excel file, search for todays date and give back the value to NinjaTrader.
I have already worked it out in VisualStudio, but i am now struggling to access the excel file with the same logic. And i wonder what my problem is. I have referenced the Microsoft.Office.Interop.Excel.dll in NinjaTrader but i dont know how to test if it actually works in NinjaTrader. Currently i just try to test it by running a backtest, but unfortunately it wont run the code linked to the excel file but only the strategy based on NinjaTraders indicators.
i will enclose the code from the already working visual studio application. I welcome every help on how to translate that code into Ninja and get it working


 
Code
public class KKK : Strategy
	{
		
		Microsoft.Office.Interop.Excel.Application excelApp;
        Microsoft.Office.Interop.Excel._Workbook excelWorkBook;
        Microsoft.Office.Interop.Excel._Worksheet excelSheet;
        Microsoft.Office.Interop.Excel.Range range;
		private string excelFile = @"C:\\Users\\Felix\\Documents\\C#\\ExcelAnfang\\Mappe6.xlsx";
		private string excelSheetName = "Tabelle";
		private bool excelSheetFound = false;
		private bool excelOpen = false;
		private string fullFileName;///dkasl
        private string simpleFileName;///aklsdf
            
			
	
	
	private void OpenWorkbook(string FileName)
        {
            try
            {
                excelApp = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
            }
            catch
            {
                excelApp = new Microsoft.Office.Interop.Excel.Application();
            }
            
            try
            {
               excelWorkBook = excelApp.Workbooks.Open(excelFile);
            }
            catch
            {
                excelWorkBook = (Microsoft.Office.Interop.Excel._Workbook) (excelApp.Workbooks.Open(excelFile,
                    false, true, Type.Missing,Type.Missing, Type.Missing, Type.Missing,Type.Missing,
                    Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing,Type.Missing));
            }
        }  
		
		private Microsoft.Office.Interop.Excel.Worksheet FindSheet(Microsoft.Office.Interop.Excel._Workbook excelWorkBook, string excelSheetName)
        {    
            foreach (Microsoft.Office.Interop.Excel.Worksheet excelSheet in excelWorkBook.Sheets)    
            {        
                if (excelSheet.Name == excelSheetName) return excelSheet;   
            }    
            return null;
        }
		
		 private void SetUpSpreadsheet()
        {
            OpenWorkbook(excelFile);
            excelSheet = (Microsoft.Office.Interop.Excel._Worksheet)FindSheet(excelWorkBook, excelSheetName);
			excelOpen = true;
            
                    

            // Set up some column colours
            
        }
		
	


public int searchExcelDate(int i)
    {
        
        
        var PCTime = DateTime.Today;
        string sDate = (excelSheet.Cells[i, 1] as Microsoft.Office.Interop.Excel.Range).Value2.ToString(); //holt Datum als String aus Zelle
        double date = double.Parse(sDate); // Double Datum der Zelle
        var excelTime = DateTime.FromOADate(date); //Excel Zeit -- Vergleich

        //Console.WriteLine(date);
        while (DateTime.Compare(excelTime, PCTime) != 0)
        {
            if ((excelSheet.Cells[i + 1, 1] as Microsoft.Office.Interop.Excel.Range).Value != null)
            {
                i++;
                sDate = (excelSheet.Cells[i, 1] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();
                date = double.Parse(sDate);
                excelTime = DateTime.FromOADate(date);
            }
            else
            {
                return -1;
            }
        }
        Console.WriteLine("Datum gefunden!");
        return i;
    }

    

public double giveValueFromToday()
    {
        int d = searchExcelDate(1);
        
        if(d > 0)
        {
            double wert = double.Parse((excelSheet.Cells[d, 2] as Microsoft.Office.Interop.Excel.Range).Value.ToString());
			Console.WriteLine(wert);
            return wert;
        }
        else
        {
            Console.WriteLine("Datum nicht gefunden!");
            return -1;
        }
     }
		
	
	
           
		protected override void OnBarUpdate()
		{
			
			
			if (BarsInProgress != 0) 
				return;

			if (CurrentBars[0] < 1)
				return;
			if (CurrentBar < BarsRequiredToTrade)
				return;
			
			KKK hallo = new KKK();
	
                         if(excelOpen == false)
			{
				hallo.SetUpSpreadsheet();
			}
			cotData = hallo.gibWertVonAktuellemDatum();
			Console.WriteLine("Sieht man das?");
			System.Windows.Forms.MessageBox.Show("sieht man das?");
			
			excelApp.Visible = false;


	}
		
	
		#region Properties
       
        
        [Description("Excel File Name with full path. Information will be displayed in an Excel Spreadsheet which is included with this indicator")]
        public string ExcelFile
        {
            get { return excelFile; }
            set { excelFile = value; }
        }
        
        [Description("Excel Sheet name e.g. Sheet1, Sheet2, Sheet3. Case Sensitive")]
        public string ExcelSheetName
        {
            get { return excelSheetName; }
            set { excelSheetName = value; }
        }
        #endregion
	
	}
	}

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
ZombieSqueeze
Platforms and Indicators
What broker to use for trading palladium futures
Commodities
MC PL editor upgrade
MultiCharts
REcommedations for programming help
Sierra Chart
Trade idea based off three indicators.
Traders Hideout
 
  #3 (permalink)
 
rleplae's Avatar
 rleplae 
Gits (Hooglede) Belgium
Legendary Market Wizard
 
Experience: Master
Platform: NinjaTrader, Proprietary,
Broker: Ninjabrokerage/IQfeed + Synthetic datafeed
Trading: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
Posts: 3,003 since Sep 2013
Thanks Given: 2,442
Thanks Received: 5,863


you will have to add that library as an external reference
so that Ninja know how to resolve and link it

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #4 (permalink)
LionAgainst
Munich
 
Posts: 2 since Feb 2019
Thanks Given: 1
Thanks Received: 0


rleplae View Post
you will have to add that library as an external reference
so that Ninja know how to resolve and link it

Ok thank you! But i dont really understand what you mean. I have referenced the Microsoft.Office.Interop.Excel.dll in NinjaTrader and moved that file in ninjatrader>bin>custom. If its what you mean? or what should i do
thank you

Reply With Quote
  #5 (permalink)
 addchild 
Bay Area California
 
Experience: None
Platform: TT T4
Broker: Phillip Capital
Trading: Futures
Posts: 809 since Nov 2011
Thanks Given: 926
Thanks Received: 898

If you already successfully demonstrated the logic in visual studio, it should be pretty trivial to test it in ninjatrader. You can attach the visual studio debugger to a ninjascript strategy and hit any breakpoints you set to check that the excel workbook is being correctly bound to and retrieving your desired values.


Sent using the NexusFi mobile app

.
Reply With Quote




Last Updated on September 28, 2019


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts