Want to use Excel with QuantShare? Getting data from Excel to QuantShare and exporting data from QuantShare to Excel? In this post, I will show you how to do that using QuantShare scripts and also give you some examples to get you started. Excel COM Automation First of all, before the example, we must add some lines of code to scripts that are going to automate Excel. The code can be found here: Excel COM Functions You just have to add it at the bottom after your own code. In the examples, we will reference this code by the following tag ([excelcode]). When you find this tag, just replace it by the above code. You must also add a reference to excel DLL in each script. To do this: - click on "Menu" button at the bottom of the script editor - Select "Add references" - Click on "Add Reference" - Select "Microsoft.Office.Interop.Excel.dll" from the QuantShare folder Exporting Strategy's Equity Line Create a new money management script from "Analysis -> Advanced Money Management" then type the following code. Global Event: (Add reference to "Microsoft.Office.Interop.Excel.dll") List [excelcode] OnEndPeriod Event: equity.Add(Portfolio.Equity); OnEndSimulation Event: Excel.Worksheet sheet = CreateExcel(); string[] data = new string[equity.Count + 1]; data[0] = "Equity"; for(int i=0;i < equity.Count;i++) { data[i + 1] = equity[i].ToString(); } FillExcel(sheet, 1, data); - Save the money management script then add it to your trading system - Each time you backtest this trading system, an excel document is automatically opened and filled with equity data. This money management script is already available in the sharing server. You can download it directly from here: Equity Export To Excel Exporting Data of Selected Chart - Create a global script using "Tools -> Script Editor" - Type the following code then add it to the bookmark panel by select "Settings -> Add Current Script to Bookmark Panel" Excel.Worksheet sheet = CreateExcel(); Chart chart = Charts.GetSelectedChart(); if(chart != null) { sheet.Cells.ClearContents(); double[] close = chart.ChartData.GetTimeSeries("close"); int count = close.Length; string[,] data = new string[count, 2]; for(int i=0;i < count;i++) { data[i, 0] = chart.ChartData.IndexToDate(i).ToString(); data[i, 1] = close[i].ToString(); } FillExcel(sheet, data); } [excelcode] - To export date/close data to an excel file, simply select a chart and double click on the script (in the bookmark panel) Reading Data from Excel - Create a global script using "Tools -> Script Editor" then use the following script: Excel.Worksheet sheet = OpenExcel("filename"); while(true) { Point symbol = new Point(1, 1); Point value = new Point(1, 2); string symbol1 = ((Excel.Range)sheet.Cells[symbol.X, symbol.Y]).Text.ToString(); string value1 = ((Excel.Range)sheet.Cells[value.X, value.Y]).Text.ToString(); double value2; if(Double.TryParse(value1, out value2)) { Global.Trace(symbol1 + " - " + value2); } App.Main.Sleep(1000); } [excelcode] This script simply reads the first and second cell of an excel file and adds the data in the output (View -> Output). The data is read once a minute. Don’t forget to replace "filename" by your excel file location. If you want to contribute to this with new code and ideas please check the thread created by our friend Alexander: An example for Excel COM automation - Further joint development?
|