Hi guys, 

Thanks for Reading!

I have been asked a few times where to find info on how to develop strategies in JForex, there are limited resources available online so i thought to help you guys by creating a few basic strategies over here.

There a few different sort of strategies such as:
- trend following strategies
- trend reversal strategies
- scalp strategies

IMHO the easiest strategies to develop are trend following strategies, because usually the take profit and stop loss defined by these strategies are wider than for scalping strategy and therefore their entries points need to be less complex.

Enough Talking let's go through your first piece of code

/*
The following lines are pretty much standard you can just 
copy them if you wish to all your strategies.
*/
package com.pipscity;
import com.dukascopy.api.*;
import java.util.*;
import java.lang.Math;
import java.text.DecimalFormat;
import java.awt.Color;

/*
The following line define the name of your strategy
In Java the Name of the Class "MyFirstStrategy"
also must be the name of the file you are going to save.
so if you create a class named MyFirstStrategy you need to save it as
MyFirstStrategy.java.
*/

public class MyFirstStrategy implements IStrategy {
 
/*
The following lines are pretty standard so don't bother 
too much with them for now
*/
    private IAccount account;
    private IContext context = null;
    private IEngine engine = null;
    private IChart chart = null;
    private IHistory history = null;
    private final Filter indFilter = Filter.ALL_FLATS;
    private IIndicators indicators = null;
    private IConsole console = null;
    public GregorianCalendar  currentDate = new GregorianCalendar(); 
  
/*
 Here is the most important part of this strategy
Usually you are going to define variables/parameters 
of your strategy at the top       

For instance here we got a takeProfit of 50 and a StopLoss of 200
The advantage of such a configuration is that it should work even if the pair you are currently trading on is currently range based.

For instance if the pair keep trending then it's going to hit your 50 pips but if it's stop trending and it stay range based it also has a good probability to hit your 50 pips. 

It is only going to hit your stopLoss if there is a change of trend or huge volatility.

*/

    public int takeProfit=50;
    public int stopLoss=200;

/* 
 The Period chosen is one hour, mind you for trend following strategies you can pretty much use whatever period you want.

We also work on EURUSD as the spread is pretty cheap and for whatever reason seems to be always a good candidate for strategies ,-)

Lot size is 1. The lot size is obviously very important, when testing a strategy live you always want to have your lot size as small as possible to avoid big drawdown.

*/
    public Period per=Period.ONE_HOUR;
    public Instrument instr=Instrument.EURUSD;
    private double lotSize=1;

/*
 Here are the parametes for our entry points
 We will go more in detail below, just remember the number 3h, 9h and 72h (or 3 days)for now
*/    
    private int sma1Period=3;
    private int sma2Period=9;
    private int sma3Period=72;
 
// The following is pretty standard, don't bother too much with it.
    public void onStart(IContext context) throws JFException {
        this.context = context;  
        engine = context.getEngine();
        indicators = context.getIndicators();
        history = context.getHistory();
        console = context.getConsole();
        indicators = context.getIndicators();
          Set subscribedInstruments = new HashSet();
           subscribedInstruments.add(instr);
          context.setSubscribedInstruments(subscribedInstruments);
          
    }

    public void onStop() throws JFException {
    }

//The onTick Method will be the object of another article
   public void onTick(Instrument instrument, ITick tick) throws JFException {
   
    }

// I will explain more in detail the following in another article
    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;
    }

/*
The method onBar start every time a new bar is created on the chart
*/
    public void onBar(Instrument instrument, Period period, IBar askbar, IBar bidbar) throws JFException {

/*
here we are checking that we are trading on the good instrument
If you go back to the top of the article you will find the variable where are have defined instr as Instrument.EURUSD
*/
     
     if(instrument==instr  {  

/*
Then we get the values for our 3 moving average based on the following periods 3h, 9h and 72h. You were supposed to remember these 3 parameters ,-)
*/
        double sma1 = this.indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, sma1Period, 0);
        double sma2 = this.indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, sma2Period, 0);
        double sma3 = this.indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, sma3Period, 0);
        
/* 
We only trade one position at a time so we first check if no trade are open
*/
          if (positionsTotal(instrument) == 0 ) {
             /* 
if the 3h Moving Average cross over the 9h MA and
the current action price is above the 72h MA then we buy  
     */      
                if (sma1>sma2 && sma3
                /*
your typical Buy Order, i will go more in detail with that later.
*/
                 engine.submitOrder("myTrade", instrument, IEngine.OrderCommand.BUY, lotSize, 0, 3, bidbar.getClose()
                        - instrument.getPipValue() *stopLoss, bidbar.getClose() + instrument.getPipValue() * takeProfit);      
                 }
                  else if(sma1bidbar.getClose()){
/* 
if the 3h Moving Average cross below the 9h MA and
the current action price is below the 72h MA then we sell  
     */                      

                      engine.submitOrder("myTrade", instrument, IEngine.OrderCommand.SELL, lotSize, 0, 3, bidbar.getClose()
                        + instrument.getPipValue() *stopLoss, bidbar.getClose() - instrument.getPipValue() * takeProfit);
                  
                  }   
                
           }
    }
 }
    
  public void onMessage(IMessage message) throws JFException {
        
      
}     

  @Override
   public void onAccount(IAccount account) throws JFException {
        this.account = account;
   }
 
}

/* That' if for now, that's already a lot of information to digest so i will get more in detail later with more concept such as optimization.
Thanks for ready!
Cheers,
PipsCity
*/
Translate to English Show original