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.

Loop between Specific Dates
 Post subject: Loop between Specific Dates Post rating: 0   New post Posted: Mon 13 Nov, 2017, 02:39 

User rating: 0
Joined: Mon 25 Jan, 2016, 05:59
Posts: 14
Location: China,
Hi

How to do “FOR” loop between specific dates

public IndicatorResult calculate(int startIndex, int endIndex) {

for (i = startIndex, j = 0; i < endIndex + 1; i++, j++) {
...
};
};

---------------------------------------------------------------------------
startIndex= ( "Start date", "2017-11-01");
endIndex= ( "End date", 2017-12-01 );


My problem is how to convert specific date to number.

Thanks,
slowloopattack


 
 Post subject: Re: Loop between Specific Dates Post rating: 0   New post Posted: Fri 29 Dec, 2017, 07:40 

User rating: 0
Joined: Mon 25 Jan, 2016, 05:59
Posts: 14
Location: China,
Hi
This is my goal idea,who can help me.Thanks.

startIndex=on Monday;
endIndex=weekend
public IndicatorResult calculate(int startIndex, int endIndex) {

for (i = startIndex, j = 0; i < endIndex + 1; i++, j++) {
...
};


 
 Post subject: Re: Loop between Specific Dates Post rating: 0   New post Posted: Thu 04 Jan, 2018, 13:02 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Here is an example of how to convert string to timestamp

package jforex;

import com.dukascopy.api.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Strategy implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;

    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();

        console.getOut().println(strToTimestamp("2017-10-10 12:12:12"));

    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") {
        {
            setTimeZone(TimeZone.getTimeZone("GMT"));
        }
    };

    public long strToTimestamp(String timeString) {
        long timestamp = 0;
        try {
            timestamp = sdf.parse(timeString).getTime();
        } catch (ParseException e) {
        }
        return timestamp;
    }


    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 {
    }
}


 
 Post subject: Re: Loop between Specific Dates Post rating: 0   New post Posted: Sat 06 Jan, 2018, 09:22 

User rating: 0
Joined: Mon 25 Jan, 2016, 05:59
Posts: 14
Location: China,
API Support Thank you very much :) .

I continue to learn,The following code has the wrong message.
I don't know where it is wrong here.
Help me,thanks

new OptInputParameterInfo("startdate", OptInputParameterInfo.Type.OTHER, new  StringOptInputDescription("2018-01-01 00:00")),
                new OptInputParameterInfo("enddate", OptInputParameterInfo.Type.OTHER,   new  StringOptInputDescription("2018-01-06 00:00")),


  
 public static int compare_date(String startdate, String enddate) {  //Syntax error on token "(", ; expected
          DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd ");
            Date dt1=null; 
            Date dt2=null;


 
 Post subject: Re: Loop between Specific Dates Post rating: 0   New post Posted: Mon 08 Jan, 2018, 11:15 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
There is a problem with opening/closing brackets. Try to return to file version that is working and gradually add code to find where the issue occurs.


 
 Post subject: Re: Loop between Specific Dates Post rating: 0   New post Posted: Wed 10 Jan, 2018, 11:59 

User rating: 0
Joined: Mon 25 Jan, 2016, 05:59
Posts: 14
Location: China,
Hi API Support thanks
I want to add the value of the calculated data within the specified date in the indicators.


package jforex;
import com.dukascopy.api.indicators.*;

import java.awt.Color;
import java.math.BigDecimal;
import java.lang.String;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import java.text.DateFormat; 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Indicator implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private double[][] inputs = new double[1][];
    private int timePeriod = 2;
    private double[][] outputs = new double[1][];
   
    //public  String  startdate = "2018-01-01 00:00:00";
    //public  String  enddate   = "2018-01-06 00:00:00";
   
    public void onStart(IIndicatorContext context) {
        indicatorInfo = new IndicatorInfo("EXAMPIND", "Sums previous values", "My indicators",
                false, false, false, 1, 1, 1);
                this.console = context.getConsole();
       
        inputParameterInfos = new InputParameterInfo[] { new InputParameterInfo("Price", InputParameterInfo.Type.BAR) };
       
        optInputParameterInfos = new OptInputParameterInfo[] {
                new OptInputParameterInfo("startdate", OptInputParameterInfo.Type.OTHER, new  StringOptInputDescription("2018-01-01 00:00:00")),
                new OptInputParameterInfo("enddate", OptInputParameterInfo.Type.OTHER,   new  StringOptInputDescription("2018-01-06 00:00:00")),
       
       
        inputParameterInfos = new InputParameterInfo[] {
            new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE)
        };
        optInputParameterInfos = new OptInputParameterInfo[] {
            new OptInputParameterInfo("Time period", OptInputParameterInfo.Type.OTHER,
                new IntegerRangeDescription(2, 2, 100, 1))
        };
        outputParameterInfos = new OutputParameterInfo[] {
            new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.DrawingStyle.LINE)
        };
    }
