Advertisement
Guest User

Untitled

a guest
Aug 30th, 2017
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. import scala.concurrent.Future
  2. import akka.stream.{ActorMaterializer, KillSwitch, KillSwitches, ThrottleMode}
  3. import akka.stream.scaladsl.{Keep, Sink, Source}
  4. import akka.actor.ActorSystem
  5.  
  6. import scala.concurrent.duration._
  7. import scala.concurrent.ExecutionContext.Implicits.global
  8. import scala.util.Random
  9.  
  10. object TradeBot {
  11.  
  12. def main(args: Array[String]): Unit = {
  13.  
  14. implicit val system = ActorSystem("TradeBot")
  15. implicit val materializer = ActorMaterializer()
  16. var currentStockPrice = 0
  17.  
  18. final case class StockQuote(symbol: String, price: Int)
  19. sealed trait Trade {
  20. val tickerSymbol: String
  21. val price: Int
  22. val shares: Int
  23. }
  24. final case class BuyTrade(tickerSymbol: String, shares: Int, price: Int) extends Trade
  25. final case class SellTrade(tickerSymbol: String, shares: Int, price: Int) extends Trade
  26. final case class TradeResult(success: Boolean, trade: Trade)
  27.  
  28. var tradeBotKillSwitch: Option[KillSwitch] = None
  29.  
  30. def sendEmail(emailAddress: String, emailMessage: String): Future[Boolean] = {
  31. println(emailMessage)
  32. Future.successful(true)
  33. }
  34.  
  35. def sendTradeEmail(emailAddress: String)(tradeMade: Trade) = {
  36. sendEmail(emailAddress, s"Trade: $tradeMade")
  37. }
  38.  
  39. def getNextStockQuote(tickerSymbol: String, priceChange: Int) = {
  40. if (priceChange % 2 == 0 && currentStockPrice - priceChange > 0) {
  41. currentStockPrice = currentStockPrice - priceChange
  42. } else {
  43. currentStockPrice = currentStockPrice + priceChange
  44. }
  45. val nextStockQuote = StockQuote(tickerSymbol, currentStockPrice)
  46. println(s"Stock Quote: $nextStockQuote")
  47. nextStockQuote
  48. }
  49.  
  50. def getQuoteStreamForStock(buyPrice: Int, sellPrice: Int)(tickerSymbol: String) = {
  51. currentStockPrice = (buyPrice + sellPrice) / 2
  52. Source.fromIterator(() => Iterator.continually(getNextStockQuote(tickerSymbol, Random.nextInt(10))))
  53. .throttle(1, 1.second, 1, ThrottleMode.shaping)
  54. .take(100)
  55. }
  56.  
  57. def makeTrade(tradeToMake: Trade): Future[Boolean] = {
  58. Future.successful(true)
  59. }
  60.  
  61. def createTrade(buyPrice: Int, sellPrice: Int)(stockQuote: StockQuote): Option[Trade] = {
  62. if (stockQuote.price < buyPrice) {
  63. Some(BuyTrade(stockQuote.symbol, 1, stockQuote.price))
  64. } else if (stockQuote.price > sellPrice) {
  65. Some(SellTrade(stockQuote.symbol, 1, stockQuote.price))
  66. } else {
  67. None
  68. }
  69. }
  70.  
  71. def startTradeBot(tickerSymbol: String, tradesPerDayLimit: Int, buyPrice: Int, sellPrice: Int, notificationEmailAddress: String): Unit = {
  72. println(s"Starting new trade bot for $tickerSymbol at $tradesPerDayLimit per day, buy at $buyPrice sell at $sellPrice")
  73. tradeBotKillSwitch.foreach(_.shutdown())
  74.  
  75. val tradeBotTradeCreator = createTrade(buyPrice, sellPrice)(_)
  76. val tradeBotEmailCreator = sendTradeEmail(notificationEmailAddress)(_)
  77.  
  78. val newTradeBotKillSwitch = getQuoteStreamForStock(buyPrice, sellPrice)(tickerSymbol)
  79. .viaMat(KillSwitches.single)(Keep.right)
  80. .map(tradeBotTradeCreator)
  81. .mapConcat(_.toList)
  82. .throttle(tradesPerDayLimit, 1.day, tradesPerDayLimit, ThrottleMode.shaping)
  83. .mapAsync(tradesPerDayLimit)( trade => makeTrade(trade).map(TradeResult(_, trade)) )
  84. .filter(_.success)
  85. .mapAsync(tradesPerDayLimit) ( tradeResult => tradeBotEmailCreator(tradeResult.trade) )
  86. .toMat(Sink.ignore)(Keep.left)
  87. .run()
  88.  
  89. tradeBotKillSwitch = Some(newTradeBotKillSwitch)
  90. }
  91.  
  92. def stopTradeBot(): Unit = {
  93. println("Stopping trade bot")
  94. tradeBotKillSwitch.foreach(_.shutdown())
  95. }
  96.  
  97. startTradeBot("GOOGL", 10, 900, 950, "foobar@gmail.com")
  98. Thread.sleep(5000)
  99. startTradeBot("TSLA", 10, 330, 340, "foobar@gmail.com")
  100. Thread.sleep(5000)
  101. stopTradeBot()
  102. startTradeBot("AAPL", 1, 163, 165, "foobar@gmail.com")
  103.  
  104. }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement