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.
|