public void Create_Custom_Instrument()
        {
            //    Create a definition for the instrument
            var swapDefinition = new InstrumentDefinition(
                name: "10mm 5Y Fixed",

                //  The set of identifiers used for identifying the instrument
                //  e.g. for uploading transactions
                identifiers: new Dictionary <string, InstrumentIdValue>
            {
                ["ClientInternal"] = new InstrumentIdValue(value: "SW-1")
            },

                //  The details for valuing the instrument
                definition: new ExoticInstrument(

                    //  Identifies which valuation engine to use
                    instrumentFormat: new InstrumentDefinitionFormat("CustomSource", "CustomFormat", "0.1.2"),
                    content: "<customFormat>upload in custom xml or JSON format</customFormat>",
                    instrumentType: LusidInstrument.InstrumentTypeEnum.ExoticInstrument
                    ));

            //    create the swap
            var createSwapResponse = _instrumentsApi.UpsertInstruments(new Dictionary <string, InstrumentDefinition>
            {
                ["correlationId"] = swapDefinition
            });

            Assert.That(createSwapResponse.Failed, Is.Empty);
        }
        public void Load_Otc_Instrument_Transaction()
        {
            var effectiveDate = new DateTimeOffset(2018, 1, 1, 0, 0, 0, TimeSpan.Zero);

            //    create the portfolio
            var portfolioCode = _testDataUtilities.CreateTransactionPortfolio(TestDataUtilities.TutorialScope);

            //    swap definition, this is uploaded in a client custom format
            var swapDefinition = new InstrumentDefinition(
                name: "10mm 5Y Fixed",
                identifiers: new Dictionary <string, InstrumentIdValue>
            {
                ["ClientInternal"] = new InstrumentIdValue(value: "SW-1")
            },
                definition: new ExoticInstrument(
                    instrumentFormat: new InstrumentDefinitionFormat("CustomSource", "CustomFormat", "0.1.2"),
                    content: "<customFormat>the definition of the swap uploaded in custom Xml or Json format. One would expect that it should contain the full custom swap detail.</customFormat>",
                    instrumentType: LusidInstrument.InstrumentTypeEnum.ExoticInstrument
                    ));

            //    create the swap
            var createSwapResponse = _instrumentsApi.UpsertInstruments(new Dictionary <string, InstrumentDefinition>
            {
                ["correlationId"] = swapDefinition
            });

            var swapId = createSwapResponse.Values.Values.Select(i => i.LusidInstrumentId).FirstOrDefault();

            //    create the transaction request
            var transaction = new TransactionRequest(

                //    unique transaction id
                transactionId: Guid.NewGuid().ToString(),

                //    instruments must already exist in LUSID and have a valid LUSID instrument id
                instrumentIdentifiers: new Dictionary <string, string>
            {
                [TestDataUtilities.LusidInstrumentIdentifier] = swapId
            },

                type: "Buy",
                totalConsideration: new CurrencyAndAmount(0.0M, "GBP"),
                transactionPrice: new TransactionPrice(0.0M),
                transactionDate: effectiveDate,
                settlementDate: effectiveDate,
                units: 1,
                source: "Custodian");

            //    add the transaction
            _transactionPortfoliosApi.UpsertTransactions(TestDataUtilities.TutorialScope, portfolioCode, new List <TransactionRequest> {
                transaction
            });

            //    get the transaction
            var transactions = _transactionPortfoliosApi.GetTransactions(TestDataUtilities.TutorialScope, portfolioCode);

            Assert.That(transactions.Values, Has.Count.EqualTo(1));
            Assert.That(transactions.Values[0].TransactionId, Is.EqualTo(transaction.TransactionId));
            Assert.That(transactions.Values[0].InstrumentUid, Is.EqualTo(swapId));
        }
Example #3
0
 private void SendInstrumentDefinition(InstrumentDefinitionRequest request, Instrument[] insts)
 {
     try {
         var data       = QBHelper.FilterInstrument(request, insts);
         var definition = new InstrumentDefinition();
         definition.Instruments = data;
         definition.ProviderId  = Id;
         definition.RequestId   = request.Id;
         definition.TotalNum    = data.Length;
         EmitInstrumentDefinition(definition);
         EmitInstrumentDefinitionEnd(request.Id, RequestResult.Completed, string.Empty);
     }
     catch (Exception ex) {
         CancelInstrumentRequest(request, ex.Message);
     }
 }
