/
StockWorker.cs
54 lines (46 loc) 路 1.28 KB
/
StockWorker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
namespace Atento
{
class Worker
{
private static int TimeBetweenApiCalls = 1000 * 60 * 5; // One request each 5 min
public static async Task watchStock(string stockSymbol, decimal priceToSell, decimal priceToBuy)
{
// These flags will be used to avoid sending multiple
// emails suggesting the same thing
bool shouldSendSellEmail = true;
bool shouldSendBuyEmail = true;
decimal stockPrice = 0;
EmailSender sender = new EmailSender();
while (true)
{
stockPrice = await ApiWrapper.getStockPrice(stockSymbol);
System.Console.WriteLine($"Valor do ativo {stockSymbol}: " + stockPrice);
if (stockPrice > priceToSell && shouldSendSellEmail)
{
await sender.sendNotificationEmail(
EmailSender.StockActionType.Sell,
stockSymbol,
stockPrice,
priceToSell
);
shouldSendSellEmail = false;
}
if (stockPrice < priceToBuy && shouldSendBuyEmail)
{
await sender.sendNotificationEmail(
EmailSender.StockActionType.Buy,
stockSymbol,
stockPrice,
priceToBuy
);
shouldSendBuyEmail = false;
}
if (stockPrice <= priceToSell)
shouldSendSellEmail = true;
if (stockPrice >= priceToBuy)
shouldSendBuyEmail = true;
await Task.Delay(TimeBetweenApiCalls);
}
}
}
}