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.39
 * Date: 06.04.2017 22:52
 */
public class PetersEMA 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 = 12;
    @Configurable("_Price_Type:")
    public IIndicators.AppliedPrice _Price_Type = IIndicators.AppliedPrice.MEDIAN_PRICE;
    @Configurable("defaultTradeAmount:")
    public double defaultTradeAmount = 0.001;
    @Configurable("_Step:")
    public double _Step = 0.1;
    @Configurable("_SMMA_Period:")
    public int _SMMA_Period = 8;
    @Configurable("_TSL_min10:")
    public int _TSL_min10 = 0;
    @Configurable("_Control_time:")
    public boolean _Control_time = false;
    @Configurable("_TrailingSL:")
    public boolean _TrailingSL = false;
    @Configurable("_EMA_Period:")
    public int _EMA_Period = 34;
    @Configurable("defaultInstrument:")
    public Instrument defaultInstrument = Instrument.EURUSD;
    @Configurable("defaultSlippage:")
    public int defaultSlippage = 1;
    @Configurable("_Martingale:")
    public boolean _Martingale = false;
    @Configurable("defaultStopLoss:")
    public int defaultStopLoss = 5;
    @Configurable("defaultPeriod:")
    public Period defaultPeriod = Period.ONE_MIN;

    private boolean _Go_Long = true;
    private double _Orig_Trade_Amount;
    private boolean _tempVar469 = true;
    private double _Increment = 1.0;
    private boolean _tempVar269 = true;
    private Tick LastTick =  null ;
    private boolean _tempVar466 = false;
    private String AccountCurrency = "";
    private Period _tempVar395 = Period.ONE_MIN;
    private boolean _tempVar472 = false;
    private boolean _tempVar471 = false;
    private boolean _tempVar470 = true;
    private int _tempVar278;
    private int _BreakEven;
    private Candle _MyLongCandle =  null ;
    private double Leverage;
    private int _TP;
    private boolean _tempVar270 = false;
    private double _Curr_ema;
    private int _tempVar92 = 21;
    private double _Curr_smma;
    private Candle candle20 =  null ;
    private Candle LastAskCandle =  null ;
    private Period _tempVar95 = Period.ONE_MIN;
    private double _smma;
    private Candle LastBidCandle =  null ;
    private String AccountId = "";
    private double Equity;
    private Period _tempVar402 = Period.ONE_MIN;
    private double _LotSize;
    private boolean _Go_Short = true;
    private int _Poz;
    private double _Prev_smma;
    private int OverWeekendEndLeverage;
    private boolean _Init = true;
    private double _ema;
    private boolean _tempVar452 = true;
    private boolean _TradeTaken = false;
    private List<IOrder> PendingPositions =  null ;
    private double _Prev_ema;
    private IOrder _ShortPositions =  null ;
    private List<IOrder> OpenPositions =  null ;
    private double UseofLeverage;
    private IOrder _Open_Position =  null ;
    private boolean _True = true;
    private IMessage LastTradeEvent =  null ;
    private boolean GlobalAccount;
    private Candle _LastCandle =  null ;
    private Candle _MyShortCandle =  null ;
    private int _Trading_Time;
    private int _tempVar222 = 0;
    private int MarginCutLevel;
    private List<IOrder> AllPositions =  null ;
    private int _SL;
    private boolean _IsLong = true;


    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("SMMA") == null) {
            indicators.registerDownloadableIndicator("1312","SMMA");
        }
        if (indicators.getIndicator("SMMA") == null) {
            indicators.registerDownloadableIndicator("1312","SMMA");
        }
        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);
            Assign_block_89(1);

    }

    public void subscriptionInstrumentCheck(Instrument instrument) {
        try {
              if (!context.getSubscribedInstruments().contains(instrument)) {
                  Set<Instrument> instruments = new HashSet<Instrument>();
                  instruments.add(instrument);
                  context.setSubscribedInstruments(instruments, true);
                  Thread.sleep(100);
              }
          } catch (InterruptedException 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_14(Integer flow) {
        int argument_1 = OpenPositions.size();
        int argument_2 = 1;
        if (argument_1< argument_2) {
            Assign_block_15(flow);
        }
        else if (argument_1> argument_2) {
            PositionsViewer_block_29(flow);
        }
        else if (argument_1== argument_2) {
            PositionsViewer_block_29(flow);
        }
    }

    private  void Assign_block_15(Integer flow) {
        int argument_1 = 0;
        _BreakEven =  argument_1;
            If_block_104(flow);
    }

    private  void GetTimeUnit_block_16(Integer flow) {
        long argument_1 = LastAskCandle.getTime();
        Date date = new Date(argument_1);
        Calendar calendar = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));
        calendar.setTime(date);
        _Trading_Time = calendar.get(Calendar.HOUR_OF_DAY);
        If_block_17(flow);
    }

    private  void If_block_17(Integer flow) {
        int argument_1 = _Trading_Time;
        int argument_2 = 5;
        if (argument_1< argument_2) {
        }
        else if (argument_1> argument_2) {
            If_block_18(flow);
        }
        else if (argument_1== argument_2) {
        }
    }

    private  void If_block_18(Integer flow) {
        int argument_1 = _Trading_Time;
        int argument_2 = 16;
        if (argument_1< argument_2) {
            If_block_119(flow);
        }
        else if (argument_1> argument_2) {
        }
        else if (argument_1== argument_2) {
        }
    }

    private  void Assign_block_25(Integer flow) {
        int argument_1 = 1;
        _BreakEven =  argument_1;
        }

    private  void Assign_block_28(Integer flow) {
        int argument_1 = 1;
        _BreakEven =  argument_1;
        }

    private  void PositionsViewer_block_29(Integer flow) {
        List<IOrder> argument_1 = OpenPositions;
        for (IOrder order : argument_1){
            if (order.getState() == IOrder.State.OPENED||order.getState() == IOrder.State.FILLED){
                _Open_Position = order;
                If_block_32(flow);
            }
        }
    }

    private  void If_block_32(Integer flow) {
        IOrder.State argument_1 = _Open_Position.getState();
        IOrder.State argument_2 = IOrder.State.FILLED;
        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_58(flow);
        }
    }

    private  void Assign_block_47(Integer flow) {
        boolean argument_1 = true;
        _Go_Long =  argument_1;
            Assign_block_77(flow);
    }

    private  void Assign_block_48(Integer flow) {
        boolean argument_1 = false;
        _Go_Short =  argument_1;
            Assign_block_47(flow);
    }

    private  void OpenatMarket_block_49(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        double argument_2 = defaultTradeAmount;
        int argument_3 = defaultSlippage;
        int argument_4 = _SL;
        int argument_5 = _TP;
        String argument_6 = "";
        ITick tick = getLastTick(argument_1);

        IEngine.OrderCommand command = IEngine.OrderCommand.BUY;

        double stopLoss = tick.getBid() - argument_1.getPipValue() * argument_4;
        double takeProfit = round(tick.getBid() + argument_1.getPipValue() * argument_5, argument_1);
        
           try {
               String label = getLabel();           
               _Open_Position = context.getEngine().submitOrder(label, argument_1, command, argument_2, 0, argument_3,  stopLoss, takeProfit, 0, argument_6);
                Assign_block_28(flow);
        TradeEventAction event3 = new TradeEventAction();
        event3.setMessageType(IMessage.Type.ORDER_FILL_OK);
        event3.setNextBlockId("Assign_block_51");
        event3.setPositionLabel(_Open_Position.getLabel());
        event3.setFlowId(flow);
        tradeEventActions.add(event3);
        } catch (JFException e) {
            e.printStackTrace();
        }
    }

    private  void OpenatMarket_block_50(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        double argument_2 = defaultTradeAmount;
        int argument_3 = defaultSlippage;
        int argument_4 = _SL;
        int argument_5 = _TP;
        String argument_6 = "";
        ITick tick = getLastTick(argument_1);

        IEngine.OrderCommand command = IEngine.OrderCommand.SELL;

        double stopLoss = tick.getAsk() + argument_1.getPipValue() * argument_4;
        double takeProfit = round(tick.getAsk() - argument_1.getPipValue() * argument_5, argument_1);
        
           try {
               String label = getLabel();           
               _Open_Position = context.getEngine().submitOrder(label, argument_1, command, argument_2, 0, argument_3,  stopLoss, takeProfit, 0, argument_6);
                Assign_block_25(flow);
        TradeEventAction event3 = new TradeEventAction();
        event3.setMessageType(IMessage.Type.ORDER_FILL_OK);
        event3.setNextBlockId("Assign_block_48");
        event3.setPositionLabel(_Open_Position.getLabel());
        event3.setFlowId(flow);
        tradeEventActions.add(event3);
        } catch (JFException e) {
            e.printStackTrace();
        }
    }

    private  void Assign_block_51(Integer flow) {
        boolean argument_1 = _tempVar269;
        _Go_Short =  argument_1;
            Assign_block_52(flow);
    }

    private  void Assign_block_52(Integer flow) {
        boolean argument_1 = _tempVar270;
        _Go_Long =  argument_1;
            Assign_block_77(flow);
    }

    private  void If_block_55(Integer flow) {
        boolean argument_1 = _Go_Long;
        boolean argument_2 = true;
        if (argument_1!= argument_2) {
        }
        else if (argument_1 == argument_2) {
            If_block_96(flow);
        }
    }

    private  void If_block_56(Integer flow) {
        boolean argument_1 = _Go_Short;
        boolean argument_2 = true;
        if (argument_1!= argument_2) {
        }
        else if (argument_1 == argument_2) {
            If_block_72(flow);
        }
    }

    private  void If_block_58(Integer flow) {
        double argument_1 = _Open_Position.getProfitLossInPips();
        int argument_2 = _SL;
        if (argument_1< argument_2) {
        }
        else if (argument_1> argument_2) {
            If_block_107(flow);
        }
        else if (argument_1== argument_2) {
        }
    }

    private  void TrailingStop_block_59(Integer flow) {
        IOrder argument_1 = _Open_Position;
        int argument_2 = _SL;
        int argument_3 = _TSL_min10;
        double stopLoss;
        try {
            if (argument_1.isLong()) {
                stopLoss = round(argument_1.getOpenPrice() - argument_1.getInstrument().getPipValue() * argument_2, argument_1.getInstrument());
                if ((stopLoss != argument_1.getStopLossPrice()) && (stopLoss != 0) && (argument_1.getState().equals(IOrder.State.OPENED) || argument_1.getState().equals(IOrder.State.FILLED))) {
                    argument_1.setStopLossPrice(stopLoss, OfferSide.BID, argument_3);
                }
            } else {
                stopLoss = round(argument_1.getOpenPrice() + argument_1.getInstrument().getPipValue() * argument_2, argument_1.getInstrument());
                if ((stopLoss != argument_1.getStopLossPrice()) && (stopLoss != 0) && (argument_1.getState().equals(IOrder.State.OPENED) || argument_1.getState().equals(IOrder.State.FILLED))) {
                    argument_1.setStopLossPrice(stopLoss, OfferSide.ASK, argument_3);
                }
            }
        } catch (JFException e) {
            e.printStackTrace();
        }
    }

    private void SMMA_block_61(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = _tempVar395;
        int argument_3 = 0;
        int argument_4 = _SMMA_Period;
        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] = _SMMA_Period;
        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,
                    "SMMA", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
            if ((new Double(((double [])indicatorResult[0])[0])) == null) {
                this._Curr_smma = Double.NaN;
            } else { 
                this._Curr_smma = (((double [])indicatorResult[0])[0]);
            } 
        SMMA_block_62(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._Curr_smma = Double.NaN;
        }
    }

    private void SMMA_block_62(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = _tempVar402;
        int argument_3 = 1;
        int argument_4 = _SMMA_Period;
        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] = _SMMA_Period;
        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,
                    "SMMA", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
            if ((new Double(((double [])indicatorResult[0])[0])) == null) {
                this._Prev_smma = Double.NaN;
            } else { 
                this._Prev_smma = (((double [])indicatorResult[0])[0]);
            } 
        EMA_block_63(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._Prev_smma = Double.NaN;
        }
    }

    private void EMA_block_63(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = _tempVar95;
        int argument_3 = 0;
        int argument_4 = _EMA_Period;
        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] = _EMA_Period;
        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._Curr_ema = Double.NaN;
            } else { 
                this._Curr_ema = (((double [])indicatorResult[0])[0]);
            } 
        EMA_block_64(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._Curr_ema = Double.NaN;
        }
    }

    private void EMA_block_64(Integer flow) {
        Instrument argument_1 = defaultInstrument;
        Period argument_2 = _tempVar95;
        int argument_3 = 1;
        int argument_4 = _SMMA_Period;
        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] = _SMMA_Period;
        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._Prev_ema = Double.NaN;
            } else { 
                this._Prev_ema = (((double [])indicatorResult[0])[0]);
            } 
        If_block_14(flow);
        } catch (JFException e) {
            e.printStackTrace();
            console.getErr().println(e);
            this._Prev_ema = Double.NaN;
        }
    }

    private  void If_block_65(Integer flow) {
        double argument_1 = LastTradeEvent.getOrder().getProfitLossInPips();
        double argument_2 = 0.0;
        if (argument_1< argument_2) {
            If_block_66(flow);
        }
        else if (argument_1> argument_2) {
            OpenatMarket_block_50(flow);
        }
        else if (argument_1== argument_2) {
            OpenatMarket_block_50(flow);
        }
    }

    private  void If_block_66(Integer flow) {
        double argument_1 = defaultTradeAmount;
        double argument_2 = 0.01;
        if (argument_1< argument_2) {
            Calculation_block_68(flow);
        }
        else if (argument_1> argument_2) {
            Calculation_block_71(flow);
        }
        else if (argument_1== argument_2) {
            Calculation_block_71(flow);
        }
    }

    private void Calculation_block_67(Integer flow) {
        double argument_1 = _Increment;
        double argument_2 = defaultTradeAmount;
        _LotSize = argument_1 * argument_2;
        Assign_block_80(flow);
    }

    private void Calculation_block_68(Integer flow) {
        double argument_1 = _Orig_Trade_Amount;
        double argument_2 = defaultTradeAmount;
        _LotSize = argument_1 + argument_2;
        Assign_block_80(flow);
    }

    private void Calculation_block_71(Integer flow) {
        double argument_1 = _Step;
        double argument_2 = _Increment;
        _Increment = argument_1 + argument_2;
        Calculation_block_67(flow);
    }

    private  void If_block_72(Integer flow) {
        boolean argument_1 = _TradeTaken;
        boolean argument_2 = true;
        if (argument_1!= argument_2) {
            OpenatMarket_block_50(flow);
        }
        else if (argument_1 == argument_2) {
            If_block_108(flow);
        }
    }

    private  void Assign_block_77(Integer flow) {
        boolean argument_1 = true;
        _TradeTaken =  argument_1;
        }

    private  void Assign_block_80(Integer flow) {
        double argument_1 = _LotSize;
        defaultTradeAmount =  argument_1;
            OpenatMarket_block_50(flow);
    }

    private  void Assign_block_88(Integer flow) {
        double argument_1 = defaultTradeAmount;
        _Orig_Trade_Amount =  argument_1;
            Assign_block_103(flow);
    }

    private  void Assign_block_89(Integer flow) {
        double argument_1 = _Step;
        _Step =  argument_1;
            Assign_block_88(flow);
    }

    private  void If_block_96(Integer flow) {
        boolean argument_1 = _TradeTaken;
        boolean argument_2 = true;
        if (argument_1!= argument_2) {
            OpenatMarket_block_49(flow);
        }
        else if (argument_1 == argument_2) {
            If_block_109(flow);
        }
    }

    private  void If_block_97(Integer flow) {
        double argument_1 = LastTradeEvent.getOrder().getProfitLossInPips();
        double argument_2 = 0.0;
        if (argument_1< argument_2) {
            If_block_98(flow);
        }
        else if (argument_1> argument_2) {
            OpenatMarket_block_49(flow);
        }
        else if (argument_1== argument_2) {
            OpenatMarket_block_49(flow);
        }
    }

    private  void If_block_98(Integer flow) {
        double argument_1 = defaultTradeAmount;
        double argument_2 = 0.01;
        if (argument_1< argument_2) {
            Calculation_block_99(flow);
        }
        else if (argument_1> argument_2) {
            Calculation_block_100(flow);
        }
        else if (argument_1== argument_2) {
            Calculation_block_100(flow);
        }
    }

    private void Calculation_block_99(Integer flow) {
        double argument_1 = _Orig_Trade_Amount;
        double argument_2 = defaultTradeAmount;
        _LotSize = argument_1 + argument_2;
        Assign_block_102(flow);
    }

    private void Calculation_block_100(Integer flow) {
        double argument_1 = _Step;
        double argument_2 = _Increment;
        _Increment = argument_1 + argument_2;
        Calculation_block_101(flow);
    }

    private void Calculation_block_101(Integer flow) {
        double argument_1 = _Increment;
        double argument_2 = defaultTradeAmount;
        _LotSize = argument_1 * argument_2;
        Assign_block_102(flow);
    }

    private  void Assign_block_102(Integer flow) {
        double argument_1 = _LotSize;
        defaultTradeAmount =  argument_1;
            OpenatMarket_block_49(flow);
    }

    private  void Assign_block_103(Integer flow) {
        boolean argument_1 = _tempVar472;
        _Control_time =  argument_1;
            Assign_block_105(flow);
    }

    private  void If_block_104(Integer flow) {
        boolean argument_1 = _Control_time;
        boolean argument_2 = true;
        if (argument_1!= argument_2) {
            If_block_119(flow);
        }
        else if (argument_1 == argument_2) {
            GetTimeUnit_block_16(flow);
        }
    }

    private  void Assign_block_105(Integer flow) {
        boolean argument_1 = _tempVar471;
        _Martingale =  argument_1;
            Assign_block_106(flow);
    }

    private  void Assign_block_106(Integer flow) {
        boolean argument_1 = _tempVar466;
        _TrailingSL =  argument_1;
            Assign_block_110(flow);
    }

    private  void If_block_107(Integer flow) {
        boolean argument_1 = _TrailingSL;
        boolean argument_2 = true;
        if (argument_1!= argument_2) {
        }
        else if (argument_1 == argument_2) {
            TrailingStop_block_59(flow);
        }
    }

    private  void If_block_108(Integer flow) {
        boolean argument_1 = _tempVar469;
        boolean argument_2 = _Martingale;
        if (argument_1!= argument_2) {
            OpenatMarket_block_50(flow);
        }
        else if (argument_1 == argument_2) {
            If_block_65(flow);
        }
    }

    private  void If_block_109(Integer flow) {
        boolean argument_1 = _tempVar470;
        boolean argument_2 = _Martingale;
        if (argument_1!= argument_2) {
            OpenatMarket_block_49(flow);
        }
        else if (argument_1 == argument_2) {
            If_block_97(flow);
        }
    }

    private  void Assign_block_110(Integer flow) {
        int argument_1 = _EMA_Period;
        _EMA_Period =  argument_1;
            Assign_block_111(flow);
    }

    private  void Assign_block_111(Integer flow) {
        int argument_1 = 13;
        _SMMA_Period =  argument_1;
            Assign_block_112(flow);
    }

    private  void Assign_block_112(Integer flow) {
        IIndicators.AppliedPrice argument_1 = IIndicators.AppliedPrice.MEDIAN_PRICE;
        _Price_Type =  argument_1;
            Assign_block_113(flow);
    }

    private  void Assign_block_113(Integer flow) {
        int argument_1 = defaultStopLoss;
        _SL =  argument_1;
            Assign_block_114(flow);
    }

    private  void Assign_block_114(Integer flow) {
        int argument_1 = defaultTakeProfit;
        _TP =  argument_1;
            Assign_block_115(flow);
    }

    private  void Assign_block_115(Integer flow) {
        int argument_1 = 0;
        _TSL_min10 =  argument_1;
            SMMA_block_61(flow);
    }

    private  void If_block_118(Integer flow) {
        double argument_1 = _Curr_ema;
        double argument_2 = _Curr_smma;
        if (argument_1< argument_2) {
            If_block_56(flow);
        }
        else if (argument_1> argument_2) {
        }
        else if (argument_1== argument_2) {
        }
    }

    private  void If_block_119(Integer flow) {
        double argument_1 = LastAskCandle.getClose();
        double argument_2 = _Curr_ema;
        if (argument_1< argument_2) {
            If_block_118(flow);
        }
        else if (argument_1> argument_2) {
            If_block_120(flow);
        }
        else if (argument_1== argument_2) {
        }
    }

    private  void If_block_120(Integer flow) {
        double argument_1 = _Curr_ema;
        double argument_2 = _Curr_smma;
        if (argument_1< argument_2) {
        }
        else if (argument_1> argument_2) {
            If_block_55(flow);
        }
        else if (argument_1== argument_2) {
        }
    }

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;
        }
    }
}