Example #4
0
        private void UpsertOtcToLusid(LusidInstrument instrument, string name, string idUniqueToInstrument)
        {
            // PACKAGE instrument for upsert
            var instrumentDefinition = new InstrumentDefinition(
                name: name,
                identifiers: new Dictionary <string, InstrumentIdValue>
            {
                ["ClientInternal"] = new InstrumentIdValue(value: idUniqueToInstrument)
            },
                definition: instrument
                );

            // put instrument into Lusid
            var response = _instrumentsApi.UpsertInstruments(new Dictionary <string, InstrumentDefinition>
            {
                ["someId1"] = instrumentDefinition
            });

            // Check the response succeeded and has no errors.
            Assert.That(response.Failed.Count, Is.EqualTo(0));
            Assert.That(response.Values.Count, Is.EqualTo(1));
        }
Example #5
0
        private void OnInstrumentDefinition(object sender, InstrumentDefinitionEventArgs args)
        {
            InstrumentDefinition definition = args.Definition;

            if (definition.RequestId != this.requestId)
            {
                return;
            }
            this.instruments.AddRange((IEnumerable <Instrument>)definition.Instruments);
            Gtk.Application.Invoke(delegate
            {
                if (definition.TotalNum <= 0)
                {
                    return;
                }
                double num = (double)this.instruments.Count / (double)definition.TotalNum * 100.0;
                if (num > 100.0)
                {
                    num = 100.0;
                }
//                    this.tsiProgress.Value = (int)num;
            });
        }
Example #6
0
        private void Dispatcher_InstrumentDefinition(object sender, InstrumentDefinitionEventArgs args)
        {
            InstrumentDefinition definition = args.Definition;

            if (definition.RequestId != this.requestId)
            {
                return;
            }
            this.instruments.AddRange((IEnumerable <Instrument>)definition.Instruments);
            this.InvokeAction((Action)(() =>
            {
                if (definition.TotalNum <= 0)
                {
                    return;
                }
                double num = (double)this.instruments.Count / (double)definition.TotalNum * 100.0;
                if (num > 100.0)
                {
                    num = 100.0;
                }
                this.tsiProgress.Value = (int)num;
            }));
        }
