package com.dukascopy.visualforex.visualjforex;

import java.util.*;
import com.dukascopy.api.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.CopyOnWriteArrayList;
import java.lang.reflect.*;
import java.math.BigDecimal;


/*
 * Created by VisualJForex Generator, version 2.97
 * Date: 31.08.2020 08:41
 */
public class FLOORTRADER3 implements IStrategy {

    private CopyOnWriteArrayList<TradeEventAction> tradeEventActions = new CopyOnWriteArrayList<TradeEventAction>();
    private static final String DATE_FORMAT_NOW = "yyyyMMdd_HHmmss";
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;

    @Configurable("defaultTakeProfit:")
    public int defaultTakeProfit = 50;
    @Configurable("defaultTradeAmount:")
    public double defaultTradeAmount = 0.001;
    @Configurable("defaultInstrument:")
    public Instrument defaultInstrument = Instrument.EURUSD;
    @Configurable("defaultSlippage:")
    public int defaultSlippage = 5;
    @Configurable("defaultStopLoss:")
    public int defaultStopLoss = 25;
    @Configurable("defaultPeriod:")
    public Period defaultPeriod = Period.TEN_MINS;

    private double _ema15_S12 = 0.0;
    private double _ema15_S10 = 0.0;
    private double _ema21_current;
    private double _Green_Ca_Body;
    private double _Limit50per;
    private Tick LastTick =  null ;
    private String AccountCurrency = "";
    private long _Last6Candles;
    private double _Green_Tail;
    private double _Limit_Entry_50Percent;
    private double _Green_Nose;
    private double _LE33Perce;
    private double Leverage;
    private double _Candles6Time;
    private double _ema21_S12;
    private double _GreenCnadleBody_PIPS = 0.0;
    private double _ema21_S10;
    private boolean _Cross = false;
    private double _ema47;
    private double _ema8_current;
    private double _Counter1 = 0.0;
    private double _ema18_S10;
    private double _ema18_S12;
    private Candle candle27 =  null ;
    private double _Trade = 0.0;
    private Candle LastAskCandle =  null ;
    private double _Limit_Entry_Point;
    private double _ResultPipSize;
    private double _Green_Tail_N;
    private Candle LastBidCandle =  null ;
    private double _Green_CandleBody = 0.0;
    private String AccountId = "";
    private double _AcctualTPPoint;
    private double _Green_Ca_Body_PIPS;
    private double Equity;
    private long _LEonNextCandle;
    private int OverWeekendEndLeverage;
    private Candle _CandleArray =  null ;
    private double _LE50Percent;
    private double _ema18_current;
    private double _ema9_current;
    private double _DistanceTP;
    private double _ema133;
    private List<IOrder> PendingPositions =  null ;
    private double _LimitE33;
    private int _DynamicShift = 0;
    private double _ema15_current = 0.0;
    private double _StopLossPoint;
    private double _Green_C_Body_PIPS;
    private double _ema9_S10;
    private List<IOrder> OpenPositions =  null ;
    private double _Green_C_Body;
    private double _ema9_S12;
    private double _ema136;
    private double _ema30_current = 0.0;
    private double UseofLeverage;
    private double _ema134;
    private double _Green_Nose_N;
    private double _ema135;
    private IMessage LastTradeEvent =  null ;
    private Candle candle129 =  null ;
    private boolean GlobalAccount;
    private double _Total_Candle_Size;
    private double _DistanceLESL;
    private int MarginCutLevel;
    private List<IOrder> AllPositions =  null ;
    private double _ema30_S12 = 0.0;
    private double _ema8_S12;
    private double _ema8_S10;
    private double _ema30_S10 = 0.0;
    private Candle candle81 =  null ;


    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();

        subscriptionInstrumentCheck(defaultInstrument);

        ITick lastITick = context.getHistory().getLastTick(defaultInstrument);
        LastTick = new Tick(lastITick, defaultInstrument);

        IBar bidBar = context.getHistory().getBar(defaultInstrument, defaultPeriod, OfferSide.BID, 1);
        IBar askBar = context.getHistory().getBar(defaultInstrument, defaultPeriod, OfferSide.ASK, 1);
        LastAskCandle = new Candle(askBar, defaultPeriod, defaultInstrument, OfferSide.ASK);
        LastBidCandle = new Candle(bidBar, defaultPeriod, defaultInstrument, OfferSide.BID);

