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.

Draw line at High+Low/2
 Post subject: Draw line at High+Low/2 Post rating: 0   New post Posted: Tue 25 Oct, 2011, 23:27 

User rating: 0
Joined: Wed 12 Oct, 2011, 22:39
Posts: 3
Location: US
High, I was looking for an indicator that would draw a line at the midpoint of the bar; basically (High+Low)/2, with a number next to it, giving the value.

__
| |
| |
| |
------ 1.4324
| |
| |
|__|

That's what it would look like, (sorry for the bad ASCII art). Thanks.


 
 Post subject: Re: Draw line at High+Low/2 Post rating: 0   New post Posted: Wed 26 Oct, 2011, 16:50 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Try the following:
package charts.test;

import java.awt.Color;
import java.text.DecimalFormat;

import com.dukascopy.api.Configurable;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IChart;
import com.dukascopy.api.IChartObject.ATTR_INT;
import com.dukascopy.api.IContext;
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.Period;
import com.dukascopy.api.drawings.ILabelChartObject;
import com.dukascopy.api.drawings.IShortLineChartObject;

public class ShortLineMidBar implements IStrategy {

   @Configurable("Period")
   public Period period = Period.TEN_SECS;
    @Configurable("Instrument")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("Line lenght (in bar widths)")
   public double barWidth = 1.5;
   
   private IChart chart;
   private DecimalFormat df = new DecimalFormat("0.00000");
   
   
   @Override
   public void onStart(IContext context) throws JFException {
      this.chart = context.getChart(Instrument.EURUSD);
   }
   @Override
   public void onTick(Instrument instrument, ITick tick) throws JFException {}
   @Override
   public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
      if (instrument != this.instrument || period != this.period){
         return;
      }   
      double price = (bidBar.getHigh() + bidBar.getLow()) /2;
      long barMidTime = bidBar.getTime() + period.getInterval()/3;
      long lineLength = (long)(barWidth * period.getInterval());
      long lineStartTime = barMidTime - lineLength/2;
      long lineEndTime = barMidTime + lineLength/2;
      
      IShortLineChartObject line = chart.getChartObjectFactory().createShortLine();
      line.setTime(0, lineStartTime);
      line.setTime(1, lineEndTime);
      line.setPrice(0, price);
      line.setPrice(1, price);
      line.setText(df.format(price));
      line.setAttrInt(ATTR_INT.WIDTH, 3);
      line.setColor(Color.BLUE);
      chart.addToMainChart(line);   
      
      ILabelChartObject label = chart.getChartObjectFactory().createLabel();
      label.setTime(0, lineEndTime);
      label.setPrice(0, price);
      label.setText(df.format(price));
      label.setColor(Color.MAGENTA);
      chart.addToMainChart(label);
      
      
   }
   @Override
   public void onMessage(IMessage message) throws JFException {}
   @Override
   public void onAccount(IAccount account) throws JFException {}
   @Override
   public void onStop() throws JFException {}

}

Note that High+Low/2 means middle point between high and low, middle of the candle would be Close+Open/2.


Attachments:
ShortLineMidBar.java [2.49 KiB]
Downloaded 304 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.
 

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