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.

help novice Java programming. Please.
 Post subject: help novice Java programming. Please. Post rating: 0   New post Posted: Sat 24 Dec, 2011, 02:05 
User avatar

User rating: 0
Joined: Sat 24 Dec, 2011, 01:33
Posts: 1
Location: Spain, sevilla
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));
            }
                                   
        }
       
    }
}


 
 Post subject: Re: help novice Java programming. Please. Post rating: 0   New post Posted: Tue 27 Dec, 2011, 15:49 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please consider taking a look at the example martingale strategy in JForex wiki: https://www.dukascopy.com/wiki/ section Strategy API/Strategies/Martingale with MA


 
 Post subject: Re: help novice Java programming. Please. Post rating: 0   New post Posted: Sun 24 Mar, 2013, 16:45 

User rating: 0
Joined: Sun 17 Mar, 2013, 07:05
Posts: 3
I had downloaded this EA (martingaleWithMA) but when compling it , everytime error message appear saying
( OrderCommand cannot be resolved ) , I am interested in this EA , Please help.


Attachments:
MartingaleWtihMA.java [4.86 KiB]
Downloaded 295 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: help novice Java programming. Please. Post rating: 0   New post Posted: Mon 25 Mar, 2013, 09:04 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
the example attached to the article has been updated.


 

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