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 high/low
 Post subject: previous high/low Post rating: 0   New post Posted: Sat 03 Mar, 2012, 23:22 
User avatar

User rating: 1
Joined: Fri 02 Mar, 2012, 13:19
Posts: 31
Location: PolandPoland
Hi,
Sorry for stupid question but I'm :mrgreen: in Java. How can I write some code that is responsible for the position was opened when the current price is higher or lower by 1pips of High or Low of the previous candle?
Thanks


 
 Post subject: Re: previous high/low Post rating: 0   New post Posted: Mon 05 Mar, 2012, 14:56 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
For bar data start here:
https://www.dukascopy.com/wiki/#Historical_Data
For orders start here:
https://www.dukascopy.com/wiki/#Orders_overview
Here is a simple strategy:
https://www.dukascopy.com/wiki/#Simple_Strategy


 
 Post subject: Re: previous high/low Post rating: 0   New post Posted: Mon 05 Mar, 2012, 19:26 
User avatar

User rating: 1
Joined: Fri 02 Mar, 2012, 13:19
Posts: 31
Location: PolandPoland
Thank you for your interest. Unfortunately I still can not cope with it. I'm trying in this way
    public void onStart(IContext context) throws JFException {
        this.context = context;
        indicators = context.getIndicators();
        engine = context.getEngine();
        history = context.getHistory();
        indicators = context.getIndicators();
        console = context.getConsole();
       
        IBar prev = history.getBar(Instrument.EURUSD, Period.ONE_MIN, OfferSide.BID, 1);
       
        double price = history.getLastTick(Instrument.EURUSD).getBid();
        double high = prev.getHigh();
        double low = prev.getLow();
         
       if (positionsTotal(Instrument.EURUSD) == 0)
       {
                if (price<low) {
                 sell(Instrument.EURUSD, engine, profitLimit, lossLimit, volume);
                 }
                else  if (price>high) {
                   buy(Instrument.EURUSD, engine, profitLimit, lossLimit, volume);
                  }
    }

but it's not working as it should :? Can you explain what is wrong or show how it should be?
thanks


 
 Post subject: Re: previous high/low Post rating: 0   New post Posted: Tue 06 Mar, 2012, 09:00 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Try the following:
    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (instrument != this.instrument) {
            return;
        }
        IBar prev = history.getBar(Instrument.EURUSD, Period.ONE_MIN, OfferSide.BID, 1);

        double price = tick.getBid();
        double high = prev.getHigh();
        double low = prev.getLow();

        if (engine.getOrders(instrument).size() == 0) {
            if (price < low) {
                engine.submitOrder("order" + ++counter, instrument, OrderCommand.SELL, 0.001);
            } else if (price > high) {
                engine.submitOrder("order" + ++counter, instrument, OrderCommand.BUY, 0.001);
            }
        }

    }


Attachments:
ExampleStrat.java [1.55 KiB]
Downloaded 306 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 high/low Post rating: 0   New post Posted: Wed 07 Mar, 2012, 17:15 
User avatar

User rating: 1
Joined: Fri 02 Mar, 2012, 13:19
Posts: 31
Location: PolandPoland
Hi,
Thanks for your code. It works as it should, but could you show how to send the order with tp and sl in this case? I am trying in several ways but I can't do that. :(


 
 Post subject: Re: previous high/low Post rating: 0   New post Posted: Thu 08 Mar, 2012, 09:41 
User avatar

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


 
 Post subject: Re: previous high/low Post rating: 0   New post Posted: Thu 08 Mar, 2012, 11:44 
User avatar

User rating: 1
Joined: Fri 02 Mar, 2012, 13:19
Posts: 31
Location: PolandPoland
Yeah I know, I look at it but I can't send orders with sl and tp. Thanks for the help anyway. Maybe someone else can add it?


 
 Post subject: Re: previous high/low Post rating: 0   New post Posted: Thu 08 Mar, 2012, 13:47 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
be_patient wrote:
Yeah I know, I look at it but I can't send orders with sl and tp.
There are a couple of examples in the article, try those and then adjust your own strategy.


 
 Post subject: Re: previous high/low Post rating: 0   New post Posted: Thu 08 Mar, 2012, 21:59 
User avatar

User rating: 1
Joined: Fri 02 Mar, 2012, 13:19
Posts: 31
Location: PolandPoland
This is one of many versions of what I created and do not work.
package jforex;

import java.util.*;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
public class nowysl implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IOrder order;
    private IUserInterface userInterface;
    private int counter = 0;
     @Configurable("Order Command")
    public OrderCommand cmd = OrderCommand.BUY;
    @Configurable("Instrument")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("Period")
    public Period selectedPeriod = Period.FIFTEEN_MINS;
      @Configurable("Stop Loss In Pips")
    public int StopLossInPips = 5;
   
   
   
   
    public void onStart(IContext context) throws JFException {
       
        engine = context.getEngine();
       console = context.getConsole();
        history = context.getHistory();
        context = context;
       indicators = context.getIndicators();
        userInterface = context.getUserInterface();
        double lastBidPrice = history.getLastTick(instrument).getBid();

        double amount = 0.001;
        double price = 0; //buy at market price
        int slippage = 5;
        double stopLossPrice = getSLPrice(lastBidPrice);
        double takeProfitPrice = 0; //no take profit
       
    }

    private double getSLPrice (double price){
        return cmd.isLong()
            ? price - instrument.getPipValue() * StopLossInPips
            : price + instrument.getPipValue() * StopLossInPips;
    }
 
    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (instrument != this.instrument) {return;}
     
     
        IBar prev = history.getBar(instrument, selectedPeriod, OfferSide.BID, 1);
 
        double price = tick.getBid();
        double high = prev.getHigh();
        double low = prev.getLow();
 
        if (engine.getOrders(instrument).size() == 0) {
            if (price < low) {
                engine.submitOrder("order" + ++counter, instrument, OrderCommand.SELL, 0.1);
            } else if (price > high) {
                engine.submitOrder("order" + ++counter, instrument, OrderCommand.BUY, 0.1);
            }
        }
 
    }
    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        //We can change SL only for FILLED and OPENED orders
        if(order.getState() == IOrder.State.FILLED || order.getState() == IOrder.State.OPENED){
           
            //2) if order has no stop loss - we add it
            if (doubleEquals(order.getStopLossPrice(),0)){
                order.setStopLossPrice(getSLPrice(order.getOpenPrice()));
                return;
            }}
    }
    //we need such function since floating point values are not exact
    private boolean doubleEquals(double d1, double d2){
        //0.1 pip is the smallest increment we work with in JForex
        return Math.abs(d1-d2) < instrument.getPipValue() / 10;
    }
}

I have no idea what is wrong...


 
 Post subject: Re: previous high/low Post rating: 0   New post Posted: Mon 12 Mar, 2012, 10:33 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
you need to use an IEngine.submitOrder method, which passes SL and TP.


 
 Post subject: Re: previous high/low Post rating: 0   New post Posted: Tue 13 Mar, 2012, 22:29 
User avatar

User rating: 1
Joined: Fri 02 Mar, 2012, 13:19
Posts: 31
Location: PolandPoland
no comment... :roll:


 

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