Create Trailing Entry Order (O2GO, CS)

From FxCodeBaseWiki
Jump to: navigation, search

The example shows how to create Trailing Entry Stop/Trailing Entry Limit (STE/LTE) orders. In this example two entry orders are created. The first is STE, it is created using the CreateEntryOrder3 method. The second is LTE, it is created using the CreateFixOrder3 method. Both CreateEntryOrder3 and CreateFixOrder3 methods can be used to create either STE or LTE orders, different methods are used just to show the possible ways.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Sample.STE_LTE
{
    class Program
    {
        static FXCore.CoreAut mCore;
        static FXCore.TradeDeskAut mDesk;        

        static void Main(string[] args)
        {
            try
            {
                if (args.Length < 2)
                {
                    Console.WriteLine("usage: sample.STE_LTE.exe user password [url] [connection]");
                    return;
                }
                string user = args[0];
                string pwd = args[1];
                string url = (args.Length > 2) ? args[2] : "http://www.fxcorporate.com";
                string connection = (args.Length > 3) ? args[3] : "Demo";

                //create o2go object and login
                mCore = new FXCore.CoreAut();
                mDesk = (FXCore.TradeDeskAut)mCore.CreateTradeDesk("trader");
                FXCore.TradingSettingsProviderAut tradingSettings = (FXCore.TradingSettingsProviderAut)mDesk.TradingSettingsProvider;
                FXCore.PermissionCheckerAut permissionsChecker = (FXCore.PermissionCheckerAut)mDesk.PermissionChecker;
                mDesk.Login(user, pwd, url, connection);

                //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);
                double ask = (double)offer.CellValue(1, "Ask");
                double bid = (double)offer.CellValue(1, "Bid");
                double ptsize = (double)offer.CellValue(1, "PointSize");
                int trailMinMove = 0;//no trailing
                object order_id_STE, order_id_LTE, di;

                //create a STE order 

                //1. check whether STE orders are supported
                bool dynamicSTEAllow = (permissionsChecker.CanUseDynamicTrailingForEntryStop() == permissionsChecker.PERMISSION_ENABLED);
                bool fluctuateSTEAllow = (permissionsChecker.CanUseFluctuateTrailingForEntryStop() == permissionsChecker.PERMISSION_ENABLED);
                if (!dynamicSTEAllow && !fluctuateSTEAllow)
                    Console.WriteLine("STE orders are not supported");
                else
                {
                    Console.WriteLine("STE orders are supported.");

                    //2. select trailing step
                    if (dynamicSTEAllow && fluctuateSTEAllow)
                    {
                        Console.WriteLine("Both dynamic and fluctuate trailing are supported.");
                        Console.WriteLine("Create STE order with dynamic trailing.");
                        trailMinMove = 1;
                    }
                    else if (dynamicSTEAllow)
                    {
                        Console.WriteLine("Only dynamic trailing is supported.");
                        Console.WriteLine("Create STE order with dynamic trailing.");
                        trailMinMove = 1;
                    }
                    else
                    {
                        Console.WriteLine("Only fluctuate trailing is supported.");
                        Console.WriteLine("Create STE order with fluctuate trailing.");
                        trailMinMove = tradingSettings.GetMinTrailingStepForEntryStop();
                    }

                    //3. create a STE order                                
                    mDesk.CreateEntryOrder3(account_id, instrument, false, amount, bid - ptsize * 100, trailMinMove, mDesk.SL_NONE, 0, 0, 0, 0, out order_id_STE, out di);
                    Console.WriteLine("The STE order is {0}", order_id_STE);

                    //4. waits until the order appears in the Orders table
                    WaitOrder((string)order_id_STE);
                    FXCore.RowAut rowOrder = (FXCore.RowAut)mDesk.FindRowInTable("orders", "OrderID", (string)order_id_STE);
                    Console.WriteLine("Trailing value for the {0} is {1}.", order_id_STE, rowOrder.CellValue("TrlMinMove"));
                }


                //create a LTE order

                //1. check whether LTE orders are supported
                bool dynamicLTEAllow = (permissionsChecker.CanUseDynamicTrailingForEntryLimit() == permissionsChecker.PERMISSION_ENABLED);
                bool fluctuateLTEAllow = (permissionsChecker.CanUseFluctuateTrailingForEntryLimit() == permissionsChecker.PERMISSION_ENABLED);
                if (!dynamicLTEAllow && !fluctuateLTEAllow)
                    Console.WriteLine("\nLTE orders are not supported");
                else
                {
                    Console.WriteLine("\nLTE orders are supported.");

                    //2. select trailing step
                    if (dynamicLTEAllow && fluctuateLTEAllow)
                    {
                        Console.WriteLine("Both dynamic and fluctuate trailing are supported.");
                        Console.WriteLine("Create LTE order with dynamic trailing.");
                        trailMinMove = 1;
                    }
                    else if (dynamicLTEAllow)
                    {
                        Console.WriteLine("Only dynamic trailing is supported.");
                        Console.WriteLine("Create LTE order with dynamic trailing.");
                        trailMinMove = 1;
                    }
                    else
                    {
                        Console.WriteLine("Only fluctuate trailing is supported.");
                        Console.WriteLine("Create LTE order with fluctuate trailing.");
                        trailMinMove = tradingSettings.GetMinTrailingStepForEntryLimit();
                    }

                    //3. create a LTE order                                
                    mDesk.CreateFixOrder3(mDesk.FIX_ENTRYLIMIT, null, ask + ptsize * 100, 0, null, account_id, instrument, false, amount, null, mDesk.SL_NONE, trailMinMove, mDesk.TIF_GTC, out order_id_LTE, out di);
                    Console.WriteLine("The LTE order is {0}", order_id_LTE);

                    //4. waits until the order appears in the Orders table
                    WaitOrder((string)order_id_LTE);
                    FXCore.RowAut rowOrder = (FXCore.RowAut)mDesk.FindRowInTable("orders", "OrderID", (string)order_id_LTE);
                    Console.WriteLine("Trailing value for the {0} is {1}.", order_id_LTE, rowOrder.CellValue("TrlMinMove"));
                }                
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}", e.ToString());
            }

             //finalizing
             mDesk.Logout();
        }

        /// <summary>
        /// Waits until the order appears in the Orders table.
        /// </summary>        
        static bool WaitOrder(string order_id)
        {            
            long start_time = DateTime.Now.Ticks;
            FXCore.TableAut orders = (FXCore.TableAut)mDesk.FindMainTable("orders");
            bool found = false;
            while (!found)
            {
                if ((DateTime.Now.Ticks - start_time) > 50000000)
                {
                    Console.WriteLine("5 second timeout to wait for the order exceeded");
                    return false;
                }

                try
                {
                    FXCore.RowAut row = (FXCore.RowAut)orders.FindRow("OrderID", (string)order_id, 0);
                    found = true;
                }
                catch (Exception)
                {
                    //the trade is not found
                    Thread.Sleep(250);
                }
            }
            return true;
        }
    }
}

This Article in Other Languages

Language: English  • español