|
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.
Problem with creating custom period |
bootor
|
Post subject: Problem with creating custom period |
Post rating: 0
|
Posted: Wed 02 Nov, 2011, 15:48
|
|
User rating: 0
Joined: Wed 02 Nov, 2011, 15:45 Posts: 6 Location: Ukraine,
|
I have an error during compiling the next part of code:
context.subscribeToBarsFeed(MyInstrument, selectedPeriod, OfferSide.BID, new IBarFeedListener());
Error: IBarFeedListener cannot be resolved to a type.
I was read the Wiki, but code example is not compilled to. Please, help me.
|
|
|
|
 |
API Support
|
Post subject: Re: Problem with creating custom period |
Post rating: 0
|
Posted: Wed 02 Nov, 2011, 16:59
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
For feeds for JForex API versions up till 2.6.38 use: import com.dukascopy.api.bar.* import com.dukascopy.api.listener.* Starting with version 2.6.43 use: import com.dukascopy.api.feed.*;
|
|
|
|
 |
janjjj
|
Post subject: Re: Problem with creating custom period |
Post rating: 0
|
Posted: Wed 09 Nov, 2011, 12:29
|
|
User rating: 3
Joined: Thu 28 Jul, 2011, 19:40 Posts: 72 Location: PolandPoland
|
Hello, I also have a problem with custom period on LIVE account. Following is strategy tested custom period (30sec, 3 min and 2 hour) package jforex;
import java.util.*;
import com.dukascopy.api.*; import com.dukascopy.api.listener.IBarFeedListener;
@RequiresFullAccess public class CustomPeriodTestDEMO01 implements IStrategy, IBarFeedListener { private IEngine engine; private IConsole console; private IHistory history; private IContext context; private IIndicators indicators; @Configurable("Instrument:") public Instrument instrumentThis = Instrument.EURUSD; 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(); context.subscribeToBarsFeed(instrumentThis, Period.createCustomPeriod(Unit.Second, 30), OfferSide.BID, this); context.subscribeToBarsFeed(instrumentThis, Period.createCustomPeriod(Unit.Hour, 2), OfferSide.BID, this); context.subscribeToBarsFeed(instrumentThis, Period.createCustomPeriod(Unit.Minute, 3), OfferSide.BID, this); }
public void onAccount(IAccount account) throws JFException { }
public void onMessage(IMessage message) throws JFException { print("m: "+message.getContent() + " "); }
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 { } public void onBar(Instrument instrument, Period period, OfferSide offerSide, IBar bar) { if(instrument == instrumentThis){ console.getOut().println("OnCustomCandle " + instrument + " " + period + " " + offerSide + " " + bar); } } void print(String what){ console.getOut().println(what); } }
On DEMO platform (version 2.6.38) it work fine, but on LIVE platform (version 2.6.33) it does not work,I recive problem: Quote: 10:11:09 ---------- 10:11:09 The method subscribeToBarsFeed(Instrument, Period, OfferSide, CustomPeriodTestDEMO01) is undefined for the type IContext 10:11:09 ^^^^^^^^^^^^^^^^^^^ 10:11:09 context.subscribeToBarsFeed(instrumentThis, Period.createCustomPeriod(Unit.Minute, 3), OfferSide.BID, this); 10:11:09 5. ERROR in C:\Users\DELL\AppData\Local\Temp\jfxide\tmp\CustomPeriodTestDEMO01.java (at line 29) 10:11:09 ---------- 10:11:09 The method subscribeToBarsFeed(Instrument, Period, OfferSide, CustomPeriodTestDEMO01) is undefined for the type IContext 10:11:09 ^^^^^^^^^^^^^^^^^^^ 10:11:09 context.subscribeToBarsFeed(instrumentThis, Period.createCustomPeriod(Unit.Hour, 2), OfferSide.BID, this); 10:11:09 4. ERROR in C:\Users\DELL\AppData\Local\Temp\jfxide\tmp\CustomPeriodTestDEMO01.java (at line 28) 10:11:09 ---------- 10:11:09 The method subscribeToBarsFeed(Instrument, Period, OfferSide, CustomPeriodTestDEMO01) is undefined for the type IContext 10:11:09 ^^^^^^^^^^^^^^^^^^^ 10:11:09 context.subscribeToBarsFeed(instrumentThis, Period.createCustomPeriod(Unit.Second, 30), OfferSide.BID, this); 10:11:09 3. ERROR in C:\Users\DELL\AppData\Local\Temp\jfxide\tmp\CustomPeriodTestDEMO01.java (at line 27) 10:11:09 ---------- 10:11:09 IBarFeedListener cannot be resolved to a type 10:11:09 ^^^^^^^^^^^^^^^^ 10:11:09 public class CustomPeriodTestDEMO01 implements IStrategy, IBarFeedListener { 10:11:09 2. ERROR in C:\Users\DELL\AppData\Local\Temp\jfxide\tmp\CustomPeriodTestDEMO01.java (at line 11) 10:11:09 ---------- 10:11:09 The import com.dukascopy.api.listener cannot be resolved 10:11:09 ^^^^^^^^^^^^^^^^^^^^^^^^^^ 10:11:09 import com.dukascopy.api.listener.IBarFeedListener; 10:11:09 1. ERROR in C:\Users\DELL\AppData\Local\Temp\jfxide\tmp\CustomPeriodTestDEMO01.java (at line 6) 10:11:09 ----------
If I use the headers: com.dukascopy.api.feed .*; There is no compiles also. Can you solve my problem? Because I can not use my strategy (using custom period) on LIVE account. And it is very important for me...
|
|
|
|
 |
API Support
|
Post subject: Re: Problem with creating custom period |
Post rating: 0
|
Posted: Wed 09 Nov, 2011, 13:08
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
Custom period is available with JForex-API 2.6.38, which is used by the current DEMO version. LIVE JForex client uses JForex-API 2.6.33. So if you want to use custom period in LIVE, your strategy has to combine custom period from ticks and consequently execute the custom period logic within onTick method.
|
|
|
|
 |
janjjj
|
Post subject: Re: Problem with creating custom period |
Post rating: 0
|
Posted: Wed 09 Nov, 2011, 15:05
|
|
User rating: 3
Joined: Thu 28 Jul, 2011, 19:40 Posts: 72 Location: PolandPoland
|
Thank you for your answer.
Your solution is quite uncomfortable. Is there somewhere an example of how exactly to do it?
When the LIVE platform will be available with custom period like in DEMO platform?
Best regards
|
|
|
|
 |
API Support
|
Post subject: Re: Problem with creating custom period |
Post rating: 0
|
Posted: Thu 10 Nov, 2011, 09:18
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
Consider the following strategy (see comments in the code): package jforex.test;
import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.TimeZone;
import com.dukascopy.api.*;
/** * The strategy demonstrates how one can handle himself custom periods, * if platform functionality is not available (e.g. for JForex API 2.6.33) * */
public class EmulateCustomPeriod implements IStrategy {
@Configurable("Instrument") public Instrument instrument = Instrument.EURUSD; @Configurable("Period (in secs)") public int periodInSecs = 7; @Configurable("Offer Side") public OfferSide side = OfferSide.BID; IHistory history; IConsole console; CustomBar customBar; ITick prevTick; long periodInMillis; @Override public void onStart(IContext context) throws JFException {
history = context.getHistory(); console = context.getConsole();
prevTick = history.getLastTick(instrument); periodInMillis = periodInSecs * 1000; } private void print(Object o){ console.getOut().println(o); }
@Override public void onTick(Instrument instrument, ITick tick) throws JFException { if(instrument != this.instrument){ return; }
//we have a new period - execute custom bar logic and create a new bar if(prevTick.getTime() / periodInMillis < tick.getTime() / periodInMillis ){ if(customBar != null){ onCustomBar(customBar); } long barTimeRounded = tick.getTime() - tick.getTime() % periodInMillis; customBar = new CustomBar(tick, side, barTimeRounded); //update current bar's data } else if (customBar != null) { customBar.updateBar(tick, side); } else { //note - here the first bar has not been created yet, so we just need to wait for the time condition to fulfill //alternatively one can load some ticks from history in order to create the first bar } prevTick = tick; } private void onCustomBar(IBar bar){ print ("TODO: action on custom bar: " + bar); }
@Override public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {} @Override public void onMessage(IMessage message) throws JFException {} @Override public void onAccount(IAccount account) throws JFException {} @Override public void onStop() throws JFException {} public class CustomBar implements IBar {
public double open; public double close; public double low; public double high; public double vol; public long time; @SuppressWarnings("serial") private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") { {setTimeZone(TimeZone.getTimeZone("GMT")); }}; private DecimalFormat df = new DecimalFormat("0.00000");
public CustomBar(ITick tick, OfferSide offerSide, long time) { double price = offerSide == OfferSide.BID ? tick.getBid() : tick.getAsk(); double volume = offerSide == OfferSide.BID ? tick.getBidVolume() : tick.getAskVolume(); open = close = low = high = price; vol = volume; this.time = time; } public CustomBar(double open, double close, double low, double high, double vol, long time) { this.open = open; this.close = close; this.low = low; this.high = high; this.vol = vol; this.time = time; } public void updateBar(ITick tick, OfferSide offerSide) { double price = offerSide == OfferSide.BID ? tick.getBid() : tick.getAsk(); double volume = offerSide == OfferSide.BID ? tick.getBidVolume() : tick.getAskVolume(); close = price; if(price < low){ low = price; } if(price > high){ high = price; } //bar volumes are sum of their tick volumes vol += volume; }
@Override public double getOpen() { return open; }
@Override public double getClose() { return close; }
@Override public double getLow() { return low; }
@Override public double getHigh() { return high; }
@Override public double getVolume() { return vol; }
@Override public long getTime() { return time; }
@Override public String toString() { StringBuilder str = new StringBuilder(); str.append(time).append("[").append(sdf.format(time)).append("] O: ") .append(df.format(open)).append(" C: ").append(df.format(close)).append(" H: ").append(df.format(high)).append(" L: ") .append(df.format(low)).append(" V: ").append(df.format(vol)); return str.toString(); }
}
}
Attachments: |
EmulateCustomPeriod.java [4.39 KiB]
Downloaded 578 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.
|
|
|
|
|
 |
janjjj
|
Post subject: Re: Problem with creating custom period |
Post rating: 0
|
Posted: Mon 14 Nov, 2011, 11:17
|
|
User rating: 3
Joined: Thu 28 Jul, 2011, 19:40 Posts: 72 Location: PolandPoland
|
Great, thank you very much!
|
|
|
|
 |
|
Pages: [
1
]
|
|
|
|
|