Class GenericCassandreStrategy
- java.lang.Object
-
- tech.cassandre.trading.bot.strategy.GenericCassandreStrategy
-
- All Implemented Interfaces:
CassandreStrategyInterface
- Direct Known Subclasses:
BasicCassandreStrategy
,BasicTa4jCassandreStrategy
public abstract class GenericCassandreStrategy extends Object implements CassandreStrategyInterface
Generic Cassandre strategy implementation.
-
-
Field Summary
Fields Modifier and Type Field Description protected CurrencyMapper
currencyMapper
Currency mapper.protected ExchangeService
exchangeService
Exchange service.protected ImportedTickersRepository
importedTickersRepository
Imported tickers repository.protected org.slf4j.Logger
logger
Logger.protected OrderMapper
orderMapper
Order mapper.protected OrderRepository
orderRepository
Order repository.protected PositionFlux
positionFlux
Position flux.protected PositionMapper
positionMapper
Position mapper.protected PositionRepository
positionRepository
Position repository.protected PositionService
positionService
Position service.protected StrategyDTO
strategy
Strategy.protected TickerMapper
tickerMapper
Ticker mapper.protected TradeMapper
tradeMapper
Trade mapper.protected TradeRepository
tradeRepository
Trade repository.protected TradeService
tradeService
Trade service.
-
Constructor Summary
Constructors Constructor Description GenericCassandreStrategy()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
accountsUpdates(Set<AccountDTO> accounts)
Method called by streams on accounts updates.boolean
canBuy(AccountDTO account, CurrencyPairDTO currencyPair, BigDecimal amount)
Returns true if we have enough assets to buy.boolean
canBuy(AccountDTO account, CurrencyPairDTO currencyPair, BigDecimal amount, BigDecimal minimumBalanceAfter)
Returns true if we have enough assets to buy and if minimumBalanceAfter is left on the account after.boolean
canBuy(CurrencyPairDTO currencyPair, BigDecimal amount)
Returns true if we have enough assets to buy.boolean
canBuy(CurrencyPairDTO currencyPair, BigDecimal amount, BigDecimal minimumBalanceAfter)
Returns true if we have enough assets to buy.boolean
canSell(AccountDTO account, CurrencyDTO currency, BigDecimal amount)
Returns true if we have enough assets to sell.boolean
canSell(AccountDTO account, CurrencyDTO currency, BigDecimal amount, BigDecimal minimumBalanceAfter)
Returns true if we have enough assets to sell and if minimumBalanceAfter is left on the account after.boolean
canSell(CurrencyDTO currency, BigDecimal amount)
Returns true if we have enough assets to sell.boolean
canSell(CurrencyDTO currency, BigDecimal amount, BigDecimal minimumBalanceAfter)
Returns true if we have enough assets to sell.void
closePosition(long id)
Close position (no matter the rules).OrderCreationResultDTO
createBuyLimitOrder(CurrencyPairDTO currencyPair, BigDecimal amount, BigDecimal limitPrice)
Creates a buy limit order.OrderCreationResultDTO
createBuyMarketOrder(CurrencyPairDTO currencyPair, BigDecimal amount)
Creates a buy market order.PositionCreationResultDTO
createLongPosition(CurrencyPairDTO currencyPair, BigDecimal amount, PositionRulesDTO rules)
Creates a long position with its associated rules.OrderCreationResultDTO
createSellLimitOrder(CurrencyPairDTO currencyPair, BigDecimal amount, BigDecimal limitPrice)
Creates a sell limit order.OrderCreationResultDTO
createSellMarketOrder(CurrencyPairDTO currencyPair, BigDecimal amount)
Creates a sell market order.PositionCreationResultDTO
createShortPosition(CurrencyPairDTO currencyPair, BigDecimal amount, PositionRulesDTO rules)
Creates a short position with its associated rules.Optional<AccountDTO>
getAccountByAccountId(String accountId)
Search and return and account by its id.Map<String,AccountDTO>
getAccounts()
Returns list of accounts.BigDecimal
getAmountsLockedByCurrency(CurrencyDTO currency)
Returns the amounts locked for a specific currency.Map<Long,CurrencyAmountDTO>
getAmountsLockedByPosition()
Getter amountsLockedByPosition.Optional<BigDecimal>
getEstimatedBuyableAmount(CurrencyAmountDTO amountToUse, CurrencyDTO currencyWanted)
Returns the amount of a currency I can buy with a certain amount of another currency.Optional<CurrencyAmountDTO>
getEstimatedBuyingCost(CurrencyPairDTO currencyPair, BigDecimal amount)
Returns the cost of buying an amount of a currency pair.ExchangeService
getExchangeService()
Getter exchangeService.Map<CurrencyDTO,GainDTO>
getGains()
Returns gains of all positions.List<TickerDTO>
getImportedTickers()
Return the list of imported tickers (ordered by timestamp).List<TickerDTO>
getImportedTickers(CurrencyPairDTO currencyPair)
Return the list of imported tickers for a specific currency pair (ordered by timestamp).BigDecimal
getLastPriceForCurrencyPair(CurrencyPairDTO currencyPair)
Returns the last price received for a currency pair.Optional<TickerDTO>
getLastTickerByCurrencyPair(CurrencyPairDTO currencyPair)
Return the last ticker for a currency pair.Map<CurrencyPairDTO,TickerDTO>
getLastTickers()
Return last received tickers.Optional<OrderDTO>
getOrderByOrderId(String orderId)
Get an order by its id.Map<String,OrderDTO>
getOrders()
Returns list of orders.Optional<PositionDTO>
getPositionByPositionId(long positionId)
Get a position by its id.Map<Long,PositionDTO>
getPositions()
Returns list of positions.StrategyDTO
getStrategyDTO()
Getter strategyDTO.Optional<AccountDTO>
getTradeAccount()
Returns the trading account.Optional<TradeDTO>
getTradeByTradeId(String tradeId)
Get a trade by its id.Map<String,TradeDTO>
getTrades()
Returns list of trades.void
initialize()
This method is called by Cassandre before flux are started.void
initializeAccounts(Map<String,AccountDTO> newAccounts)
Initialize strategy accounts with exchange accounts data.void
onAccountsUpdates(Map<String,AccountDTO> accounts)
Method called by Cassandre when there are accounts updates.void
onOrdersUpdates(Map<String,OrderDTO> orders)
Method called by Cassandre when there are orders updates.void
onPositionsStatusUpdates(Map<Long,PositionDTO> positions)
Method called by Cassandre when there are positions status updates.void
onPositionsUpdates(Map<Long,PositionDTO> positions)
Method called by Cassandre when there are positions updates.void
onTickersUpdates(Map<CurrencyPairDTO,TickerDTO> tickers)
Method called by Cassandre when there are tickers updates.void
onTradesUpdates(Map<String,TradeDTO> trades)
Method called by Cassandre when there are trades updates.void
ordersUpdates(Set<OrderDTO> orders)
Method called by streams on orders updates.void
positionsUpdates(Set<PositionDTO> positions)
Method called by streams on positions updates.void
setExchangeService(ExchangeService newExchangeService)
Setter for exchangeService.void
setImportedTickersRepository(ImportedTickersRepository newImportedTickersRepository)
Setter ImportedTickersRepository.void
setOrderRepository(OrderRepository newOrderRepository)
Setter order repository.void
setPositionFlux(PositionFlux newPositionFlux)
Setter position flux.void
setPositionRepository(PositionRepository newPositionRepository)
Setter positionRepository.void
setPositionService(PositionService newPositionService)
Setter for positionService.void
setStrategy(StrategyDTO newStrategyDTO)
Setter strategyDTO.void
setTradeRepository(TradeRepository newTradeRepository)
Setter trade repository.void
setTradeService(TradeService newTradeService)
Setter for tradeService.void
tickersUpdates(Set<TickerDTO> tickers)
Method called by streams on tickers updates.void
tradesUpdates(Set<TradeDTO> trades)
Method called by streams on trades updates.void
updatePositionRules(long id, PositionRulesDTO newRules)
Update position rules.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface tech.cassandre.trading.bot.strategy.CassandreStrategyInterface
getRequestedCurrencyPairs, getTradeAccount
-
-
-
-
Field Detail
-
currencyMapper
protected final CurrencyMapper currencyMapper
Currency mapper.
-
orderMapper
protected final OrderMapper orderMapper
Order mapper.
-
tradeMapper
protected final TradeMapper tradeMapper
Trade mapper.
-
positionMapper
protected final PositionMapper positionMapper
Position mapper.
-
tickerMapper
protected final TickerMapper tickerMapper
Ticker mapper.
-
logger
protected final org.slf4j.Logger logger
Logger.
-
strategy
protected StrategyDTO strategy
Strategy.
-
positionFlux
protected PositionFlux positionFlux
Position flux.
-
orderRepository
protected OrderRepository orderRepository
Order repository.
-
tradeRepository
protected TradeRepository tradeRepository
Trade repository.
-
positionRepository
protected PositionRepository positionRepository
Position repository.
-
importedTickersRepository
protected ImportedTickersRepository importedTickersRepository
Imported tickers repository.
-
exchangeService
protected ExchangeService exchangeService
Exchange service.
-
tradeService
protected TradeService tradeService
Trade service.
-
positionService
protected PositionService positionService
Position service.
-
-
Method Detail
-
getStrategyDTO
public final StrategyDTO getStrategyDTO()
Getter strategyDTO.- Returns:
- strategyDTO
-
getExchangeService
public final ExchangeService getExchangeService()
Getter exchangeService.- Returns:
- exchangeService
-
setStrategy
public final void setStrategy(StrategyDTO newStrategyDTO)
Description copied from interface:CassandreStrategyInterface
Setter strategyDTO.- Specified by:
setStrategy
in interfaceCassandreStrategyInterface
- Parameters:
newStrategyDTO
- strategy DTO.
-
setPositionFlux
public void setPositionFlux(PositionFlux newPositionFlux)
Description copied from interface:CassandreStrategyInterface
Setter position flux.- Specified by:
setPositionFlux
in interfaceCassandreStrategyInterface
- Parameters:
newPositionFlux
- position flux
-
setPositionRepository
public final void setPositionRepository(PositionRepository newPositionRepository)
Description copied from interface:CassandreStrategyInterface
Setter positionRepository.- Specified by:
setPositionRepository
in interfaceCassandreStrategyInterface
- Parameters:
newPositionRepository
- the positionRepository to set
-
setOrderRepository
public final void setOrderRepository(OrderRepository newOrderRepository)
Description copied from interface:CassandreStrategyInterface
Setter order repository.- Specified by:
setOrderRepository
in interfaceCassandreStrategyInterface
- Parameters:
newOrderRepository
- order repository
-
setTradeRepository
public final void setTradeRepository(TradeRepository newTradeRepository)
Description copied from interface:CassandreStrategyInterface
Setter trade repository.- Specified by:
setTradeRepository
in interfaceCassandreStrategyInterface
- Parameters:
newTradeRepository
- trade repository.
-
setImportedTickersRepository
public void setImportedTickersRepository(ImportedTickersRepository newImportedTickersRepository)
Description copied from interface:CassandreStrategyInterface
Setter ImportedTickersRepository.- Specified by:
setImportedTickersRepository
in interfaceCassandreStrategyInterface
- Parameters:
newImportedTickersRepository
- ImportedTickers Repository
-
setExchangeService
public void setExchangeService(ExchangeService newExchangeService)
Description copied from interface:CassandreStrategyInterface
Setter for exchangeService.- Specified by:
setExchangeService
in interfaceCassandreStrategyInterface
- Parameters:
newExchangeService
- exchange service
-
setTradeService
public final void setTradeService(TradeService newTradeService)
Description copied from interface:CassandreStrategyInterface
Setter for tradeService.- Specified by:
setTradeService
in interfaceCassandreStrategyInterface
- Parameters:
newTradeService
- the tradeService to set
-
setPositionService
public final void setPositionService(PositionService newPositionService)
Description copied from interface:CassandreStrategyInterface
Setter for positionService.- Specified by:
setPositionService
in interfaceCassandreStrategyInterface
- Parameters:
newPositionService
- position service
-
initializeAccounts
public void initializeAccounts(Map<String,AccountDTO> newAccounts)
Description copied from interface:CassandreStrategyInterface
Initialize strategy accounts with exchange accounts data.- Specified by:
initializeAccounts
in interfaceCassandreStrategyInterface
- Parameters:
newAccounts
- accounts
-
accountsUpdates
public void accountsUpdates(Set<AccountDTO> accounts)
Description copied from interface:CassandreStrategyInterface
Method called by streams on accounts updates.- Specified by:
accountsUpdates
in interfaceCassandreStrategyInterface
- Parameters:
accounts
- accounts updates
-
tickersUpdates
public void tickersUpdates(Set<TickerDTO> tickers)
Description copied from interface:CassandreStrategyInterface
Method called by streams on tickers updates.- Specified by:
tickersUpdates
in interfaceCassandreStrategyInterface
- Parameters:
tickers
- tickers updates
-
ordersUpdates
public void ordersUpdates(Set<OrderDTO> orders)
Description copied from interface:CassandreStrategyInterface
Method called by streams on orders updates.- Specified by:
ordersUpdates
in interfaceCassandreStrategyInterface
- Parameters:
orders
- orders updates
-
tradesUpdates
public void tradesUpdates(Set<TradeDTO> trades)
Description copied from interface:CassandreStrategyInterface
Method called by streams on trades updates.- Specified by:
tradesUpdates
in interfaceCassandreStrategyInterface
- Parameters:
trades
- trades updates
-
positionsUpdates
public void positionsUpdates(Set<PositionDTO> positions)
Description copied from interface:CassandreStrategyInterface
Method called by streams on positions updates.- Specified by:
positionsUpdates
in interfaceCassandreStrategyInterface
- Parameters:
positions
- positions updates
-
getAccounts
public final Map<String,AccountDTO> getAccounts()
Returns list of accounts.- Returns:
- accounts
-
getAccountByAccountId
public final Optional<AccountDTO> getAccountByAccountId(String accountId)
Search and return and account by its id.- Parameters:
accountId
- account id- Returns:
- account
-
getTradeAccount
public final Optional<AccountDTO> getTradeAccount()
Description copied from interface:CassandreStrategyInterface
Returns the trading account.- Specified by:
getTradeAccount
in interfaceCassandreStrategyInterface
- Returns:
- your trading account
-
getAmountsLockedByPosition
public final Map<Long,CurrencyAmountDTO> getAmountsLockedByPosition()
Getter amountsLockedByPosition.- Returns:
- amountsLockedByPosition
-
getAmountsLockedByCurrency
public final BigDecimal getAmountsLockedByCurrency(CurrencyDTO currency)
Returns the amounts locked for a specific currency.- Parameters:
currency
- currency- Returns:
- amount
-
getLastTickers
public final Map<CurrencyPairDTO,TickerDTO> getLastTickers()
Return last received tickers.- Returns:
- ticker
-
getLastTickerByCurrencyPair
public final Optional<TickerDTO> getLastTickerByCurrencyPair(CurrencyPairDTO currencyPair)
Return the last ticker for a currency pair.- Parameters:
currencyPair
- currency pair- Returns:
- last ticker received
-
getLastPriceForCurrencyPair
public final BigDecimal getLastPriceForCurrencyPair(CurrencyPairDTO currencyPair)
Returns the last price received for a currency pair.- Parameters:
currencyPair
- currency pair- Returns:
- last price
-
getImportedTickers
public final List<TickerDTO> getImportedTickers()
Return the list of imported tickers (ordered by timestamp).- Returns:
- imported tickers
-
getImportedTickers
public final List<TickerDTO> getImportedTickers(CurrencyPairDTO currencyPair)
Return the list of imported tickers for a specific currency pair (ordered by timestamp).- Parameters:
currencyPair
- currency pair- Returns:
- imported tickers
-
getOrderByOrderId
public final Optional<OrderDTO> getOrderByOrderId(String orderId)
Get an order by its id.- Parameters:
orderId
- order id- Returns:
- order
-
getTradeByTradeId
public final Optional<TradeDTO> getTradeByTradeId(String tradeId)
Get a trade by its id.- Parameters:
tradeId
- trade id- Returns:
- trade
-
getPositions
public final Map<Long,PositionDTO> getPositions()
Returns list of positions.- Returns:
- positions
-
getPositionByPositionId
public final Optional<PositionDTO> getPositionByPositionId(long positionId)
Get a position by its id.- Parameters:
positionId
- position id- Returns:
- position
-
getGains
public final Map<CurrencyDTO,GainDTO> getGains()
Returns gains of all positions.- Returns:
- total gains
-
createBuyMarketOrder
public OrderCreationResultDTO createBuyMarketOrder(CurrencyPairDTO currencyPair, BigDecimal amount)
Creates a buy market order.- Parameters:
currencyPair
- currency pairamount
- amount- Returns:
- order result (order id or error)
-
createSellMarketOrder
public OrderCreationResultDTO createSellMarketOrder(CurrencyPairDTO currencyPair, BigDecimal amount)
Creates a sell market order.- Parameters:
currencyPair
- currency pairamount
- amount- Returns:
- order result (order id or error)
-
createBuyLimitOrder
public OrderCreationResultDTO createBuyLimitOrder(CurrencyPairDTO currencyPair, BigDecimal amount, BigDecimal limitPrice)
Creates a buy limit order.- Parameters:
currencyPair
- currency pairamount
- amountlimitPrice
- the highest acceptable price- Returns:
- order result (order id or error)
-
createSellLimitOrder
public OrderCreationResultDTO createSellLimitOrder(CurrencyPairDTO currencyPair, BigDecimal amount, BigDecimal limitPrice)
Creates a sell limit order.- Parameters:
currencyPair
- currency pairamount
- amountlimitPrice
- the lowest acceptable price- Returns:
- order result (order id or error)
-
createLongPosition
public PositionCreationResultDTO createLongPosition(CurrencyPairDTO currencyPair, BigDecimal amount, PositionRulesDTO rules)
Creates a long position with its associated rules. Long position is nothing but buying share. If you are bullish (means you think that price of X share will rise) at that time you buy some amount of Share is called taking Long Position in share.- Parameters:
currencyPair
- currency pairamount
- amountrules
- rules- Returns:
- position creation result
-
createShortPosition
public PositionCreationResultDTO createShortPosition(CurrencyPairDTO currencyPair, BigDecimal amount, PositionRulesDTO rules)
Creates a short position with its associated rules. Short position is nothing but selling share. If you are bearish (means you think that price of xyz share are going to fall) at that time you sell some amount of share is called taking Short Position in share.- Parameters:
currencyPair
- currency pairamount
- amountrules
- rules- Returns:
- position creation result
-
updatePositionRules
public void updatePositionRules(long id, PositionRulesDTO newRules)
Update position rules.- Parameters:
id
- position idnewRules
- new rules
-
closePosition
public void closePosition(long id)
Close position (no matter the rules). The closing will happen when the next ticker arrives.- Parameters:
id
- position id
-
initialize
public void initialize()
Description copied from interface:CassandreStrategyInterface
This method is called by Cassandre before flux are started. For example, you can implement this method to prepare your historical data.- Specified by:
initialize
in interfaceCassandreStrategyInterface
-
onAccountsUpdates
public void onAccountsUpdates(Map<String,AccountDTO> accounts)
Description copied from interface:CassandreStrategyInterface
Method called by Cassandre when there are accounts updates.- Specified by:
onAccountsUpdates
in interfaceCassandreStrategyInterface
- Parameters:
accounts
- accounts updates
-
onTickersUpdates
public void onTickersUpdates(Map<CurrencyPairDTO,TickerDTO> tickers)
Description copied from interface:CassandreStrategyInterface
Method called by Cassandre when there are tickers updates.- Specified by:
onTickersUpdates
in interfaceCassandreStrategyInterface
- Parameters:
tickers
- tickers updates
-
onOrdersUpdates
public void onOrdersUpdates(Map<String,OrderDTO> orders)
Description copied from interface:CassandreStrategyInterface
Method called by Cassandre when there are orders updates.- Specified by:
onOrdersUpdates
in interfaceCassandreStrategyInterface
- Parameters:
orders
- orders updates
-
onTradesUpdates
public void onTradesUpdates(Map<String,TradeDTO> trades)
Description copied from interface:CassandreStrategyInterface
Method called by Cassandre when there are trades updates.- Specified by:
onTradesUpdates
in interfaceCassandreStrategyInterface
- Parameters:
trades
- trades updates
-
onPositionsUpdates
public void onPositionsUpdates(Map<Long,PositionDTO> positions)
Description copied from interface:CassandreStrategyInterface
Method called by Cassandre when there are positions updates.- Specified by:
onPositionsUpdates
in interfaceCassandreStrategyInterface
- Parameters:
positions
- positions updates
-
onPositionsStatusUpdates
public void onPositionsStatusUpdates(Map<Long,PositionDTO> positions)
Description copied from interface:CassandreStrategyInterface
Method called by Cassandre when there are positions status updates.- Specified by:
onPositionsStatusUpdates
in interfaceCassandreStrategyInterface
- Parameters:
positions
- positions status updates
-
getEstimatedBuyableAmount
public final Optional<BigDecimal> getEstimatedBuyableAmount(CurrencyAmountDTO amountToUse, CurrencyDTO currencyWanted)
Returns the amount of a currency I can buy with a certain amount of another currency.- Parameters:
amountToUse
- amount you want to use buy the currency you wantcurrencyWanted
- the currency you want to buy- Returns:
- amount of currencyWanted you can buy with amountToUse
-
getEstimatedBuyingCost
public final Optional<CurrencyAmountDTO> getEstimatedBuyingCost(CurrencyPairDTO currencyPair, BigDecimal amount)
Returns the cost of buying an amount of a currency pair.- Parameters:
currencyPair
- currency pairamount
- amount- Returns:
- cost
-
canBuy
public final boolean canBuy(CurrencyPairDTO currencyPair, BigDecimal amount)
Returns true if we have enough assets to buy.- Parameters:
currencyPair
- currency pairamount
- amount- Returns:
- true if we have enough assets to buy
-
canBuy
public final boolean canBuy(CurrencyPairDTO currencyPair, BigDecimal amount, BigDecimal minimumBalanceAfter)
Returns true if we have enough assets to buy.- Parameters:
currencyPair
- currency pairamount
- amountminimumBalanceAfter
- minimum balance that should be left after buying- Returns:
- true if we have enough assets to buy
-
canBuy
public final boolean canBuy(AccountDTO account, CurrencyPairDTO currencyPair, BigDecimal amount)
Returns true if we have enough assets to buy.- Parameters:
account
- accountcurrencyPair
- currency pairamount
- amount- Returns:
- true if we have enough assets to buy
-
canBuy
public final boolean canBuy(AccountDTO account, CurrencyPairDTO currencyPair, BigDecimal amount, BigDecimal minimumBalanceAfter)
Returns true if we have enough assets to buy and if minimumBalanceAfter is left on the account after.- Parameters:
account
- accountcurrencyPair
- currency pairamount
- amountminimumBalanceAfter
- minimum balance that should be left after buying- Returns:
- true if we have enough assets to buy
-
canSell
public final boolean canSell(CurrencyDTO currency, BigDecimal amount)
Returns true if we have enough assets to sell.- Parameters:
currency
- currencyamount
- amount- Returns:
- true if we have enough assets to sell
-
canSell
public final boolean canSell(CurrencyDTO currency, BigDecimal amount, BigDecimal minimumBalanceAfter)
Returns true if we have enough assets to sell.- Parameters:
currency
- currencyamount
- amountminimumBalanceAfter
- minimum balance that should be left after selling- Returns:
- true if we have enough assets to sell
-
canSell
public final boolean canSell(AccountDTO account, CurrencyDTO currency, BigDecimal amount)
Returns true if we have enough assets to sell.- Parameters:
account
- accountcurrency
- currency pairamount
- amount- Returns:
- true if we have enough assets to sell
-
canSell
public final boolean canSell(AccountDTO account, CurrencyDTO currency, BigDecimal amount, BigDecimal minimumBalanceAfter)
Returns true if we have enough assets to sell and if minimumBalanceAfter is left on the account after.- Parameters:
account
- accountcurrency
- currencyamount
- amountminimumBalanceAfter
- minimum balance that should be left after selling- Returns:
- true if we have enough assets to sell
-
-