//----------------
 public static boolean compare_dateStr(String startdate, String enddate) {
         boolean isFirstBig = false;
         DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd ");                           
      try {
       Date dt1 = df1.parse(startdate);
       Date dt2 = df1.parse(enddate);
       if (dt1.getTime() > dt2.getTime()) {
         System.out.println("dt1>dt2");
         isFirstBig = true; 
       } else if (dt1.getTime() < dt2.getTime()) {
         System.out.println("dt1<dt2");
         isFirstBig = false; 
       } else {
         isFirstBig = false; 
       }
     } catch (Exception exception) {
       exception.printStackTrace();
     }
    return isFirstBig;
   }
//----------------
    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex = getLookback();
        }
        if (startIndex > endIndex) {
            return new IndicatorResult(0, 0);
        }
       
//---------       
        int i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
             if( isFirstBig = true ) {
     //    I want to calculate the data in the date set here ,Do not write code please help instruct   
           
            double value = 0;
            for (int k = timePeriod; k > 0; k--) {
                value += inputs[0][i - k];
            }
            outputs[0][j] = value;
        }
        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 timePeriod;
    }

    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) {
        inputs[index] = (double[]) array;
    }

    public void setOptInputParameter(int index, Object value) {
        timePeriod = (Integer) value;
    }

    public void setOutputParameter(int index, Object array) {
        outputs[index] = (double[]) array;
    }
}


 
 Post subject: Re: Loop between Specific Dates Post rating: 0   New post Posted: Thu 11 Jan, 2018, 16:02 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Best option is to change indicator input parameters from double to IBar. More about IBar interface you can read in documentation: https://www.dukascopy.com/client/javadoc3/com/dukascopy/api/IBar.html
Method compare_dateStr compares two optional input parameters, what makes not sense in this case. It's better to use some parser which takes String as input parameter and returns Date.
Then to make sure what current bar is in the specified period/ Here is a example which can help get and compare bars by time: https://www.dukascopy.com/wiki/en/development/indicator-api/examples/indicator-with-inputs-of-different-periods
I slightly corrected your indicator. There were small issues with brackets

import com.dukascopy.api.indicators.*;

import java.lang.String;
import java.text.SimpleDateFormat;

import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.*;

