示例#1
1
文件: UpdateHandler.cs 项目: ifzz/FDK
        public UpdateHandler(DataTrade trade, DataFeed feed, Action<SymbolInfo[], AccountInfo, Quote> updateCallback, Processor processor)
        {
            if (trade == null)
                throw new ArgumentNullException(nameof(trade));

            if (feed == null)
                throw new ArgumentNullException(nameof(feed));

            if (updateCallback == null)
                throw new ArgumentNullException(nameof(updateCallback));

            if (processor == null)
                throw new ArgumentNullException(nameof(processor));

            this.updateCallback = updateCallback;
            this.processor = processor;

            this.SyncRoot = new object();

            feed.SymbolInfo += this.OnSymbolInfo;
            feed.Tick += this.OnTick;

            trade.AccountInfo += this.OnAccountInfo;
            trade.BalanceOperation += this.OnBalanceOperation;
            trade.ExecutionReport += this.OnExecutionReport;
            trade.PositionReport += this.OnPositionReport;
        }
示例#2
0
        /// <summary>
        /// Creates new financial state of account calculator.
        /// </summary>
        /// <param name="trade">valid instance of not started data trade</param>
        /// <param name="feed">valid instance of not started data feed</param>
        public StateCalculator(DataTrade trade, DataFeed feed)
        {
            if (trade == null)
                throw new ArgumentNullException(nameof(trade), "Data trade argument can not be null");

            if (trade.IsStarted)
                throw new ArgumentException("Started data trade can not be used for creating state calculator.", nameof(trade));

            if (feed == null)
                throw new ArgumentNullException(nameof(feed), "Data feed argument can not be null");

            if (feed.IsStarted)
                throw new ArgumentException("Started data feed can not be used for creating state calculator.", nameof(feed));


            this.quotes = new Dictionary<string, Quote>();
            this.calculatorQuotes = new Dictionary<string, Quote>();

            this.calculator = new FinancialCalculator();
            this.account = new AccountEntry(calculator);
            this.calculator.Accounts.Add(this.account);

            this.trade = trade;
            this.feed = feed;

            this.processor = new Processor(this.Calculate);

            this.processor.Exception += this.OnException;
            this.processor.Executed += this.OnExecuted;

            this.updateHandler = new UpdateHandler(trade, feed, this.OnUpdate, processor);
        }
示例#3
0
文件: TestBase.cs 项目: hombrevrc/FDK
 public void Cleanup()
 {
     Cleanup(this.dataFeed);
     this.dataFeed = null;
     Cleanup(this.dataTrade);
     this.dataTrade = null;
 }
示例#4
0
        void CloseBy(ConnectionStringBuilder builder)
        {
            var connectionString = builder.ToString();

            using (this.dataTrade = new DataTrade(connectionString))
            {
                this.dataTrade.Logon       += OnLogon;
                this.dataTrade.AccountInfo += DataTrade_AccountInfo;
                this.dataTrade.Start();

                bool status = this.logonEvent.WaitOne(LogonWaitingTimeout);
                status &= this.accountInfoEvent.WaitOne(LogonWaitingTimeout);
                Assert.IsTrue(status, "Timeout of logon event");

                TradeRecord order1 = this.dataTrade.Server.SendOrderEx("EURUSD", TradeCommand.Market, TradeRecordSide.Buy, 0, 10000, null, null, null, null, null, "comment", null, null, 1000000);
                TradeRecord order2 = this.dataTrade.Server.SendOrderEx("EURUSD", TradeCommand.Market, TradeRecordSide.Sell, 0, 10000, null, null, null, null, null, "comment", null, null, 1000000);

                Assert.IsTrue(dataTrade.Server.CloseByPositions(order1.OrderId, order2.OrderId));
                var iter = this.dataTrade.Server.GetTradeTransactionReports(TimeDirection.Backward, false, DateTime.UtcNow.AddMinutes(-5), DateTime.UtcNow.AddMinutes(5), false);
                TradeTransactionReport tradeReport1 = iter.Item;
                iter.Next();
                TradeTransactionReport tradeReport2 = iter.Item;

                //Assert.IsTrue(tradeReport1.PositionById == order1.OrderId);
                Assert.IsTrue(tradeReport2.PositionById == order2.OrderId);


                this.dataTrade.Logon       -= this.OnLogon;
                this.dataTrade.AccountInfo -= DataTrade_AccountInfo;
                this.dataTrade.Stop();
            }
        }
