Click here to Login




Export Trading Orders from a Portfolio to a File

Updated on 2013-11-16





After generating orders from a trading system, the next step is usually to execute these orders in your broker platform.

Some brokers allow you to bulk import trades and thus the ability to export orders generated by the portfolio tool of QuantShare can save you huge amount of time. Of course, exporting orders can be useful in several other ways too.


Run a Portfolio Programmatically

- Select "Tools" then "Script Editor"
- Create a new script by selecting "File" then "New"
- To run a portfolio called "MyPorfolio", type the following script then click on "Execute" button at the top:

Portfolio.Run("MyPorfolio");


Export Portfolio's Orders Programmatically

Now, we want to get orders generated by this portfolio, add them to a file then save the file on your computer so that you can use it for example to bulk import trades in your broker platform.

Here is the script to do this:

string portfolioName = "test";
PortfolioTask task = Portfolio.Run(portfolioName);
// Wait until new signals are generated from the portfolio
while(!task.IsCompleted)
{
App.Main.Sleep(1000);
}

// Get portfolio
QPortfolio portfolio = Portfolio.GetPortfolio(portfolioName);
// Get pending orders
Order[] orders = portfolio.GetOrders();

// Create csv file
System.Text.StringBuilder text = new System.Text.StringBuilder("");
for(int i=0;i < orders.Length;i++)
{
Order order = orders[i];
text.AppendLine(order.Symbol + ";" + order.Shares);
// Remove "//" if you want to submit orders in QuantShare programmatically
//order.SubmitOrder();
}

// Save the file under "c:\temp", update this directory or make sure it exists
System.IO.File.WriteAllText("c:\\temp\\" + portfolioName + "_orders.txt", text.ToString());



In the above script, each line added to the csv file is created using the "text.AppendLine" instruction. As you can see the format is here: Symbol;NbShares
Of course, you can update the format of your csv file to match your broker format.

You can for example, add whether to enter or exit a position by creating a new "type" variable:

string type = "Exit";
if(order.OrderOrigin == "Buy" || order.OrderOrigin == "Short")
{
type = "Enter";
}
text.AppendLine(order.Symbol + ";" + order.Shares + ";" + type);

// OrderOrigin contains one of the following values: Buy, Sell, Short, Cover or Stop.


The same idea of exporting trades/orders is also available in the following post:
Running QuantShare on Amazon's EC2 Cloud & Automate Strategies Signals


Exporting Orders for Several Portfolios At Once

You can repeat the process for several portfolios by adding few lines of code.
The new script will contain a list that you fill with portfolio names.The script loops through each portfolio in the list and executes the previous code.

Code:

ArrayList list = new ArrayList();
list.Add("portfolio1");
for(int i=0;i < list.Count;i++)
{
string portfolioName = list[i].ToString();
/// Here you must copy the script we used above to export trades (just remove the first line);
}



Bookmark Panel

Finally, it would be much easier if you add a button that executes that script in the bookmark panel. You will no longer need to open the script editor to execute the script. Just click on a button under QuantShare menus and you are done.

In the script editor, click on "Settings -> Add current script to bookmark panel"

Don’t know what the bookmark panel is?
Please read this.












2 comments (Log in)

QuantShare Blog
QuantShare
Search Posts




QuantShare
Recent Posts

Create Graphs using the Grid Tool
Posted 1239 days ago

Profile Graphs
Posted 1344 days ago

QuantShare
Previous Posts

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.