Exemple #1
0
        /// <summary>
        /// The method opens a new order.
        /// </summary>
        /// <param name="operationId">
        /// Can be null, in this case FDK generates a new unique operation ID automatically.
        /// Otherwise, please use GenerateOperationId method of DataClient object.
        /// </param>
        /// <param name="symbol">Trading currency pair symbol; can not be null.</param>
        /// <param name="command">Market, limit or stop.</param>
        /// <param name="side">Trade record side: buy or sell.</param>
        /// <param name="volume">Requsted volume.</param>
        /// <param name="maxVisibleVolume">Max visible volume.</param>
        /// <param name="price">Activating price for pending orders; price threshold for market orders.</param>
        /// <param name="stopPrice">Stop price.</param>
        /// <param name="stopLoss">Stop loss price.</param>
        /// <param name="takeProfit">Take profit price.</param>
        /// <param name="expiration">Expiration time, should be specified for pending orders.</param>
        /// <param name="comment">User defined comment for a new opening order. Null is interpreded as empty string.</param>
        /// <param name="tag">User defined tag for a new opening order. Null is interpreded as empty string.</param>
        /// <param name="magic">User defined magic number for a new opening order. Null is not defined.</param>
        /// <param name="timeoutInMilliseconds">Timeout of the synchronous operation.</param>
        /// <returns>A new trade record; can not be null.</returns>
        TradeRecord SendOrderEx(string operationId, string symbol, TradeCommand command, TradeRecordSide side, double volume, double? maxVisibleVolume, double? price, double? stopPrice, double? stopLoss, double? takeProfit, DateTime? expiration, string comment, string tag, int? magic, int timeoutInMilliseconds)
        {
#if LOG_PERFORMANCE
            ulong timestamp = Client.loggerIn_.GetTimestamp();
#endif
            var order = this.Client.DataTradeHandle.OpenNewOrder(operationId, symbol, command, side, volume, maxVisibleVolume, price, stopPrice, stopLoss, takeProfit, expiration, comment ?? string.Empty, tag ?? string.Empty, magic, timeoutInMilliseconds);
            TradeRecord tradeRecord = new TradeRecord(this.Client, order);

#if LOG_PERFORMANCE
            Client.loggerIn_.LogTimestamp(tradeRecord.ClientOrderId, timestamp, "NewOrder");
#endif
            return tradeRecord;
        }
Exemple #2
0
 /// <summary>
 /// Closes by two orders.
 /// </summary>
 /// <param name="other">Another order; can not be null.</param>
 /// <param name="timeoutInMilliseconds">Timeout of the operation in milliseconds.</param>
 /// <returns>True, if the operation has been succeeded; otherwise false.</returns>
 /// <returns>Can not be null.</returns>
 public bool CloseByEx(TradeRecord other, int timeoutInMilliseconds)
 {
     return this.DataTrade.Server.CloseByPositionsEx(this.OrderId, other.OrderId, timeoutInMilliseconds);
 }
Exemple #3
0
 /// <summary>
 /// Closes by two orders.
 /// </summary>
 /// <param name="other">Another order; can not be null.</param>
 /// <returns>True, if the operation has been succeeded; otherwise false.</returns>
 /// <returns>Can not be null.</returns>
 public bool CloseBy(TradeRecord other)
 {
     return this.DataTrade.Server.CloseByPositions(this.OrderId, other.OrderId);
 }
Exemple #4
0
        void Update(StateInfo info)
        {
            var accountInfo = this.AccountInfo;
            var positions = new Dictionary<string, Position>();
            var tradeRecords = new List<TradeRecord>();

            if (accountInfo != null)
            {
                var newAccountInfo = new AccountInfo
                {
                    Leverage = accountInfo.Leverage,
                    Currency = accountInfo.Currency,
                    AccountId = accountInfo.AccountId,
                    Type = accountInfo.Type,
                    MarginCallLevel = accountInfo.MarginCallLevel,
                    StopOutLevel = accountInfo.StopOutLevel,

                    Balance = info.Equity,
                    Margin = info.Margin,
                    Equity = info.Equity
                };
                accountInfo = newAccountInfo;


                foreach (var element in info.Positions)
                {
                    positions.Add(element.Symbol, element);

                    var volume = element.BuyAmount - element.SellAmount;
                    if (volume != 0.0)
                    {
                        var record = new TradeRecord
                        {
                            Symbol = element.Symbol,
                            Volume = Math.Abs(volume),
                            OrderId = this.VirtualOrderIdFromSymbol(element.Symbol),
                            Type = TradeRecordType.Position
                        };
                        if (volume >= 0)
                        {
                            record.Side = TradeRecordSide.Buy;
                            record.Price = element.BuyPrice ?? Math.Abs(element.SettlementPrice);
                        }
                        else
                        {
                            record.Side = TradeRecordSide.Sell;
                            record.Price = element.SellPrice ?? Math.Abs(element.SettlementPrice);
                        }
                        
                        record.Price = Math.Abs(element.SettlementPrice);
                        tradeRecords.Add(record);
                    }
                }
                tradeRecords.AddRange(info.TradeRecords);
            }

            lock (this.synchronizer)
            {
                this.AccountInfo = accountInfo;
                this.TradeRecords = tradeRecords;
                this.Positions = positions;

                foreach (var element in info.Prices)
                {
                    Quote quote;
                    if (!info.Quotes.TryGetValue(element.Key, out quote))
                        continue;

                    this.Quotes[element.Key] = new Quote(element.Key, quote.CreatingTime, element.Value.Bid, element.Value.Ask);
                }
            }

            this.syncEvent.Set();
        }
