I'm trying to make a simple tool in which you can track your invest with BTC. I have all the data a need to calculate everything but when comes to the exchange price changing my "investment total" doesn't change. Meaning when BTC goes up, my result of how much money I made stays the same almost and the same for when it goes down.
I have posted my code and tried a bunch of different solutions, including different types: floats, decimals, int's, doubles and it always turns out the same way. What am I missing?
EDIT - CODE:
namespace BTC_Profit_Projections
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Core c = new Core();
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
var ExchangeRate = c.GrabBTCAmount();
var AmountInBTC = (c.USDtoBTC(15000, ExchangeRate));
var AmountAtMarket = (AmountInBTC * ExchangeRate);
ListViewItem i = new ListViewItem("15000");
i.SubItems.Add(AmountInBTC.ToString());
i.SubItems.Add(AmountAtMarket.ToString());
i.SubItems.Add(ExchangeRate.ToString());
mainlist.Items.Add(i);
}
}
}
Core Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace BTC_Profit_Projections
{
class Core
{
public decimal GrabBTCAmount()
{
WebClient w = new WebClient();
string btc = w.DownloadString("https://www.bitstamp.net/api/ticker/");
int pos1 = btc.IndexOf("last", 0);
int pos2 = btc.IndexOf(":", pos1);
int pos3 = btc.IndexOf(",", pos2);
return Convert.ToDecimal(btc.Substring(pos2 + 3, pos3 - pos2 - 4));
}
public decimal USDtoBTC(decimal money, decimal rate)
{
return money / rate;
}
}
}
It is not clear why you expect this code to behave differently. Look at these lines:
var ExchangeRate = c.GrabBTCAmount();
var AmountInBTC = (c.USDtoBTC(15000, ExchangeRate));
var AmountAtMarket = (AmountInBTC * ExchangeRate);
If I inline USDtoBTC
and inline everything to calculate AmountAtMarket
the formula would be
AmountAtMarket = (15000 / ExchangeRate) * ExchangeRate
So you should always get 15000
with some rounding error.
The bug seems to be in the line where you calculate AmountInBTC
. To calculate AmountInBTC
you should divide your investment sum (I assume 15000
) by the exchange rate at the moment you did your investment rather than current exchange rate.