示例#5
0
        void CreateAndDestroy(ConnectionStringBuilder builder)
        {
            var connectionString = builder.ToString();

            this.dataTrade = new DataTrade(connectionString);
            this.dataTrade.Dispose();
        }
示例#6
0
        public void StateInfoChanged()
        {
            string connectionString = Configuration.ConnectionBuilders(AccountType.Gross, Configuration.ConnectionType.Feed).First().ToString();

            this.dataFeed        = new DataFeed(connectionString);
            this.dataFeed.Logon += this.OnFeedLogon;

            connectionString      = Configuration.ConnectionBuilders(AccountType.Gross, Configuration.ConnectionType.Trade).First().ToString();
            this.dataTrade        = new DataTrade(connectionString);
            this.dataTrade.Logon += this.OnTradeLogon;

            StateCalculator stateCalculator = new StateCalculator(dataTrade, dataFeed);

            stateCalculator.StateInfoChanged += StateCalculator_StateInfoChanged;

            this.dataFeed.Start();
            this.dataTrade.Start();
            var status = this.feedLogonEvent.WaitOne(LogonWaitingTimeout);

            Assert.IsTrue(status, "Timeout of feed logon event");
            status = this.tradeLogonEvent.WaitOne(LogonWaitingTimeout);
            Assert.IsTrue(status, "Timeout of trade logon event");

            status = this.stateInfoEvent.WaitOne(LogonWaitingTimeout * 10000);

            this.dataFeed.Stop();
            this.dataTrade.Stop();
            this.dataFeed.Logon  -= this.OnFeedLogon;
            this.dataTrade.Logon -= this.OnTradeLogon;
        }
示例#7
0
        void ModifyPosition(ConnectionStringBuilder builder)
        {
            var connectionString = builder.ToString();

            this.dataTrade = new DataTrade(connectionString);

            this.dataTrade.Logon       += OnLogon;
            this.dataTrade.AccountInfo += DataTrade_AccountInfo;
            this.dataTrade.Start();

            bool status = this.logonEvent.WaitOne(LogonWaitingTimeout);

            status &= this.accountInfoEvent.WaitOne(LogonWaitingTimeout);

            Assert.IsTrue(status, "Timeout of logon event");

            var         start    = DateTime.UtcNow;
            TradeRecord order    = this.dataTrade.Server.SendOrderEx("EURUSD", TradeCommand.Market, TradeRecordSide.Buy, 0, 10000, null, null, null, null, null, "comment", null, null, 1000000);
            DateTime    end      = DateTime.UtcNow;
            TimeSpan    interval = (end - start);

            Console.WriteLine("Interval = {0}", interval);

            var modified = order.Modify(null, null, null, 1.0, null, null, null, null);

            order.Close();

            this.dataTrade.Logon       -= this.OnLogon;
            this.dataTrade.AccountInfo -= DataTrade_AccountInfo;
            this.dataTrade.Stop();
            this.dataTrade.Dispose();
        }
示例#8
0
        public TradeRecord[] GetCacheOrders(DataTrade dataTrade)
        {
            this.VerifyInitialized();

            var orders = Native.TradeCache.GetRecords(this.handle);

            return(orders.Select(o => new TradeRecord(dataTrade, o))
                   .ToArray());
        }
示例#9
0
        public        TradeRecord[] GetOrders(DataTrade dataTrade, int timeoutInMilliseconds)
        {
            this.VerifyInitialized();

            var orders = Native.TradeServer.GetRecords(this.handle, (uint)timeoutInMilliseconds);

            return(orders.Select(o => new TradeRecord(dataTrade, o))
                   .ToArray());
        }
