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.

Help developing this simple strategy
 Post subject: Help developing this simple strategy Post rating: 0   New post Posted: Tue 12 Apr, 2011, 20:51 

User rating: -
Hello Dukascopy Support Board and you guys in the forum
I am a novice at Java programing and in Jforex API too so any help is welcomed and my anticipated excuses for any programming violation.
My strategy, or what i mean it to be, is so simple as i will post it here. Basically it is based on pivot points as follows if the opening price is greater that the pivot(central point) calculated with the previous day values, i place a buy order with the shown st loss and tprofit else a sell order is placed. I have make anotations in order to signal my ideas and doubts. Then there goes my strategy:

package jforex;

import java.util.*;

import com.dukascopy.api.*;

import com.dukascopy.api.IEngine.OrderCommand;

import java.text.NumberFormat;

import java.text.SimpleDateFormat;

import java.text.ParseException;

public class myprefFibPivotStrategy implements IStrategy
{
    //Configurable parameters
  @Configurable("Instrument") public Instrument myprefInstrument = Instrument.EURUSD;   
  @Configurable("Amount") public double myprefamount = 0.01;
  //@Configurable("Filter") public Filter mypreffilter = Filter.WEEKENDS;
 private IEngine engine;
   private IConsole console;
   private IHistory history;
   private IContext context;
   private IIndicators indicators;
   private IUserInterface userInterface;
    private IAccount account;
    private int tagcounter = 0;
    private IOrder myprefOrder = null;
    private static double myprefstoplossB = 0;
    private static double myprefstoplossS = 0;
    private static double mypreftakeprofitB = 0;
    private static double mypreftakeprofitS = 0;
    private static long lastTickTime = 0;
    private static double dailyAtrInd = 0;
    private static double myOpenPrice = 0;
    private static long myReferenceTime = 0;
    static SimpleDateFormat myCrazyFormat;
 public static double pivCentPoint = 0 , r1 = 0 , s1 = 0 , r2 = 0, s2 = 0 , r3 = 0, s3 = 0;
   public void onStart(IContext context)
       {
              
      this.engine = context.getEngine();
      this.console = context.getConsole();
      this.history = context.getHistory();
      this.context = context;
      this.indicators = context.getIndicators();
      this.userInterface = context.getUserInterface();
        myCrazyFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        myCrazyFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        }
       

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

   public void onMessage(IMessage message) throws JFException
       {
        }

   //closing opened orders
   public void onStop()
       {
       try   
       {for (IOrder order : engine.getOrders())
       {engine.getOrder(order.getLabel()).close();
        }
        }
        catch (JFException e)
        {
        console.getOut().println("exception thrown on the onstop method");
        e.printStackTrace();
        }
        }

