In JForex4, you can create a custom indicator in java by creating a class that implements the IIndicator interface, and then implementing these two methods
public void onStart(IIndicatorContext context)
public IndicatorResult calculate(int startIndex, int endIndex)
In onStart, you can specify how many outputs you wish the indicator to have, as well set a shift value. This can be useful when you want to have an indicator predict future values. Is it possible to define an optional parameter that determines the shift amount?
Assistant
User
When making my own indicator, is there a way to specify the min and max for the chart? So for example, if the min is -20 and max is 130, only values within this range would be seen when plotted.
Assistant
In JForex4, you can create a custom indicator that implements the IIndicator interface and allows for the specification of parameters, including a shift amount. To achieve this, you can define an optional parameter in your indicator class, typically by using a constructor or defining it as a class variable.
Here's a basic outline of how you might implement this:
Define a Class Variable for Shift Amount: Create a variable in your class to hold the shift value.
Create a Constructor or Method for Setting Parameters: In your constructor or an initialization method, define how to handle the shift parameter.
Use the Shift Parameter in calculate Method: Apply the shift value when you are calculating the result in the calculate method.
Here is a simplified example of how you might implement this:
java
import com.dukascopy.api.indicators.IIndicator;
import com.dukascopy.api.indicators.IIndicatorContext;
import com.dukascopy.api.indicators.IndicatorResult;
public class CustomIndicator implements IIndicator {
private int shiftAmount;
// Constructor to initialize with a default shift amount
public CustomIndicator() {
this.shiftAmount = 0; // Default shift value
}
// Optional method to set the shift amount if desired
public void setShiftAmount(int shift) {
this.shiftAmount = shift;
}
@Override
public void onStart(IIndicatorContext context) {
// You can specify outputs and shifts here as required
context.getIndicators().registerOutput("output1", OutputType.LINE);
context.getIndicators().registerOutput("output2", OutputType.LINE);
}
@Override
public IndicatorResult calculate(int startIndex, int endIndex) {
// Example calculation (supplied values would be from your specific logic)
IndicatorResult result = new IndicatorResult();
// Perform your calculations here
for (int i = startIndex; i <= endIndex; i++) {
// Use the shift amount in your calculations
double value = //... calculate value based on historic data;
result.setValue(i + shiftAmount, value); // Shift the output as needed
}
return result;
}
}
Explanation:
Shift Amount: The shiftAmount variable holds your shift value, which can be set using the setShiftAmount(int shift) method.
onStart: During the onStart method execution, the outputs are defined.
calculate: In the calculate method, the shiftAmount is applied while calculating results, allowing for future value predictions.
Usage:
You can create an instance of CustomIndicator, and call setShiftAmount(int shift) before starting your calculations. The shift amount can be updated based on different user preferences or scenarios before invoking the calculations.
This approach provides flexibility in defining how many outputs you want and how they are shifted while maintaining the ability to customize parameters dynamically.
In the JForex platform, when you are creating a custom indicator and want to specify a minimum and maximum range for the values displayed on the chart, you typically do this by setting the specific bounds within your indicator class. However, the JForex platform doesn't provide a direct method to enforce min/max limits on the chart itself. Instead, you can manage this in your calculation and output logic.
Here’s a general approach you can take:
Modify the Calculation Method: When calculating your indicator values, you can clamp the values to your specified minimum and maximum limits before you set them for output.
Output Handling: When adding values to the output, ensure that you only add values that fall within the specified range.
Here’s a simple example to illustrate how you might implement this:
java
import com.dukascopy.api.indicators.IIndicator;
import com.dukascopy.api.indicators.IIndicatorContext;
import com.dukascopy.api.indicators.IndicatorResult;
import com.dukascopy.api.indicators.OutputType;
public class CustomIndicator implements IIndicator {
private static final double MIN_VALUE = -20.0;
private static final double MAX_VALUE = 130.0;
@Override
public void onStart(IIndicatorContext context) {
// Register the output for the indicator
context.getIndicators().registerOutput("output", OutputType.LINE);
}
@Override
public IndicatorResult calculate(int startIndex, int endIndex) {
IndicatorResult result = new IndicatorResult();
for (int i = startIndex; i <= endIndex; i++) {
// Example calculation (you would replace this with your actual logic)
double calculatedValue = someCalculatingMethod(i);
// Clamp the values to the specified range
if (calculatedValue < MIN_VALUE) {
calculatedValue = MIN_VALUE;
} else if (calculatedValue > MAX_VALUE) {
calculatedValue = MAX_VALUE;
}
// Set the clamped value to the output
result.setValue(i, calculatedValue);
}
return result;
}
private double someCalculatingMethod(int index) {
// Example logic that generates a value
// Replace this with your calculation logic
return Math.random() * 150 - 30; // Values could range from -30 to 120
}
}
Explanation:
MIN_VALUE and MAX_VALUE: These constants define the minimum and maximum values allowed for your indicator.
Value Clamping: Inside the calculate method, after computing the calculatedValue, we check if it falls outside the defined limits and clamp it accordingly.
Setting Output Values: The clamped value is then set in the IndicatorResult, ensuring that only the restricted values are portrayed on the chart.
Chart Behavior:
While this method effectively restricts the indicator values computed and displayed, keep in mind that the scaling of the y-axis on the chart may or may not adjust automatically to these values depending on how the trading platform handles the rendering of indicators. Typically, you would still see the y-axis adjusting based on the values you provide, so if you want the chart to adhere strictly to your defined range visually, additional configuration might be required in the platform settings, although options are somewhat limited.
Alternative:
If you need visual boundaries strictly enforced, consider custom alerting or user interaction approaches that will guide users to understand that the effective range for interpretation should be bounded by your set values.