I need help with java programming JForex. I'm starting with java programming.
I have a martingale type strategy. I need from a particular batch of trade output is equal to 0 (no loss, no gain) with respect to lot cycle start from the first lot. In the example of java code, the first lot to 0.001 and the balance lot is from 0.064 or 0.128 or 0.256, etc when the condition no gain, no loss. Can you please help me with the code?
THANKS
package jforex;
import java.util.*;
import com.dukascopy.api.*;
public class StrategyCizihourGAN implements IStrategy {
//---- PARAM
@Configurable("cLot") public double cLot;
@Configurable("vw") public int vw;
@Configurable("xw") public int xw;
@Configurable("sl") public double sl;
@Configurable("tp") public double tp;
@Configurable("lb") public double lb;
@Configurable("winit") public int winit;
@Configurable("_UseHourTrad")public int _UseHourTrade=0;
@Configurable("_FromHourTrade")public int _FromHourTrade=5;
@Configurable("_ToHourTrade")public int _ToHourTrade=15;
//----
private int counter;
private int w;
private double varLot;
[u]private double getEquity;
private double getBalance; [/u]
private HashMap Orders;
//private HashMap varLots;
private IOrder order;
private IAccount account;
private IEngine engine;
private IConsole console;
private IHistory history;
private IContext context;
private IIndicators indicators;
private IUserInterface userInterface;
public int _hours;
public int _minutes;
public long _signalBarTime = 1;
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.console = context.getConsole();
this.history = context.getHistory();
this.context = context;
this.indicators = context.getIndicators();
this.userInterface = context.getUserInterface();
counter=0;
Orders= new HashMap();
//varLots= new HashMap();
// console.getOut().println("pepeillo");
order=null;
}
public IOrder Buy(Instrument instrument, double amount, double price, double stopLoss, double takeProfit) throws JFException
{
String OrLabel="B"+counter+++String.valueOf(new Date().getTime());
IOrder or= engine.submitOrder(OrLabel, instrument, IEngine.OrderCommand.BUY, amount, price, 5);
for(int i=0;i<5;i++)
{
or.waitForUpdate(5000);
if(or.getState() == IOrder.State.FILLED)
break;
}
if(or.getState() == IOrder.State.FILLED)
{
double op=or.getOpenPrice();
or.setStopLossPrice(op-stopLoss*instrument.getPipValue());
or.setTakeProfitPrice(op+takeProfit*instrument.getPipValue());
or.waitForUpdate(5000);
}else return null;
return or;
}
public IOrder Sell(Instrument instrument, double amount, double price, double stopLoss, double takeProfit) throws JFException
{
String OrLabel="S"+counter+++String.valueOf(new Date().getTime());
IOrder or= engine.submitOrder(OrLabel, instrument, IEngine.OrderCommand.SELL, amount, price, 5);
/*while (or.getState() != IOrder.State.FILLED && or.getState() != IOrder.State.CANCELED)
{
order.waitForUpdate(30000);
}*/
for(int i=0;i<5;i++)
{
or.waitForUpdate(5000);
if(or.getState() == IOrder.State.FILLED)
break;
}
if(or.getState() == IOrder.State.FILLED)
{
double op=or.getOpenPrice();
or.setStopLossPrice(op+stopLoss*instrument.getPipValue());
or.setTakeProfitPrice(op-takeProfit*instrument.getPipValue());
or.waitForUpdate(5000);
} else return null;
return or;
}
public void onAccount(IAccount account) throws JFException {
this.account=account;
[u]if (varLot>=0.064) {
if (getEquity >= getBalance) {
order.close();
return; [/u]
}
}
}
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
{
_signalBarTime=bidBar.getTime();
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.setTimeInMillis(_signalBarTime);
_hours=calendar.get(Calendar.HOUR_OF_DAY);
_minutes=calendar.get(Calendar.MINUTE);
if (varLot==0.001)
if (_UseHourTrade==1) {
if (!(_hours >= _FromHourTrade && _hours <= _ToHourTrade)) {
return;
}
}
if(bidBar.getOpen()==bidBar.getClose() || bidBar.getVolume()==0)
return;
// if(instrument!=Instrument.EURUSD)
//return;
//(account.getEquity()/1000000)*cLot;
if(period == Period.ONE_MIN)
{
order=(IOrder)Orders.get(instrument);
if(order==null)
for (IOrder o : engine.getOrders(instrument))
{
order=o;Orders.put(instrument,o);break;
}
//if(order==null)
//console.getOut().println("NULL");
if(order==null)// || true)
{
Calendar c = Calendar.getInstance();c.add(Calendar.DATE, -7);
// console.getOut().println("f"+c.getTime().getTime());
// console.getOut().println("t"+new Date().getTime());
for(IOrder o : history.getOrdersHistory(instrument,c.getTime().getTime(),new Date().getTime()))
{
//order=or;
console.getOut().println("o"+o.getFillTime());
}
}//order.getState()==IOrder.State.CLOSED
//console.getOut().println(varLot);
if(order==null || order.getState()==IOrder.State.CLOSED)
{
//Double lv=(Double)varLots.get(instrument);
//if(lv!=null)
// varLot=lv.doubleValue();
//else
if(order!=null)
{
//varLot=0;
varLot=order.getRequestedAmount();
double lp=order.getProfitLossInPips();
if(lp>0)//(order.isLong() && order.getOpenPrice()<order.getClosePrice()) || (!order.isLong() && order.getOpenPrice()>order.getClosePrice()) )
{
// cLot*=1.2+varLot/5;// <------ N/U
varLot=lb;console.getOut().println("V"+lp);
if(order.isLong())w=vw; else w=xw;
//lb+=cLot;// <------ N/U
}
else
{
// cLot*=0.8+varLot/5; // <------ N/U
varLot*=cLot;
console.getOut().println("X"+lp);
if(order.isLong()) w=xw; else w=vw;
//lb-=cLot; // <------ N/U
}
}else
{
varLot=lb;
w=winit;
}
//if(varLot>=0.0012) varLot*=5;
if(w==1)
{
Orders.put(instrument,Buy(instrument,varLot,0,sl,tp));
}else
{
Orders.put(instrument,Sell(instrument,varLot,0,sl,tp));
}
//console.getOut().println(varLot);
//varLots.put(instrument, new Double(varLot));
}
}
}
}