   public void onTick(Instrument instrument, ITick tick) throws JFException
       {
        if (!instrument.equals(myprefInstrument))
        {
            return;
        }
       
     
        }
     }
   //our logic goes on the onBar method
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException   
        {
         if (!instrument.equals(myprefInstrument))
          {
            return;
          }
 for ( int i = 0; i <= 750; i++)
        {
         if ( i == 1 || i == 2 )
            continue;
         else {
             
         
         myReferenceTime = history.getStartTimeOfCurrentBar( myprefInstrument, Period.DAILY);
       
         // //calling the FibPivot indicator method which returns a two dimensional array
         //fibPivot(Instrument instrument, Period period, OfferSide side, int timePeriod, Filter filter, int numberOfCandlesBefore, long time, int numberOfCandlesAfter)
         double [][] myprefFiboPivot = new double [7][4];
          myprefFiboPivot = indicators.fibPivot(myprefInstrument, myprefselectedPeriod, OfferSide.BID, 7, Filter.WEEKENDS, 2,
                           myReferenceTime , 0 );
           pivCentPoint = myprefFiboPivot [0][0];

             r1 = myprefFiboPivot [1][0];
             s1 = myprefFiboPivot [2][0];
             r2 = myprefFiboPivot [3][0];
             s2 = myprefFiboPivot [4][0];
             r3 = myprefFiboPivot [5][0];
             s3 = myprefFiboPivot [6][0];           
 //in order to access to the desire bar(next day bar to compare it  with the pivot.
           
           ArrayList<IBar> myallbars = new ArrayList<IBar>(20); //This returns an arraylist object.
           
           //adding all the retrieved bars with the getBars method to the arraylist
           myallbars.addAll(history.getBars(myprefInstrument, myprefselectedPeriod, OfferSide.BID, Filter.WEEKENDS, 2,
                           myReferenceTime , 0 ));
 IBar solebar = (IBar)myallbars.get(1);
           //accessing to the obtained bar open
           myOpenPrice = solebar.getOpen();
           
           //comparing the open price of the bar with the pivot obtained from the previous day and submitting orders
           if ( myOpenPrice > pivCentPoint )
           {
            do 
            {
              myprefOrder = submitOrder(getLabel(myprefInstrument), myprefInstrument, IEngine.OrderCommand.BUY, 0.01, 0, 2, myformatprefstoplosspriceB( myOpenPrice, dailyAtrInd ),
                             myformatpreftakeprofitpriceB( myOpenPrice, dailyAtrInd ));//, (history.getStartTimeOfCurrentBar(myprefInstrument,Period.DAILY) + 85800000));   
             }
             while (   ( myprefOrder == null )
                    || ( engine.getOrders().size() == 0 )
                    || ( myprefOrder.getState().equals(IOrder.State.CLOSED) )
                     
                   );
           }
           
           //again comparing the opening price of the bar with
           else if ( myOpenPrice < pivCentPoint )
           {
             do 
            {
              myprefOrder = submitOrder(getLabel(myprefInstrument), myprefInstrument, IEngine.OrderCommand.SELL, 0.01, 0, 2, myformatprefstoplossPriceS( myOpenPrice, dailyAtrInd ),
                             myformatpreftakeprofitpriceS( myOpenPrice, dailyAtrInd ));//, (history.getStartTimeOfCurrentBar(myprefInstrument,Period.DAILY) + 85800000));   
            }
             while (   ( myprefOrder == null )
                    || ( engine.getOrders().size() == 0 )
                    || ( myprefOrder.getState().equals(IOrder.State.CLOSED) )
                   
                   );
                 
           }
//if the order fill time plus the 23hrs and 30 mins is less than the time for the ask Bar of the 10 secs period we will close the order(s)
           
           while ( engine.getOrders().size() > 0 )
           {

            long myTimeToRegardClosing = history.getStartTimeOfCurrentBar( myprefInstrument, Period.TEN_SECS );
             
              for (IOrder orderInMarket : engine.getOrders())
              {
                if (( orderInMarket.getFillTime() + ( 23 * 60 * 60 * 1000 + 1800000 )) < myTimeToRegardClosing )//adding 23hrs and 30 mins to the order filled time so that we know that almost 24 hrs has
                                                                                                                //has gone by and thus the order(s) should be closed. 
                {
                    if ( ! ( (orderInMarket.getState().equals(IOrder.State.CLOSED ))
                             || ( orderInMarket.getState().equals(IOrder.State.CANCELED))
                             || (orderInMarket.getState().equals(IOrder.State.CREATED))
                             || (orderInMarket.getState().equals(IOrder.State.OPENED))
                           )
                       ) 
 
                  orderInMarket.close();
                }   
              } //close of the inner for loop which checks for orderInMarket
             
             
            }/
 }//close of the else statement which is within the outer for loop. This else statement check that i!=1 nor 2
          }//close of the for loop (outer) that increments i and serves as a general counter of the days indirectly and its iterator(i) serves us to skip day 1 and 2
               
          } //close of the onbar method brace             
               
                     
      //A method for the orders creation submission and return
     
     private IOrder submitOrder(String label, Instrument instrument, IEngine.OrderCommand orderCommand, double amount, double price, double slippage,
                                 double stopLossPrice, double takeProfitPrice) throws JFException 
      {
          return engine.submitOrder(getLabel(myprefInstrument), myprefInstrument, orderCommand, amount, price, slippage, stopLossPrice, takeProfitPrice);
       
       }     
    //stoploss calculation method for buy orders       
        private static double myformatprefstoplosspriceB( double myOpenPrice, double pivot(or x value ))
                {                   
               
                myprefstoplossB = myOpenPrice - ( pivot(or x value)* stLossPercent);
                NumberFormat myFormattedSLossB = NumberFormat.getNumberInstance();
                myFormattedSLossB.setMaximumFractionDigits(4);
                myFormattedSLossB.setMinimumFractionDigits(4);
                return Double.parseDouble(myFormattedSLossB.format(myprefstoplossB));
               
                }
               
                //targetprofit calculation and formatting for buy orders
               
        private static double myformatpreftakeprofitpriceB( double myOpenPrice, double pivot(or x value ) )
                {
               
                mypreftakeprofitB = myOpenPrice + ( pivot( or x value)  * tgProfitPercent);
                NumberFormat myFormattedTProfB = NumberFormat.getNumberInstance();
                myFormattedTProfB.setMaximumFractionDigits(4);
                myFormattedTProfB.setMinimumFractionDigits(4);
                return Double.parseDouble(myFormattedTProfB.format(mypreftakeprofitB));
                                             
                }
  //stoploss calculation and formatting for selling
           
        private static double myformatprefstoplossPriceS( double myOpenPrice, double pivot(or x value ) )
               {
                myprefstoplossS = myOpenPrice + ( pivot( or x value) * stLossPercent );
                NumberFormat myFormattedSLossS = NumberFormat.getNumberInstance();
                myFormattedSLossS.setMaximumFractionDigits(4);
                myFormattedSLossS.setMinimumFractionDigits(4);
                return Double.parseDouble(myFormattedSLossS.format(myprefstoplossS));
               }
               
               //targetprofit calculation and formatting for selling
               
       private static double myformatpreftakeprofitpriceS( double myOpenPrice, double pivot(or x value )
               {
                mypreftakeprofitS = myOpenPrice - ( pivot(or x value ) * tgProfitPercent );
                NumberFormat myFormattedTProfS = NumberFormat.getNumberInstance();
                myFormattedTProfS.setMaximumFractionDigits(4);
                myFormattedTProfS.setMinimumFractionDigits(4);
                return Double.parseDouble(myFormattedTProfS.format(mypreftakeprofitS));
               }
         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.substring(0, 2) + label.substring(3, 5);
            label = label + (tagcounter++);
            label = label.toLowerCase();
            return label;
          }
}

Thats it, i cannot get it done here the strategy does compile but once when i try to run it does nothing. Any suggerence would be welcomed again.
Thanks in advance to all of you;
jahzlone


 
 Post subject: Re: Help developing this simple strategy Post rating: 0   New post Posted: Thu 09 Jun, 2011, 13:26 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The strategy does not compile, could you please provide a version of it which you managed to compile?


 

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