Ho to change update interval via a parameter?

Custom indicators, trading strategies, data export and recording and more...
dmavro
Posts: 52
Joined: Thu Sep 24, 2020 9:20 pm
Been thanked: 1 time

Ho to change update interval via a parameter?

Post by dmavro » Fri Mar 05, 2021 3:17 am

Currently, im using code like below to set my how frequently i read my Csv file and addpoints. How would i go about adding a parameter so i can set a different update Interval in seconds instead of having the update interval coded into the project via getInterval()? Any help would be appreciated.

Thanks,
Dean 
 

Code: Select all

 
  
 @Override
 public long getInterval() {
     return Intervals.INTERVAL_30_SECONDS;// add parameter so u can set time updates
 }
 
 @Override
 public void onInterval() {
  //This runs on getInterval time interval thats set above
  Log.info("onINterval " + symStr + " " +  ts()); 
  readCsv(filePath) ;   

  ATRH.addPoint(atrhilevel/pipV);   
  ATRL.addPoint(atrlolevel/pipV);  
 
 }  

Andry API support
Posts: 548
Joined: Mon Jul 09, 2018 11:18 am
Has thanked: 25 times
Been thanked: 85 times

Re: Ho to change update interval via a parameter?

Post by Andry API support » Fri Mar 05, 2021 9:12 am

Hi. You can not change the initial interval but what you can do is:
- set some min interval in the getInterval method
- set some custom interval which is a multiple of min interval
- count min intervals
- trigger your method when min_intervals_count is a multiple of (custom_interval/min_interval ratio)
Example:
min_interval = 2
custom_interval = 6 (is a multiple of 2? Yes)
ratio = custom_interval/min_interval = 6/2 = 3
interval_count:
1 - is 1 a multiple of ratio? No
2 - is 2 a multiple of ratio? No
3 - is 3 a multiple of ratio? Yes -> trigger your method
4 - is 4 a multiple of ratio? No
5 - is 5 a multiple of ratio? No
6 - is 6 a multiple of ratio? Yes -> trigger your method
...

Another example (with @Parameter):

Code: Select all

package velox.api.layer1.simplified.demo;

import java.awt.Color;

import velox.api.layer1.annotations.Layer1ApiVersion;
import velox.api.layer1.annotations.Layer1ApiVersionValue;
import velox.api.layer1.annotations.Layer1SimpleAttachable;
import velox.api.layer1.annotations.Layer1StrategyName;
import velox.api.layer1.common.Log;
import velox.api.layer1.data.InstrumentInfo;
import velox.api.layer1.data.TradeInfo;
import velox.api.layer1.messages.indicators.Layer1ApiUserMessageModifyIndicator.GraphType;
import velox.api.layer1.simplified.Api;
import velox.api.layer1.simplified.CustomModule;
import velox.api.layer1.simplified.Indicator;
import velox.api.layer1.simplified.InitialState;
import velox.api.layer1.simplified.IntervalListener;
import velox.api.layer1.simplified.Intervals;
import velox.api.layer1.simplified.Parameter;
import velox.api.layer1.simplified.TradeDataListener;

@Layer1SimpleAttachable
@Layer1StrategyName("Last trade: Custom intervals")
@Layer1ApiVersion(Layer1ApiVersionValue.VERSION2)
public class LastTradeDemoIntervals implements CustomModule, TradeDataListener, IntervalListener
{
    protected Indicator lastTradeIndicator;
    private double lastTradePrice;
    
    final long minimalInterval= Intervals.INTERVAL_50_MILLISECONDS;
    
    @Parameter(name = "customIntervalMillis")
    private Integer customIntervalMillis = 100;
    
    private int minimalIntervalCount;

    @Override
    public void initialize(String alias, InstrumentInfo info, Api api, InitialState initialState) {
        lastTradeIndicator = api.registerIndicator("Last trade, no history",
                GraphType.PRIMARY);
        lastTradeIndicator.setColor(Color.GREEN);
    }
    
    @Override
    public void stop() {
    }

    @Override
    public void onTrade(double price, int size, TradeInfo tradeInfo) {
        lastTradePrice = price;
    }
    
    @Override
    public void onInterval() {
        long customInterval = (long) customIntervalMillis * 1_000_000L;//millis to nanos
        
        if (customInterval != 0 //customInterval is valid
                && customInterval % minimalInterval == 0 // customInterval is a multiple of minimalInterval 
                && minimalIntervalCount % (customInterval/minimalInterval) == 0 // minimalIntervalCount is a multiple of custom/minimal ratio
                ) {
            lastTradeIndicator.addPoint(lastTradePrice);
            Log.info("addPoint");//take a look into the log how often this one is triggered
        }
        minimalIntervalCount++;
    }
    
    @Override
    public long getInterval() {
        return minimalInterval;
    }
}


dmavro
Posts: 52
Joined: Thu Sep 24, 2020 9:20 pm
Been thanked: 1 time

Re: Ho to change update interval via a parameter?

Post by dmavro » Thu Mar 11, 2021 5:46 pm

Andrey,

I will try to add the code when i get a chance over weekend.

Thanks,
Dean

Post Reply