        if (indicators.getIndicator("EMA") == null) {
            indicators.registerDownloadableIndicator("12084","EMA");
        }
        if (indicators.getIndicator("EMA") == null) {
            indicators.registerDownloadableIndicator("12084","EMA");
        }
        if (indicators.getIndicator("EMA") == null) {
            indicators.registerDownloadableIndicator("12084","EMA");
        }
        if (indicators.getIndicator("EMA") == null) {
            indicators.registerDownloadableIndicator("12084","EMA");
        }
        if (indicators.getIndicator("EMA") == null) {
            indicators.registerDownloadableIndicator("12084","EMA");
        }
        if (indicators.getIndicator("EMA") == null) {
            indicators.registerDownloadableIndicator("12084","EMA");
        }
        if (indicators.getIndicator("EMA") == null) {
            indicators.registerDownloadableIndicator("12084","EMA");
        }
        if (indicators.getIndicator("EMA") == null) {
            indicators.registerDownloadableIndicator("12084","EMA");
        }
        if (indicators.getIndicator("EMA") == null) {
            indicators.registerDownloadableIndicator("12084","EMA");
        }
        if (indicators.getIndicator("EMA") == null) {
            indicators.registerDownloadableIndicator("12084","EMA");
        }
        if (indicators.getIndicator("EMA") == null) {
            indicators.registerDownloadableIndicator("12084","EMA");
        }
        if (indicators.getIndicator("EMA") == null) {
            indicators.registerDownloadableIndicator("12084","EMA");
        }
        subscriptionInstrumentCheck(Instrument.fromString("EUR/USD"));

    }

    public void onAccount(IAccount account) throws JFException {
        AccountCurrency = account.getCurrency().toString();
        Leverage = account.getLeverage();
        AccountId= account.getAccountId();
        Equity = account.getEquity();
        UseofLeverage = account.getUseOfLeverage();
        OverWeekendEndLeverage = account.getOverWeekEndLeverage();
        MarginCutLevel = account.getMarginCutLevel();
        GlobalAccount = account.isGlobal();
    }

    private void updateVariables(Instrument instrument) {
        try {
            AllPositions = engine.getOrders();
            List<IOrder> listMarket = new ArrayList<IOrder>();
            for (IOrder order: AllPositions) {
                if (order.getState().equals(IOrder.State.FILLED)){
                    listMarket.add(order);
                }
            }
            List<IOrder> listPending = new ArrayList<IOrder>();
            for (IOrder order: AllPositions) {
                if (order.getState().equals(IOrder.State.OPENED)){
                    listPending.add(order);
                }
            }
            OpenPositions = listMarket;
            PendingPositions = listPending;
        } catch(JFException e) {
            e.printStackTrace();
        }
    }

    public void onMessage(IMessage message) throws JFException {
        if (message.getOrder() != null) {
            updateVariables(message.getOrder().getInstrument());
            LastTradeEvent = message;
            for (TradeEventAction event :  tradeEventActions) {
                IOrder order = message.getOrder();
                if (order != null && event != null && message.getType().equals(event.getMessageType())&& order.getLabel().equals(event.getPositionLabel())) {
                    Method method;
                    try {
                        method = this.getClass().getDeclaredMethod(event.getNextBlockId(), Integer.class);
                        method.invoke(this, new Integer[] {event.getFlowId()});
                    } catch (SecurityException e) {
                            e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                          e.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } 
                    tradeEventActions.remove(event); 
                }
            }   
        }
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
        LastTick = new Tick(tick, instrument);
        updateVariables(instrument);


    }

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        LastAskCandle = new Candle(askBar, period, instrument, OfferSide.ASK);
        LastBidCandle = new Candle(bidBar, period, instrument, OfferSide.BID);
        updateVariables(instrument);
            If_block_10(1);

    }

    public void subscriptionInstrumentCheck(Instrument instrument) {
        try {
              Set<Instrument> instruments = new HashSet<Instrument>();
              instruments.add(instrument);
              context.setSubscribedInstruments(instruments, true);
          } catch (Exception e) {
              e.printStackTrace();
          }
        }

    public double round(double price, Instrument instrument) {
        BigDecimal big = new BigDecimal("" + price); 
        big = big.setScale(instrument.getPipScale() + 1, BigDecimal.ROUND_HALF_UP); 
        return big.doubleValue(); 
    }

    public ITick getLastTick(Instrument instrument) {
        try { 
            return (context.getHistory().getTick(instrument, 0)); 
        } catch (JFException e) { 
             e.printStackTrace();  
         } 
         return null; 
    }

    private  void If_block_10(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Instrument argument_2 = LastBidCandle.getInstrument();
        if (argument_1 == null && argument_2 !=null || (argument_1!= null && !argument_1.equals(argument_2))) {
        }
        else if (argument_1!= null && argument_1.equals(argument_2)) {
            If_block_11(flow);
        }
    }

    private  void If_block_11(Integer flow) {
        Period argument_1 = Period.ONE_HOUR;
        Period argument_2 = LastBidCandle.getPeriod();
        if (argument_1 == null && argument_2 !=null || (argument_1!= null && !argument_1.equals(argument_2))) {
        }
        else if (argument_1!= null && argument_1.equals(argument_2)) {
            EMA_block_12(flow);
        }
    }

    private void EMA_block_12(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = Period.ONE_HOUR;
        int argument_3 = 10;
        int argument_4 = 9;
        OfferSide[] offerside = new OfferSide[1];
        IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
        offerside[0] = OfferSide.BID;
        appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
        Object[] params = new Object[1];
        params[0] = 9;
        try {
            subscriptionInstrumentCheck(argument_1);
            long time = context.getHistory().getBar(argument_1, argument_2, OfferSide.BID, argument_3).getTime();
            Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside,
                    "EMA", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
            if ((new Double(((double [])indicatorResult[0])[0])) == null) {
                this._ema9_S10 = Double.NaN;
            } else { 
                this._ema9_S10 = (((double [])indicatorResult[0])[0]);
            } 
        EMA_block_13(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._ema9_S10 = Double.NaN;
        }
    }

    private void EMA_block_13(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = Period.ONE_HOUR;
        int argument_3 = 12;
        int argument_4 = 9;
        OfferSide[] offerside = new OfferSide[1];
        IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
        offerside[0] = OfferSide.BID;
        appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
        Object[] params = new Object[1];
        params[0] = 9;
        try {
            subscriptionInstrumentCheck(argument_1);
            long time = context.getHistory().getBar(argument_1, argument_2, OfferSide.BID, argument_3).getTime();
            Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside,
                    "EMA", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
            if ((new Double(((double [])indicatorResult[0])[0])) == null) {
                this._ema9_S12 = Double.NaN;
            } else { 
                this._ema9_S12 = (((double [])indicatorResult[0])[0]);
            } 
        EMA_block_14(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._ema9_S12 = Double.NaN;
        }
    }

    private void EMA_block_14(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = Period.ONE_HOUR;
        int argument_3 = 10;
        int argument_4 = 18;
        OfferSide[] offerside = new OfferSide[1];
        IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
        offerside[0] = OfferSide.BID;
        appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
        Object[] params = new Object[1];
        params[0] = 18;
        try {
            subscriptionInstrumentCheck(argument_1);
            long time = context.getHistory().getBar(argument_1, argument_2, OfferSide.BID, argument_3).getTime();
            Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside,
                    "EMA", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
            if ((new Double(((double [])indicatorResult[0])[0])) == null) {
                this._ema18_S10 = Double.NaN;
            } else { 
                this._ema18_S10 = (((double [])indicatorResult[0])[0]);
            } 
        EMA_block_15(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._ema18_S10 = Double.NaN;
        }
    }

    private void EMA_block_15(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = Period.ONE_HOUR;
        int argument_3 = 12;
        int argument_4 = 18;
        OfferSide[] offerside = new OfferSide[1];
        IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
        offerside[0] = OfferSide.BID;
        appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
        Object[] params = new Object[1];
        params[0] = 18;
        try {
            subscriptionInstrumentCheck(argument_1);
            long time = context.getHistory().getBar(argument_1, argument_2, OfferSide.BID, argument_3).getTime();
            Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside,
                    "EMA", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
            if ((new Double(((double [])indicatorResult[0])[0])) == null) {
                this._ema18_S12 = Double.NaN;
            } else { 
                this._ema18_S12 = (((double [])indicatorResult[0])[0]);
            } 
        EMA_block_60(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._ema18_S12 = Double.NaN;
        }
    }

    private  void If_block_16(Integer flow) {
        double argument_1 = _ema9_S12;
        double argument_2 = _ema18_S12;
        if (argument_1< argument_2) {
        }
        else if (argument_1> argument_2) {
            If_block_17(flow);
        }
        else if (argument_1== argument_2) {
        }
    }

    private  void If_block_17(Integer flow) {
        double argument_1 = _ema9_S10;
        double argument_2 = _ema18_S10;
        if (argument_1< argument_2) {
        }
        else if (argument_1> argument_2) {
            Assign_block_19(flow);
        }
        else if (argument_1== argument_2) {
        }
    }

    private  void Assign_block_19(Integer flow) {
        boolean argument_1 = true;
        _Cross =  argument_1;
        }

    private  void MultipleAction_block_21(Integer flow) {
        If_block_22(flow);
        If_block_89(flow);
        If_block_59(flow);
    }

    private  void If_block_22(Integer flow) {
        boolean argument_1 = _Cross;
        boolean argument_2 = true;
        if (argument_1!= argument_2) {
        }
        else if (argument_1 == argument_2) {
            EMA_block_131(flow);
        }
    }

    private  void Assign_block_24(Integer flow) {
        boolean argument_1 = false;
        _Cross =  argument_1;
        }

    private  void If_block_45(Integer flow) {
        double argument_1 = candle81.getOpen();
        double argument_2 = candle81.getClose();
        if (argument_1< argument_2) {
        }
        else if (argument_1> argument_2) {
            Calculation_block_94(flow);
        }
        else if (argument_1== argument_2) {
        }
    }

    private  void If_block_59(Integer flow) {
        double argument_1 = _ema9_current;
        double argument_2 = _ema18_current;
        if (argument_1< argument_2) {
        }
        else if (argument_1> argument_2) {
            Assign_block_24(flow);
        }
        else if (argument_1== argument_2) {
        }
    }

    private void EMA_block_60(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = Period.ONE_HOUR;
        int argument_3 = 1;
        int argument_4 = 18;
        OfferSide[] offerside = new OfferSide[1];
        IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
        offerside[0] = OfferSide.BID;
        appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
        Object[] params = new Object[1];
        params[0] = 18;
        try {
            subscriptionInstrumentCheck(argument_1);
            long time = context.getHistory().getBar(argument_1, argument_2, OfferSide.BID, argument_3).getTime();
            Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside,
                    "EMA", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
            if ((new Double(((double [])indicatorResult[0])[0])) == null) {
                this._ema18_current = Double.NaN;
            } else { 
                this._ema18_current = (((double [])indicatorResult[0])[0]);
            } 
        EMA_block_61(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._ema18_current = Double.NaN;
        }
    }

    private void EMA_block_61(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = Period.ONE_HOUR;
        int argument_3 = 1;
        int argument_4 = 9;
        OfferSide[] offerside = new OfferSide[1];
        IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
        offerside[0] = OfferSide.BID;
        appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
        Object[] params = new Object[1];
        params[0] = 9;
        try {
            subscriptionInstrumentCheck(argument_1);
            long time = context.getHistory().getBar(argument_1, argument_2, OfferSide.BID, argument_3).getTime();
            Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside,
                    "EMA", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
            if ((new Double(((double [])indicatorResult[0])[0])) == null) {
                this._ema9_current = Double.NaN;
            } else { 
                this._ema9_current = (((double [])indicatorResult[0])[0]);
            } 
        MultipleAction_block_21(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._ema9_current = Double.NaN;
        }
    }

    private  void GetHistoricalCandle_block_81(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = Period.ONE_HOUR;
        OfferSide argument_3 = OfferSide.BID;
        int argument_4 = 1;
            subscriptionInstrumentCheck(argument_1);
         
        try {
            IBar tempBar = history.getBar(argument_1, argument_2, argument_3, argument_4); 
            candle81 = new Candle(tempBar, argument_2, argument_1, argument_3); 
        } catch (JFException e) {
            e.printStackTrace();
        }
        If_block_45(flow);
    }

    private  void If_block_89(Integer flow) {
        double argument_1 = _ema9_current;
        double argument_2 = _ema18_current;
        if (argument_1< argument_2) {
            If_block_16(flow);
        }
        else if (argument_1> argument_2) {
        }
        else if (argument_1== argument_2) {
        }
    }

    private void Calculation_block_94(Integer flow) {
        double argument_1 = candle81.getOpen();
        double argument_2 = candle81.getClose();
        _Green_C_Body = argument_1 - argument_2;
        Calculation_block_96(flow);
    }

    private void Calculation_block_96(Integer flow) {
        double argument_1 = _Green_C_Body;
        double argument_2 = defaultInstrument.getPipValue();
        _Green_C_Body_PIPS = argument_1 / argument_2;
        If_block_97(flow);
    }

    private  void If_block_97(Integer flow) {
        double argument_1 = _Green_C_Body_PIPS;
        double argument_2 = 0.0;
        if (argument_1< argument_2) {
        }
        else if (argument_1> argument_2) {
            If_block_98(flow);
        }
        else if (argument_1== argument_2) {
        }
    }

    private  void If_block_98(Integer flow) {
        double argument_1 = _Green_C_Body_PIPS;
        double argument_2 = 15.0;
        if (argument_1< argument_2) {
            CalculationExpression_block_99(flow);
        }
        else if (argument_1> argument_2) {
        }
        else if (argument_1== argument_2) {
        }
    }

    private void CalculationExpression_block_99(Integer flow) {
        _Green_Nose = (candle81.getClose()-candle81.getLow())/ (float)defaultInstrument.getPipValue();
        If_block_100(flow);
    }

    private  void If_block_100(Integer flow) {
        double argument_1 = _Green_Nose;
        double argument_2 = 5.0;
        if (argument_1< argument_2) {
            CalculationExpression_block_101(flow);
        }
        else if (argument_1> argument_2) {
        }
        else if (argument_1== argument_2) {
        }
    }

    private void CalculationExpression_block_101(Integer flow) {
        _Green_Tail = (candle81.getHigh()-candle81.getOpen())/ (float)defaultInstrument.getPipValue();
        If_block_102(flow);
    }

    private  void If_block_102(Integer flow) {
        double argument_1 = _Green_Tail;
        double argument_2 = 0.0;
        if (argument_1< argument_2) {
        }
        else if (argument_1> argument_2) {
            If_block_103(flow);
        }
        else if (argument_1== argument_2) {
            If_block_103(flow);
        }
    }

    private  void If_block_103(Integer flow) {
        double argument_1 = _Green_Tail;
        double argument_2 = 7.0;
        if (argument_1< argument_2) {
            GetHistoricalCandle_block_129(flow);
        }
        else if (argument_1> argument_2) {
        }
        else if (argument_1== argument_2) {
        }
    }

    private  void PendingOpen_block_105(Integer flow) {
        double argument_1 = _Limit_Entry_Point;
        Instrument argument_2 = defaultInstrument;
        double argument_3 = 0.001;
        int argument_4 = -1;
        double argument_5 = _StopLossPoint;
        double argument_6 = _AcctualTPPoint;
        String argument_7 = "";
        long argument_8 = _LEonNextCandle;
        IEngine.OrderCommand command = IEngine.OrderCommand.SELLLIMIT;

        double stopLoss = round(argument_5, argument_2);
        double takeProfit = round(argument_6, argument_2);
        
        try {
            String label = getLabel();           
            IOrder order = context.getEngine().submitOrder(label, argument_2, command, argument_3, round(argument_1, argument_2), (double)argument_4, stopLoss, takeProfit, argument_8, argument_7);
                } catch (JFException e) {
            e.printStackTrace();
        }
    }

    private void Calculation_block_106(Integer flow) {
        double argument_1 = candle81.getHigh();
        double argument_2 = candle81.getLow();
        _Total_Candle_Size = argument_1 - argument_2;
        Calculation_block_107(flow);
    }

    private void Calculation_block_107(Integer flow) {
        double argument_1 = _Total_Candle_Size;
        double argument_2 = 2.0;
        _Limit50per = argument_1 / argument_2;
        Calculation_block_108(flow);
    }

    private void Calculation_block_108(Integer flow) {
        double argument_1 = candle81.getLow();
        double argument_2 = _Limit50per;
        _Limit_Entry_Point = argument_1 + argument_2;
        Calculation_block_119(flow);
    }

    private void Calculation_block_119(Integer flow) {
        double argument_1 = 1.0;
        double argument_2 = defaultInstrument.getPipValue();
        _ResultPipSize = argument_1 * argument_2;
        Calculation_block_122(flow);
    }

    private void Calculation_block_120(Integer flow) {
        double argument_1 = _StopLossPoint;
        double argument_2 = _Limit_Entry_Point;
        _DistanceLESL = argument_1 - argument_2;
        Calculation_block_123(flow);
    }

    private void Calculation_block_121(Integer flow) {
        double argument_1 = _Limit_Entry_Point;
        double argument_2 = _DistanceTP;
        _AcctualTPPoint = argument_1 - argument_2;
        Calculation_block_127(flow);
    }

    private void Calculation_block_122(Integer flow) {
        double argument_1 = candle81.getHigh();
        double argument_2 = _ResultPipSize;
        _StopLossPoint = argument_1 + argument_2;
        Calculation_block_120(flow);
    }

    private void Calculation_block_123(Integer flow) {
        double argument_1 = _DistanceLESL;
        double argument_2 = 1.0;
        _DistanceTP = argument_1 * argument_2;
        Calculation_block_121(flow);
    }

    private void Calculation_block_127(Integer flow) {
        long argument_1 = LastTick.getTime();
        double argument_2 = 3600000.0;
        _LEonNextCandle = (long)(argument_1 + argument_2);
        PendingOpen_block_105(flow);
    }

    private  void If_block_128(Integer flow) {
        double argument_1 = candle81.getHigh();
        double argument_2 = _ema9_current;
        if (argument_1< argument_2) {
        }
        else if (argument_1> argument_2) {
            If_block_130(flow);
        }
        else if (argument_1== argument_2) {
        }
    }

    private  void GetHistoricalCandle_block_129(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = Period.ONE_HOUR;
        OfferSide argument_3 = OfferSide.BID;
        int argument_4 = 2;
            subscriptionInstrumentCheck(argument_1);
         
        try {
            IBar tempBar = history.getBar(argument_1, argument_2, argument_3, argument_4); 
            candle129 = new Candle(tempBar, argument_2, argument_1, argument_3); 
        } catch (JFException e) {
            e.printStackTrace();
        }
        If_block_128(flow);
    }

    private  void If_block_130(Integer flow) {
        double argument_1 = candle81.getClose();
        double argument_2 = candle129.getLow();
        if (argument_1< argument_2) {
            Calculation_block_106(flow);
        }
        else if (argument_1> argument_2) {
        }
        else if (argument_1== argument_2) {
        }
    }

    private void EMA_block_131(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = Period.FOUR_HOURS;
        int argument_3 = 10;
        int argument_4 = 9;
        OfferSide[] offerside = new OfferSide[1];
        IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
        offerside[0] = OfferSide.BID;
        appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
        Object[] params = new Object[1];
        params[0] = 9;
        try {
            subscriptionInstrumentCheck(argument_1);
            long time = context.getHistory().getBar(argument_1, argument_2, OfferSide.BID, argument_3).getTime();
            Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside,
                    "EMA", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
            if ((new Double(((double [])indicatorResult[0])[0])) == null) {
                this._ema9_S10 = Double.NaN;
            } else { 
                this._ema9_S10 = (((double [])indicatorResult[0])[0]);
            } 
        EMA_block_132(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._ema9_S10 = Double.NaN;
        }
    }

    private void EMA_block_132(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = Period.FOUR_HOURS;
        int argument_3 = 12;
        int argument_4 = 9;
        OfferSide[] offerside = new OfferSide[1];
        IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
        offerside[0] = OfferSide.BID;
        appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
        Object[] params = new Object[1];
        params[0] = 9;
        try {
            subscriptionInstrumentCheck(argument_1);
            long time = context.getHistory().getBar(argument_1, argument_2, OfferSide.BID, argument_3).getTime();
            Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside,
                    "EMA", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
            if ((new Double(((double [])indicatorResult[0])[0])) == null) {
                this._ema9_S12 = Double.NaN;
            } else { 
                this._ema9_S12 = (((double [])indicatorResult[0])[0]);
            } 
        EMA_block_133(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._ema9_S12 = Double.NaN;
        }
    }

    private void EMA_block_133(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = Period.FOUR_HOURS;
        int argument_3 = 10;
        int argument_4 = 18;
        OfferSide[] offerside = new OfferSide[1];
        IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
        offerside[0] = OfferSide.BID;
        appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
        Object[] params = new Object[1];
        params[0] = 18;
        try {
            subscriptionInstrumentCheck(argument_1);
            long time = context.getHistory().getBar(argument_1, argument_2, OfferSide.BID, argument_3).getTime();
            Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside,
                    "EMA", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
            if ((new Double(((double [])indicatorResult[0])[0])) == null) {
                this._ema133 = Double.NaN;
            } else { 
                this._ema133 = (((double [])indicatorResult[0])[0]);
            } 
        EMA_block_134(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._ema133 = Double.NaN;
        }
    }

    private void EMA_block_134(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = Period.FOUR_HOURS;
        int argument_3 = 12;
        int argument_4 = 18;
        OfferSide[] offerside = new OfferSide[1];
        IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
        offerside[0] = OfferSide.BID;
        appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
        Object[] params = new Object[1];
        params[0] = 18;
        try {
            subscriptionInstrumentCheck(argument_1);
            long time = context.getHistory().getBar(argument_1, argument_2, OfferSide.BID, argument_3).getTime();
            Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside,
                    "EMA", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
            if ((new Double(((double [])indicatorResult[0])[0])) == null) {
                this._ema134 = Double.NaN;
            } else { 
                this._ema134 = (((double [])indicatorResult[0])[0]);
            } 
        EMA_block_135(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._ema134 = Double.NaN;
        }
    }

    private void EMA_block_135(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = Period.FOUR_HOURS;
        int argument_3 = 1;
        int argument_4 = 18;
        OfferSide[] offerside = new OfferSide[1];
        IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
        offerside[0] = OfferSide.BID;
        appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
        Object[] params = new Object[1];
        params[0] = 18;
        try {
            subscriptionInstrumentCheck(argument_1);
            long time = context.getHistory().getBar(argument_1, argument_2, OfferSide.BID, argument_3).getTime();
            Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside,
                    "EMA", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
            if ((new Double(((double [])indicatorResult[0])[0])) == null) {
                this._ema135 = Double.NaN;
            } else { 
                this._ema135 = (((double [])indicatorResult[0])[0]);
            } 
        EMA_block_136(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._ema135 = Double.NaN;
        }
    }

    private void EMA_block_136(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = Period.FOUR_HOURS;
        int argument_3 = 1;
        int argument_4 = 9;
        OfferSide[] offerside = new OfferSide[1];
        IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
        offerside[0] = OfferSide.BID;
        appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
        Object[] params = new Object[1];
        params[0] = 9;
        try {
            subscriptionInstrumentCheck(argument_1);
            long time = context.getHistory().getBar(argument_1, argument_2, OfferSide.BID, argument_3).getTime();
            Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside,
                    "EMA", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
            if ((new Double(((double [])indicatorResult[0])[0])) == null) {
                this._ema136 = Double.NaN;
            } else { 
                this._ema136 = (((double [])indicatorResult[0])[0]);
            } 
        MultipleAction_block_137(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._ema136 = Double.NaN;
        }
    }

    private  void MultipleAction_block_137(Integer flow) {
        If_block_138(flow);
        If_block_139(flow);
        If_block_141(flow);
    }

    private  void If_block_138(Integer flow) {
        boolean argument_1 = _Cross;
        boolean argument_2 = true;
        if (argument_1!= argument_2) {
        }
        else if (argument_1 == argument_2) {
            GetHistoricalCandle_block_81(flow);
        }
    }

    private  void If_block_139(Integer flow) {
        double argument_1 = _ema9_current;
        double argument_2 = _ema18_current;
        if (argument_1< argument_2) {
            If_block_140(flow);
        }
        else if (argument_1> argument_2) {
        }
        else if (argument_1== argument_2) {
        }
    }

    private  void If_block_140(Integer flow) {
        double argument_1 = _ema9_S12;
        double argument_2 = _ema18_S12;
        if (argument_1< argument_2) {
        }
        else if (argument_1> argument_2) {
            If_block_142(flow);
        }
        else if (argument_1== argument_2) {
        }
    }

    private  void If_block_141(Integer flow) {
        double argument_1 = _ema9_current;
        double argument_2 = _ema18_current;
        if (argument_1< argument_2) {
        }
        else if (argument_1> argument_2) {
            Assign_block_143(flow);
        }
        else if (argument_1== argument_2) {
        }
    }

    private  void If_block_142(Integer flow) {
        double argument_1 = _ema9_S10;
        double argument_2 = _ema18_S10;
        if (argument_1< argument_2) {
        }
        else if (argument_1> argument_2) {
            Assign_block_144(flow);
        }
        else if (argument_1== argument_2) {
        }
    }

    private  void Assign_block_143(Integer flow) {
        boolean argument_1 = false;
        _Cross =  argument_1;
        }

    private  void Assign_block_144(Integer flow) {
        boolean argument_1 = true;
        _Cross =  argument_1;
        }

class Candle  {

    IBar bar;
    Period period;
    Instrument instrument;
    OfferSide offerSide;

    public Candle(IBar bar, Period period, Instrument instrument, OfferSide offerSide) {
        this.bar = bar;
        this.period = period;
        this.instrument = instrument;
        this.offerSide = offerSide;
    }

    public Period getPeriod() {
        return period;
    }

    public void setPeriod(Period period) {
        this.period = period;
    }

    public Instrument getInstrument() {
        return instrument;
    }

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

    public OfferSide getOfferSide() {
        return offerSide;
    }

    public void setOfferSide(OfferSide offerSide) {
        this.offerSide = offerSide;
    }

    public IBar getBar() {
        return bar;
    }

    public void setBar(IBar bar) {
        this.bar = bar;
    }

    public long getTime() {
        return bar.getTime();
    }

    public double getOpen() {
        return bar.getOpen();
    }

    public double getClose() {
        return bar.getClose();
    }

    public double getLow() {
        return bar.getLow();
    }

    public double getHigh() {
        return bar.getHigh();
    }

    public double getVolume() {
        return bar.getVolume();
    }
}
class Tick {

    private ITick tick;
    private Instrument instrument;

    public Tick(ITick tick, Instrument instrument){
        this.instrument = instrument;
        this.tick = tick;
    }

    public Instrument getInstrument(){
       return  instrument;
    }

    public double getAsk(){
       return  tick.getAsk();
    }

    public double getBid(){
       return  tick.getBid();
    }

    public double getAskVolume(){
       return  tick.getAskVolume();
    }

    public double getBidVolume(){
        return tick.getBidVolume();
    }

   public long getTime(){
       return  tick.getTime();
    }

   public ITick getTick(){
       return  tick;
    }
}

    protected String getLabel() {
        String label;
        label = "IVF" + getCurrentTime(LastTick.getTime()) + generateRandom(10000) + generateRandom(10000);
        return label;
    }

    private String getCurrentTime(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
        return sdf.format(time);
    }

    private static String generateRandom(int n) {
        int randomNumber = (int) (Math.random() * n);
        String answer = "" + randomNumber;
        if (answer.length() > 3) {
            answer = answer.substring(0, 4);
        }
        return answer;
    }

    class TradeEventAction {
        private IMessage.Type messageType;
        private String nextBlockId = "";
        private String positionLabel = "";
        private int flowId = 0;

        public IMessage.Type getMessageType() {
            return messageType;
        }

        public void setMessageType(IMessage.Type messageType) {
            this.messageType = messageType;
        }

        public String getNextBlockId() {
            return nextBlockId;
        }

        public void setNextBlockId(String nextBlockId) {
            this.nextBlockId = nextBlockId;
        }
        public String getPositionLabel() {
            return positionLabel;
       }

        public void setPositionLabel(String positionLabel) {
            this.positionLabel = positionLabel;
        }
        public int getFlowId() {
            return flowId;
        }
        public void setFlowId(int flowId) {
            this.flowId = flowId;
        }
    }
}