Чтение и запись данных в Excel

Код показывает как читать/записывать данные в Excel файл при запущеной стратегии.

To build the sample you should add a reference to the Interop.Excel.dll, using the following steps:

using System;
using System.Drawing;

using OpenQuant.API;
using OpenQuant.API.Indicators;

using Interop.Excel;

public class MyStrategy : Strategy
{
   SMA sma1;
   SMA sma2;
   
   Workbook workbook;
   Worksheet worksheet;
   
   public override void OnStrategyStart()
   {
      // open Excel file
      Application excel = new Application();

      workbook = excel.Workbooks.Open(@"C:\Users\s.b\Documents\SampleBook.xlsx", 0, false, 5, "", "", true, Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, null, null);

      Sheets sheets = workbook.Worksheets;
      worksheet = (Worksheet)sheets.get_Item(1);

      // read A1 and A2 values
      double length1 = (double)worksheet.get_Range("A1", "A1").Value2;
      double length2 = (double)worksheet.get_Range("A2", "A2").Value2;
      
      //      
      sma1 = new SMA(Bars, (int)length1);
      sma2 = new SMA(Bars, (int)length2);
   }

   public override void OnBar(Bar bar)
   {
      worksheet.get_Range("B1", "B1").Value2 = bar.Close;      
   }

   public override void OnStrategyStop()
   {
      // save and close the workbook
      workbook.Save();
      workbook.Close(true, null, null);
   }
}