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.

NEED HELP
 Post subject: NEED HELP Post rating: 0   New post Posted: Thu 24 Jan, 2013, 23:04 

User rating: 0
Joined: Wed 13 Jun, 2012, 20:49
Posts: 5
Location: Cote D'Ivoire, ABIDJAN
PLEASE can SOMEONE helpe me wthis this code i want to trade only between 20h gmt and 11h gmt cant someone add this code for me?

package jforex;

import java.util.*;
import java.text.*;
import com.dukascopy.api.*;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IIndicators.AppliedPrice;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.indicators.IIndicator;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;


public class brek implements IStrategy {
private IEngine engine;
private IConsole console;
private IHistory history;
private IAccount ACCOUNT;
private IIndicators indicators;
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
private int counter =0;
private IOrder order = null;
public Instrument instrument= Instrument.EURUSD;
public Period selectedPeriod = Period.FIFTEEN_MINS;
private double rsi;
private double rsi1;
@Configurable("Start hour")
public int fromHour= 19;
@Configurable("end hour")
public int toHour = 11;
private int rsi_period=17;
public double amount=0.01;
public int stopLossPips=130;
public int takeProfitPips=70;
@Configurable("Offer side")
public OfferSide offerSide = OfferSide.BID;
private SimpleDateFormat gmtSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


public void onStart(IContext context) throws JFException {
Set subscribedInstruments = new HashSet();
subscribedInstruments.add(Instrument.EURUSD);
context.setSubscribedInstruments(subscribedInstruments);
this.engine = context.getEngine();
this.console = context.getConsole();
this.history = context.getHistory();
this.indicators = context.getIndicators();
gmtSdf.setTimeZone(TimeZone.getTimeZone("GMT 0"));

}

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 {

}


//use of calendar
private boolean isValidTime(int fromHour, int fromMin, int toHour, int toMin) throws JFException {

long lastTickTime = history.getLastTick(instrument).getTime();
Calendar calendar = Calendar.getInstance();
//you want to work with the date of the last tick - in a case you are back-testing
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
calendar.setTimeInMillis(lastTickTime);
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
long from = calendar.getTimeInMillis();

calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
calendar.setTimeInMillis(lastTickTime);
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
long to = calendar.getTimeInMillis();

print(String.format("calendar: %s - %s last tick: %s", gmtSdf.format(from), gmtSdf.format(to), gmtSdf.format(lastTickTime)));

boolean result = lastTickTime > from && lastTickTime < to;
return result;
}


public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {


if (instrument != Instrument.EURUSD || period != Period.ONE_HOUR) return;

stopLossPips=130;
takeProfitPips=150;
rsi = indicators.rsi(instrument, selectedPeriod, OfferSide.BID, AppliedPrice.CLOSE,rsi_period,1);
rsi1=indicators.rsi(instrument, selectedPeriod, OfferSide.BID, AppliedPrice.CLOSE,rsi_period,2);


//open positions long and short

if (positionsTotal(instrument) == 0) {
if (( rsi1>70 && rsi<70)) {

order = engine.submitOrder(getLabel(instrument), instrument, OrderCommand.SELL,amount,0,3,history.getLastTick(this.instrument).getBid() + getPipPrice(this.stopLossPips),history.getLastTick(this.instrument).getBid() - getPipPrice(this.takeProfitPips));
}
else {
if(( rsi1<30 && rsi>30))
order = engine.submitOrder(getLabel(instrument), instrument, OrderCommand.BUY,amount,0,3,history.getLastTick(this.instrument).getBid() - getPipPrice(this.stopLossPips), history.getLastTick(this.instrument).getAsk() + getPipPrice(this.takeProfitPips));

}
}

//close long order

if ( rsi1<70 && rsi>70) {
if (engine.getOrders().size() > 0) {
for (IOrder orderInMarket : engine.getOrders()) {
if (orderInMarket.isLong()) {
print("Closing Long position");
orderInMarket.close();
}
}
}
}
//close short order

if( rsi1>30 && rsi<30) {
if (engine.getOrders().size() > 0) {
for (IOrder orderInMarket : engine.getOrders()) {
if (!orderInMarket.isLong()) {
print("Closing Short position");
orderInMarket.close();
}
}
}
}
}



private double getPipPrice(int pips) {
return pips *this.instrument.getPipValue();
}

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();
label = label + (counter++);
label = label.toUpperCase();
return label;
}

public void print(String message) {
console.getOut().println(message);
}
}


 

Jump to:  

  © 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