Reputation: 113
I made an alert function which gets price data from websocket and send alert if condition is fulfilled. I only have "price condition" now, and I want to add "percentage Condition". So What I'm trying to do is
To do so, I need to compute limitation price when opening the websocket. But I can't figure out "where and how" can I store limitation price..
This is my websocket code implementing price condition(not "percentage condition")
WebSocketClientEndPoint
@ClientEndpoint
public class WebsocketClientEndpoint {
Session userSession = null;
private MessageHandler messageHandler;
public WebsocketClientEndpoint() {
}
public Session connect(URI endpointURI) {
try {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
userSession = container.connectToServer(this, endpointURI);
return userSession;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Callback hook for Connection open events.
*
* @param userSession the userSession which is opened.
*/
@OnOpen
public void onOpen(Session userSession) {
System.out.println("opening websocket");
this.userSession = userSession;
}
/**
* Callback hook for Connection close events.
*
* @param userSession the userSession which is getting closed.
* @param reason the reason for connection close
*/
@OnClose
public void onClose(Session userSession, CloseReason reason) {
System.out.println("closing websocket");
this.userSession = null;
}
/**
* Callback hook for Message Events. This method will be invoked when a client send a message.
*
* @param message The text message
*/
@OnMessage
public void onMessage(String message) throws ParseException, IOException {
if (this.messageHandler != null) {
this.messageHandler.handleMessage(message);
}
}
@OnMessage
public void onMessage(ByteBuffer bytes) {
System.out.println("Handle byte buffer");
}
/**
* register message handler
*
* @param msgHandler
*/
public void addMessageHandler(MessageHandler msgHandler) {
this.messageHandler = msgHandler;
}
/**
* Send a message.
*
* @param message
*/
public void sendMessage(String message) {
this.userSession.getAsyncRemote().sendText(message);
}
/**
* Message handler.
*
* @author Jiji_Sasidharan
*/
public static interface MessageHandler {
public void handleMessage(String message) throws ParseException, IOException;
}
}
AlertUserByPrice
public void AlertUserByPrice(Long id) {
Alert alert = alertRepository.findById(id).orElseThrow(() -> new NoSuchElementException());
String type = alert.getAlertType().getKey();
double SetPrice = alert.getPrice();
String ticker = alert.getTicker();
JSONParser jsonParser = new JSONParser();
final NotificationRequest build;
if (type == "l_break") {
build = NotificationRequest.builder()
.title(ticker + " alert")
.message(SetPrice + "broke down")
.token(notificationService.getToken(userDetailService.returnUser().getEmail()))
.build();
}
else { // upper_break
build = NotificationRequest.builder()
.title(ticker + " alert")
.message(SetPrice + "pierced upward")
.token(notificationService.getToken(userDetailService.returnUser().getEmail()))
.build();
}
try {
final WebsocketClientEndpoint clientEndPoint = new WebsocketClientEndpoint();
Session session = clientEndPoint.connect(new URI("wss://ws.coincap.io/prices?assets=" + ticker));
WebsocketClientEndpoint.MessageHandler handler = new WebsocketClientEndpoint.MessageHandler() {
public void handleMessage(String message) throws ParseException, IOException {
Object obj = jsonParser.parse(message);
JSONObject jsonObject = (JSONObject) obj;
double price = Double.parseDouble(jsonObject.get(ticker).toString());
System.out.println("가격 : " + price);
if (type == "l_break") {
if (price < SetPrice) {
System.out.println("끝");
notificationService.sendNotification(build);
session.close();
}
} else {
if (price > SetPrice) {
System.out.println("끝");
notificationService.sendNotification(build);
session.close();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.err.println("InterruptedException exception: " + ex.getMessage());
}
}
};
clientEndPoint.addMessageHandler(handler);
} catch (URISyntaxException ex) {
System.err.println("URISyntaxException exception: " + ex.getMessage());
}
}
what should I do to implement "alert by perentage condition"?? Someone please help.. Thanks in advance
Upvotes: 0
Views: 295
Reputation: 36
double percent = 0.05;
if (price-(price*percent) < SetPrice) {
System.out.println("끝");
notificationService.sendNotification(build);
session.close();
}
if (price-(price*percent) > SetPrice) {
System.out.println("끝");
notificationService.sendNotification(build);
session.close();
}
Try if this helps
Upvotes: 1