Exemple #5
0
        TradeRecord ModifyLimitOrder(TradeRecord initial, double? newActivationPrice, double? newStopLoss, double? newTakeProfit, DateTime? newExpirationTime)
        {
            var start = DateTime.UtcNow;
            TradeRecord result = null; 
            try
            {
                result = initial.Modify(newActivationPrice, newStopLoss, newTakeProfit, newExpirationTime);
            }
            catch
            {
                TestBase.Output("(price = {0}; sl = {1}; tp = {2}; expiration = {3}) = {4}", newActivationPrice, newStopLoss, newTakeProfit, newExpirationTime, "exception");
                throw;
            }
            
            var finish = DateTime.UtcNow;
            var interval = (finish - start);

            TestBase.Output("(price = {0}; sl = {1}; tp = {2}; expiration = {3}) = {4}", newActivationPrice, newStopLoss, newTakeProfit, newExpirationTime, interval);

            return result;
        }
Exemple #6
0
        void ModifyLimitOrder(TradeRecord order, double initialActivationPrice, double? newActivationPrice, double? newStopLoss, double? newTakeProfit, DateTime? newExpirationTime)
        {
            var initial = ModifyLimitOrder(order, initialActivationPrice, null, null, null); // reset all


            // check all
            Assert.IsTrue(initial.Price == initialActivationPrice);
            //Assert.IsNull(initial.StopLoss);
            //Assert.IsNull(initial.TakeProfit);
            //Assert.IsNull(initial.Expiration);

            // modify
            var modified = this.ModifyLimitOrder(initial, newActivationPrice, newStopLoss, newTakeProfit, newExpirationTime);
            
            // check
            if (newActivationPrice == null)
            {
                Assert.IsTrue(initialActivationPrice == modified.Price);
            }
            else
            {
                Assert.IsTrue(newActivationPrice == modified.Price);
            }

            Assert.IsTrue(newStopLoss == modified.StopLoss);
            Assert.IsTrue(newTakeProfit == modified.TakeProfit);
            if (newExpirationTime == null)
            {
                Assert.IsNull(modified.Expiration);
            }
            else
            {
                Assert.IsNotNull(modified.Expiration);
                var interval = (DateTime)modified.Expiration - (DateTime)newExpirationTime;
                Assert.IsTrue(Math.Abs(interval.TotalMilliseconds) < 1000);
            }
        }
Exemple #7
0
 /// <summary>
 /// Closes by two orders.
 /// </summary>
 /// <param name="operationId">Operation Id</param>
 /// <param name="other">Another order; can not be null.</param>
 /// <param name="timeoutInMilliseconds">Timeout of the operation in milliseconds.</param>
 /// <returns>True, if the operation has been succeeded; otherwise false.</returns>
 /// <returns>Can not be null.</returns>
 public bool CloseByEx(string operationId, TradeRecord other, int timeoutInMilliseconds)
 {
     return(this.DataTrade.Server.CloseByPositionsEx(operationId, this.OrderId, other.OrderId, timeoutInMilliseconds));
 }
Exemple #8
0
 /// <summary>
 /// Closes by two orders.
 /// </summary>
 /// <param name="other">Another order; can not be null.</param>
 /// <param name="timeoutInMilliseconds">Timeout of the operation in milliseconds.</param>
 /// <returns>True, if the operation has been succeeded; otherwise false.</returns>
 /// <returns>Can not be null.</returns>
 public bool CloseByEx(TradeRecord other, int timeoutInMilliseconds)
 {
     return(this.CloseByEx(null, other, timeoutInMilliseconds));
 }
Exemple #9
0
 /// <summary>
 /// Closes by two orders.
 /// </summary>
 /// <param name="other">Another order; can not be null.</param>
 /// <returns>True, if the operation has been succeeded; otherwise false.</returns>
 /// <returns>Can not be null.</returns>
 public bool CloseBy(TradeRecord other)
 {
     return(this.DataTrade.Server.CloseByPositions(this.OrderId, other.OrderId));
 }