package jforex.strategies.renko;




import com.dukascopy.api.*;
import com.dukascopy.api.feed.IFeedDescriptor;
import com.dukascopy.api.feed.util.*;
import com.dukascopy.api.feed.*;
import java.util.*;

import com.dukascopy.api.*;


import com.dukascopy.api.IContext;
import com.dukascopy.api.IBar;
import com.dukascopy.api.PriceRange;
import com.dukascopy.api.feed.FeedDescriptor;
import com.dukascopy.api.feed.IFeedDescriptor;
import com.dukascopy.api.feed.IRenkoBar;
import com.dukascopy.api.feed.IRenkoBarFeedListener;


public class Renko_and_MedianRenkoV1 implements IStrategy , IRenkoBarFeedListener {
    
    
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
    
    @Configurable("Instrument")   public Instrument selectedInstrument = Instrument.EURUSD;
    @Configurable("OfferSide")    public OfferSide selectedOfferSide = OfferSide.BID;
    @Configurable("Renko bar size (in pips)") public int selectedRenkoBarSizeInPips = 10;    
    @Configurable("Renko type")   public RenkoType renkoType = RenkoType.REGULAR;     
    
    
    
    
    
    
    
    private PriceRange renkoPriceRange;    
    private IFeedDescriptor feedDescriptor;
    private PriceRange selectedPriceRange;    

    
    
    
    
    @Override
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
        
        
        this.selectedPriceRange = PriceRange.valueOf(selectedRenkoBarSizeInPips);
        this.context.subscribeToRenkoBarFeed(selectedInstrument,selectedOfferSide, selectedPriceRange, this);

        this.feedDescriptor = new FeedDescriptor();
        feedDescriptor.setDataType(DataType.RENKO);
        feedDescriptor.setRenkoType(renkoType);
        feedDescriptor.setInstrument(selectedInstrument);
        feedDescriptor.setOfferSide(selectedOfferSide);
        feedDescriptor.setPriceRange(selectedPriceRange);
       
        renkoPriceRange = PriceRange.valueOf(selectedRenkoBarSizeInPips);
        
       IChart chart = context.getChart(selectedInstrument); 
        if (chart == null) {
           context.openChart(feedDescriptor);
        }        
        
        
        
    }
    private void print(String message) {
        console.getOut().println(message);
    }
    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 {
    }
    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
    @Override
    public void onBar(Instrument instrument, OfferSide offerSide, PriceRange brickSize, IRenkoBar renkoBar) {
        if (instrument != this.selectedInstrument) {
            return;
        }    
        
        try {
                if (instrument.equals(selectedInstrument) && offerSide.equals(selectedOfferSide) && brickSize.getPipCount() == selectedRenkoBarSizeInPips) {
                     
                    IBar Bar1 = history.getRenkoBar(instrument, selectedOfferSide, renkoPriceRange, 1);
                    IBar Bar2 = history.getRenkoBar(instrument, selectedOfferSide, renkoPriceRange, 2);  
                    print("Bar 1 OPEN  = " + Bar1.getOpen() + "   " + "Bar 1 CLOSE = " + Bar1.getClose());
                  }
              } catch (Exception ex) {
            console.getErr().println(ex.getMessage());
        }       
                
    
    }





}    
    
    
    
    
    
