I checked the OpenTrade method on all available for me accounts on UK and US Real databases. The only situation when I can reproduce the hangs is when the wrong account id is specified. Please find the working example below. Pay attention to the lines in bold. If you will comment the first bolded line and uncomment the last, the example will hang. It is a usual problem to use account_name instead of account_id. For some databases these fields may contain the same values, but for some may not.
If this does not help, please send me the login name (I don't need a password, only the login name to find out the database where the associated account is located).
Code:
class Program
{
/// <summary>
/// Instance of the O2GO core.
///
/// It is strongly recommended to keep the core instance in
/// the static or instance property of the class instead of
/// local variable.
/// </summary>
static FXCore.CoreAut mCore;
/// <summary>
/// Instance of the O2GO Trade Desk.
///
/// It is strongly recommended to keep the core instance in
/// the static or instance property of the class instead of
/// local variable.
/// </summary>
static FXCore.TradeDeskAut mDesk;
static void Main(string[] args)
{
if (args.Length != 4)
{
Console.WriteLine("Usage: opentrade_sample user password url connection");
return;
}
try
{
//Create an instance of O2GO core
mCore = new FXCore.CoreAut();
//Create an instance of O2GO core
mDesk = (FXCore.TradeDeskAut)mCore.CreateTradeDesk("trader");
}
catch (Exception e)
{
Console.WriteLine("Creation of O2GO objects failed");
Console.WriteLine("{0}", e.ToString());
return ;
}
try
{
mDesk.Login(args[0], args[1], args[2], args[3]);
}
catch (Exception e)
{
Console.WriteLine("Login failed");
Console.WriteLine("{0}", e.ToString());
return;
}
try
{
//account information
string account_id;
string account_name;
int min_amount;
//qoute information
string instrument;
double rate;
string quote_id;
try
{
//find the account information for the trade
FXCore.TableAut accounts = (FXCore.TableAut)mDesk.FindMainTable("accounts");
//...and get the account identifier, printable account name and
// minimal amount for the trade for the first existing account
account_id = (string)accounts.CellValue(1, "AccountID");
account_name = (string)accounts.CellValue(1, "AccountName");
min_amount = (int)accounts.CellValue(1, "BaseUnitSize");
}
catch (Exception e)
{
Console.WriteLine("Failed to get the account information");
Console.WriteLine("{0}", e.ToString());
return;
}
try
{
//find the quote information
FXCore.TableAut offers = (FXCore.TableAut)mDesk.FindMainTable("offers");
//get the instrument name, the current rate and quote identifier for the
//first available instrument.
instrument = (string)offers.CellValue(1, "Instrument");
rate = (double)offers.CellValue(1, "Ask");
quote_id = (string)offers.CellValue(1, "QuoteID");
}
catch (Exception e)
{
Console.WriteLine("Failed to get the quote information");
Console.WriteLine("{0}", e.ToString());
return;
}
object order_id = null, di = null;
try
{
//open the trade at the fixed rate
mDesk.OpenTrade(account_id, instrument, true, min_amount, rate, quote_id, 0, 0, 0, 0, out order_id, out di);
//the code above works, but the code below hangs
//mDesk.OpenTrade(account_name, instrument, true, min_amount, rate, quote_id, 0, 0, 0, 0, out order_id, out di);
}
catch (Exception e)
{
Console.WriteLine("Failed to open the order");
Console.WriteLine("{0}", e.ToString());
return;
}
Console.WriteLine("Order {0} to buy {1} lots of {2} for {3} on account {4} is created",
order_id, min_amount, instrument, rate, account_name);
//wait while the trade appeared in the table, but no more than
//5 seconds
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) > 500000000000)
{
Console.WriteLine("5 second timeout to wait for the trade exceeded");
return;
}
try
{
FXCore.RowAut row = (FXCore.RowAut)trades.FindRow("OpenOrderID", order_id, 0);
trade_id = (string)row.CellValue("TradeID");
}
catch (Exception )
{
//the trade is not found
Thread.Sleep(250);
}
}
Console.WriteLine("The ticket is {0}", trade_id);
//and the close the trade by the current market price
try
{
mDesk.CloseTrade(trade_id, min_amount, 0, "", 0, out order_id, out di);
}
catch (Exception e)
{
Console.WriteLine("Close trade operation is failed");
Console.WriteLine("{0}", e.ToString());
return;
}
Console.WriteLine("The trade {0} is closed by order {1}", trade_id, order_id);
}
finally
{
mDesk.Logout();
}
}
}