Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

BBANDS - average indicator?
 Post subject: BBANDS - average indicator? Post rating: 0   New post Posted: Wed 18 May, 2011, 10:14 

User rating: -
hello!
can anybody tell me if there is an indicator or some other way to get the average difference of the bollinger band's lower to upper line in a certain period of time?
would be really great!

thanks!


 
 Post subject: Re: BBANDS - average indicator? Post rating: 0   New post Posted: Tue 24 May, 2011, 08:13 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Consider the following strategy which prints the average difference between bollinger's lower and upper bounds over the last 5 bars.
package jforex.strategies.indicators;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IIndicators.MaType;

public class BBands implements IStrategy {
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   private IIndicators indicators;
   private SimpleDateFormat sdf;

   @Configurable("Instrument")
   public Instrument instrument = Instrument.EURUSD;
   @Configurable("Period")
   public Period selectedPeriod = Period.ONE_MIN;
   @Configurable("BBands filter")
   public Filter indicatorFilter = Filter.NO_FILTER;
   @Configurable("Amount")
   public double amount = 0.02;

   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.console = context.getConsole();
      this.history = context.getHistory();
      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;

      int timePeriod = 5;
      MaType maType = MaType.EMA;
      double nbDevDn = 2;
      double nbDevUp = 2;
      
      IBar firstBar = history.getBar(instrument, selectedPeriod, OfferSide.BID, 5);
      IBar lastBar = history.getBar(instrument, selectedPeriod, OfferSide.BID, 1);
      
      double [][] bbands = indicators.bbands(instrument, period, OfferSide.BID,
            AppliedPrice.CLOSE,timePeriod, nbDevUp, nbDevDn, maType, firstBar.getTime(), lastBar.getTime());
      
      double bbandsAvg = getAverageOverPeriod(bbands,timePeriod);
      
      //period, average bband and the whole array
      print(sdf.format(firstBar.getTime()) + " - " + sdf.format(lastBar.getTime()) + " avg: "
            + (new DecimalFormat("#.000000")).format(bbandsAvg) + " (Ups; Mids; Lows - " +arrayToString(bbands) + ")");

   }

   private double getAverageOverPeriod(double [][] arr, int period){

      //sum averages of each time point
      double avgSum = 0;
       for (int c=0; c<arr[0].length; c++) {
          //0=high, 2=low
          avgSum += (arr[0][c] - arr[2][c]) / 2;
          //to calculate median values use:
          //avgSum += (arr[0][c] - arr[2][c]) + arr[2][c];
       }
      
       return avgSum/period;
   }
   
   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++) {
          for (int c=0; c<arr[r].length; c++) {
             str += " " + (new DecimalFormat("0.000000")).format(arr[r][c]);
          }
          str += "; ";
      }
      return str;
   }

}


 
 Post subject: Re: BBANDS - average indicator? Post rating: 0   New post Posted: Wed 25 May, 2011, 21:40 

User rating: -
hello!
sounds great, but I receive the following error when I try to compile this stradegy:

Image


 
 Post subject: Re: BBANDS - average indicator? Post rating: 0   New post Posted: Thu 26 May, 2011, 15:12 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
If you rename the java file you should as well rename the java class name, that is
public class BBands_average implements IStrategy {


 

Jump to:  

cron
  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com