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.

custom bars
 Post subject: custom bars Post rating: 0   New post Posted: Wed 08 May, 2013, 15:02 
User avatar

User rating: 0
Joined: Thu 13 Oct, 2011, 17:27
Posts: 21
Location: SwitzerlandSwitzerland
hi, could you please provide a simple example strategy which is trading on customized bars of 1 sec basing on an indicator. thank you


 
 Post subject: Re: custom bars Post rating: 0   New post Posted: Wed 08 May, 2013, 15:18 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
See:
https://www.dukascopy.com/wiki/#Chart_feeds/Feed_history,_indicator_calculation_and_chart_opening


 
 Post subject: Re: custom bars Post rating: 0   New post Posted: Wed 08 May, 2013, 15:48 
User avatar

User rating: 0
Joined: Thu 13 Oct, 2011, 17:27
Posts: 21
Location: SwitzerlandSwitzerland
ok thank you for the link. i have seen that already and tried to implement it but i do not yet understand it.

all this feed stuff is put in the onStart method. what will be the Period in the onBar method then?

i was hoping it would be possible to get the Period under onBar defined to 1 sec.


 
 Post subject: Re: custom bars Post rating: 0   New post Posted: Wed 08 May, 2013, 15:52 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
TAA wrote:
i was hoping it would be possible to get the Period under onBar defined to 1 sec.
Simply modify the strategy by adding another feed to the list i.e.:
CANDLESTICKS_1_SEC(new TimePeriodAggregationFeedDescriptor(Instrument.EURUSD, Period.ONE_SEC, OfferSide.ASK, Filter.NO_FILTER));
And then select it in parameters dialog on strategy run.
TAA wrote:
what will be the Period in the onBar method then?
See the bullet about the onBar method:
https://www.dukascopy.com/wiki/#Strategy_API


 
 Post subject: Re: custom bars Post rating: 0   New post Posted: Wed 08 May, 2013, 17:46 
User avatar

User rating: 0
Joined: Thu 13 Oct, 2011, 17:27
Posts: 21
Location: SwitzerlandSwitzerland
now i have implemented it as follows. the code compiles and runs but all time settings for the mom inticator give the same result in testing. so i guess the onBar method does not receive the proper data for a 1 second period. what am i doing wrong? thanks

    @Configurable("Period") 
    public Period period = Period.createCustomPeriod(Unit.Second, 1);
    @Configurable("Feed data count for indicator calculation")
    public int dataCount = 5;
    @Configurable("feed type")
    public MyFeeds feedDataType = MyFeeds.CANDLESTICKS_1_SEC_ASK;
    @Configurable("open chart")
    public boolean openChart = false;
    enum MyFeeds {
        //
        CANDLESTICKS_1_SEC_ASK(new TimePeriodAggregationFeedDescriptor(Instrument.XAUUSD, Period.ONE_SEC, OfferSide.ASK, Filter.NO_FILTER));
        //
        private final IFeedDescriptor feedDescriptor;
        MyFeeds(IFeedDescriptor feedDescriptor) { this.feedDescriptor = feedDescriptor; }
        public IFeedDescriptor getFeedDescriptor() { return feedDescriptor; }
    }   


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

        IFeedDescriptor feedDescriptor = feedDataType.getFeedDescriptor();
        context.setSubscribedInstruments(new HashSet<Instrument>(Arrays.asList(new Instrument[] { feedDescriptor.getInstrument() })), true);
        if(openChart){           
            context.openChart(feedDescriptor);
        }
        final int dataCount = 3;
        context.subscribeToFeed(feedDescriptor, new IFeedListener() {
        @Override
        public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData) {
            console.getOut().println(feedData + " of feed: " + feedDescriptor);
           
        }
    });
    }


public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if (period != this.period) return;   
        if (instrument != this.instrument) return; 
        double mombuy      = indicators.mom(instrument, period, OfferSide.ASK, appliedprice, time, 1);
        double momsell     = indicators.mom(instrument, period, OfferSide.BID, appliedprice, time, 1);
}


 
 Post subject: Re: custom bars Post rating: 0   New post Posted: Thu 09 May, 2013, 07:38 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You have to do the calculations inside the onFeedData method. And for calculation on feeds use the calculation method used in the feed example, more examples here:
https://www.dukascopy.com/wiki/#Indicator_Calculation/Universal_price_feed


 
 Post subject: Re: custom bars Post rating: 0   New post Posted: Thu 09 May, 2013, 09:32 
User avatar

User rating: 0
Joined: Thu 13 Oct, 2011, 17:27
Posts: 21
Location: SwitzerlandSwitzerland
ok i see... but does it mean it is not possible to tell the onBar method to run every 1 second?
that would be a lot more comfortable


 
 Post subject: Re: custom bars Post rating: 0   New post Posted: Thu 09 May, 2013, 17:03 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You can pull out the onFeedData method, the following way:
package jforex.feed.indicators;

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.feed.*;
import com.dukascopy.api.feed.util.TimePeriodAggregationFeedDescriptor;
import com.dukascopy.api.util.DateUtils;

public class MomOn1SecBars implements IStrategy, IFeedListener {

    private IIndicators indicators;
    private IConsole console;

    IFeedDescriptor feedDescriptorBuy = new TimePeriodAggregationFeedDescriptor(Instrument.EURUSD, Period.ONE_SEC, OfferSide.ASK, Filter.NO_FILTER);
    IFeedDescriptor feedDescriptorSell = new TimePeriodAggregationFeedDescriptor(Instrument.EURUSD, Period.ONE_SEC, OfferSide.BID, Filter.NO_FILTER);

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

    }

    @Override
    public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData) {
        try {
            Object[] momBuyResult = indicators.calculateIndicator(feedDescriptorBuy, new OfferSide[] { feedDescriptor.getOfferSide() }, "MOM",
                    new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { 10 }, 1);
            Object[] momSellResult = indicators.calculateIndicator(feedDescriptorSell, new OfferSide[] { feedDescriptor.getOfferSide() },
                    "MOM", new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { 10 }, 1);
            double momBuy = (Double) momBuyResult[0];
            double momSell = (Double) momSellResult[0];
            console.getOut().format("sell %.7f buy %.7f at %s", momBuy, momSell, DateUtils.format(feedData.getTime())).println();
        } catch (JFException e) {
            console.getErr().println(e);
        }

    }

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

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}

    public void onMessage(IMessage message) throws JFException {}

    public void onAccount(IAccount account) throws JFException {}

    public void onStop() throws JFException {}

}



Attachments:
MomOn1SecBars.java [2.15 KiB]
Downloaded 360 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: custom bars Post rating: 0   New post Posted: Thu 09 May, 2013, 18:06 
User avatar

User rating: 0
Joined: Thu 13 Oct, 2011, 17:27
Posts: 21
Location: SwitzerlandSwitzerland
ok i got it ... thank you very much for your help


 

Jump to:  

  © 1998-2026 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