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.

getClose for a certain time frame
 Post subject: getClose for a certain time frame Post rating: 0   New post Posted: Mon 15 Mar, 2010, 18:42 

User rating: -
I am looking to code a way to show if getClose for a certain time frame is higher/lower then bollinger bands for that period. What would be the proper way to structure this? Plus, how do you code to wait until x number of bars pass before entering a trade again if the profit/stop has been triggered? Any help would be greatly appreciated.
Thank you


 
 Post subject: Re: getClose for a certain time frame Post rating: 0   New post Posted: Wed 31 Mar, 2010, 13:05 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hi,
we prepared sample:
package jforex;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IIndicators.*;
import com.dukascopy.api.IOrder.State;

public class Strategy implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IIndicators indicators;
    private IOrder myOrder;
    private int counter = 0;
    private boolean isFirst = true;
   
    @Configurable("Instrument")         public Instrument instrument = Instrument.EURUSD;
    @Configurable("Period")             public Period     period = Period.TEN_SECS;
    @Configurable("Interval")           public int interval = 6;
    @Configurable("Slippage (pips)")    public int slpp = 25;
    @Configurable("Stop Loss (pips)")   public int stopLoss = 100;
    @Configurable("Take Profit (pips)") public int takeProfit = 100;
   
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.indicators = context.getIndicators();
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {       
    }

    public void onStop() throws JFException {
        //close our order on exit
        if (myOrder != null
                && myOrder.getState() != State.CANCELED){
            myOrder.close();
        }       
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if(this.period == period && this.instrument == instrument){
            //get bbands value for last period
            double bbands = indicators.bbands(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 20, 2, 2, MaType.EMA, 0)[1];
            //get bbands value for last candle
            double closePrice = bidBar.getClose();
            if (bbands > closePrice)
                console.getOut().println(" bbands is higher then close price!" );
            else
                console.getOut().println(" bbands is lover then close price!" );
               
            //check if defined count of bars has passed
            if (counter++ == interval || isFirst){
                counter = 0;
                if (engine.getOrder("My_Order")==null){               
                    double sl = closePrice - (stopLoss*0.0001);
                    double tp = closePrice + (takeProfit*0.0001);
                   
                    myOrder = engine.submitOrder("My_Order", instrument, OrderCommand.BUY, 0.1, closePrice, (slpp*0.0001), sl, tp);                   
                }   
            }
            isFirst = false;           
        }
    }
}

If you still have question, please do not hesitate to ask.


 
 Post subject: Re: getClose for a certain time frame Post rating: 0   New post Posted: Wed 15 Dec, 2010, 18:46 

User rating: 0
Joined: Wed 17 Nov, 2010, 20:55
Posts: 3
The following line in the strategy apparently only defines the middle bband, right?
double bbands = indicators.bbands(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 20, 2, 2, MaType.EMA, 0)[1];

So, I went through the Wiki and searched the Forum, and I can't seem to figure out a simple way to identify the values of both the upper and lower bbands? Is there a single statement like the one above for each, or do I need to identify the middle band, then add 2 standard deviations to that value to get the upper, and subtract two standard deviations to get the value of the lower? If so, then why even bother pulling bbands, and why not just do an EMA?

I'd ideally like something like:

double bbandsUpper = ???
double bbandsMiddle = [the line of code that is listed above]
double bbandsLower = ???


Thanks for any help anyone can offer!


 
 Post subject: Re: getClose for a certain time frame Post rating: 0   New post Posted: Thu 16 Dec, 2010, 15:49 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
2010noob wrote:
The following line in the strategy apparently only defines the middle bband, right?
double bbands = indicators.bbands(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 20, 2, 2, MaType.EMA, 0)[1];


Yes, array element with an index 1 is the middle bband.
Please take a look at the following JForex Wiki page https://www.dukascopy.com/wiki/index.php ... nger_Bands we have added description of band values.


 

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