package jforex.requests;

import java.util.*;
import java.text.SimpleDateFormat;
import java.text.DecimalFormat;

import com.dukascopy.api.*;
import com.dukascopy.api.drawings.IOhlcChartObject;
import com.dukascopy.api.feed.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.indicators.IIndicator;

@RequiresFullAccess
public class MaStrategia_Renko_ish1 implements IStrategy {
    private IConsole console;
    private IHistory history;
    private IIndicators indicators;
    
    public Instrument instrument = Instrument.EURUSD;
    public Period selectedPeriod = Period.ONE_MIN;
    public Filter filter = Filter.NO_FILTER;
    public OfferSide side = OfferSide.BID;
    public int priceRangePips = 3;
    public FeedDescriptor feedDescriptor;
    private PriceRange priceRange;
    
    private double[] sum;
    private int timePeriod_sum = 14;
    private double[] pchan_U;
    private double[] pchan_L;
    private int timePeriod_pchan = 2;
    private long tBar, tBar_p;
   
    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.indicators = context.getIndicators();

        priceRange = PriceRange.valueOf(priceRangePips);
        
        feedDescriptor = new FeedDescriptor();
        feedDescriptor.setInstrument(instrument);
        feedDescriptor.setDataType(DataType.PRICE_RANGE_AGGREGATION);
        feedDescriptor.setOfferSide(side);
        feedDescriptor.setFilter(filter);
        feedDescriptor.setPriceRange(priceRange);
    }

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

        IRangeBar lastRangeBar = history.getRangeBar(instrument, side, priceRange, 0);
        tBar=lastRangeBar.getTime();
        
        if(tBar_p!=tBar) {
            tBar_p=tBar;

            //calculate SUM
            Object[] sumFeed = indicators.calculateIndicator(feedDescriptor, new OfferSide[] { OfferSide.BID }, "SUM",
                    new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { timePeriod_sum },  2, lastRangeBar.getTime(), 0);
            sum = (double[]) sumFeed[0];

            //calculate PCHANNEL
            Object[] pchanFeed = indicators.calculateIndicator(feedDescriptor, new OfferSide[] { OfferSide.BID }, "PCHANNEL",
                    new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { timePeriod_pchan },  2, lastRangeBar.getTime(), 0);
            pchan_U = (double[]) pchanFeed[0];
            pchan_L = (double[]) pchanFeed[1];

            print( "SUM: " + (new DecimalFormat("#.####")).format(sum[0]) + ", " +     (new DecimalFormat("#.####")).format(sum[1]) +
             "  PCHAN_U: " + (new DecimalFormat("#.####")).format(pchan_U[0]) + ", " + (new DecimalFormat("#.####")).format(pchan_U[1]) +
             "  PCHAN_L: " + (new DecimalFormat("#.####")).format(pchan_L[0]) + ", " + (new DecimalFormat("#.####")).format(pchan_L[1]));
        }
    }
    
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }

    private void print(Object o) {
         console.getOut().println(o);
    }
}