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.

How to load Bars with Period/OfferSide selected by user
 Post subject: How to load Bars with Period/OfferSide selected by user Post rating: 0   New post Posted: Tue 02 Aug, 2011, 10:09 
User avatar

User rating: 3
Joined: Wed 18 May, 2011, 16:25
Posts: 331
Location: SwitzerlandSwitzerland
Is it possible to let the user select the OfferSide and Period of an input barfeed independantly of the currently selected timeframe in the chart?

Quote:
InputParameterInfo inputBars = new InputParameterInfo("Bars", InputParameterInfo.Type.BAR);
inputBars.setOfferSide(<OfferSide selected by user to be included here>);
inputBars.setPeriod(<Period selected by user to be included here>);


Best, RR.


 
 Post subject: Re: How to load Bars with Period/OfferSide selected by user Post rating: 0   New post Posted: Tue 02 Aug, 2011, 15:49 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Consider the following approach:
package jforex.indicators;

import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.indicators.*;

public class MultiBarsFromParams implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private IBar[][] inputs = new IBar[1][];   
    private double[][] outputs = new double[1][];
    private InputParameterInfo barInput;
       
    private IConsole console;   
    private Instrument instrument = Instrument.EURUSD;
   
    public void onStart(IIndicatorContext context) {       
       
       this.console = context.getConsole();
       
       //load Period values and names
        int[] periodValues = new int[Period.values().length];
        String[] periodNames = new String[Period.values().length];
        for (int i = 0; i < periodValues.length; i++) {
            periodValues[i] = i;
            periodNames[i] = Period.values()[i].name();
        }
       
       //load OfferSide values and names
        int[] offerSideValues = new int[OfferSide.values().length];
        String[] offerSideNames = new String[OfferSide.values().length];
        for (int i = 0; i < offerSideValues.length; i++) {
           offerSideValues[i] = i;
           offerSideNames[i] = OfferSide.values()[i].name();
        }
       
        indicatorInfo = new IndicatorInfo("OPTPARAMS", "Period and Offer side from opt input params", "My indicators", false, false, false, 1, 2, 1);

        optInputParameterInfos = new OptInputParameterInfo[] {
              //period as optional input
              new OptInputParameterInfo("Period", OptInputParameterInfo.Type.OTHER,
                    new IntegerListDescription(Period.ONE_MIN.ordinal(), periodValues, periodNames)),
              //offer side as optional input
              new OptInputParameterInfo("Offer Side", OptInputParameterInfo.Type.OTHER,
                    new IntegerListDescription(OfferSide.BID.ordinal(), offerSideValues, offerSideNames))
        };
       
        barInput = new InputParameterInfo("bar from input", InputParameterInfo.Type.BAR){{    setInstrument(instrument); }};      
        inputParameterInfos = new InputParameterInfo[] { barInput };

        outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE)};
       
    }
   
    //always returns 1
    public IndicatorResult calculate(int startIndex, int endIndex) {
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }       
        int i, j;
       
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
            outputs[0][j] = 1.0;
        }
        return new IndicatorResult(startIndex, j);
       
    }

    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

    public InputParameterInfo getInputParameterInfo(int index) {
        if (index <= inputParameterInfos.length) {
            return inputParameterInfos[index];
        }
        return null;
    }

    public int getLookback() {
        return 0;
    }

    public int getLookforward() {
        return 0;
    }

    public OptInputParameterInfo getOptInputParameterInfo(int index) {
        if (index <= optInputParameterInfos.length) {
            return optInputParameterInfos[index];
        }
        return null;
    }

    public OutputParameterInfo getOutputParameterInfo(int index) {
        if (index <= outputParameterInfos.length) {
            return outputParameterInfos[index];
        }
        return null;
    }

    public void setInputParameter(int index, Object array) {
       //log the received data of the currently forming bar
       print(index + ". input: " + arrayToString((IBar[]) array));
        inputs[index] = (IBar[]) array;
    }

   //this method reacts on every optional input parameter change
    public void setOptInputParameter(int index, Object value) {
       int enumIndex = ((Integer) value).intValue();
       if (index == 0) {
          barInput.setPeriod(Period.values()[enumIndex]);
       }
       if (index == 1) {
          barInput.setOfferSide(OfferSide.values()[enumIndex]);
       }
    }

    public void setOutputParameter(int index, Object array) {
        outputs[index] = (double[]) array;
    }
       
 
    public static String arrayToString(Object [] arr){
        String str = "";
        for (int r=0; r<arr.length; r++) {
            str += "[" + r + "] "+ arr[r] + "; ";
        }
        return str;
     }
   
   
    private void print(Object o){    this.console.getOut().println(o);   }
}


 

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