Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Threading.Tasks;
  7. using Akka.Actor;
  8. using Akka.Util.Internal;
  9. using CoinMM.Messages;
  10. using Newtonsoft.Json.Schema;
  11.  
  12. namespace CoinMM
  13. {
  14.  
  15.  
  16. public class FxConverter : ReceiveActor
  17. {
  18. private readonly IActorRef fxBroker;
  19. private readonly IActorRef MySelf;
  20.  
  21. private readonly Dictionary<string, FxItem> fxRates = new Dictionary<string, FxItem>()
  22. {
  23. // /USD
  24. {"EUR", new FxItem() {symbol = "EURUSD", isInverse = false}},
  25. {"GBP", new FxItem() {symbol = "GBPUSD", isInverse = false}},
  26. {"AUD", new FxItem() {symbol = "AUDUSD", isInverse = false}},
  27. {"NZD", new FxItem() {symbol = "NZDUSD", isInverse = false}},
  28.  
  29. // USD/
  30. {"JPY", new FxItem() {symbol = "USDJPY", isInverse = true}},
  31. {"CNY", new FxItem() {symbol = "USDCNY", isInverse = true}},
  32. {"HKD", new FxItem() {symbol = "USDHKD", isInverse = true}},
  33. {"SGD", new FxItem() {symbol = "USDSGD", isInverse = true}},
  34. {"ZAR", new FxItem() {symbol = "USDZAR", isInverse = true}}
  35. };
  36.  
  37. class FxItem
  38. {
  39. public string symbol;
  40. public bool isInverse;
  41. public double Price = double.NaN;
  42. }
  43.  
  44.  
  45. public FxConverter( IActorRef fxBroker)
  46. {
  47. this.fxBroker = fxBroker;
  48. MySelf = this.Self;
  49.  
  50. Receive<FxConverterStart>(OnStart);
  51. Receive<List<MarketData>>(OnMarketData);
  52. Receive<ConvertFxRequest>(OnConvertRequest);
  53. }
  54.  
  55. public bool OnStart(FxConverterStart start)
  56. {
  57. try
  58. {
  59. Console.WriteLine( $"FxConverter is subscribing quotes with {fxRates.Count} symbols. MyName:{MySelf}");
  60. fxRates.ForEach( async fx =>
  61. {
  62. var subscriptionResult = await fxBroker.Ask<MarketDataSubscriptionResult>( new MarketDataSubscription() {symbol = fx.Value.symbol , subscriber = MySelf});
  63. if (subscriptionResult.ok)
  64. {
  65. Console.WriteLine($"FxConverter added symbol: {fx.Value.symbol}");
  66. }
  67. else
  68. {
  69. Console.WriteLine($"FxConverter error adding symbol: {fx.Value.symbol} - symbol is not supported.");
  70. }
  71. });
  72. MySelf.Tell(new List<MarketData>());
  73. }
  74. catch (Exception ex)
  75. {
  76. Console.WriteLine($"FxConverter Subscribe exception: " + ex.Message);
  77. }
  78. return true;
  79. }
  80.  
  81.  
  82. public bool OnMarketData( List<MarketData> marketDataList)
  83. {
  84. Console.WriteLine($"fxConverter.OnMarketData..");
  85. if (marketDataList != null)
  86. {
  87. try
  88. {
  89. marketDataList.ForEach(marketData =>
  90. {
  91. if (marketData != null)
  92. {
  93. var foundItem = fxRates.FirstOrDefault(item => item.Value.symbol == marketData.symbol);
  94. if (foundItem.Key != null)
  95. {
  96. foundItem.Value.Price = (double) marketData.last;
  97. }
  98. else
  99. {
  100. Console.WriteLine(
  101. $"fxConverter has received symbol, but is not keeping this in it symbol list: {marketData.symbol}");
  102. }
  103. }
  104. });
  105. }
  106. catch (Exception ex)
  107. {
  108. Console.WriteLine($"FXConverter OnMarketDataReceived Exception {ex.Message}.");
  109. }
  110. }
  111. return true;
  112. }
  113.  
  114. public bool OnConvertRequest(ConvertFxRequest convertRequest)
  115. {
  116. Console.WriteLine($"fxConverter is converting {convertRequest.Price} from {convertRequest.Currency}..");
  117. if (convertRequest.Currency == "USD")
  118. {
  119. Sender.Tell(new ConvertFxResult {Price = convertRequest.Price});
  120. return true;
  121. }
  122.  
  123. if (fxRates.ContainsKey(convertRequest.Currency))
  124. {
  125. var fx = fxRates[convertRequest.Currency];
  126. if (fx.isInverse)
  127. {
  128. var convPrice = convertRequest.Price * fx.Price;
  129. Console.WriteLine($"fxConverter converted {convertRequest.Price} from {convertRequest.Currency} to {convPrice}.");
  130. Sender.Tell(new ConvertFxResult {Price = convPrice});
  131. }
  132. else
  133. {
  134. var convPrice = convertRequest.Price / fx.Price;
  135. Console.WriteLine($"fxConverter converted {convertRequest.Price} from {convertRequest.Currency} to {convPrice}.");
  136. Sender.Tell(new ConvertFxResult {Price = convPrice});
  137. }
  138. return true;
  139. }
  140.  
  141. Console.WriteLine($"fxConverter could not convert unknown currency: {convertRequest.Currency}.");
  142. Sender.Tell(new ConvertFxResult {Price= double.NaN});
  143. return true;
  144. }
  145.  
  146. }
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement