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.

strategy to increase Period by one unit
 Post subject: strategy to increase Period by one unit Post rating: 0   New post Posted: Tue 19 Dec, 2017, 16:26 

User rating: 0
Joined: Mon 14 Nov, 2011, 05:16
Posts: 33
Location: PhilippinesPhilippines
Hello support!

I use time frames as an optimization parameter, and wish to look at indicators in the time frame one level above my trading time frame. How will I code this in jforex (based on the below code snippet)?

Object[] thisindicatorTSIOutput = indicators.calculateIndicator(
Instrument.EURUSD,
period.FOUR_HOURS,
new OfferSide[]{OfferSide.BID},
"TSI",
new AppliedPrice[]{IIndicators.AppliedPrice.CLOSE},
new Object[] {20,13,7},
0);

i.e. so that period.FOUR_HOURS becomes a variable in the lines of "selectedPeriod + 1" ...

thanks in advance


 
 Post subject: Re: strategy to increase Period by one unit Post rating: 0   New post Posted: Fri 05 Jan, 2018, 11:39 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You can do it in this way:

package jforex;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;

public class IncreasePeriodStrategy implements IStrategy {
  IContext context;
  IConsole console;
  IIndicators indicators;

  Instrument myInstrument = Instrument.EURUSD;
  Period myPeriod = Period.FOUR_HOURS;

  @Override
  public void onStart(IContext context) throws JFException {
    this.context = context;
    this.console = context.getConsole();
    this.indicators = context.getIndicators();
  }

  @Override
  public void onTick(Instrument instrument, ITick tick) throws JFException {

  }

  @Override
  public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar)
      throws JFException {
    if (instrument != myInstrument || period != myPeriod) return;
    Object[] thisIndicatorTSIOutput = indicators.calculateIndicator(myInstrument, myPeriod, new OfferSide[]{OfferSide.BID},
        "TSI", new AppliedPrice[]{AppliedPrice.CLOSE}, new Object[]{20,13,7}, 0);
    console.getOut().println("Period: " + myPeriod +" " + thisIndicatorTSIOutput[0] + " " + thisIndicatorTSIOutput[1] + " " + thisIndicatorTSIOutput[2]);
    //Some logic to check indicator in next time frame
    Period nextPeriod = getNextPeriod(myPeriod);
    Object[] nextPeriodIndicatorTSIOutput = indicators.calculateIndicator(myInstrument, nextPeriod, new OfferSide[]{OfferSide.BID},
        "TSI", new AppliedPrice[]{AppliedPrice.CLOSE}, new Object[]{20,13,7}, 0);
    console.getOut().println("Period: " + nextPeriod + " " + nextPeriodIndicatorTSIOutput[0] + " " + nextPeriodIndicatorTSIOutput[1] + " " + nextPeriodIndicatorTSIOutput[2]);
  }

  @Override
  public void onMessage(IMessage message) throws JFException {

  }

  @Override
  public void onAccount(IAccount account) throws JFException {

  }

  @Override
  public void onStop() throws JFException {

  }

  private Period getNextPeriod(Period period) {
    Period[] periods = Period.values();
    int index = period.ordinal();
    int nextIndex = index + 1;
    nextIndex %= periods.length;
    return periods[nextIndex];
  }
}


 
 Post subject: Re: strategy to increase Period by one unit Post rating: 0   New post Posted: Fri 05 Jan, 2018, 16:07 

User rating: 0
Joined: Mon 14 Nov, 2011, 05:16
Posts: 33
Location: PhilippinesPhilippines
Thanks! That's great!

It appears that I may be able to make the below snip (+1) a configurable variable, and thus optimize on periods without creating currentperiod and nextperiod indicators...

"int nextIndex = index + 1;"


 

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