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.

Buy/Sell Reverse Problem
 Post subject: Buy/Sell Reverse Problem Post rating: 0   New post Posted: Mon 25 Feb, 2013, 08:02 
User avatar

User rating: 0
Joined: Mon 14 May, 2012, 06:56
Posts: 6
Location: Italy, Rome
Hi Support,

I have to issues regarding my code that I hope you could help me out with:

- the strategy doesn't open the reversed position when the candle cross through the sma (as shown in the code below);
- the for(IOrder o: engine.getOrders()){System.out.println(o.getClosePrice());}; shows only 0.0;

Thank you for your precious help!


package jforex;

import com.dukascopy.api.*;
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.OrderCommand;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IIndicators;
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 java.io.*;
import java.io.PrintStream;
import java.text.*;
import java.text.DecimalFormat;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

public class EasySMA implements IStrategy{
    private IEngine engine;
    private IHistory history;
    private IIndicators indicators;
    private IOrder order;
    private IConsole console;
    private int counter = 0;
    private PrintStream print;
    public static DecimalFormat df = new DecimalFormat("0.0000");
   
    public Instrument instrument = Instrument.EURUSD;
    public Period selectedPeriod = Period.ONE_HOUR;
    public Filter indicatorFilter = Filter.ALL_FLATS;
    public double amount = 0.02;
    public int stopLossPips = 50;
    public int takeProfitPips = 190;
    public int smaTimePeriod = 200;
    public String label;
   
   
        public void onStart(IContext context)throws JFException{
            console = context.getConsole();
            this.engine = context.getEngine();
            this.history = context.getHistory();
            this.indicators = context.getIndicators();
            System.out.println("System Test");
        }
       
       
        public void onAccount (IAccount account)throws JFException{
        }
        public void onMessage (IMessage message)throws JFException{
        }
       
        public void onStop() throws JFException{
            for (IOrder order : engine.getOrders()){
                engine.getOrder(order.getLabel()).close();
                }
        }
        public void onTick(Instrument instrument,ITick tick)throws JFException{
                     }
       
        public void onBar(Instrument instrument,Period period,IBar askbar, IBar bidBar)throws JFException{
           
            if(!instrument.equals(Instrument.EURUSD)||!period.equals(Period.ONE_HOUR))
                return;
           
            IBar prevBar = history.getBar(instrument, selectedPeriod, OfferSide.BID, 1);
            IBar currBar = history.getBar(instrument, selectedPeriod, OfferSide.BID, 0);
                                 
            double[] sma = indicators.sma(this.instrument, selectedPeriod, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, smaTimePeriod,indicatorFilter,200,prevBar.getTime(),0);
           
           
            if (prevBar.getOpen()< sma[199] && prevBar.getClose()> sma[199]){for(IOrder o : engine.getOrders()){if(o.getState()== IOrder.State.FILLED){o.close();}};submitOrder(OrderCommand.BUY);System.out.println("BUY");}
            if (prevBar.getOpen()> sma[199] && prevBar.getClose()< sma[199]){for(IOrder o : engine.getOrders()){if(o.getState()== IOrder.State.FILLED){o.close();}};submitOrder(OrderCommand.SELL);System.out.println("SELL");}
           
           
            long time = currBar.getTime();
            Date d = new Date(time);
           
            System.out.println(d);
            System.out.println("sma[0]: " + sma[0]);
            System.out.println("sma[1]: " + sma[1]);
            System.out.println("sma[2]: " + sma[2]);
            System.out.println("sma[198]: " + sma[198]);
            System.out.println("sma[199]: " + sma[199]);

            System.out.println("Active orders: " + engine.getOrders());
            System.out.println(label);
            for(IOrder o: engine.getOrders()){System.out.println(o.getClosePrice());};
           
            }
           
           
                       
               
               
               
        private IOrder submitOrder(OrderCommand orderCmd) throws JFException{
         
            double stopLossPrice, takeProfitPrice;
           
            if (orderCmd==OrderCommand.BUY){stopLossPrice = history.getLastTick(this.instrument).getBid()-getPipPrice(this.stopLossPips);
            takeProfitPrice = history.getLastTick(this.instrument).getBid()+getPipPrice(this.takeProfitPips);}
            else
            {stopLossPrice = history.getLastTick(this.instrument).getAsk()+getPipPrice(this.stopLossPips);
            takeProfitPrice = history.getLastTick(this.instrument).getAsk()-getPipPrice(this.takeProfitPips);}
           
         
        return engine.submitOrder(getLabel(instrument),this.instrument,orderCmd,this.amount,0,20,stopLossPrice,takeProfitPrice,0);
            }
       
        protected String getLabel(Instrument instrument){
            label = instrument.name();
            label = label + (counter++);
            label = label.toUpperCase();
            return label;
        }
       
        private double getPipPrice(int pips){
            return pips * this.instrument.getPipValue();
        }
      }
     


 
 Post subject: Re: Buy/Sell Reverse Problem Post rating: 0   New post Posted: Mon 25 Feb, 2013, 08:24 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
JackTheDog wrote:
- the strategy doesn't open the reversed position when the candle cross through the sma (as shown in the code below);
Consider logging all values that are used to detect the order submit condition to figure out why the strategy does not trade by your algorithm:
https://www.dukascopy.com/wiki/#IConsole/Logging_values
JackTheDog wrote:
- the for(IOrder o: engine.getOrders()){System.out.println(o.getClosePrice());}; shows only 0.0;
See javadocs for:
https://www.dukascopy.com/client/javadoc/com/dukascopy/api/IEngine.html#getOrders()
and
https://www.dukascopy.com/client/javadoc/com/dukascopy/api/IOrder.html#getClosePrice()
For order history use the following methods:
https://www.dukascopy.com/wiki/#Order_history


 

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