3

We have an IEnumerable Objects in C# that we want to import into the Wolfram Financial Platform. In the current C# API, we use LINQ queries to select from the Objects the specific Object that we need. In Wolfram Financial Platform we can import the IEnumerable Objects through NET/Link (More info: http://reference.wolfram.com/language/NETLink/tutorial/Overview.html)

Once the object is imported, there does not seem to be a way to use LINQ queries even after importing the LINQ library. Additionally, lambda expressions used in the LINQ queries do not have a clear replacement in Wolfram Financial Platform.

Examples

C#

Object.Where(x => x.Property == Target).FirstOrDefault();

Wolfram

???[# == #.Property &, Object]`FirstOrDefault[];

Current Attempt + Error

C#

Option call = optionChain.Options
                                  .Where(x => (x.Expiration - DateTime.Today).TotalDays > 7 && (x.Expiration - DateTime.Today).TotalDays < 45 && x.PutCall == PutCall.Call && x.Class.Settlement == null)
                                  .OrderBy(x => Math.Abs(msft.Trade.Last - x.Strike)).FirstOrDefault();

Wolfram (baby steps version)

call = optionChain@Options`FirstOrDefault[];

Error

"No public instance method named "FirstOrDefault" exists for the .NET type "Core.Data.MarketData.OptionChain".

Also tried using First[optionChain@Options]; and got the non-atomic error. Found that could mean an infinite loop or non-existent variable. Checked that optionChain@Options exists by outputting it through NETConsole, which makes me think that NETObjects won't work with First[] (?)

End Goal

Import specific values from a C# API with LINQ queries, or equivalent, intact into Wolfram Financial Platform for the purpose of writing a program in the Wolfram Financial Platform language (not allowed to do it the other way around, as much as I want to).

Originally posted this on StackExchange, but it looks like it is pending removal due to being "off-topic"

  • Does optionChain@Options give a Wolfram list of option objects? If so have you tried Select? Something like Select[DateDifference[#`Expiration - Today, "Day"] > Quantity[7, "Days"] &]@(optionChain@Options). Elements of Lists guide – Edmund Mar 22 '16 at 17:06
  • optionChain@Options returns a System.Collections.Generic.List`1[Core.Data.Securities.Option] object, which gives a non-atomic exception when using Select. – user1981756 Mar 22 '16 at 17:37
  • Add @ToArray[] immediately after Options inside of the bracket. This will convert it into a Wolfram list. Then the Select should be happy. Well the only part I am unsure about is the #`Expiration bit. – Edmund Mar 22 '16 at 17:44
  • Sorry, the - in DateDifference should be a ,. – Edmund Mar 22 '16 at 17:49
  • Alright, cooking with gas, now. The DateDifference is having difficulties with the Expiration's .NETDateTime object, but now Select, First, etc. work with the Options object. – user1981756 Mar 22 '16 at 18:36
  • Currently

    call = First[# &]@ Select[DateDifference[DateObject[#@Expiration@Day] - Today, "Day"] < Quantity[15, "Days"] && #@Strike == 45 &]@(optionChain@ Options@ToArray[])

    Error

    DateDifference::date: Expression -42449.days cannot be interpreted as a date specification. >>

    – user1981756 Mar 22 '16 at 18:57

1 Answers1

2

A solution to this is to convert the .Net collection into a Wolfram List and then use Select. ToArray can be called on Options for this conversion.

Also (from our chat), since Expiration is not static in C# then @ can be used instead of `. Combining these we get:

Select[DateDifference[DateObject[#@Expiration /@ {Year, Month, Day}], 
     Today, "Day"] < Quantity[15, "Days"] &]@
 (optionChain @ Options @ ToArray[])

The components of Expiration are taken into a Wolfram DateObject specification by #@Expiration /@ {Year, Month, Day}

Additional conditions can be added to Select.

Hope this helps.

Edmund
  • 42,267
  • 3
  • 51
  • 143