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.

MinMax returning strange values
 Post subject: MinMax returning strange values Post rating: 0   New post Posted: Thu 27 Jun, 2013, 01:42 
User avatar

User rating: 0
Joined: Mon 24 Jun, 2013, 01:13
Posts: 3
Location: GermanyGermany
Hello,

maybe someone can help me here. I am trying to implement a strategy based on the minmax indicator. I might have to say, that I am new to the JForex API, but I am an experienced Java programmer. The strategy is fairly simple. If there is a new high, I go long, vice versa on selling. When I run the history tester, it seems that for some reasons the min (max) value is ignored.

You can clearly see what I mean when you look at the picture. A buy (sell) order is only generated with a new high(low), however the strategy is placing new orders although I am clearly in an uptrend. How can this be possible? FYI: The minimum is calculated on a 55 bars thirty Minute base. The picture displays one bar as an hour.

You will find the complete source code below.
Let me just point out the area where the minimum is calculated.

If you have any idea how this issue can be fixed please let me know.
Thank you in advance for your help

Tag

    private void calculateMin()throws JFException{
       
        long prevBarTime = history.getBar(instrument, Period.THIRTY_MINS, OfferSide.BID, 1).getTime();
        double[][]minArray = indicators.minMax(instrument, Period.THIRTY_MINS, OfferSide.ASK, AppliedPrice.LOW, 55, Filter.WEEKENDS, prevBarTime, prevBarTime);

        if(minArray[0].length > 0){
            this.min = minArray[0][0];
        }
       
    }



import com.dukascopy.api.Filter;
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.IIndicators;
import com.dukascopy.api.IIndicators.AppliedPrice;
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.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.RequiresFullAccess;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 *
 * @author Daniel
 * @version 0.1
 * Copyrights Daniel
 */
@RequiresFullAccess
public class FirstStrategy implements IStrategy{

    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IAccount account;
   
    Instrument instrument = Instrument.EURUSD;
    private List<Instrument> instruments  = Arrays.asList(instrument);
   
    private double max = 10000;
    private double min = -10000;
    private boolean hasActiveOrder = false;
   
    @Override
    public void onStart(IContext context) throws JFException {
        setGlobals(context);
        calculateMinMax();
        console.getOut().println("Greetings Old Friend");
    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {
       double ask = tick.getAsk();
       
       if(ask > max){
           if(!hasActiveOrder()){
              placeLongOrder(tick);
           }
       }
       
       if(ask < min){
           if(!hasActiveOrder()){
              placeShortOrder(tick);
           }           
       }

    }

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        calculateMinMax();
    }

    @Override
    public void onMessage(IMessage message) throws JFException {
       
    }

    @Override
    public void onAccount(IAccount account) throws JFException {
       
    }

    @Override
    public void onStop() throws JFException {
     
    }
   
    private boolean hasActiveOrder() throws JFException{
        IOrder orderLong = engine.getOrder("WideLong");
        if(orderLong != null){
            return true;
        }
       
        IOrder orderShort = engine.getOrder("WideShort");
        if(orderShort != null){
            return true;
        }
        return false;
    }
   
    /**
     * This here is not working yet properly - some debugging has to be done
     * @param tick
     * @throws JFException
     */
    private void placeLongOrder(ITick tick) throws JFException{
        double ask = tick.getAsk();
        double stopLoss = ask - 0.001;
        double takeProfit = ask + 0.0100;
       
        double lastBidPrice = history.getLastTick(instrument).getAsk();
 
        IOrder order = engine.submitOrder("WideLong", instrument, OrderCommand.BUY, 0.01, lastBidPrice, 2, stopLoss, takeProfit);
       
 
    }
   
    private void placeShortOrder(ITick tick) throws JFException{
        double bid = tick.getBid();
        double stopLoss = bid + 0.0005;
        double takeProfit = bid - 0.0100;
        double lastBidPrice = history.getLastTick(instrument).getBid();
        IOrder order = engine.submitOrder("WideShort", instrument, OrderCommand.SELL, 0.01, lastBidPrice, 2, stopLoss, takeProfit);
    }

    private void calculateMinMax()throws JFException{
         calculateMin();
         calculateMax();
    }
   
    private void calculateMin()throws JFException{
       
        long prevBarTime = history.getBar(instrument, Period.THIRTY_MINS, OfferSide.BID, 1).getTime();
        double[][]minArray = indicators.minMax(instrument, Period.THIRTY_MINS, OfferSide.ASK, AppliedPrice.LOW, 55, Filter.WEEKENDS, prevBarTime, prevBarTime);

        if(minArray[0].length > 0){
            this.min = minArray[0][0];
        }
       
    }
   
     private void calculateMax()throws JFException{
         long prevBarTime = history.getBar(instrument, Period.THIRTY_MINS, OfferSide.ASK, 1).getTime();
         double[][] maxArray = indicators.minMax(instrument, Period.THIRTY_MINS, OfferSide.ASK, AppliedPrice.HIGH, 55, Filter.WEEKENDS, prevBarTime, prevBarTime);
         

         if(maxArray[0].length > 0 ){
            this.max = maxArray[1][0];
         }

     }
     
    private void setGlobals(IContext context){
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators(); 
        this.account = context.getAccount();
        context.setSubscribedInstruments(new HashSet<Instrument>(instruments));
    } 
   
}



Image


Attachments:
Problem.JPG [23.22 KiB]
Downloaded 526 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: MinMax returning strange values Post rating: 0   New post Posted: Thu 27 Jun, 2013, 15:58 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Consider logging the relevant values to see why particular execution paths get taken, see:
https://www.dukascopy.com/wiki/#IConsole/Logging_values


 

Jump to:  

  © 1998-2026 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