Click here to Login




Reading and Exporting Data from QuantShare to Excel Programmatically

Updated on 2014-01-21





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 equity = new 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?












3 comments (Log in)

QuantShare Blog
QuantShare
Search Posts




QuantShare
Recent Posts

Create Graphs using the Grid Tool
Posted 1233 days ago

Profile Graphs
Posted 1338 days ago

QuantShare
Previous Posts

Troubleshooting a Trading System
Posted 3751 days ago


More Posts

Back







QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright © 2024 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.