public class Indicator implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private IBar[][] inputs = new IBar[1][];
    private int timePeriod = 2;
    private double[][] outputs = new double[1][];

    IConsole console;

    public  String  startdate = "2018-01-01 00:00:00";
    public  String  enddate   = "2018-01-06 00:00:00";

    public void onStart(IIndicatorContext context) {
        indicatorInfo = new IndicatorInfo("EXAMPIND", "Sums previous values", "My indicators",
            false, false, false, 1, 1, 1);
        this.console = context.getConsole();

        inputParameterInfos = new InputParameterInfo[] { new InputParameterInfo("Price", InputParameterInfo.Type.BAR) };

        optInputParameterInfos = new OptInputParameterInfo[]{
            new OptInputParameterInfo("startdate", OptInputParameterInfo.Type.OTHER,
                new StringOptInputDescription("2018-01-01 00:00:00")),
            new OptInputParameterInfo("enddate", OptInputParameterInfo.Type.OTHER,
                new StringOptInputDescription("2018-01-06 00:00:00")),
        };

            inputParameterInfos = new InputParameterInfo[] {
                new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE)
            };
        optInputParameterInfos = new OptInputParameterInfo[] {
            new OptInputParameterInfo("Time period", OptInputParameterInfo.Type.OTHER,
                new IntegerRangeDescription(2, 2, 100, 1))
        };
        outputParameterInfos = new OutputParameterInfo[] {
            new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.DrawingStyle.LINE)
        };
    }
//----------------
    public static boolean compare_dateStr(String startdate, String enddate) {
        boolean isFirstBig = false;
        DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd ");
        try {
            Date dt1 = df1.parse(startdate);
            Date dt2 = df1.parse(enddate);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1>dt2");
                isFirstBig = true;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1<dt2");
                isFirstBig = false;
            } else {
                isFirstBig = false;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return isFirstBig;
    }

    public Date stringToDateParser(String date) {
       DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
        Date newDate = null;
        try {
            newDate = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return newDate;
    }
    //----------------
    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex = getLookback();
        }
        if (startIndex > endIndex) {
            return new IndicatorResult(0, 0);
        }

//---------
        int i, j;
        Date startDate = stringToDateParser(startdate);
        Date endDate = stringToDateParser(enddate);
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
            if (/* Here you need implement condition what indicator date is between startDate and endDate*/ ) {
                //    I want to calculate the data in the date set here ,Do not write code please help instruct
               // Some calculation logic

                double value = 0;
                for (int k = timePeriod; k > 0; k--) {
                    value += inputs[0][i - k].getClose();
                }
                outputs[0][j] = value;
            }
        }
            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 timePeriod;
    }

    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) {
        inputs[index] = (IBar[]) array;
    }

    public void setOptInputParameter(int index, Object value) {
        timePeriod = (Integer) value;
    }

    public void setOutputParameter(int index, Object array) {
        outputs[index] = (double[]) array;
    }
}


 
 Post subject: Re: Loop between Specific Dates Post rating: 0   New post Posted: Fri 12 Jan, 2018, 07:13 

User rating: 0
Joined: Mon 25 Jan, 2016, 05:59
Posts: 14
Location: China,
To API Support Please teach again.thanks

        int i, j;
        Date startDate = stringToDateParser(startdate);
        Date endDate = stringToDateParser(enddate);
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
                IBar bar = inputs[0][i];
                long barTime = bar.getTime();         
         if (int res = startDate.compareTo(endDate)|| inputs[inputIndex][j].getTime() != barTime) {
             // Please correct the date range.
               
                double todayHigh = inputs[0][i].getHigh();
                double todayLow  = inputs[0][i].getLow();   
               
                double value = 0;
                for (int k = timePeriod; k > 0; k--) {
                    value += inputs[0][i - k].getClose();
                }
                  outputs[0][j] = todayHigh;
                  outputs[0][j] = todayLow;
                  outputs[0][j] = value;
            }
        }
            return new IndicatorResult(startIndex, j);
    }


 
 Post subject: Re: Loop between Specific Dates Post rating: 0   New post Posted: Fri 12 Jan, 2018, 08:47 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
In this forum we try to focus on JForex API issues.
There are a number of strategies and indicators available from https://www.dukascopy.com/jstore/
Alternatively it is possible to to request external programmer to implement a strategy or indicator for you:
https://www.dukascopy.com/fxcomm/member ... am_service


 

Jump to:  

  © 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