0

I am writing a bot for trading. I need to get the price difference in time intervals. Let's say at 00:00:00:0000 GMT the price of a pair is 100.0000, and at 00:00:00:0005 GMT the price is 101.0000, I want to get this difference.

I am using Spring boot scheduler, when I run the app it always returns that the price isn't much. I want it to continually check nutil the price difference gets to the price.

This is to check every 5 minutes

@Scheduled(fixedRate = (10*60*1000))
private double getInitialPrice(){
    try {
        initialPrice = Double.parseDouble(String.valueOf(api.pricesMap().get("BTCUSDT")));
    } catch (BinanceApiException e) {
        e.printStackTrace();
    }
    return initialPrice;
}

This is to check every 5 milliseconds

@Scheduled(fixedRate = (5))
private double getInstancePrice(){
    try {
        instancePrice = Double.parseDouble(String.valueOf(api.pricesMap().get("BTCUSDT")));
    } catch (BinanceApiException e) {
        e.printStackTrace();
    }
    return instancePrice;
}

I'm now checking with the hope that the getInstancePrice() method will always check until it's true. It failed the test as it always returns that the price is not higher, whereas when manually checked it's higher. How do I make it continually check until it gives the time difference?

if( getInstancePrice() > getInitialPrice() ){
   //do this
}
D. Lawrence
  • 943
  • 1
  • 10
  • 23
ken4ward
  • 2,246
  • 5
  • 49
  • 89
  • When do you run the check, *that* should be the one which is scheduled, not the other two, they should simply be a cache of some sort. – luk2302 Dec 05 '19 at 09:09
  • I'm liking your point. Could you please elaborate on this. Presently I declared them as ```private static double initialPrice = 0; private static double instancePrice = 0;``` What do you think? – ken4ward Dec 05 '19 at 09:28

1 Answers1

1

I think you don't need to have the attributes as static, because that means they have the same value for all the class instances.

Another point is returning a value from a method that use the @Scheduled annotation.

In your code:

if( getInstancePrice() > getInitialPrice() ){
   //do this
}

You're just executing the api call and update your values, that will be pretty much the same if they get the same time provided from the API.

What you should do is just set up your initialPrice at the beginning, then compare it to the new value got from the api:

private Double initialPrice = null;
private double instancePrice;
...
@Scheduled(fixedRate = (5))
private double getInstancePrice(){
    try {
        instancePrice = Double.parseDouble(String.valueOf(api.pricesMap().get("BTCUSDT")));
        if (initialPrice == null) {
            initialPrice = instancePrice;
        } else if (instancePrice > initialPrice) {
            // do this
        }
    } catch (BinanceApiException e) {
        e.printStackTrace();
    }
    return instancePrice;
}

You need initialPrice to be an object of Double to be able to set it to null and initialize it. If you are using Java 8 or greater thant you should use an Optional instead.

Mario Santini
  • 2,905
  • 2
  • 20
  • 27