Hola,
No sé si esto te servirá... Cuando estaba probando cosas con anillos me hice una función para calcular el valor del pip según los lotes, para saber que lotes poner para que todas las patas tuvieran el mismo valor por pip... Total que este es el código de una eegia que hice para probar que me salía lo mismo que en la calculadora de dukas... En general iba bastante bien creo recordar, a veces descuadraban por decimales, y creo que en algunos pares daba la impresión de descuadrar los decimales siempre más que en otros ¿?. Pero prueba, a lo mejor te sirve, y si alguien la mejora o lo que sea... Por cierto las cantidades se ponen en millones, como en la calculadora... si quieres ponerlas en otro formato tendrás que tocar un valor que verás en la fórmula que tiene 1000000. Y si quieres visualizar más decimales, en los prints puedes cambiar .2f por .4f por ejemplo. De todas formas eso sólo lo cambia en la impresión no en los cálculos...
Código:
package jforex;
import java.util.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import com.dukascopy.api.*;
public class valorPip implements IStrategy {
@Configurable("Instrument 1") public Instrument eInstrument1 = Instrument.EURUSD;
@Configurable("Cantidad 1") public double eAmount1 = 0.01;
private IEngine engine;
private IConsole console;
private IHistory history;
private IContext context;
private IIndiors indiors;
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.console = context.getConsole();
this.history = context.getHistory();
this.context = context;
this.indiors = context.getIndiors();
console.getOut().println(String.format("En : %s %.2f = %.2f", context.getAccount().getCurrency(), eAmount1, getPipValueInAccountCurrency(eInstrument1,eAmount1)));
console.getOut().println(String.format("En : %s %.2f = %.2f", eInstrument1.getPrimaryJFCurrency(), eAmount1, getPipValue(Currency.getInstance(eInstrument1.getPrimaryJFCurrency().toString()),eInstrument1,eAmount1)));
console.getOut().println(String.format("En : %s %.2f = %.2f", eInstrument1.getSecondaryJFCurrency(), eAmount1, getPipValue(Currency.getInstance(eInstrument1.getSecondaryJFCurrency().toString()),eInstrument1,eAmount1)));
console.getOut().println(String.format("En : %s %.2f = %.2f", "USD", eAmount1, getPipValue(Currency.getInstance("USD"),eInstrument1,eAmount1)));
context.stop();
}
public void onAccount(IAccount account) throws JFException {
}
public void onMessage(IMessage message) throws JFException {
}
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 {
}
//Calcula el valor del pip de un par y unidades en una moneda dada...
public double getPipValue(Currency c, Instrument i, double a)throws JFException{
Instrument i2;
double unidades = 1000000;
if(c==i.getSecondaryCurrency())
return i.getPipValue()*a*unidades;
else if (c==i.getPrimaryCurrency())
return (i.getPipValue()*a*unidades)/history.getLastTick(i).getBid();
else{
if ((i2 = Instrument.fromString(i.getSecondaryCurrency()+"/"+c))==null)
i2 = Instrument.fromInvertedString(i.getSecondaryCurrency()+"/"+c);
return (i.getPipValue()*a*unidades)/history.getLastTick(i2).getBid();
}
}
//Calcula el valor del pip de un par y unidades en la moneda de la cuenta...
public double getPipValueInAccountCurrency(Instrument i, double a)throws JFException{
return getPipValue(context.getAccount().getCurrency(), i, a);
}
//Cuando los parámetros de cantidad 2 y 3 están a 0, se calculan para que el valor del pip sea constante en todos las patas...
public double calculaLotesHedge( double pLotes, double pValorPip1, double pValorPip2){
return (( pLotes * pValorPip1 )/pValorPip2)<0.001?0.001:(( pLotes * pValorPip1 )/pValorPip2);
}
}