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.

One strategy - different behavior in real and demo trading
 Post subject: One strategy - different behavior in real and demo trading Post rating: 0   New post Posted: Fri 14 Sep, 2012, 10:25 
User avatar

User rating: 0
Joined: Wed 20 Jul, 2011, 05:45
Posts: 20
Location: BulgariaBulgaria
Strategy generates a lot of position with different currency pairs and is set to deal with Eur / usd only. There is no problem in demo trading, it happens in real trade only.


 
 Post subject: Re: One strategy - different behavior in real and demo trading Post rating: 0   New post Posted: Fri 14 Sep, 2012, 11:09 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
please provide the strategy source code


 
 Post subject: Re: One strategy - different behavior in real and demo trading Post rating: 0   New post Posted: Mon 17 Sep, 2012, 10:02 
User avatar

User rating: 0
Joined: Wed 20 Jul, 2011, 05:45
Posts: 20
Location: BulgariaBulgaria
here is the strategy




package singlejartest;

import com.dukascopy.api.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IIndicators.MaType;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.OfferSide;

public class test implements IStrategy {
   
 
    private IEngine engine = null;
    private IIndicators indicators = null;
    private IConsole console = null;
    private double volume = 0.01;
    private int profitLimit = 100;
    private int lossLimit = 150;
    private double bidPrice;
    private double askPrice;
    public Instrument instrument= Instrument.EURUSD;
    public Period period = Period.FIFTEEN_MINS;
    public IOrder marketorder;
    public boolean tradeallowed;
 
    public void onStart(IContext context) throws JFException {
         Set<Instrument> subscribedInstruments = new HashSet<Instrument>();
         subscribedInstruments.add(Instrument.EURUSD);
         context.setSubscribedInstruments(subscribedInstruments);
        engine = context.getEngine();
        indicators = context.getIndicators();
        context.getHistory();
        console = context.getConsole();
        indicators = context.getIndicators();
     
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
           if (instrument != Instrument.EURUSD || period != Period.FIFTEEN_MINS) return;       

    }

    // opened positions counted
    protected int positionsTotal(Instrument instrument) throws JFException {
        int counter = 0;
        for (IOrder order : engine.getOrders(instrument)) {
            if (order.getState() == IOrder.State.FILLED) {
                counter++;
            }
        }
        return counter;
    }

    protected String getLabel(Instrument instrument) {
        String label = instrument.name();
       
        long time = new java.util.Date().getTime();

        label = label.substring(0, 2) + label.substring(3, 5);
        label = label + time;
        label = label.toLowerCase();
        return label;
    }

    public void onBar(Instrument instrument, Period period, IBar askbar, IBar bidbar) throws JFException {
       

  if(period == Period.FIFTEEN_MINS) {
     // Time of opening trade
       Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT 0"));
        int DayOfWeek=calendar.get(Calendar.DAY_OF_WEEK);
            int Hour=calendar.get(Calendar.HOUR_OF_DAY);
       
        if (Hour>9 && Hour<16){
            tradeallowed = true;
            }
            else {
                tradeallowed = false;
            }
      // End of opening trade
        profitLimit = 10;
        lossLimit = 250;
             
                     
      if (askbar.getVolume() == 0) return;
     
             
             askPrice = askbar.getClose();
             bidPrice = bidbar.getClose();
               
double tvs = this.indicators.tvs(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 18, 0);
double tvs1 = this.indicators.tvs(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 18, 1);
double sma14 = this.indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 14, 0);
double sma14h = this.indicators.sma(instrument, Period.FOUR_HOURS, OfferSide.BID, IIndicators.AppliedPrice.CLOSE,14, 0);
double sma14ha = this.indicators.sma(instrument, Period.ONE_HOUR, OfferSide.BID, IIndicators.AppliedPrice.CLOSE,14, 0);
double rsi = indicators.rsi(instrument, Period.ONE_HOUR, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 14, 0);
               
       if (positionsTotal(instrument) == 0) {
           
            if (bidPrice > sma14  && tvs > 0 &&  tvs1 > 0 && tvs > tvs1 ) {
                     
               
                 marketorder = buy(instrument, engine, profitLimit, lossLimit, volume);
                 
                 }
               
                else  if (bidPrice < sma14  && tvs < 0 &&  tvs1 < 0 && tvs < tvs1 ) {
                   
               marketorder = sell(instrument, engine, profitLimit, lossLimit, volume);   
                   
                  }   
          }
         
           }
           }
         
   
   

  public void onMessage(IMessage message) throws JFException {
       
        if (message != null && message.getType() == IMessage.Type.ORDER_CLOSE_OK) {
           
            IOrder lastOne = message.getOrder();
           
            double profitsLoss = lastOne.getProfitLossInPips();
           
            console.getOut().println("Order : "+lastOne.getLabel()+ " "+ lastOne.getOrderCommand()+ " Pips: "+profitsLoss);
           
           
        }
}
     





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

   }
    public IOrder sell(Instrument instrument, IEngine engine, int takeProfitPipLevel, int endLossPipLevel, double volumeParam)  throws JFException {
       
        return engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.SELL, volumeParam, 0, 3, bidPrice
                        + instrument.getPipValue() *endLossPipLevel, bidPrice - instrument.getPipValue() * takeProfitPipLevel);
     }
     
      public IOrder buy(Instrument instrument, IEngine engine, int takeProfitPipLevel, int endLossPipLevel, double volumeParam)  throws JFException {
       
         return engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.BUY, volumeParam, 0, 3, askPrice
                        - instrument.getPipValue() * endLossPipLevel, askPrice + instrument.getPipValue() * takeProfitPipLevel);
     }
}


 
 Post subject: Re: One strategy - different behavior in real and demo trading Post rating: 0   New post Posted: Wed 19 Sep, 2012, 14:19 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The problem could be that in onBar you filter only the period, presumably you want to filter the instrument as well, see:
https://www.dukascopy.com/wiki/#Filter_Ticks_Bars


 

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