SmartQuant Discussion
http://www.smartquant.com/forums/

Best way to call an array of price values?
http://www.smartquant.com/forums/viewtopic.php?f=44&t=8791
Page 1 of 1

Author:  L.C. [ Tue Feb 08, 2011 6:03 am ]
Post subject:  Best way to call an array of price values?

Hi,

Whats the most effecient way to reference an array of openquant data... for example of the last 1000 closing values of an instrument so that it can be passed to a double[] for other analysis? I'm trying to accomplish something like this

double[] dataArray = new double[1000];
for (int i = 0; i < dataArray.Length; i++)
{
dataArray[i] = instrument1.Bar.Close[i];

}

using (Engine eng = Engine.Open())
{
eng.SetVariable("data", dataArray);
eng.Eval("ReturnSeries1 = price2ret(data)");
eng.Eval("plot(ReturnSeries1);

I'm just a bit confused because it seems that it can be done a few ways in openquant and I've usually only needed first and last values to do similar things.

Thanks In Advance
L.C.

Author:  cs96ai [ Mon Feb 14, 2011 9:17 pm ]
Post subject:  Re: Best way to call an array of price values?

Sorry... Don't really understand what you're getting at.
You just want to copy array values?

If that's what you want to do you could:
Code:
//
        // Instantiate the source array.
        //
        int[] source = new int[5];
        source[0] = 1;
        source[1] = 2;
        source[2] = 3;
        source[3] = 4;
        source[4] = 5;
        //
        // Instantiate and allocate the target array.
        //
        int[] target = new int[5];
        //
        // Copy the source to the target.
        //
        Array.Copy(source, target, 5);


If you want to just pass a pointer... in C# you need to use the fixed keyword.
http://msdn.microsoft.com/en-us/library/f58wzh21%28v=vs.80%29.aspx

If you want to copy values from a list of objects you can use linq.
http://msdn.microsoft.com/en-us/vcsharp/aa336746

But for just copying values from an array... you got it... just use a good ol for loop.

Author:  L.C. [ Tue Mar 08, 2011 7:03 pm ]
Post subject:  Re: Best way to call an array of price values?

Hi,

Thanks for the response...I'm actually just now returning to this project... To explain a bit better I'm using some Matlab wrappers that I need to convert BarSeries data into a type double array for a very lengthy script. My question would probably be better put is there a better way to place all the close data of a barseries in dataArray other than defining each value as Barseries.Ago(0).Close etc....?

Sorry the code is a little sloppy... I was just playing arround with a few ideas... Any help is greatly appeciated

Thanks In Advance


Code:
      
      Instrument TSEURUSD = InstrumentManager.Instruments["TSEURUSD"];
      BarSeries EURBars = DataManager.GetHistoricalBars(TSEURUSD, BarType.Time, 60);
      DateTime datetime2 = DateTime.Now;
      DateTime datetime1 = datetime2.AddHours(-4);
      foreach (Bar bar in DataManager.GetHistoricalBars(TSEURUSD, datetime1, datetime2, BarType.Time, 60))
      EURBars.Add(bar);


         double[] dataArray = new double[6];
         for (int i = 0; i < dataArray.Length; i++)
         {
            dataArray[0] = EURBars.Ago(0).Close ;   
            dataArray[1] = EURBars.Ago(1).Close ;
            dataArray[2] = EURBars.Ago(2).Close ;   
            dataArray[3] = EURBars.Ago(3).Close ;
            dataArray[4] = EURBars.Ago(4).Close ;
            dataArray[5] = EURBars.Ago(5).Close ;
            Array.Reverse(dataArray);

         }
         
         

         using (Engine eng = Engine.Open())
         {
            eng.SetVariable("data", dataArray);
            eng.Eval("retseries = price2ret(data)");
            eng.Eval("data_mean = mean(data)");
            Console.WriteLine("The mean is : {0}", eng.GetVariable<double("data_mean"));
                      eng.Eval("plot(retseries); figure(gcf)");
         
            // Clean up
            eng.Eval("clear ");
            eng.Eval("clc");         
         }
         Console.Read();
      }
   }

Page 1 of 1 All times are UTC + 3 hours
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/