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.

previous candle size in pips
 Post subject: previous candle size in pips Post rating: 0   New post Posted: Wed 30 Nov, 2011, 13:01 

User rating: 0
Joined: Fri 25 Nov, 2011, 02:49
Posts: 17
Location: Malaysia,
Could you please show me what the code would be if in my order logic I don't want it to enter orders if the previous candle was over a certain size ie 40 pips etc.

Thanks


 
 Post subject: Re: previous candle size in pips Post rating: 0   New post Posted: Wed 30 Nov, 2011, 18:31 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Consider the following example strategy
package jforex.strategies.oneorder;

import java.text.DecimalFormat;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;

/**
 * The example strategy creates an order on every bar if the
 * bar height does not exceed barMaxPips
 *
 */
@RequiresFullAccess
public class OrderDependOnPrevBarSize implements IStrategy {
   
   @Configurable("Instrument")
   public Instrument instrument = Instrument.EURUSD;
   @Configurable("Period")
   public Period period = Period.TEN_SECS;
   @Configurable("Bar max height in pips")
   public int barMaxPips = 1;
   
   private IEngine engine;
   private IConsole console;
   private int counter;
   
   private DecimalFormat df = new DecimalFormat("0.00000");

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

   @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 != this.instrument || period != this.period){
         return;
      }
      double barHeight = Math.abs(bidBar.getOpen() - bidBar.getClose());      
      //create order if the candle height is less than barMaxPips pips
      boolean createOrder = barHeight < barMaxPips * instrument.getPipValue();
      if(createOrder){
         engine.submitOrder("order" + ++counter, instrument, OrderCommand.BUY, 0.001);
      }
      print("Candle height in pips: " + df.format(barHeight / instrument.getPipValue()) + " create order? " + (createOrder? "YES!":"NO!"));
      
   }
   
   private void print(Object o){
      console.getOut().println(o);
   }

   @Override
   public void onMessage(IMessage message) throws JFException {
      //print order related messages
      if(message.getOrder() != null){
         print(message);
      }
   }

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

   @Override
   public void onStop() throws JFException {
      //close all orders on strategy stop
      for(IOrder o: engine.getOrders()){
         o.close();
      }
   }

}


Attachments:
OrderDependOnPrevBarSize.java [2 KiB]
Downloaded 283 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: previous candle size in pips Post rating: 0   New post Posted: Thu 01 Dec, 2011, 01:02 

User rating: 0
Joined: Fri 25 Nov, 2011, 02:49
Posts: 17
Location: Malaysia,
Great Thanks


 

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