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 OrderMapper
orderMapper
Order mapper.protected PositionMapper
positionMapper
Position mapper.protected TradeMapper
tradeMapper
Trade mapper.
-
Constructor Summary
Constructors Constructor Description GenericCassandreStrategy()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
accountUpdate(AccountDTO account)
Method called by streams at every account update.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.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 amount locked by currency.Map<Long,CurrencyAmountDTO>
getAmountsLockedByPosition()
Getter amountsLockedByPosition.Optional<CurrencyAmountDTO>
getEstimatedBuyingCost(CurrencyPairDTO currencyPair, BigDecimal amount)
Returns the cost of buying an amount of a currency pair.HashMap<CurrencyDTO,GainDTO>
getGains()
Returns gains of all positions.Optional<TickerDTO>
getLastTickerByCurrencyPair(String currencyPair)
Return the last ticker 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 your trading account.Optional<TradeDTO>
getTradeByTradeId(String tradeId)
Get a trade by its id.Map<String,TradeDTO>
getTrades()
Returns list of trades.void
onAccountUpdate(AccountDTO account)
Method triggered at every account update.void
onOrderUpdate(OrderDTO order)
Method triggered on every order update.void
onPositionStatusUpdate(PositionDTO position)
Method triggered on every position status update.void
onPositionUpdate(PositionDTO position)
Method triggered on every position update.void
onTickerUpdate(TickerDTO ticker)
Method triggered at every ticker update.void
onTradeUpdate(TradeDTO trade)
Method triggered on every trade update.void
orderUpdate(OrderDTO order)
Method called by streams on every order update.void
positionUpdate(PositionDTO position)
Method called by streams on every position update.void
setOrderRepository(OrderRepository newOrderRepository)
Setter order repository.void
setPositionRepository(PositionRepository newPositionRepository)
Setter positionRepository.void
setPositionService(PositionService newPositionService)
Setter for positionService.void
setStrategyDTO(StrategyDTO newStrategyDTO)
Setter strategyDTO.void
setTradeRepository(TradeRepository newTradeRepository)
Setter trade repository.void
setTradeService(TradeService newTradeService)
Setter for tradeService.void
tickerUpdate(TickerDTO ticker)
Method called by streams at every ticker update.void
tradeUpdate(TradeDTO trade)
Method called by streams on every trade update.-
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.
-
-
Method Detail
-
getStrategyDTO
public final StrategyDTO getStrategyDTO()
Getter strategyDTO.- Returns:
- strategyDTO
-
setStrategyDTO
public final void setStrategyDTO(StrategyDTO newStrategyDTO)
Description copied from interface:CassandreStrategyInterface
Setter strategyDTO.- Specified by:
setStrategyDTO
in interfaceCassandreStrategyInterface
- Parameters:
newStrategyDTO
- strategy DTO.
-
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.
-
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
-
accountUpdate
public void accountUpdate(AccountDTO account)
Description copied from interface:CassandreStrategyInterface
Method called by streams at every account update.- Specified by:
accountUpdate
in interfaceCassandreStrategyInterface
- Parameters:
account
- account
-
tickerUpdate
public void tickerUpdate(TickerDTO ticker)
Description copied from interface:CassandreStrategyInterface
Method called by streams at every ticker update.- Specified by:
tickerUpdate
in interfaceCassandreStrategyInterface
- Parameters:
ticker
- ticker
-
orderUpdate
public void orderUpdate(OrderDTO order)
Description copied from interface:CassandreStrategyInterface
Method called by streams on every order update.- Specified by:
orderUpdate
in interfaceCassandreStrategyInterface
- Parameters:
order
- order
-
tradeUpdate
public void tradeUpdate(TradeDTO trade)
Description copied from interface:CassandreStrategyInterface
Method called by streams on every trade update.- Specified by:
tradeUpdate
in interfaceCassandreStrategyInterface
- Parameters:
trade
- trade
-
positionUpdate
public void positionUpdate(PositionDTO position)
Description copied from interface:CassandreStrategyInterface
Method called by streams on every position update.- Specified by:
positionUpdate
in interfaceCassandreStrategyInterface
- Parameters:
position
- trade
-
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 your trading account.- Specified by:
getTradeAccount
in interfaceCassandreStrategyInterface
- Returns:
- trading account
-
getAmountsLockedByPosition
public final Map<Long,CurrencyAmountDTO> getAmountsLockedByPosition()
Getter amountsLockedByPosition.- Returns:
- amountsLockedByPosition
-
getAmountsLockedByCurrency
public final BigDecimal getAmountsLockedByCurrency(CurrencyDTO currency)
Returns the amount locked by 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(String currencyPair)
Return the last ticker for a currency pair.- Parameters:
currencyPair
- currency pair- Returns:
- last ticker received
-
getLastTickerByCurrencyPair
public final Optional<TickerDTO> getLastTickerByCurrencyPair(CurrencyPairDTO currencyPair)
Return the last ticker for a currency pair.- Parameters:
currencyPair
- currency pair- Returns:
- last ticker received
-
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 HashMap<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
-
onAccountUpdate
public void onAccountUpdate(AccountDTO account)
Description copied from interface:CassandreStrategyInterface
Method triggered at every account update.- Specified by:
onAccountUpdate
in interfaceCassandreStrategyInterface
- Parameters:
account
- account
-
onTickerUpdate
public void onTickerUpdate(TickerDTO ticker)
Description copied from interface:CassandreStrategyInterface
Method triggered at every ticker update.- Specified by:
onTickerUpdate
in interfaceCassandreStrategyInterface
- Parameters:
ticker
- ticker
-
onOrderUpdate
public void onOrderUpdate(OrderDTO order)
Description copied from interface:CassandreStrategyInterface
Method triggered on every order update.- Specified by:
onOrderUpdate
in interfaceCassandreStrategyInterface
- Parameters:
order
- order
-
onTradeUpdate
public void onTradeUpdate(TradeDTO trade)
Description copied from interface:CassandreStrategyInterface
Method triggered on every trade update.- Specified by:
onTradeUpdate
in interfaceCassandreStrategyInterface
- Parameters:
trade
- trade
-
onPositionUpdate
public void onPositionUpdate(PositionDTO position)
Description copied from interface:CassandreStrategyInterface
Method triggered on every position update.- Specified by:
onPositionUpdate
in interfaceCassandreStrategyInterface
- Parameters:
position
- position
-
onPositionStatusUpdate
public void onPositionStatusUpdate(PositionDTO position)
Description copied from interface:CassandreStrategyInterface
Method triggered on every position status update.- Specified by:
onPositionStatusUpdate
in interfaceCassandreStrategyInterface
- Parameters:
position
- position
-
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 there is 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 there is 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 there is 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 there is 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 there is 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 there is 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 there is 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 there is enough assets to sell
-
-