示例#10
0
        void ModifyLimitOrder(ConnectionStringBuilder builder)
        {
            var connectionString = builder.ToString();

            this.dataTrade = new DataTrade(connectionString);

            this.dataTrade.Logon += this.OnLogon;
            this.dataTrade.Start();

            var status = this.logonEvent.WaitOne(LogonWaitingTimeout);

            Assert.IsTrue(status, "Timeout of logon event");

            //const double activationPrice = 1.1;
            //const double newActivationPrice = 1.0;
            //const double newStopLoss = 0.8;
            //const double newTakeProfit = 1.3;
            var newExpirationTime = DateTime.UtcNow.AddHours(1);

            var order = this.dataTrade.Server.SendOrderEx("EURUSD", TradeCommand.Limit, TradeRecordSide.Buy, 1.1, 10000, null, null, null, null, null, "comment", null, null, 1000000);

/*
 *          //this.ModifyLimitOrder(order, activationPrice, null, null, null, null);
 *          this.ModifyLimitOrder(order, activationPrice, null, null, null, newExpirationTime);
 *
 *          //this.ModifyLimitOrder(order, activationPrice, null, null, newTakeProfit, null);
 *          this.ModifyLimitOrder(order, activationPrice, null, null, newTakeProfit, newExpirationTime);
 *
 *
 *          //this.ModifyLimitOrder(order, activationPrice, null, newStopLoss, null, null);
 *          this.ModifyLimitOrder(order, activationPrice, null, newStopLoss, null, newExpirationTime);
 *
 *          //this.ModifyLimitOrder(order, activationPrice, null, newStopLoss, newTakeProfit, null);
 *          this.ModifyLimitOrder(order, activationPrice, null, newStopLoss, newTakeProfit, newExpirationTime);
 *
 *
 *
 *          //this.ModifyLimitOrder(order, activationPrice, newActivationPrice, null, null, null);
 *          this.ModifyLimitOrder(order, activationPrice, newActivationPrice, null, null, newExpirationTime);
 *
 *          //this.ModifyLimitOrder(order, activationPrice, newActivationPrice, null, newTakeProfit, null);
 *          this.ModifyLimitOrder(order, activationPrice, newActivationPrice, null, newTakeProfit, newExpirationTime);
 *
 *          //this.ModifyLimitOrder(order, activationPrice, newActivationPrice, newStopLoss, null, null);
 *          this.ModifyLimitOrder(order, activationPrice, newActivationPrice, newStopLoss, null, newExpirationTime);
 *
 *          //this.ModifyLimitOrder(order, activationPrice, newActivationPrice, newStopLoss, newTakeProfit, null);
 *          this.ModifyLimitOrder(order, activationPrice, newActivationPrice, newStopLoss, newTakeProfit, newExpirationTime);
 */
            order.Delete();

            this.dataTrade.Logon -= this.OnLogon;
            this.dataTrade.Stop();
            this.dataTrade.Dispose();
        }
示例#11
0
        public void Dispose()
        {
            if (this.Trade == null)
            {
                return;
            }

            this.Trade.Stop();
            this.Trade.Dispose();
            this.Trade = null;
        }
示例#12
0
 public void Cleanup()
 {
     if (this.dataTrade != null)
     {
         if (this.dataTrade.IsStarted)
         {
             this.dataTrade.Stop();
         }
         this.dataTrade.Dispose();
         this.dataTrade = null;
     }
 }
