For daily and weekly pivot levels consider the following example strategy:
package jforex.strategies.indicators;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import com.dukascopy.api.*;
/**
* The startegy prints out daily and weekly pivot levels
*
*/
public class Pivot implements IStrategy {
private IConsole console;
private IIndicators indicators;
private SimpleDateFormat sdf;
private Instrument instrument = Instrument.EURUSD;
private Period selectedPeriod = Period.DAILY;
private static String[] pivotLevelNames = { "P", "R1", "S1", "R2", "S2", "R3", "S3" };
public void onStart(IContext context) throws JFException {
this.console = context.getConsole();
this.indicators = context.getIndicators();
String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
print("start");
}
public void onAccount(IAccount account) throws JFException {}
public void onMessage(IMessage message) throws JFException {}
public void onStop() throws JFException {}
public void onTick(Instrument instrument, ITick tick) throws JFException {}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
if (!instrument.equals(this.instrument) || !period.equals(selectedPeriod))
return;
boolean showHistoricalLevels = true;
int shift = 0;
double [] pivotDaily = indicators.pivot(instrument, selectedPeriod, OfferSide.BID, 7, showHistoricalLevels, shift);
double [] pivotWeekly = indicators.pivot(instrument, selectedPeriod, OfferSide.BID, 8, showHistoricalLevels, shift);
print(sdf.format(bidBar.getTime())
+ " ## "+ " pivotWeekly " +arrayToString(pivotWeekly)
+ " ## "+ " pivotDaily " +arrayToString(pivotDaily)
);
}
private void print(Object message) {
console.getOut().println(message);
}
public static String arrayToString(double [] arr){
String str = "";
for (int r=0; r<arr.length; r++) {
str += " " + pivotLevelNames[r] + ": "+ (new DecimalFormat("#.#######")).format(arr[r]) + "; ";
}
return str;
}
}
Currently JForex does not have an indicator for daily median pivot levels, however you might obtain such indicator by a slight PivotIndicator (find it in the API's library com.dukascopy.indicators) modification:
by changing
double p = (previousBar.getClose() + previousBar.getHigh() + previousBar.getLow())/3;
to
double p = ( previousBar.getHigh() + previousBar.getLow())/2;
at line 151.