|
|
|
@ -9,23 +9,14 @@ |
|
|
|
{-# LANGUAGE TypeSynonymInstances #-} |
|
|
|
{-# LANGUAGE TypeSynonymInstances #-} |
|
|
|
|
|
|
|
|
|
|
|
module ATrade.RoboCom.Monad ( |
|
|
|
module ATrade.RoboCom.Monad ( |
|
|
|
RState, |
|
|
|
|
|
|
|
RConfig, |
|
|
|
|
|
|
|
RActions, |
|
|
|
|
|
|
|
REnv, |
|
|
|
|
|
|
|
StrategyEnvironment(..), |
|
|
|
StrategyEnvironment(..), |
|
|
|
seInstanceId, |
|
|
|
seInstanceId, |
|
|
|
seAccount, |
|
|
|
seAccount, |
|
|
|
seVolume, |
|
|
|
seVolume, |
|
|
|
seBars, |
|
|
|
seBars, |
|
|
|
seLastTimestamp, |
|
|
|
seLastTimestamp, |
|
|
|
StrategyElement, |
|
|
|
|
|
|
|
runStrategyElement, |
|
|
|
|
|
|
|
EventCallback, |
|
|
|
EventCallback, |
|
|
|
Event(..), |
|
|
|
Event(..), |
|
|
|
StrategyMonad, |
|
|
|
|
|
|
|
StrategyAction(..), |
|
|
|
|
|
|
|
tellAction, |
|
|
|
|
|
|
|
MonadRobot(..), |
|
|
|
MonadRobot(..), |
|
|
|
also, |
|
|
|
also, |
|
|
|
st |
|
|
|
st |
|
|
|
@ -34,8 +25,6 @@ module ATrade.RoboCom.Monad ( |
|
|
|
import ATrade.RoboCom.Types |
|
|
|
import ATrade.RoboCom.Types |
|
|
|
import ATrade.Types |
|
|
|
import ATrade.Types |
|
|
|
|
|
|
|
|
|
|
|
import Ether |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import Control.Lens |
|
|
|
import Control.Lens |
|
|
|
import Data.Aeson.Types |
|
|
|
import Data.Aeson.Types |
|
|
|
import qualified Data.Text as T |
|
|
|
import qualified Data.Text as T |
|
|
|
@ -57,19 +46,6 @@ class (Monad m) => MonadRobot m c s | m -> c, m -> s where |
|
|
|
setState (f oldState) |
|
|
|
setState (f oldState) |
|
|
|
getEnvironment :: m StrategyEnvironment |
|
|
|
getEnvironment :: m StrategyEnvironment |
|
|
|
|
|
|
|
|
|
|
|
data RState |
|
|
|
|
|
|
|
data RConfig |
|
|
|
|
|
|
|
data RActions |
|
|
|
|
|
|
|
data REnv |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type StrategyMonad c s = WriterT RActions [StrategyAction] (StateT RState s (ReaderT REnv StrategyEnvironment (Reader RConfig c))) |
|
|
|
|
|
|
|
type StrategyElement c s r = (StrategyMonad c s) r |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
runStrategyElement :: c -> s -> StrategyEnvironment -> StrategyElement c s r -> (s, [StrategyAction], r) |
|
|
|
|
|
|
|
runStrategyElement conf sta env action = (newState, actions, retValue) |
|
|
|
|
|
|
|
where |
|
|
|
|
|
|
|
((retValue, actions), newState) = runReader @RConfig (runReaderT @REnv (runStateT @RState (runWriterT @RActions action) sta) env) conf |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type EventCallback c s = forall m . MonadRobot m c s => Event -> m () |
|
|
|
type EventCallback c s = forall m . MonadRobot m c s => Event -> m () |
|
|
|
|
|
|
|
|
|
|
|
data Event = NewBar Bar |
|
|
|
data Event = NewBar Bar |
|
|
|
@ -82,12 +58,6 @@ data Event = NewBar Bar |
|
|
|
| ActionCompleted Int Value |
|
|
|
| ActionCompleted Int Value |
|
|
|
deriving (Show, Eq) |
|
|
|
deriving (Show, Eq) |
|
|
|
|
|
|
|
|
|
|
|
data StrategyAction = ActionOrder Order |
|
|
|
|
|
|
|
| ActionCancelOrder OrderId |
|
|
|
|
|
|
|
| ActionLog T.Text |
|
|
|
|
|
|
|
| ActionSetupTimer UTCTime |
|
|
|
|
|
|
|
| ActionIO Int (IO Value) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data StrategyEnvironment = StrategyEnvironment { |
|
|
|
data StrategyEnvironment = StrategyEnvironment { |
|
|
|
_seInstanceId :: !T.Text, -- ^ Strategy instance identifier. Should be unique among all strategies (very desirable) |
|
|
|
_seInstanceId :: !T.Text, -- ^ Strategy instance identifier. Should be unique among all strategies (very desirable) |
|
|
|
_seAccount :: !T.Text, -- ^ Account string to use for this strategy instance. Broker-dependent |
|
|
|
_seAccount :: !T.Text, -- ^ Account string to use for this strategy instance. Broker-dependent |
|
|
|
@ -98,27 +68,6 @@ data StrategyEnvironment = StrategyEnvironment { |
|
|
|
makeLenses ''StrategyEnvironment |
|
|
|
makeLenses ''StrategyEnvironment |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
instance Show StrategyAction where |
|
|
|
|
|
|
|
show (ActionOrder order) = "ActionOrder " ++ show order |
|
|
|
|
|
|
|
show (ActionCancelOrder oid) = "ActionCancelOrder " ++ show oid |
|
|
|
|
|
|
|
show (ActionLog t) = "ActionLog " ++ show t |
|
|
|
|
|
|
|
show (ActionIO x _) = "ActionIO " ++ show x |
|
|
|
|
|
|
|
show (ActionSetupTimer t) = "ActionSetupTimer e" ++ show t |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tellAction :: StrategyAction -> StrategyElement c s () |
|
|
|
|
|
|
|
tellAction a = tell @RActions [a] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
instance MonadRobot (StrategyMonad c s) c s where |
|
|
|
|
|
|
|
submitOrder order = tellAction $ ActionOrder order |
|
|
|
|
|
|
|
cancelOrder oId = tellAction $ ActionCancelOrder oId |
|
|
|
|
|
|
|
appendToLog = tellAction . ActionLog |
|
|
|
|
|
|
|
setupTimer = tellAction . ActionSetupTimer |
|
|
|
|
|
|
|
enqueueIOAction actionId action = tellAction $ ActionIO actionId action |
|
|
|
|
|
|
|
getConfig = ask @RConfig |
|
|
|
|
|
|
|
getState = get @RState |
|
|
|
|
|
|
|
setState = put @RState |
|
|
|
|
|
|
|
getEnvironment = ask @REnv |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
also :: EventCallback c s -> EventCallback c s -> EventCallback c s |
|
|
|
also :: EventCallback c s -> EventCallback c s -> EventCallback c s |
|
|
|
also cb1 cb2 = (\event -> cb1 event >> cb2 event) |
|
|
|
also cb1 cb2 = (\event -> cb1 event >> cb2 event) |
|
|
|
|
|
|
|
|
|
|
|
|