Skip to content

Commit

Permalink
fixed bug when the month is 12 and added error checking for failed we…
Browse files Browse the repository at this point in the history
…b queries
  • Loading branch information
steveja42 committed Dec 2, 2016
1 parent 3699f6b commit edcd2a1
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions OptionSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ enum OptionLetter { P, C }
protected string optionLetter;
protected string webQueryTables; // 12 is header/date, 14 is puts

Workbook outputWorkbook;
const int symbolColumn = 2;

Workbook outputWorkbook;
Microsoft.Office.Interop.Excel.Application app;
Worksheet sheet;
Workbook sourceWorkbook;
Expand All @@ -61,6 +63,7 @@ protected OptionSheet(Workbook sourceWorkbook, Stock? stockIn)
stock = (Stock)stockIn;
this.sourceWorkbook = sourceWorkbook;
}
//makeSheet - fills in a new sheet with option prices for the coming 2 Januarys and the upcoming 2 months
public void makeSheet()
{
app = sourceWorkbook.Application;
Expand Down Expand Up @@ -92,8 +95,12 @@ public void makeSheet()
r = r.Offset[2, 0];
r = GetOptionStrikePrices(stock.symbol, iYear + 1, 1, r);
r = r.Offset[2, 0];
r = GetOptionStrikePrices(stock.symbol, iYear, iMonth + 1, r);
r = r.Offset[2, 0];

if (iMonth != 12) //january is already done above
{
r = GetOptionStrikePrices(stock.symbol, (iMonth == 12 ? iYear + 1 : iYear), (iMonth + 1) % 12, r);
r = r.Offset[2, 0];
}
r = GetOptionStrikePrices(stock.symbol, iYear, iMonth, r);


Expand All @@ -111,32 +118,37 @@ Range GetOptionStrikePrices(string stockSymbol, int iYear, int iMonth, Range des
webQuery.WebSelectionType = XlWebSelectionType.xlSpecifiedTables;
webQuery.WebTables = webQueryTables;

// webQuery.WebTables = "12,13"; //14 is puts , 12 is header/date

webQuery.WebFormatting = XlWebFormatting.xlWebFormattingRTF;
webQuery.BackgroundQuery = false;
webQuery.AdjustColumnWidth = false;
webQuery.Refresh();

int firstRow = destRange.Row + 3;
Range r = destRange.Offset[2, 0]; // app.Selection;
int firstDataRow = destRange.Row + 3; //first 3 rows are date info
Range r = destRange.Offset[2, 0];
if (sheet.Cells[firstDataRow, symbolColumn].value == null)
{
sheet.Cells[firstDataRow, symbolColumn].value = $"ERROR: Expected data from web query here- url: ${url}";
return r;
}

r = r.End[XlDirection.xlDown];
int lastRow = r.Row;

fillInFormulas(firstRow, lastRow);
fillInFormulas(firstDataRow, lastRow);
return r;
}

void fillInFormulas(int firstRow, int lastRow)
{
const int symbolColumn = 2;

const char bidColumn = 'J';
const char askColumn = 'K';
//=RTD("tos.rtd", , E$1, ".UPRO180119P"&$A6)
//=RTD("tos.rtd", , E$1, ".UPRO180119P"&Strike_Price)


string optionSymbol = sheet.Cells[firstRow, symbolColumn].value;

string baseSymbol = "." + optionSymbol.Remove(1 + optionSymbol.LastIndexOf(optionLetter));
string bidCellFormula = $"=RTD(\"tos.rtd\", , {bidColumn}$1, \"{baseSymbol}\"&Strike_Price";
string askCellFormula = $"=RTD(\"tos.rtd\", , {askColumn}$1, \"{baseSymbol}\"&Strike_Price";
Expand Down

0 comments on commit edcd2a1

Please sign in to comment.