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.

How to display a comment on a strategy
 Post subject: How to display a comment on a strategy Post rating: 0   New post Posted: Fri 23 Dec, 2011, 12:04 
User avatar

User rating: 0
Joined: Sun 18 Sep, 2011, 09:19
Posts: 7
Location: FranceFrance
Hello,

I use the following strategy to help me in manual trading.

It draw an arrow up when the macd cross up and an arrow down when the arrow cross down.

What I would like is also to "print" in big letters on chart "BUY" when macd cross up until it cross down again and vice versa.

If somebody can help me, i will really appriciate.

package jforex;

import java.io.File;
import com.dukascopy.api.IChartObject;
import com.dukascopy.api.drawings.IChartObjectFactory ;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

import com.dukascopy.api.Configurable;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IChart;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IMessage;
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 com.dukascopy.api.RequiresFullAccess;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IIndicators.MaType;

import java.text.*;
import java.awt.*;


@RequiresFullAccess
public class SIMPLE_MACD_4H implements IStrategy {
    @Configurable("Instrument") public Instrument instrument = Instrument.EURUSD;
    @Configurable("Path to alert sound file") public String pathToAlert = "/Users/Thierry/JForex/Strategies/sound/trentemins.wav";
    public Period timeFrame = Period.FOUR_HOURS;     
    public OfferSide offerSide = OfferSide.BID;
    public AppliedPrice appliedPrice = AppliedPrice.CLOSE;
    public int ma1TimePeriod = 2;
    public MaType ma1Type = MaType.EMA;
    public Period period2 = Period.FOUR_HOURS;
       
    private IConsole console;   
    private IIndicators indicators;   
    private IChart chart;
       
    public void onStart(IContext context) throws JFException {       
        this.console = context.getConsole();               
        this.indicators = context.getIndicators();               
        this.chart = context.getChart(instrument);       
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }

    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(this.instrument) || period != this.timeFrame) return;
       
        double[] Macd,Macdprev,Macdnext;

        Macd =     indicators.macd(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,12,26,9,1);
        Macdprev = indicators.macd(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,12,26,9,2);
        Macdnext = indicators.macd(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,12,26,9,0);
       
        double MaFast = indicators.ma(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,this.ma1TimePeriod,this.ma1Type,0);                 
                         
       
        if (Macdprev[0] < Macdprev[1] && Macd[0] > Macd[1] && Macdnext[0] > Macdnext[1]){
               
                double chartPrice = MaFast-(0.0050);
                chart.draw(Long.toString(System.currentTimeMillis()), IChart.Type.SIGNAL_UP, bidBar.getTime(), chartPrice);
                playSound(pathToAlert, 1);
                // Here I would like to print on the right up corner "BUY"
               

         
        }
       
        if (Macdprev[0] > Macdprev[1] && Macd[0] < Macd[1] && Macdnext[0] < Macdnext[1] ){
               
                double chartPrice = MaFast+(0.0050);
                chart.draw(Long.toString(System.currentTimeMillis()), IChart.Type.SIGNAL_DOWN, bidBar.getTime(), chartPrice);
                playSound(pathToAlert, 1); 
                // Here I would like to print on the right up corner "SELL"         
        }
       
         }
   
    private void playSound(String _sWavFile, int _nRepeats)
    {
       try
       {
          AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(_sWavFile));
          AudioFormat af = audioInputStream.getFormat();
          int nSize = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
          byte[] audio = new byte[nSize];
          DataLine.Info info = new DataLine.Info(Clip.class, af, nSize);
          audioInputStream.read(audio, 0, nSize);

          for(int i=0; i < _nRepeats; i++)
          {
             Clip clip = (Clip)AudioSystem.getLine(info);
             clip.open(af, audio, 0, nSize);
             clip.start();
          }
       }
       catch(Exception e)
       {
          console.getOut().println(e.getMessage());
       }     
    }
   
    private class Popup extends Thread{
        private String message;
        public Popup(String msg){
            this.message = msg;
        }
        public void run(){
            showPopup(this.message);
        }
        private void showPopup(String text) {
            JOptionPane optionPane = new JOptionPane(text, JOptionPane.WARNING_MESSAGE);
            JDialog dialog = optionPane.createDialog("Alert 30 mins");
            dialog.setVisible(true);
        }
    }       
}


 
 Post subject: Re: How to display a comment on a strategy Post rating: 0   New post Posted: Wed 28 Dec, 2011, 15:37 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Here is the strategy:
package jforex.test;

import java.awt.Color;
import java.awt.Font;
import java.io.File;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

import com.dukascopy.api.Configurable;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IChart;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IIndicators.MaType;
import com.dukascopy.api.IMessage;
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 com.dukascopy.api.RequiresFullAccess;

