【发布时间】:2022-08-17 18:34:35
【问题描述】:
为了更好地理解继承/接口/抽象/oop等在现实世界中的使用。我有点尝试逆向工程在github上找到的项目中到底发生了什么。
github 项目是一个用于算法交易的多模块 maven 项目。
很长一段时间后,我仍然不明白作者是如何从交易者币安模块中获取数据并将其拉入交易者核心模块的。
- 外部资源(无论是 Git 存储库还是其他任何东西)的链接只有在它们是您问题的补充时才可以使用 在这种情况下,我认为删除 github 项目的链接应该没问题:https://github.com/uniVocity/univocity-trader
父模块:交易者
子模块 1:trader-binance——用于从经纪人那里获取数据等
子模块 2:trader-core——与处理接收到的数据相关的一切
trader-core 模块有一个名为 Exchange 的接口。 其中包含实现交换(当然)所需的所有方法,因此您可以下订单和提取数据等。
对于这个例子,我把它缩小到下面的界面)
public interface Exchange<T, C extends AccountConfiguration<C>> {
/**
* Starts a thread that periodically sends a keep-alive message to the underlying connection.
*/
default void startKeepAlive() { }
/**
* Connects to the live exchange stream to receive real time signals which will be delegated to a given {@link TickConsumer}.
*
* On top of the live stream, the {@link LiveTrader} will continuously check for updates on the signals of the symbols subscribed to with this method.
* If the {@link LiveTrader} does not receive price updates within the given {@code tickInterval}, symbol prices will be polled using
* {@link #getLatestTick(String, TimeInterval)}.
*
* @param symbols a comma separated list of symbols to subscribe to.
* @param tickInterval the frequency of the signals to be received such as every 1 minute, 1 hour, 5 seconds, etc (whichever is supported by the exchange)
* @param consumer a consumer of {@code Exchange}-specific candle/tick details whose data need to be converted into a {@link Candle} and then submitted
* for further processing (i.e. {@link Strategy} analysis, {@link Signal} generation and potential trading by {@link Client})
*/
void openLiveStream(String symbols, TimeInterval tickInterval, TickConsumer<T> consumer);
/**
* Disconnects from the live exchange stream opened with {@link #openLiveStream(String, TimeInterval, TickConsumer)}
*
* @throws Exception in case any error occurs closing the stream.
*/
void closeLiveStream() throws Exception;
}
trader-binance 模块有一个名为 BinanceExchange 的类,它实现了这个接口。 (我再次精简了班级以适应这个例子)
class BinanceExchange implements Exchange<Candlestick, Account> {
@Override
public void startKeepAlive(){
new KeepAliveUserDataStream(restClient()).start();
}
@Override
public void openLiveStream(String symbols, TimeInterval tickInterval, TickConsumer<Candlestick> consumer) {
CandlestickInterval interval = CandlestickInterval.fromTimeInterval(tickInterval);
log.info(\"Opening Binance {} live stream for: {}\", tickInterval, symbols);
socketClientCloseable = socketClient().onCandlestickEvent(symbols, interval, new BinanceApiCallback<>() {
@Override
public void onResponse(CandlestickEvent response) {
try {
priceReceived(response.getSymbol(), Double.parseDouble(response.getClose()));
} catch (Exception e){
log.warn(\"Error updating latest price of \" + response.getSymbol(), e);
}
consumer.tickReceived(response.getSymbol(), response);
}
public void onFailure(Throwable cause) {
consumer.streamError(cause);
}
public void onClose() {
consumer.streamClosed();
}
});
}
@Override
public void closeLiveStream() {
if (socketClientCloseable != null) {
socketClientCloseable.sendCloseFrame();
socketClientCloseable = null;
}
}
}
public interface TickConsumer<T> {
void tickReceived(String symbol, T tick);
void streamError(Throwable cause);
void streamClosed();
}
现在我的问题是,这究竟是如何允许将数据从交易者币安模块提取到交易者核心模块中的? 因为交易者核心不依赖交易者币安模块?这里到底发生了什么/我错过了什么。
我知道这是一个有点抽象的问题,如果有人能向我解释这一点,我将不胜感激。
标签: java maven oop inheritance dependencies