Чтение и запись данных в Excel
C#
Код показывает как читать/записывать данные в Excel файл при запущеной стратегии.
Код
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
= new Application();
Application excel
= 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);
workbook
= workbook.Worksheets;
Sheets sheets = (Worksheet)sheets.get_Item(1);
worksheet
// read A1 and A2 values
double length1 = (double)worksheet.get_Range("A1", "A1").Value2;
double length2 = (double)worksheet.get_Range("A2", "A2").Value2;
//
= new SMA(Bars, (int)length1);
sma1 = new SMA(Bars, (int)length2);
sma2 }
public override void OnBar(Bar bar)
{
.get_Range("B1", "B1").Value2 = bar.Close;
worksheet}
public override void OnStrategyStop()
{
// save and close the workbook
.Save();
workbook.Close(true, null, null);
workbook}
}