Permalink
Browse files

log yesterday values in example

  • Loading branch information...
1 parent 44b64f0 commit 7770c0c14d9077a5d927556e23c6c1cabdd2c5eb @zhensydow committed Apr 19, 2012
Showing with 28 additions and 7 deletions.
  1. +28 −7 examples/example03.hs
View
35 examples/example03.hs
@@ -1,4 +1,3 @@
-
{- -----------------------------------------------------------------------------
PTrader is a Personal Stock Trader Toolbox.
Copyright (C) 2012 Luis Cabellos
@@ -20,15 +19,16 @@ module Main where
-- -----------------------------------------------------------------------------
import Control.Concurrent( ThreadId, forkIO, killThread )
-import Control.Monad( forM, forever )
+import Control.Monad( forM, forM_, forever )
+import Safe( readDef )
import System.Exit( exitSuccess )
import System.Posix.Signals( Handler(..), installHandler, sigINT )
import System.Posix.Unistd( sleep )
import PTrader.Report(
Report, runReport, newLine, newScreen, outStrLn,
stocksState, indexState, stocksProfit, showHolds )
import PTrader.Portfolio(
- runPortfolio, ownedStocks, calcStockNet, calcStockPrice, holds )
+ runPortfolio, ownedStocks, calcStockNet, calcStockPrice, holds, logStockValue )
import PTrader.Graph( GraphConfig(..), runGraph )
import PTrader.Query( StockValue(..), getMulValue )
import PTrader.Util( timeStamp )
@@ -58,8 +58,18 @@ myReport filename = do
timeStamp >>= outStrLn
-- -----------------------------------------------------------------------------
-graphUpdate :: [String] -> IO [Double]
-graphUpdate stocks = fmap (fmap read) $ getMulValue stocks Bid
+myDoubleRead :: String -> Double
+myDoubleRead [] = 0
+myDoubleRead (x:xs)
+ | x == '+' = readDef 0 xs
+ | otherwise = readDef 0 (x:xs)
+
+-- -----------------------------------------------------------------------------
+graphUpdate :: [String] -> [String] -> IO [Double]
+graphUpdate inds stocks = do
+ vis <- fmap (fmap myDoubleRead) $ getMulValue inds Change
+ vss <- fmap (fmap myDoubleRead) $ getMulValue stocks Bid
+ return $! vis ++ vss
-- -----------------------------------------------------------------------------
graphConfig :: GraphConfig
@@ -68,13 +78,24 @@ graphConfig = GraphConfig Nothing 30
graphThread :: String -> IO ThreadId
graphThread filename = do
stocks <- fmap (fmap fst) $ runPortfolio ownedStocks filename
- refvals <- fmap (fmap read) $ getMulValue stocks PreviousClose
- forkIO $ runGraph graphConfig stocks refvals (graphUpdate stocks)
+ refvals <- fmap (fmap $ readDef 0)
+ $ getMulValue stocks PreviousClose :: IO [Double]
+ let zeros = replicate (length indexes) 0
+ forkIO $ runGraph graphConfig (indexes ++ stocks) (zeros ++ refvals)
+ (graphUpdate indexes stocks)
+
+-- -----------------------------------------------------------------------------
+logYesterday :: String -> IO ()
+logYesterday filename = do
+ stocks <- fmap (fmap fst) $ runPortfolio ownedStocks filename
+ forM_ (indexes ++ stocks) $ \s ->
+ runPortfolio (logStockValue s) filename
-- -----------------------------------------------------------------------------
mainLoop :: String -> IO ()
mainLoop filename = do
gtid <- graphThread filename
+ logYesterday filename
_ <- installHandler sigINT (CatchOnce (killThread gtid >> exitSuccess)) Nothing
forever $ do
-- print myReport using colors

0 comments on commit 7770c0c

Please sign in to comment.