user6703592 - 1 year ago 58

C# Question

I found a very strange thing:

`var PerformanceSingleList = TradePositionList//Compute the data for each stock everyday`

.SelectMany((o, idx) => o.Select(p =>

{

//default variable

double CloseYesterday = TradePositionList.ElementAt(0).Where(q => q.Stock.Symbol == p.Stock.Symbol).Select(q => (double)q.Stock.Close).First();

int PositionYesterday = TradePositionList.ElementAt(0).Where(q => q.Stock.Symbol == p.Stock.Symbol).Select(q => q.Position).First();

int TradePositionToday = 0;

if (idx != 0) //idx cann't be 0 otherwise, idx - 1 throw a error

{

PositionYesterday = TradePositionList.ElementAt(idx - 1).Where(q => q.Stock.Symbol == p.Stock.Symbol).Select(q => q.Position).First();

CloseYesterday = TradePositionList.ElementAt(idx - 1).Where(q => q.Stock.Symbol == p.Stock.Symbol).Select(q => (double)q.Stock.Close).First();

TradePositionToday = TradePositionList.ElementAt(idx - 1).Where(q => q.Stock.Symbol == p.Stock.Symbol).Select(q => q.RealDesignTradeShare).First();

}

double Commission = Math.Abs(TradePositionToday) * (double)p.Stock.Close * C;

return new

{

p.Stock,

p.Position,

p.RealDesignPosition,

p.RealDesignTradeShare,

TradePositionToday,

Commission,

PLN = ((double)p.Stock.Close - CloseYesterday) * PositionYesterday + TradePositionToday * (double)p.Stock.Close - Math.Abs(TradePositionToday) * (double)p.Stock.Close * SA,

NAV = (double)p.Stock.Close * p.Position - Math.Abs(p.Position) * (double)p.Stock.Close * SA - Commission

};

}

).ToList()

).ToList();

But, all the Commision in

`PerformanceSingleList`

`TradePositionToday`

`[61] { Stock = {{ Symbol = 1088 HK, Close = 12.860000000, Date = 3/7/2016 12:00:00 AM, Vol = 0.0490939300240106 }}, Position = -562, RealDesignPosition = 67, RealDesignTradeShare = 629, TradePositionToday = -562, Commission = 0, PLN = -7227.32, NAV = -7227.32 } <Anonymous Type>`

Commission 0 double

NAV -7227.32 double

PLN -7227.32 double

Position -562 int

RealDesignPosition 67 int

RealDesignTradeShare 629 int

Stock { Symbol = "1088 HK", Close = 12.860000000, Date = {3/7/2016 12:00:00 AM}, Vol = 0.049093930024010565 } <Anonymous Type>

TradePositionToday -562 int

Answer Source

Unfortunately, `const double C = 10/10000`

is indeed `0`

. 10 and 10000 are both integers, when the division is done, the result is stored as int (common type) before being converted to C as double.

In case if you'd like to have the C not to be zero, you need to explicitly tell one of the value as float/ double type, by simply using this statement instead:

```
const double C = 10.0/10000;
```

or

```
const double C = ((double)10)/10000;
```