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.

OHLC widget - display indicator values - number of digits
 Post subject: OHLC widget - display indicator values - number of digits Post rating: 1   New post Posted: Fri 10 Aug, 2018, 09:55 
User avatar

User rating: 7
Joined: Thu 05 Sep, 2013, 12:43
Posts: 56
Location: Russian Federation, Tomsk
Hello,
I created custom indicator that calculate average spread. When I display value from indicator to OHLC widget it show me 0.40000

Image

but I need it show only 0.4 without zeros. Indicator buffer value round to 1 digit after point in code but OHLC widget display all digits (instrument.getPipValue() digits). How to display only one digit after point?


 
 Post subject: Re: OHLC widget - display indicator values - number of digits Post rating: 0   New post Posted: Fri 10 Aug, 2018, 10:54 
User avatar

User rating: 13
Joined: Mon 27 Jul, 2015, 16:30
Posts: 110
Location: Canada, Mission
depends how you wrote your strategy, there is something about round decimal


 
 Post subject: Re: OHLC widget - display indicator values - number of digits Post rating: 2   New post Posted: Fri 10 Aug, 2018, 12:00 
User avatar

User rating: 7
Joined: Thu 05 Sep, 2013, 12:43
Posts: 56
Location: Russian Federation, Tomsk
I think it's about the internal formatting of the value in the OHLC widget. I use double type of value and next code:
avgSpread = Math.round(avgSpread * 10.0) / 10.0

Sometimes it displays correct (ex. 0.4) but sometimes not (ex. 0.40000)


 
 Post subject: Re: OHLC widget - display indicator values - number of digits Post rating: 0   New post Posted: Tue 14 Aug, 2018, 00:15 
User avatar

User rating: 13
Joined: Mon 27 Jul, 2015, 16:30
Posts: 110
Location: Canada, Mission
https://www.dukascopy.com/wiki/en/development/strategy-api/practices/rounding-prices


 
 Post subject: Re: OHLC widget - display indicator values - number of digits Post rating: 0   New post Posted: Tue 14 Aug, 2018, 00:56 
User avatar

User rating: 13
Joined: Mon 27 Jul, 2015, 16:30
Posts: 110
Location: Canada, Mission
Here is an example of how roundToPippette works on the spread by calculating only 0.1 increments


import java.math.BigDecimal;
import java.util.Stack;

import com.dukascopy.api.Configurable;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Period;

public class Grider_with_Spread_Control_Rounded_Decimal implements IStrategy {

    private IEngine engine;
    private IConsole console;
    private int counter = 0;
    private IHistory history;
    private ITick previousTick = null;
    private Stack<IOrder> buyOrdrs = new Stack<IOrder>();
    private Stack<IOrder> sellOrdrs = new Stack<IOrder>();

    @Configurable("Step pips")
    public double step = 20;
    @Configurable("Instrument")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("Slippage")
    public double slippage = 0;
    @Configurable("Amount")
    public double amount = 0.1;
    @Configurable("Max Spread To Open Trade")
    public double spreadmax = 0.1;

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

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if (instrument != this.instrument) {
            return;
        }
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
        double lastBidPrice = history.getLastTick(instrument).getBid();
        double lastAskPrice = history.getLastTick(instrument).getAsk();
        double spread = lastAskPrice - lastBidPrice;

        if (instrument != this.instrument) {
            return;
        }
        if (previousTick == null) {
            previousTick = tick;
            return;
        }
        if (tick.getBid() >= previousTick.getBid() + getPipPrice(step) && (roundToPippette(spread, instrument) <= getPipPrice(spreadmax))) {
            if (!buyOrdrs.isEmpty()) {
                closeOrder(buyOrdrs.pop());
            } else {
                sellOrdrs.push(submitOrder(OrderCommand.SELL, instrument));
            }
            previousTick = tick;
        } else if (tick.getAsk() <= previousTick.getAsk() - getPipPrice(step) && (roundToPippette(spread, instrument) <= getPipPrice(spreadmax))) {
            if (!sellOrdrs.isEmpty()) {
                closeOrder(sellOrdrs.pop());
            } else {
                buyOrdrs.push(submitOrder(OrderCommand.BUY, instrument));
            }
            previousTick = tick;
        }
    }

    private IOrder submitOrder(OrderCommand orderCmd, Instrument instrument) throws JFException {

        return engine.submitOrder(getLabel(instrument), instrument, orderCmd, amount, 0, slippage, 0, 0);
    }

    private void closeOrder(IOrder order) throws JFException {
        if (order != null && isActive(order)&&order.getProfitLossInPips() >= step) {
            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;
    }

    private double getPipPrice(double pips) {
        return pips * this.instrument.getPipValue();
    }

    private String getLabel(Instrument instrument) {
        String label = instrument.name();
        label = label + (counter++);
        label = label.toUpperCase();
        return label;
    }

    public void onMessage(IMessage message) throws JFException {
        print(message);
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onStop() throws JFException {
    }

    private void print(Object o) {
        console.getOut().println(o);
    }

    public IHistory getHistory() {
        return history;
    }

    public void setHistory(IHistory history) {
        this.history = history;
    }
   
    private static double roundToPippette(double amount, Instrument instrument) {
        return round(amount, instrument.getPipScale() + 1);
    }

    private static double round(double amount, int decimalPlaces) {
        return (new BigDecimal(amount)).setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}


Attachments:
Grider_with_Spread_Control_Rounded_Decimal.java [4.62 KiB]
Downloaded 139 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: OHLC widget - display indicator values - number of digits Post rating: 0   New post Posted: Sun 19 Aug, 2018, 11:45 
User avatar

User rating: 13
Joined: Mon 27 Jul, 2015, 16:30
Posts: 110
Location: Canada, Mission
https://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=165&t=52911


 

Jump to:  

cron
  © 1998-2024 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