package Indicators;

import java.awt.Color;
import com.dukascopy.api.*;
import com.dukascopy.connector.engine.*;
import com.dukascopy.connector.engine.IndicatorBuffer;
import com.dukascopy.connector.engine.Properties;

public class RSXConnector extends MQL4ConnectorIndicator {

    protected void initProperties() {
        if (properties == null)
            properties = new Properties();

        properties.setProperty("connector_recalculate_all", "true");
        properties.setProperty("connector_calculate_once_per_bar", "true");
        properties.setProperty("connector_max_bars", "500");
        properties.setProperty("indicator_buffers","3");
        properties.setProperty("indicator_maximum","1.0");
        properties.setProperty("indicator_width1","5");
        properties.setProperty("indicator_color3","Gray");
        properties.setProperty("indicator_minimum","-1.0");
        properties.setProperty("indicator_separate_window","true");
        properties.setProperty("indicator_color2","Red");
        properties.setProperty("indicator_color1","Lime");
    }

    int gi_76 = 21;

    @Configurable("")
    public int CountBars = 3000;

    @IndicatorBuffer("")
    public double[] g_ibuf_84 = new double[0];

    int gi_96 = 0;

    int g_count_100 = 0;

    int gi_104 = 0;

    boolean gi_108 = true;

    int g_ind_counted_112 = 0;

    long g_time_116 = System.currentTimeMillis();
    long g_time_120 = System.currentTimeMillis();
    long g_time_124 = System.currentTimeMillis();

    double gd_128 = 0.0;
    double gd_136 = 0.0;
    double gd_144 = 0.0;
    double gd_152 = 0.0;
    double gd_160 = 0.0;
    double gd_168 = 0.0;
    double gd_176 = 0.0;
    double gd_184 = 0.0;
    double gd_192 = 0.0;
    double gd_208 = 0.0;
    double gd_216 = 0.0;
    double gd_224 = 0.0;
    double gd_232 = 0.0;
    double gd_240 = 0.0;
    double gd_248 = 0.0;
    double gd_256 = 0.0;
    double gd_264 = 0.0;
    double gd_272 = 0.0;
    double gd_280 = 0.0;
    double gd_288 = 0.0;
    double gd_296 = 0.0;
    double gd_304 = 0.0;
    double gd_320 = 0.0;
    double gd_328 = 0.0;
    double gd_336 = 0.0;
    double gd_344 = 0.0;
    double gd_352 = 0.0;
    double gd_360 = 0.0;
    double gd_368 = 0.0;
    double gd_376 = 0.0;
    double gd_384 = 0.0;
    double gd_392 = 0.0;
    double gd_400 = 0.0;
    double gd_408 = 0.0;
    double gd_416 = 0.0;
    double gd_424 = 0.0;

    public int init() throws JFException {

        SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID);

        SetIndexBuffer(0,g_ibuf_84);

        IndicatorShortName("RSXConnector");

        SetIndexDrawBegin(0, Bars - CountBars);

        IndicatorDigits(MarketInfo(Instrument(), MODE_DIGITS));

        gi_104 = gi_76 - 1;

        gd_416 = 3.0 / (gi_76 + 2.0);

        gd_424 = 1.0 - gd_416;

        return toInt(0);
    }

    public int start() throws JFException {
        g_ind_counted_112 = IndicatorCounted();

        if (g_ind_counted_112 < 0)
            return -1;

        if (g_ind_counted_112 > gi_76)
            gi_96 = Bars - g_ind_counted_112 - 1;
        else
            gi_96 = Bars - gi_76-1;

        g_time_124 = Time(gi_96 + 1);

        if (g_time_124 != g_time_116 && gi_96 < Bars - gi_76 - 1) {
            if (g_time_124 == g_time_120) {
                gd_320 = gd_208;
                gd_328 = gd_216;
                gd_336 = gd_224;
                gd_344 = gd_232;
                gd_352 = gd_240;
                gd_360 = gd_248;
                gd_368 = gd_256;
                gd_376 = gd_264;
                gd_384 = gd_272;
                gd_392 = gd_280;
                gd_400 = gd_288;
                gd_408 = gd_296;
            } else
                return -1;
        }

        gi_104 = gi_76 - 1;

        gd_416 = 3.0 / (gi_76 + 2.0);

        gd_424 = 1.0 - gd_416;

        while (gi_96 >= 0) {
            if (g_count_100 == 0) {
                g_count_100 = 1;
                gi_108 = false;
            } else if (g_count_100 >= gi_104)
                g_count_100 = gi_104 + 1;
            else
                g_count_100++;

            gd_136 = Close(gi_96) - (Close(gi_96 + 1));

            gd_192 = MathAbs(gd_136);

            gd_320 = gd_424 * gd_320 + gd_416 * gd_136;
            gd_328 = gd_416 * gd_320 + gd_424 * gd_328;
            gd_176 = 1.5 * gd_320 - gd_328 / 2.0;

            gd_336 = gd_424 * gd_336 + gd_416 * gd_176;
            gd_344 = gd_416 * gd_336 + gd_424 * gd_344;
            gd_144 = 1.5 * gd_336 - gd_344 / 2.0;

            gd_352 = gd_424 * gd_352 + gd_416 * gd_144;
            gd_360 = gd_416 * gd_352 + gd_424 * gd_360;
            gd_152 = 1.5 * gd_352 - gd_360 / 2.0;

            gd_368 = gd_424 * gd_368 + gd_416 * gd_192;
            gd_376 = gd_416 * gd_368 + gd_424 * gd_376;
            gd_160 = 1.5 * gd_368 - gd_376 / 2.0;

            gd_384 = gd_424 * gd_384 + gd_416 * gd_160;
            gd_392 = gd_416 * gd_384 + gd_424 * gd_392;
            gd_184 = 1.5 * gd_384 - gd_392 / 2.0;

            gd_400 = gd_424 * gd_400 + gd_416 * gd_184;
            gd_408 = gd_416 * gd_400 + gd_424 * gd_408;
            gd_168 = 1.5 * gd_400 - gd_408 / 2.0;

            if (g_count_100 <= gi_104 && gd_136 != 0.0)
                gi_108 = true;

            if (g_count_100 == gi_104 && !gi_108)
                g_count_100 = 0;

            if (g_count_100 > gi_104 && gd_168 > 0.0000000001) {
                gd_128 = 50.0 * (gd_152 / gd_168 + 1.0);

                if (gd_128>100.0)
                    gd_128 = 100.0;
                if (gd_128 < 0.0)
                    gd_128 = 0.0;
            } else
                gd_128 = 50.0;

            gd_304 = gd_128 / 50.0 - 1.0;

            if (gi_96 == 1) {
                g_time_120 = Time(1);
                g_time_116 = Time(0);

                gd_208 = gd_320;
                gd_216 = gd_328;
                gd_224 = gd_336;
                gd_232 = gd_344;
                gd_240 = gd_352;
                gd_248 = gd_360;
                gd_256 = gd_368;
                gd_264 = gd_376;
                gd_272 = gd_384;
                gd_280 = gd_392;
                gd_288 = gd_400;
                gd_296 = gd_408;
            }

            g_ibuf_84[gi_96] = gd_304;

            gi_96--;
        }

        return 0;
    }
};