Example #7
0
        private void ReturnInstrumentDefinition(InstrumentDefinitionRequest request)
        {
            Dictionary <string, List <string> > dict = new Dictionary <string, List <string> >();

            List <Instrument> instruments = new List <Instrument>();

            // 不知道这样能不能解决死锁的问题
            List <InstrumentField> _instruments = new List <InstrumentField>(_dictInstruments.Values);

            foreach (InstrumentField contract in _instruments)
            {
                SmartQuant.InstrumentType instrumentType = (SmartQuant.InstrumentType)contract.Type;

                // filter by type
                if (request.FilterType != null &&
                    request.FilterType != instrumentType)
                {
                    continue;
                }

                // filter by exchange
                if (request.FilterExchange != null && !contract.ExchangeID.StartsWith(request.FilterExchange, StringComparison.CurrentCultureIgnoreCase))
                {
                    continue;
                }

                // filter by symbol
                if (request.FilterSymbol != null &&
                    !contract.Symbol.StartsWith(request.FilterSymbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    continue;
                }

                Instrument instrument = null;

                //if (UpdateInstrument)
                //{
                //    instrument = framework.InstrumentManager.Get(contract.Symbol);
                //    //if (instrument != null)
                //    //{
                //    //    AltId ai = instrument.AltId.Get(this.id);
                //    //    if (ai != null)
                //    //    {
                //    //        instrument.AltId.Remove(ai);
                //    //    }
                //    //}
                //}

                if (instrument == null)
                {
                    instrument = new Instrument(instrumentType, contract.Symbol);
                }

                instrument.AltId.Add(new AltId(this.id, contract.InstrumentID, contract.ExchangeID));

                instrument.PutCall = (SmartQuant.PutCall)contract.OptionsType;
                instrument.Strike  = contract.StrikePrice;

                instrument.Exchange   = contract.ExchangeID;
                instrument.CurrencyId = CurrencyId.CNY;
                instrument.TickSize   = contract.PriceTick;
                instrument.Factor     = contract.VolumeMultiple;

                double x = instrument.TickSize;
                if (x > 0.000001)
                {
                    int i = 0;
                    for (; x - (int)x != 0; ++i)
                    {
                        x = x * 10;
                    }
                    instrument.PriceFormat = string.Format("F{0}", i);
                }

                var description = new InstrumentJson();
                description.InstrumentName = contract.InstrumentName();
                description.ProductID      = contract.ProductID;
                description.Underlying     = contract.UnderlyingInstrID;
                instrument.Description     = JsonConvert.SerializeObject(description, description.GetType(), null);


                if (!string.IsNullOrWhiteSpace(contract.UnderlyingInstrID))
                {
                    // 要求先导入标的合约,再导入期权,如果在这里生成会丢失合约信息
                    Instrument UnderlyingInstrID = framework.InstrumentManager.Get(contract.UnderlyingInstrID);
                    if (UnderlyingInstrID == null)
                    {
                        //xlog.Warn("合约:{0},存在标的物字段,请先导入标的物合约{1},导入后放在Parent属性中", contract.Symbol, contract.UnderlyingInstrID);
                        List <string> list = null;
                        if (!dict.TryGetValue(contract.UnderlyingInstrID, out list))
                        {
                            list = new List <string>();
                            dict.Add(contract.UnderlyingInstrID, list);
                        }
                        list.Add(contract.Symbol);
                    }
                    else
                    {
                        instrument.Legs.Add(new Leg(UnderlyingInstrID));
                        //= framework.InstrumentManager.Get(contract.UnderlyingInstrID);
                    }
                }

                if (contract.ExpireDate > 0)
                {
                    int yyyy = contract.ExpireDate / 10000;
                    int MM   = contract.ExpireDate % 10000 / 100;
                    int dd   = contract.ExpireDate % 100;
                    // 居然有期权传回来的到期时间没有日
                    dd = Math.Max(dd, 1);
                    instrument.Maturity = new DateTime(yyyy, MM, dd);
                }

                //if (UpdateInstrument)
                //{
                //    framework.InstrumentManager.Save(instrument);
                //}

                instruments.Add(instrument);
            }

            if (dict.Count > 0)
            {
                xlog.Warn("标的物合约必须先导入,然后再Request,衍生品合约的Legs才会有一条记录指向标的物");
                foreach (var kv in dict)
                {
                    xlog.Warn("错过的标的物合约:{0},错过的衍生品合约数量{1}", kv.Key, kv.Value.Count);
                }
                xlog.Warn("请将以上合约先导入");
                xlog.Warn("如果在Request后列表中没有标的物合约,则需要手工添加");
            }


            instruments.Sort(SortInstrument);

            //
            InstrumentDefinition definition = new InstrumentDefinition();

            definition.RequestId   = request.Id;
            definition.ProviderId  = this.id;
            definition.Instruments = instruments.ToArray();
            definition.TotalNum    = instruments.Count;

            EmitInstrumentDefinition(definition);

            //
            InstrumentDefinitionEnd end = new InstrumentDefinitionEnd();

            end.RequestId = request.Id;
            end.Result    = RequestResult.Completed;
            end.Text      = string.Format("{0:n0} instrument(s) found.", instruments.Count);

            EmitInstrumentDefinitionEnd(end);
        }
Example #8
0
 public OnInstrumentDefinition(InstrumentDefinition definition)
 {
     InstrumentDefinition = definition;
 }
 public OnInstrumentDefinition(InstrumentDefinition definition)
 {
     this.Definition = definition;
 }
Example #10
0
 protected internal void EmitInstrumentDefinition(InstrumentDefinition definition)
 {
     OpenInstrumentQueue();
     this.instrumentQueue.Enqueue(new OnInstrumentDefinition(definition));
 }
Example #11
0
 public void OnInstrumentDefinition(InstrumentDefinition definition) => OnEvent(new OnInstrumentDefinition(definition));
Example #12
0
 public void OnInstrumentDefinition(InstrumentDefinition definition) => OnEvent(new OnInstrumentDefinition(definition));