@RequiresFullAccess
public class SIMPLE_MACD_4H_withComment implements IStrategy {
   @Configurable("Instrument")
   public Instrument instrument = Instrument.EURUSD;
   @Configurable("Path to alert sound file")
   public String pathToAlert = "/Users/Thierry/JForex/Strategies/sound/trentemins.wav";
   public Period timeFrame = Period.FOUR_HOURS;
   public OfferSide offerSide = OfferSide.BID;
   public AppliedPrice appliedPrice = AppliedPrice.CLOSE;
   public int ma1TimePeriod = 2;
   public MaType ma1Type = MaType.EMA;
   public Period period2 = Period.FOUR_HOURS;

   private IConsole console;
   private IIndicators indicators;
   private IChart chart;

   public void onStart(IContext context) throws JFException {
      this.console = context.getConsole();
      this.indicators = context.getIndicators();
      this.chart = context.getChart(instrument);
   }

   public void onAccount(IAccount account) throws JFException {
   }

   public void onMessage(IMessage message) throws JFException {
   }

   public void onStop() throws JFException {
   }

   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(this.instrument) || period != this.timeFrame)
         return;

      double[] Macd, Macdprev, Macdnext;

      Macd = indicators.macd(this.instrument, this.timeFrame, this.offerSide, this.appliedPrice, 12, 26, 9, 1);
      Macdprev = indicators.macd(this.instrument, this.timeFrame, this.offerSide, this.appliedPrice, 12, 26, 9, 2);
      Macdnext = indicators.macd(this.instrument, this.timeFrame, this.offerSide, this.appliedPrice, 12, 26, 9, 0);

      double MaFast = indicators.ma(this.instrument, this.timeFrame, this.offerSide, this.appliedPrice, this.ma1TimePeriod, this.ma1Type,
            0);

      if (Macdprev[0] < Macdprev[1] && Macd[0] > Macd[1] && Macdnext[0] > Macdnext[1]) {

         double chartPrice = MaFast - (0.0050);
         chart.draw(Long.toString(System.currentTimeMillis()), IChart.Type.SIGNAL_UP, bidBar.getTime(), chartPrice);
         playSound(pathToAlert, 1);
         // Here I would like to print on the right up corner "BUY"
         setCommentText("BUY", Color.GREEN);
      }

      if (Macdprev[0] > Macdprev[1] && Macd[0] < Macd[1] && Macdnext[0] < Macdnext[1]) {

         double chartPrice = MaFast + (0.0050);
         chart.draw(Long.toString(System.currentTimeMillis()), IChart.Type.SIGNAL_DOWN, bidBar.getTime(), chartPrice);
         playSound(pathToAlert, 1);
         // Here I would like to print on the right up corner "SELL"
         setCommentText("SELL", Color.RED);
      }

   }
   
   private void setCommentText(String text, Color color){
        chart.comment(text);
        chart.setCommentColor(color);
        chart.setCommentHorizontalPosition(SwingConstants.RIGHT);
        chart.setCommentVerticalPosition(SwingConstants.TOP);
        chart.setCommentFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
   }

   private void playSound(String _sWavFile, int _nRepeats) {
      try {
         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(_sWavFile));
         AudioFormat af = audioInputStream.getFormat();
         int nSize = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
         byte[] audio = new byte[nSize];
         DataLine.Info info = new DataLine.Info(Clip.class, af, nSize);
         audioInputStream.read(audio, 0, nSize);

         for (int i = 0; i < _nRepeats; i++) {
            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(af, audio, 0, nSize);
            clip.start();
         }
      } catch (Exception e) {
         console.getOut().println(e.getMessage());
      }
   }

   private class Popup extends Thread {
      private String message;

      public Popup(String msg) {
         this.message = msg;
      }

      public void run() {
         showPopup(this.message);
      }

      private void showPopup(String text) {
         JOptionPane optionPane = new JOptionPane(text, JOptionPane.WARNING_MESSAGE);
         JDialog dialog = optionPane.createDialog("Alert 30 mins");
         dialog.setVisible(true);
      }
   }
}
We tested by backtesting in visual mode over 2011.12.13 - 2011.12.14 and by changing timeFrame variable to ONE_MIN


Attachments:
SIMPLE_MACD_4H_withComment.java [5.12 KiB]
Downloaded 321 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: How to display a comment on a strategy Post rating: 0   New post Posted: Thu 12 Jan, 2012, 07:18 
User avatar

User rating: 0
Joined: Sun 18 Sep, 2011, 09:19
Posts: 7
Location: FranceFrance
Thanks very much. It does work fine.


 

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