Create ELS Order(O2GO, CS)

From FxCodeBaseWiki

Jump to: navigation, search

The example shows how to create an ELS order with an entry order as a primary order.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
 
class SampleTradeOpener
{
    /*
     *  This class does a simple operation of getting some info from Accounts table and Offers table and using that
     *  data creates an Entry Order with Entry Stop (ELS).
    */
    static FXCore.CoreAut  fxCore;
    static FXCore.TradeDeskAut  tradeDesk;
 
    static string username;
    static string password;
    static string connection = "Demo";
    static string url = "www.fxcorporate.com";
 
    public static void Main(string[] args)
    {
        if(args.Length < 2)
        {
            Console.WriteLine("USAGE: [application].exe [username] [password]");
            return;
        }
 
        username = args[0];
        password = args[1];
 
        #region Initialize
        //--------------------------------------------------------------------------------
        // Create FXCore and TradeDesk
        //--------------------------------------------------------------------------------
        Console.WriteLine("Creating objects....");
        fxCore = new FXCore.CoreAut();
        tradeDesk = (FXCore.TradeDeskAut)fxCore.CreateTradeDesk("trader");
        // Set an extended row filter to include stop orders into the orders table
        tradeDesk.SetRowsFilterType(tradeDesk.ROWSFILTER_EXTENDED);
        Console.WriteLine("Objects created.");
        #endregion
 
        #region Login
        //--------------------------------------------------------------------------------
        // Login process
        //--------------------------------------------------------------------------------
        Console.WriteLine("Logging into the trading desk...");
        tradeDesk.Login(username, password, url, connection);
        Console.WriteLine("Logged in.");
        #endregion
 
        #region Get all needed data
        //--------------------------------------------------------------------------------
        // Get information about an offer
        //--------------------------------------------------------------------------------
        Console.WriteLine("Getting object reference to table 'Offers' and getting Instrument, Ask, and QuoteID for ANY first available instrument...");
        FXCore.TableAut offers = (FXCore.TableAut)tradeDesk.FindMainTable("offers");
        string inst = (string)offers.CellValue(1, "Instrument");
        double rate = (double)offers.CellValue(1, "Ask");
        double pointSize = (double)offers.CellValue(1, "PointSize");
        Console.WriteLine("Got the table. Instrument: {0}, Ask Rate: {1}.", inst, rate);
 
        //--------------------------------------------------------------------------------
        // Get information about an account
        //--------------------------------------------------------------------------------
        Console.WriteLine("Getting object reference to table 'Accounts' and getting AccountID and Minimal Lot Size.....");
        FXCore.TableAut accounts = (FXCore.TableAut)tradeDesk.FindMainTable("accounts");
        string accountId = (string)accounts.CellValue(1, "AccountID");
        FXCore.TradingSettingsProviderAut settingsProvider = (FXCore.TradingSettingsProviderAut)tradeDesk.TradingSettingsProvider;
        int minAmount = settingsProvider.GetBaseUnitSize(inst, accountId);
        Console.WriteLine("Got the table. AccountID: {0}, Lot Size: {1}.", accountId, minAmount);
        #endregion
 
        #region Create Entry Order
        //--------------------------------------------------------------------------------
        // Create an entry order with an entry stop
        //--------------------------------------------------------------------------------
        object result;
 
        Console.WriteLine("Creating a buy entry order using our variables...");
        tradeDesk.CreateEntryOrderELS(accountId, inst, true, minAmount, rate + pointSize * 50, 0, tradeDesk.SL_STOP,
            rate - pointSize * 10, 0, 0, 0, tradeDesk.TIF_GTC, out result);
 
        //--------------------------------------------------------------------------------
        // Print results
        //--------------------------------------------------------------------------------
        FXCore.OrdersBatchResultEnumAut batchResult = (FXCore.OrdersBatchResultEnumAut)result;
        FXCore.TableAut orders = (FXCore.TableAut)tradeDesk.FindMainTable("orders");
        for (int i = 0; i < batchResult.Count; i++)
        {
            FXCore.OrderBatchResultAut orderResult = (FXCore.OrderBatchResultAut)batchResult.Item(i + 1);
            if (orderResult.Success)
            {
                FXCore.RowAut orderRow = FindOrderRow(orders, orderResult.OrderID, 10000);
                if (orderRow == null)
                    // already executed/deleted/didn't received yet
                    continue;
                string contingencyType = GetContingencyTypeDescription((int)orderRow.CellValue("ContingencyType"));
                Console.WriteLine("  Order has been Created ({0}, {2}). OrderID is: {1}",
                    (string)orderRow.CellValue("Type"),
                    orderResult.OrderID,
                    contingencyType);
            }
            else
                Console.WriteLine("Failed to create an order");
        }
        #endregion
 
        #region Logout
        //--------------------------------------------------------------------------------
        // Logout
        //--------------------------------------------------------------------------------
        Console.WriteLine("Logging out.....");
        tradeDesk.Logout();
        Console.WriteLine("Logged out.");
        #endregion
    }
 
    /// <summary>
    /// Gets description for a contingency type.
    /// </summary>
    /// <param name="contingencyType">Contingency type</param>
    /// <returns>Description of the given contingency type.</returns>
    private static string GetContingencyTypeDescription(int contingencyType)
    {
        switch (contingencyType)
        {
            case 0:
                return "Entry order";
            case 1:
                return "OCO order";
            case 2:
                return "OTO order";
            case 3:
                return "ELS order";
        }
        return "Unknown order";
    }
 
    /// <summary>
    /// Wait for order ID addition in the table and returns order row. May return null (in case of timeout).
    /// </summary>
    /// <param name="orders">Orders table</param>
    /// <param name="orderId">Order ID of the order to find</param>
    /// <param name="timeout">Timeout in milliseconds</param>
    /// <returns>Order row or null.</returns>
    static FXCore.RowAut FindOrderRow(FXCore.TableAut orders, string orderId, int timeout)
    {
        DateTime start = DateTime.Now;
        TimeSpan timeSpan;
        do
        {
            try
            {
                FXCore.RowAut row = (FXCore.RowAut)orders.FindRow("OrderID", orderId, 0);
                return row;
            }
            catch
            {
            }
            Thread.Sleep(100);
            timeSpan = start - DateTime.Now;
        } while (timeSpan.Milliseconds < timeout);
 
        return null;
    }
}

This Article in Other Languages

Language: English  • EspaƱol
Personal tools