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.

Getting Indicator value and sound Alert
 Post subject: Getting Indicator value and sound Alert Post rating: 0   New post Posted: Wed 23 Jun, 2010, 08:39 
User avatar

User rating: 5
Joined: Fri 02 Sep, 2011, 10:08
Posts: 157
Location: FranceFrance
Hi, I've made my first indicator for Jforex.
I'm very new at this.

I want to use the STDDEV to check the market volatility because
I trade only accelerations following a price squeez.

I managed to make my indicator, but yet I want to do this :

- Getting the value of the indicator
- Testing if STDDEV value < 1 and triggering an Alert box showing the Instrument, the period
and the event ("Price squeez").

Last question :
If I want to check the STDDEV of an MACD how can I do this ? I mean taking the MACD
value instead of Price.

Thank a lot for your help.

Here is my STDDEV indicator

package jforex;
import com.dukascopy.api.*;
import com.dukascopy.api.indicators.*;
 
public class ET_Nico implements IIndicator {
    private IConsole console;
    private IContext context;
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;

    private double[][] inputs = new double[1][];
    private int[] timePeriod = new int[3];
    private double[][] outputs = new double[3][];
    private IIndicator ET_Price;
 
    public void onStart(IIndicatorContext context) {
       
        console = context.getConsole();
     
        IIndicatorsProvider indicatorsProvider = context.getIndicatorsProvider();
        ET_Price = indicatorsProvider.getIndicator("STDDEV");
               

        indicatorInfo = new IndicatorInfo("ET_Nico", "Affichage ET Price", "My indicators", false, false, false, 1, 1, 1);   
       
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE)};
        optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("Period", OptInputParameterInfo.Type.OTHER,
                new IntegerRangeDescription(20, 2, 100, 1))};
        outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("ET", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.DrawingStyle.LINE)};

    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
         // Set parameters to ET_Price and calculate it's values:
         
        ET_Price.setInputParameter(0, inputs[0]);
        ET_Price.setOptInputParameter(0, timePeriod[0]);
        ET_Price.setOutputParameter(0, outputs[0]);
        ET_Price.calculate(startIndex, endIndex);

        return ET_Price.calculate(startIndex, endIndex);
    }
 
    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }
 
    public InputParameterInfo getInputParameterInfo(int index) {
        if (index <= inputParameterInfos.length) {
            return inputParameterInfos[index];
        }
        return null;
    }
     // Lookback of the indicator should be biggest lookback of all three ET_Prices.
    public int getLookback() {
        ET_Price.setOptInputParameter(0, timePeriod[0]);
        int ema1Lookback = ET_Price.getLookback();
       
        return ema1Lookback;
    }
 
    public int getLookforward() {
        return 0;
    }
 
    public OptInputParameterInfo getOptInputParameterInfo(int index) {
        if (index <= optInputParameterInfos.length) {
            return optInputParameterInfos[index];
        }
        return null;
    }
 
    public OutputParameterInfo getOutputParameterInfo(int index) {
        if (index <= outputParameterInfos.length) {
            return outputParameterInfos[index];
        }
        return null;
    }
 
    public void setInputParameter(int index, Object array) {
        inputs[index] = (double[]) array;
    }
     // Also change method that saves time period parameters
    public void setOptInputParameter(int index, Object value) {
        timePeriod[index] = (Integer) value;
    }
 
    public void setOutputParameter(int index, Object array) {
        outputs[index] = (double[]) array;
    }
}


 
 Post subject: Re: Getting Indicator value and sound Alert Post rating: 0   New post Posted: Fri 30 Jul, 2010, 08:07 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hi,
We prepared a sample for you, where the returned value by indicator is compared against 1. If it's less than 1, then a popup window appears.
Please consider the folowing code:
 import javax.swing.JDialog;
import javax.swing.JOptionPane;
import com.dukascopy.api.*;

public class TestIndicator implements IStrategy {
   private IEngine engine;
   private IConsole console;
   private IContext context;
   private IIndicators indicators;

   @Configurable("Instrument")
   public Instrument choosedInstrument = Instrument.EURUSD;
   @Configurable("Period")
   public Period selectedPeriond = Period.TEN_SECS;
   @Configurable("STDDEV min value")
   public double stddevMin = 1.0;
   
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.console = context.getConsole();
      this.context = context;
      this.indicators = context.getIndicators();
      this.indicators = context.getIndicators();
   }

   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(choosedInstrument) && period.equals(selectedPeriond)) {
         Object[] outputs = indicators.calculateIndicator(instrument, period, new OfferSide[] { OfferSide.BID },
                     "ET_Nico", new IIndicators.AppliedPrice[] { IIndicators.AppliedPrice.CLOSE }, new Object[] { 20, 20 }, 1);
         for (Object stddevOutput : outputs) {
            double value = (Double) stddevOutput;
            if (value < stddevMin) {
               activateAlert(createAlert(instrument, period, value));
            }
         }
      }
   }

   private Alert createAlert(Instrument instrument, Period period, double indicatorValue) {
      return new Alert(instrument, context.getHistory(), indicatorValue, period);
   }

   private void activateAlert(Alert alert) throws JFException {
      Thread alertThread = new Thread(alert);
      alertThread.start();
   }


   class Alert  extends Thread {
      private Instrument instrument;
      private double indicatorValue;
      private Period period;

      public Alert(Instrument instrument, IHistory priceHistory, double indicatorValue, Period period) {
         this.instrument = instrument;
         this.indicatorValue = indicatorValue;
         this.period = period;
      }

      public Instrument getInstrument() {
         return instrument;
      }

      public void setInstrument(Instrument newInstrument) {
         if (newInstrument != instrument) {
            this.instrument = newInstrument;
         }
      }

      public double getPrice() {
         return indicatorValue;
      }

      public void setPrice(double price) {
         this.indicatorValue = price;
      }

      public String toString() {
            return instrument + "; Period: " + period + "; Price squezze! " + indicatorValue  ;
      }

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

       private void showPopup(String text) {
         JOptionPane optionPane = new JOptionPane(text, JOptionPane.WARNING_MESSAGE);
         JDialog dialog = optionPane.createDialog("Alert");
         dialog.setVisible(true);
      }
   }
}
 
One comment about popup: exchange rate values has small differences, that why you will receive popup after
each bar, but you can change popup trigger value. About using MACD in your indicator, your can’t pass one indicator value as input parameters to another. Instead of it, you can call MACD indicator from your custom indicator:
macd = context.getIndicatorsProvider().getIndicator("MACD"); and then set parameters for macd indicator.  Please see OsMAIndicator class as example viewtopic.php?f=6&t=8379.


 

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