|
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.
Getting High Low using listing of Bars |
drishti
|
Post subject: Getting High Low using listing of Bars |
Post rating: 0
|
Posted: Fri 25 May, 2012, 10:23
|
|
User rating: 8
Joined: Tue 31 Jan, 2012, 11:24 Posts: 72 Location: India, Gurgaon
|
Dear Support, I am trying to get high/low by Listing Bars, however it is not providing lowest price value correctly. Is there something wrong in code, please suggest. int BarCount = 5; long lastTickTime = history.getLastTick(instrument).getTime(); long lastBarTime = history.getBarStart(selectedPeriod, lastTickTime); List<IBar> bars = history.getBars(instrument, selectedPeriod, OfferSide.BID, Filter.NO_FILTER, BarCount, lastBarTime, 1); double maxHigh = 0; double maxLow = 0; for(IBar bar : bars){ if (maxHigh < bar.getHigh()){ maxHigh = bar.getHigh(); } if (maxLow < bar.getLow()){ maxLow = bar.getLow(); }
Thanks, Santosh.
|
|
|
|
 |
API Support
|
Post subject: Re: Getting High Low using listing of Bars |
Post rating: 0
|
Posted: Fri 25 May, 2012, 10:31
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
flip the comparison operation on 14th line. Also assign the intial high to Double.MIN_VALUE and the low to Double.MAX_VALUE.
|
|
|
|
 |
drishti
|
Post subject: Re: Getting High Low using listing of Bars |
Post rating: 0
|
Posted: Fri 25 May, 2012, 12:15
|
|
User rating: 8
Joined: Tue 31 Jan, 2012, 11:24 Posts: 72 Location: India, Gurgaon
|
API Support wrote: flip the comparison operation on 14th line. Also assign the intial high to Double.MIN_VALUE and the low to Double.MAX_VALUE. Thanks for quick reply, and I have tried it, however still not getting correct high and low value. I am trying to make a strategy, which will calculate last few bars high and low, on specific time period, and then put BUY-STOP and SELL-STOP accordingly. long lastTickTime = history.getLastTick(instrument).getTime(); long lastBarTime = history.getBarStart(selectedPeriod, lastTickTime); List<IBar> bars = history.getBars(instrument, selectedPeriod, OfferSide.BID, Filter.WEEKENDS, BarCount, lastBarTime, 1); double maxHigh = Double.MIN_VALUE; double maxLow = Double.MAX_VALUE; for(IBar bar : bars){ if (maxHigh < bar.getHigh()){ maxHigh = bar.getHigh(); } if (maxLow > bar.getLow()){ maxLow = bar.getLow(); } I tried to test last week high low on Historical Tester, and it gave me following result with 6 look back candle on 4Hour chart. 2012-05-16 00:00:01 Order submitted Order [EURUSD4, EUR/USD, BUYSTOP, 10000.0 at 1.28686] submitted by the strategy Comment : High is Correct 2012-05-16 00:00:01 Order submitted Order [EURUSD5, EUR/USD, SELLSTOP, 10000.0 at 1.2822] submitted by the strategy Comment : Low is 1.2721 2012-05-17 00:00:00 Order submitted Order [EURUSD6, EUR/USD, BUYSTOP, 10000.0 at 1.2725] submitted by the strategy Comment : High is 2757 2012-05-17 00:00:00 Order submitted Order [EURUSD7, EUR/USD, SELLSTOP, 10000.0 at 1.26811] submitted by the strategy Comment : Low is Correct I am not able to understand, why it is providing incorrect result. Can you tell me other methods to get it correctly.
|
|
|
|
 |
API Support
|
Post subject: Re: Getting High Low using listing of Bars |
Post rating: 0
|
Posted: Fri 25 May, 2012, 15:08
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
|
|
|
 |
drishti
|
Post subject: Re: Getting High Low using listing of Bars |
Post rating: 0
|
Posted: Fri 25 May, 2012, 20:01
|
|
User rating: 8
Joined: Tue 31 Jan, 2012, 11:24 Posts: 72 Location: India, Gurgaon
|
Thanks for correcting me. Could you please look into this also, as I was confused because order for SELL-STOP is not getting executing with STOP price, and it executes on Lowest value which it calculate from previous candle. BUYSTOP is perfectly fine. longOrder = submitOrder(OrderCommand.BUYSTOP, maxHigh + stop); console.getOut().println("Buy Stop Order Created" + longOrder.getOpenPrice() + " With Stop Loss Price" + longOrder.getStopLossPrice() ); shortOrder = submitOrder(OrderCommand.SELLSTOP, minLow - stop); console.getOut().println(" Sell Stop Order Created" + shortOrder.getOpenPrice() +" With Stop Loss Price" + shortOrder.getStopLossPrice() ); Thanks once again for quick resolution. Regards, Santosh.
|
|
|
|
 |
API Support
|
Post subject: Re: Getting High Low using listing of Bars |
Post rating: 0
|
Posted: Mon 28 May, 2012, 13:46
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
drishti wrote: Could you please look into this also, as I was confused because order for SELL-STOP is not getting executing with STOP price, and it executes on Lowest value which it calculate from previous candle. BUYSTOP is perfectly fine. We don't fully understand what you mean, for more efficient assistance could you please : - Provide full example strategy which represents the problem. The more concise, the better.
- Provide the launch scenario (whether it's with live data or in Historical Tester, if the latter, provide the used settings).
- Describe the behavior that you find being wrong.
- Describe the expected behavior.
|
|
|
|
 |
drishti
|
Post subject: Re: Getting High Low using listing of Bars |
Post rating: 0
|
Posted: Mon 28 May, 2012, 19:08
|
|
User rating: 8
Joined: Tue 31 Jan, 2012, 11:24 Posts: 72 Location: India, Gurgaon
|
Hi Support, In strategy, I want to use Breakout of some previous bars, and opening two orders at using some stop price (E.g 30 Pips) from high and low. Now my problem is, it is not opening orders at right value, like if high calculated from previous 5 bars is 1.2960, and after adding 30 Pips, it should be 1.2990, then one buy order should be created at 1.2990. However, it opens order at high, or even below high. Please look into the code, and correct me. However if I use daily candle to get high and low, then it works fine. package singlejartest; import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.TimeZone; import java.math.BigDecimal; import java.math.RoundingMode; import com.dukascopy.api.*; import com.dukascopy.api.IEngine.OrderCommand; import com.dukascopy.api.IIndicators.AppliedPrice; import com.dukascopy.api.indicators.IIndicator; import java.util.Calendar; import java.util.GregorianCalendar; import java.text.ParseException; import java.util.TimeZone; import java.util.List;
import sun.awt.SunHints.Value;
public class dbrkouttest2 implements IStrategy { private IEngine engine; private IConsole console; private IHistory history; private IIndicators indicators; private IContext context; private int counter = 0; private IOrder longOrder = null; private IOrder shortOrder = null; private SimpleDateFormat gmtSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Configurable("Instrument") public Instrument instrument = Instrument.EURUSD; @Configurable("Period of Bar") public Period selectedPeriod = Period.FOUR_HOURS; @Configurable("Bar Count") public int BarCount = 5;
@Configurable("Slippage") public double slippage = 3; @Configurable("Amount") public double amount = 0.01; @Configurable("Take profit pips") public int takeProfitPips = 20; @Configurable("Stop loss in pips") public int stopLossPips = 20; @Configurable("Stop price from HIGH/LOW") public int stop = 30; @Configurable("Open hour GMT") public int openHour = 2; @Configurable("Open min GMT") public int openMin = 0; @Configurable("Close hour GMT") public int closeHour = 20; @Configurable("Close min GMT") public int closeMin = 0; public void onStart(IContext context) throws JFException { this.console = context.getConsole(); this.indicators = context.getIndicators(); this.history = context.getHistory(); this.engine = context.getEngine(); gmtSdf.setTimeZone(TimeZone.getTimeZone("GMT")); }
public void onTick(Instrument instrument, ITick tick) throws JFException { if (instrument != this.instrument) { return; } if( timeIsEqual(tick.getTime(), openHour, openMin)) { long lastTickTime = history.getLastTick(instrument).getTime(); long lastBarTime = history.getBarStart(selectedPeriod, lastTickTime); List<IBar> bars = history.getBars(instrument, selectedPeriod, OfferSide.BID, Filter.NO_FILTER, BarCount, lastBarTime, 1); double maxHigh = Double.MIN_VALUE; double minLow = Double.MAX_VALUE; for(IBar bar : bars){ if (maxHigh < bar.getHigh()){ maxHigh = bar.getHigh(); console.getOut().println("maxHigh" + maxHigh); } if (minLow > bar.getLow()){ minLow = bar.getLow(); console.getOut().println("maxLow" + minLow); } if (longOrder == null && shortOrder == null){ double buyPrice = maxHigh + instrument.getPipValue()*stop; double sellPrice = minLow - instrument.getPipValue()*stop; longOrder = submitOrder(OrderCommand.BUYSTOP, buyPrice); console.getOut().println("Buy Stop Order Created" + longOrder.getOpenPrice() + " With Stop Loss Price" + longOrder.getStopLossPrice() ); shortOrder = submitOrder(OrderCommand.SELLSTOP, sellPrice); console.getOut().println(" Sell Stop Order Created" + shortOrder.getOpenPrice() +" With Stop Loss Price" + shortOrder.getStopLossPrice() ); } } } // Expire or Orders if(timeIsEqual(tick.getTime(), closeHour, closeMin)){ if (longOrder != null) { if(longOrder.getState() == IOrder.State.OPENED) { longOrder.close();
} if(longOrder.getState() != IOrder.State.FILLED) { longOrder = null; } } if (shortOrder != null) { if(shortOrder.getState() == IOrder.State.OPENED) { shortOrder.close(); } if(shortOrder.getState() != IOrder.State.FILLED) { shortOrder = null; } } } // Expire of Orders }
public boolean timeIsEqual(long time, int hour, int min){ Calendar cal = new GregorianCalendar(); cal.setTimeZone(TimeZone.getTimeZone("GMT")); cal.setTimeInMillis(time); cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, min); Calendar cal2 = new GregorianCalendar(); cal2.setTimeZone(TimeZone.getTimeZone("GMT")); cal2.setTimeInMillis(cal.getTimeInMillis()); cal2.add(Calendar.MINUTE, 5); if(cal.getTimeInMillis() <= time && time <= cal2.getTimeInMillis() ) { return true; } return false; } public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { if (period != this.selectedPeriod || instrument != this.instrument) { return; } } public void onMessage(IMessage message) throws JFException { if (message != null && message.getType() == IMessage.Type.ORDER_CLOSE_OK) { IOrder lastOne = message.getOrder(); double profitsLoss = lastOne.getProfitLossInPips(); console.getOut().println("Order : "+lastOne.getLabel()+ " "+ lastOne.getOrderCommand()+ " Pips: "+profitsLoss); } if(message.getOrder() != null && message.getOrder() == longOrder) print(message); if(message.getOrder() != null && message.getOrder() == shortOrder) print(message); } private void print(IMessage message) { // TODO Auto-generated method stub }
public void onAccount(IAccount account) throws JFException { } public void onStop() throws JFException { } private IOrder submitOrder(OrderCommand orderCmd, double price) throws JFException { double stopLossPrice = 0.0, takeProfitPrice = 0.0; // Calculating order price, stop loss and take profit prices if (orderCmd == OrderCommand.BUYSTOP) { if (stopLossPips > 0) { stopLossPrice = price - getPipPrice(stopLossPips); } if (takeProfitPips > 0) { takeProfitPrice = price + getPipPrice(takeProfitPips); } } else { if (stopLossPips > 0) { stopLossPrice = price + getPipPrice(stopLossPips); } if (takeProfitPips > 0) { takeProfitPrice = price - getPipPrice(takeProfitPips); } } return engine.submitOrder(getLabel(instrument), instrument, orderCmd, amount, price, slippage, stopLossPrice, takeProfitPrice); } private double getPipPrice(int pips) { return pips * this.instrument.getPipValue(); } private String getLabel(Instrument instrument) { String label = instrument.name(); label = label + (counter++); label = label.toUpperCase(); return label; } private void closeOrder(IOrder order) throws JFException { if (order != null && isActive(order)) { order.close(); } } private boolean isActive(IOrder order) throws JFException { if (order != null && order.getState() != IOrder.State.CLOSED && order.getState() != IOrder.State.CREATED && order.getState() != IOrder.State.CANCELED) { return true; } return false; } } Thanks for your support. Regards,
|
|
|
|
 |
API Support
|
 |
Post subject: Re: Getting High Low using listing of Bars |
Post rating: 0
|
Posted: Tue 29 May, 2012, 08:46
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
The problem is that you submit orders before you have found out the actual maxHigh and minLow values - the submitOrder function should get put after the for cycle which looks for the values. Do you use any IDE for writing your java code (Eclipse, Netbeans, IntelliJ)? If not see: https://www.dukascopy.com/wiki/#Use_in_EclipseIn IDE you can format your code such that the structure helps you find such bugs just by reading the code.
|
|
|
|
 |
|
Pages: [
1
]
|
|
|
|
|