Set Time In Force for At Best Market Order (O2GO, CS)

From FxCodeBaseWiki

Jump to: navigation, search

The example shows how to set the Time In Force (TIF) for an At Best market order. There are three possible values: GTC, IOC, and FOK. In this example three orders with the three possible TIF values are created. In this example the difference in execution of these orders is not shown because the TIF affects order execution only in case there is not enough liquidity to fill the entire order, and it is impossible to simulate lack of liquidity in this example.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
 
namespace Sample.TIF
{
    class Program
    {
        static FXCore.CoreAut mCore;
        static FXCore.TradeDeskAut mDesk;        
 
        static void Main(string[] args)
        {
            try
            {
                if (args.Length < 2)
                {
                    Console.WriteLine("usage: sample.TIF.exe user password");
                    return;
                }
 
                //create an o2go object and log in                
                mCore = new FXCore.CoreAut();
                mDesk = (FXCore.TradeDeskAut)mCore.CreateTradeDesk("trader");
                FXCore.TradingSettingsProviderAut tradingSettings = (FXCore.TradingSettingsProviderAut)mDesk.TradingSettingsProvider;
                mDesk.Login(args[0], args[1], "http://www.fxcorporate.com", "Demo");
 
                //collect all required information
                FXCore.TableAut acct = (FXCore.TableAut)mDesk.FindMainTable("accounts");
                FXCore.TableAut offer = (FXCore.TableAut)mDesk.FindMainTable("offers");
 
                string instrument = (string)offer.CellValue(1, "Instrument");
                string account_id = (string)acct.CellValue(1, "AccountID");
                int amount = tradingSettings.GetMinQuantity(instrument, account_id);               
 
                //open trades
                object trade_order_id1, trade_order_id2, trade_order_id3, di;
 
                //open a GTC trade
                mDesk.OpenTrade3(account_id, instrument, true, amount, 0, null, 0, mDesk.SL_NONE, 0, 0, 0, 0, mDesk.TIF_GTC, out trade_order_id1, out di);
 
                //open an IOC trade
                mDesk.OpenTrade3(account_id, instrument, true, amount, 0, null, 0, mDesk.SL_NONE, 0, 0, 0, 0, mDesk.TIF_IOC, out trade_order_id2, out di);
 
                //open an FOK trade
                mDesk.OpenTrade3(account_id, instrument, true, amount, 0, null, 0, mDesk.SL_NONE, 0, 0, 0, 0, mDesk.TIF_FOK, out trade_order_id3, out di);
 
                //wait until the trades appear in the table, but not more than 5 seconds                
                string trade_id1 = WaitTrade((string)trade_order_id1);
                string trade_id2 = WaitTrade((string)trade_order_id2);
                string trade_id3 = WaitTrade((string)trade_order_id3);
 
                Console.WriteLine("A trade opened by the GTC market order is {0}.", trade_id1);
                Console.WriteLine("A trade opened by the IOC market order is {0}.", trade_id2);
                Console.WriteLine("A trade opened by the FOK market order is {0}.", trade_id3);
 
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}", e.ToString());
            }
 
             //finalizing
             mDesk.Logout();
        }
 
        /// <summary>
        /// Waits for a trade opened by the order during a 5 second timeout.
        /// Returns the identifier of the trade or null.
        /// </summary>        
        static string WaitTrade(string trade_order_id)
        {            
            string trade_id = null;
            long start_time = DateTime.Now.Ticks;
            FXCore.TableAut trades = (FXCore.TableAut)mDesk.FindMainTable("trades");
            while (trade_id == null)
            {                
                if ((DateTime.Now.Ticks - start_time) > 50000000)
                {
                    Console.WriteLine("5 second timeout to wait for the trade expired");
                    return null;
                }
 
                try
                {
                    FXCore.RowAut row = (FXCore.RowAut)trades.FindRow("OpenOrderID", (string)trade_order_id, 0);
                    trade_id = (string)row.CellValue("TradeID");
                }
                catch (Exception)
                {
                    //the trade is not found
                    Thread.Sleep(250);
                }
            }
            return trade_id;
        }
    }

This Article in Other Languages

Language: English  • EspaƱol
Personal tools