示例#13
0
        static void Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.WriteLine("Usage: AccountInfo.exe address login password");
                return;
            }

            try
            {
                if (!Directory.Exists(LogPath))
                {
                    Directory.CreateDirectory(LogPath);
                }

                string address  = args[0];
                string login    = args[1];
                string password = args[2];

                // Create data trade interface
                var trade = new DataTrade {
                    SynchOperationTimeout = 30000
                };

                // Create connection string
                FixConnectionStringBuilder builder = CreateBuilder(address, login, password);
                var connectionString = builder.ToString();

                // Initialize data trade interface
                trade.Initialize(connectionString);

                // Subscribe to data trade events
                trade.Logon       += OnLogon;
                trade.Logout      += OnLogout;
                trade.AccountInfo += OnAccountInfo;

                // Start data trade
                trade.Start();
                Console.WriteLine("DataTrade started!");
                Console.WriteLine("Please wait for login status...");

                // Wait for exit
                Console.ReadKey();

                // Stop data trade
                trade.Stop();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
示例#14
0
        public void TestDataTradeIsolation()
        {
            const string address  = "tpdemo.fxopen.com";
            const string username = "******";
            const string password = "******";

            EnsureDirectoriesCreated();

            // Create builder
            var builder = new FixConnectionStringBuilder
            {
                TargetCompId     = "EXECUTOR",
                ProtocolVersion  = FixProtocolVersion.TheLatestVersion.ToString(),
                SecureConnection = true,
                Port             = 5004,
                //ExcludeMessagesFromLogs = "W",
                DecodeLogFixMessages = true,

                Address  = address,
                Username = username,
                Password = password,

                FixLogDirectory     = LogPath,
                FixEventsFileName   = string.Format("{0}.trade.events.log", username),
                FixMessagesFileName = string.Format("{0}.trade.messages.log", username)
            };
            var trade = new DataTrade
            {
                SynchOperationTimeout = 30000
            };
            var connectionString = builder.ToString();

            trade.Initialize(connectionString);
            trade.Logon += OnLogon;
            trade.Start();
            var timeoutInMilliseconds = trade.SynchOperationTimeout;

            if (!_syncEvent.WaitOne(timeoutInMilliseconds))
            {
                throw new TimeoutException("Timeout of logon waiting has been reached");
            }
            RunExample(trade);

            trade.Dispose();
        }
示例#15
0
        public void Connect(string address, string username, string password, string logPath)
        {
            EnsureDirectoriesCreated(logPath);

            // Create builder
            SetupBuilder(address, username, password, logPath);
            var connectionString = Builder.ToString();

            Trade = new DataTrade();
            Trade.Initialize(connectionString);
            var isConnected = Trade.Start(30000);

            if (!isConnected)
            {
                Trade.Stop();
                throw new TimeoutException();
            }
        }
示例#16
0
        void SendMarketOrder(ConnectionStringBuilder builder)
        {
            var connectionString = builder.ToString();

            this.dataTrade = new DataTrade(connectionString);

            this.dataTrade.Logon += this.OnLogon;
            this.dataTrade.Start();

            var status = this.logonEvent.WaitOne(LogonWaitingTimeout);

            Assert.IsTrue(status, "Timeout of logon event");
            var order = this.dataTrade.Server.SendOrderEx("EURUSD", TradeCommand.Market, TradeRecordSide.Buy, 1.1, 10000, 0, null, null, 0, null, "comment", null, null, 1000000);

            Assert.IsTrue(order.Price > 0, "Invalid order price = {0}", order.Price);
            this.dataTrade.Logon -= this.OnLogon;
            this.dataTrade.Stop();
            this.dataTrade.Dispose();
        }
示例#17
0
        internal Snapshot(DataTrade trade, DataFeed feed, StateCalculator calculator, object synchronizer, AutoResetEvent syncEvent)
        {
            this.synchronizer = synchronizer;
            this.syncEvent    = syncEvent;
            this.calculator   = calculator;

            this.Quotes = new Dictionary <string, Quote>();

            trade.Logon       += this.OnTradeLogon;
            trade.SessionInfo += this.OnTradeSession;
            trade.AccountInfo += this.OnAccountInfo;
            trade.Logout      += this.OnTradeLogout;

            feed.Logon       += this.OnFeedLogon;
            feed.Logout      += this.OnFeedLogout;
            feed.SymbolInfo  += this.OnFeedSymbolInfo;
            feed.SessionInfo += this.OnFeedSessionInfo;

            calculator.StateInfoChanged += this.OnFinancialInfoChanged;
        }
示例#18
0
        /// <summary>
        /// Creates new financial state of account calculator.
        /// </summary>
        /// <param name="trade">valid instance of not started data trade</param>
        /// <param name="feed">valid instance of not started data feed</param>
        public StateCalculator(DataTrade trade, DataFeed feed)
        {
            if (trade == null)
            {
                throw new ArgumentNullException(nameof(trade), "Data trade argument can not be null");
            }

            if (trade.IsStarted)
            {
                throw new ArgumentException("Started data trade can not be used for creating state calculator.", nameof(trade));
            }

            if (feed == null)
            {
                throw new ArgumentNullException(nameof(feed), "Data feed argument can not be null");
            }

            if (feed.IsStarted)
            {
                throw new ArgumentException("Started data feed can not be used for creating state calculator.", nameof(feed));
            }


            this.quotes           = new Dictionary <string, Quote>();
            this.calculatorQuotes = new Dictionary <string, Quote>();

            this.calculator = new FinancialCalculator();
            this.account    = new AccountEntry(calculator);
            this.calculator.Accounts.Add(this.account);

            this.trade = trade;
            this.feed  = feed;

            this.processor = new Processor(this.Calculate);

            this.processor.Exception += this.OnException;
            this.processor.Executed  += this.OnExecuted;

            this.updateHandler = new UpdateHandler(trade, feed, this.OnUpdate, processor);
        }
示例#19
0
        public void Connect(string address, string username, string password, string logPath)
        {
            EnsureDirectoriesCreated(logPath);

            // Create builder
            SetupBuilder(address, username, password, logPath);
            Trade = new DataTrade
            {
                SynchOperationTimeout = 300000
            };
            var connectionString = Builder.ToString();

            Trade.Initialize(connectionString);
            Trade.Logon += OnLogon;
            Trade.Start();
            var timeoutInMilliseconds = Trade.SynchOperationTimeout;

            if (!_syncEvent.WaitOne(timeoutInMilliseconds))
            {
                throw new TimeoutException("Timeout of logon waiting has been reached");
            }
        }
示例#20
0
        void TradeReports(ConnectionStringBuilder builder, TestContext testContext)
        {
            var connectionString = builder.ToString();

            this.dataTrade = new DataTrade(connectionString);

            this.dataTrade.Logon += this.OnLogon;
            this.dataTrade.Start();

            var status = this.logonEvent.WaitOne(LogonWaitingTimeout);

            Assert.IsTrue(status, "Timeout of logon event");

            //Test Limit order
            var order = this.dataTrade.Server.SendOrderEx(testContext.Symbol, TradeCommand.Limit, TradeRecordSide.Buy, testContext.VeryLowPrice, testContext.Volume, null, null, null, null, DateTime.UtcNow.AddHours(1), testContext.Comment, null, null, 1000000);

            Assert.IsTrue(order.Price == testContext.VeryLowPrice, "Invalid order price = {0}", order.Price);
            order.Delete();

            Reports.TradeTransactionReport tradeReport = this.dataTrade.Server.GetTradeTransactionReports(TimeDirection.Backward, false, DateTime.UtcNow.AddMinutes(-5), DateTime.UtcNow.AddMinutes(5), false).Item;
            Assert.IsTrue(tradeReport.TradeRecordType == order.Type);
            Assert.IsTrue(tradeReport.Price == order.Price);

            //Test stop order
            order = this.dataTrade.Server.SendOrderEx(testContext.Symbol, TradeCommand.Stop, TradeRecordSide.Buy, testContext.VeryHighPrice, testContext.Volume, null, null, null, null, DateTime.UtcNow.AddHours(1), testContext.Comment, null, null, 1000000);
            Assert.IsTrue(order.Price == testContext.VeryHighPrice, "Invalid order price = {0}", order.Price);
            order.Delete();

            tradeReport = this.dataTrade.Server.GetTradeTransactionReports(TimeDirection.Backward, false, DateTime.UtcNow.AddMinutes(-5), DateTime.UtcNow.AddMinutes(5), false).Item;
            Assert.IsTrue(tradeReport.TradeRecordType == order.Type);
            Assert.IsTrue(tradeReport.StopPrice == order.Price);


            this.dataTrade.Logon -= this.OnLogon;
            this.dataTrade.Stop();
            this.dataTrade.Dispose();
        }
示例#21
0
        public UpdateHandler(DataTrade trade, DataFeed feed, Action <CurrencyInfo[], SymbolInfo[], AccountInfo, Quote> updateCallback, Processor processor)
        {
            if (trade == null)
            {
                throw new ArgumentNullException(nameof(trade));
            }

            if (feed == null)
            {
                throw new ArgumentNullException(nameof(feed));
            }

            if (updateCallback == null)
            {
                throw new ArgumentNullException(nameof(updateCallback));
            }

            if (processor == null)
            {
                throw new ArgumentNullException(nameof(processor));
            }

            this.updateCallback = updateCallback;
            this.processor      = processor;

            this.SyncRoot = new object();

            feed.CurrencyInfo += this.OnCurrencyInfo;
            feed.SymbolInfo   += this.OnSymbolInfo;
            feed.Tick         += this.OnTick;

            trade.AccountInfo      += this.OnAccountInfo;
            trade.BalanceOperation += this.OnBalanceOperation;
            trade.ExecutionReport  += this.OnExecutionReport;
            trade.PositionReport   += this.OnPositionReport;
        }
示例#22
0
文件: FxDataTrade.cs 项目: ifzz/FDK
        public TradeRecord[] GetOrders(DataTrade dataTrade, int timeoutInMilliseconds)
        {
            this.VerifyInitialized();

            var orders = Native.TradeServer.GetRecords(this.handle, (uint)timeoutInMilliseconds);

            return orders.Select(o => new TradeRecord(dataTrade, o))
                         .ToArray();
        }
示例#23
0
文件: FxDataTrade.cs 项目: ifzz/FDK
        public TradeRecord[] GetCacheOrders(DataTrade dataTrade)
        {
            this.VerifyInitialized();

            var orders = Native.TradeCache.GetRecords(this.handle);

            return orders.Select(o => new TradeRecord(dataTrade, o))
                         .ToArray();
        }
示例#24
0
 private static void RunExample(DataTrade trade)
 {
     var records = trade.Server.GetTradeRecords();
 }