protected override void OnTick()
        {
            if (Trade.IsExecuting)
            {
                return;
            }

            //Local declaration
            TriState _Open_Sell_Position = new TriState();
            TriState _Open_Buy_Position  = new TriState();

            //Step 1
            _Open      = MarketSeries.Open.Last(1);
            _Close     = MarketSeries.Close.Last(1);
            _Transform = Transform(_Candle_Points, 2);

            //Step 2

            //Step 3

            //Step 4

            //Step 5
            if ((((_Open - (_Close)) >= _Transform) &&
                 (MarketSeries.TickVolume.LastValue == 1)))
            {
                _Open_Sell_Position = _OpenPosition(0, true, Symbol.Code, TradeType.Sell, _Lots, 10, _SL_Points, _TP_Points, "");
            }
            if ((((_Close - (_Open)) >= _Transform) &&
                 (MarketSeries.TickVolume.LastValue == 1)))
            {
                _Open_Buy_Position = _OpenPosition(0, true, Symbol.Code, TradeType.Buy, _Lots, 10, _SL_Points, _TP_Points, "");
            }
        }
        TriState Close_All_Long_Trades(double magicIndex)
        {
            var res = new TriState();

            foreach (Position pos in Positions.FindAll("FxProQuant_" + magicIndex.ToString("F0"), Symbol, TradeType.Buy))
            {
                var result = ClosePosition(pos);
                if (result.IsSuccessful && res.IsNonExecution)
                {
                    res = true;
                }
                else
                {
                    Thread.Sleep(400);
                    res = false;
                }
            }
            return(res);
        }
        protected override void OnTick()
        {
            if (Trade.IsExecuting)
            {
                return;
            }

            //Local declaration
            TriState _Close_All_Long_Trades  = new TriState();
            TriState _Close_All_Short_Trades = new TriState();
            TriState _Open_Sell_Position     = new TriState();
            TriState _Open_Buy_Position      = new TriState();

            //Step 1
            _Historic_data   = MarketSeries.Close.Last(1);
            _Historic_data_1 = MarketSeries.Close.Last(0);

            //Step 2

            //Step 3
            _Price_cross_BB_downward = ((_Historic_data >= i_Bollinger_Bands.Bottom.Last(1)) &&
                                        (_Historic_data_1 < i_Bollinger_Bands_1.Bottom.Last(0)));
            _Price_cross_BB_upward = ((_Historic_data_1 > i_Bollinger_Bands_3.Top.Last(0)) &&
                                      (_Historic_data <= i_Bollinger_Bands_2.Top.Last(1)));

            //Step 4
            if (_Price_cross_BB_downward)
            {
                _Close_All_Long_Trades = Close_All_Long_Trades(0);
            }
            if (_Price_cross_BB_upward)
            {
                _Close_All_Short_Trades = Close_All_Short_Trades(0);
            }
            if (_Price_cross_BB_downward)
            {
                _Open_Sell_Position = _OpenPosition(0, true, Symbol.Code, TradeType.Sell, _Lots, 10, _SL_Points, _TP_Points, "");
            }
            if (_Price_cross_BB_upward)
            {
                _Open_Buy_Position = _OpenPosition(0, true, Symbol.Code, TradeType.Buy, _Lots, 10, _SL_Points, _TP_Points, "");
            }
        }
Example #4
0
        TriState Buy(double magicIndex, double Lots, int StopLossMethod, double stopLossValue, int TakeProfitMethod, double takeProfitValue, double Slippage, double MaxOpenTrades, double MaxFrequencyMins, string TradeComment)
        {
            double?stopLossPips, takeProfitPips;
            int    numberOfOpenTrades = 0;
            var    res = new TriState();

            foreach (Position pos in Positions.FindAll("FxProQuant_" + magicIndex.ToString("F0"), Symbol))
            {
                numberOfOpenTrades++;
            }

            if (MaxOpenTrades > 0 && numberOfOpenTrades >= MaxOpenTrades)
            {
                return(res);
            }

            if (MaxFrequencyMins > 0)
            {
                if (((TimeSpan)(Server.Time - LastTradeExecution)).TotalMinutes < MaxFrequencyMins)
                {
                    return(res);
                }

                foreach (Position pos in Positions.FindAll("FxProQuant_" + magicIndex.ToString("F0"), Symbol))
                {
                    if (((TimeSpan)(Server.Time - pos.EntryTime)).TotalMinutes < MaxFrequencyMins)
                    {
                        return(res);
                    }
                }
            }

            int pipAdjustment = Convert.ToInt32(Symbol.PipSize / Symbol.TickSize);

            if (stopLossValue > 0)
            {
                if (StopLossMethod == 0)
                {
                    stopLossPips = stopLossValue / pipAdjustment;
                }
                else if (StopLossMethod == 1)
                {
                    stopLossPips = stopLossValue;
                }
                else
                {
                    stopLossPips = (Symbol.Ask - stopLossValue) / Symbol.PipSize;
                }
            }
            else
            {
                stopLossPips = null;
            }

            if (takeProfitValue > 0)
            {
                if (TakeProfitMethod == 0)
                {
                    takeProfitPips = takeProfitValue / pipAdjustment;
                }
                else if (TakeProfitMethod == 1)
                {
                    takeProfitPips = takeProfitValue;
                }
                else
                {
                    takeProfitPips = (takeProfitValue - Symbol.Ask) / Symbol.PipSize;
                }
            }
            else
            {
                takeProfitPips = null;
            }

            Slippage /= pipAdjustment;
            long volume = Symbol.NormalizeVolume(Lots * 100000, RoundingMode.ToNearest);

            if (!ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "FxProQuant_" + magicIndex.ToString("F0"), stopLossPips, takeProfitPips, Slippage, TradeComment).IsSuccessful)
            {
                Thread.Sleep(400);
                return(false);
            }
            LastTradeExecution = Server.Time;
            return(true);
        }
Example #5
0
        TriState Simple_Trailing_Stop(double magicIndex, int WaitForProfit, double TrailingStopPoints, double MinAdjustmentPoints)
        {
            double pnlPoints = 0;
            double newSl;
            var    res = new TriState();

            foreach (Position pos in Positions.FindAll("FxProQuant_" + magicIndex.ToString("F0"), Symbol))
            {
                if (pos.TradeType == TradeType.Buy)
                {
                    if (WaitForProfit == 0)
                    {
                        pnlPoints = (Symbol.Bid - pos.EntryPrice) / Symbol.TickSize;
                        if (pnlPoints < TrailingStopPoints)
                        {
                            continue;
                        }
                    }

                    newSl = Math.Round(Symbol.Bid - TrailingStopPoints * Symbol.TickSize, Symbol.Digits);

                    if (pos.StopLoss != null)
                    {
                        if (newSl <= pos.StopLoss)
                        {
                            continue;
                        }
                        if (newSl <= pos.StopLoss + MinAdjustmentPoints * Symbol.TickSize)
                        {
                            continue;
                        }
                    }

                    var result = ModifyPosition(pos, newSl, pos.TakeProfit);
                    if (result.IsSuccessful && res.IsNonExecution)
                    {
                        res = true;
                    }
                    else
                    {
                        Thread.Sleep(400);
                        res = false;
                    }
                }
                else
                {
                    if (WaitForProfit == 0)
                    {
                        pnlPoints = (pos.EntryPrice - Symbol.Ask) / Symbol.TickSize;
                        if (pnlPoints < TrailingStopPoints)
                        {
                            continue;
                        }
                    }

                    newSl = Math.Round(Symbol.Ask + TrailingStopPoints * Symbol.TickSize, Symbol.Digits);

                    if (pos.StopLoss != null)
                    {
                        if (newSl >= pos.StopLoss)
                        {
                            continue;
                        }
                        if (newSl >= pos.StopLoss - MinAdjustmentPoints * Symbol.TickSize)
                        {
                            continue;
                        }
                    }

                    var result = ModifyPosition(pos, newSl, pos.TakeProfit);
                    if (result.IsSuccessful && res.IsNonExecution)
                    {
                        res = true;
                    }
                    else
                    {
                        Thread.Sleep(400);
                        res = false;
                    }
                }
            }
            return(res);
        }
Example #6
0
        protected override void OnTick()
        {
            if (Trade.IsExecuting)
            {
                return;
            }

            //Local declaration
            TriState _Simple_Trailing_Stop   = new TriState();
            TriState _Close_All_Long_Trades  = new TriState();
            TriState _Close_All_Short_Trades = new TriState();
            TriState _Sell_MM        = new TriState();
            TriState _Buy_MM         = new TriState();
            TriState _Buy_Fixed_Lot  = new TriState();
            TriState _Sell_Fixed_Lot = new TriState();

            //Step 1
            _IsTime = IsTime(_Start_Hour, _End_Hour, 0, 0);
            _MACD   = i_MACD.Histogram.Last(0);
            _CCI_0  = i_CCI_0.Result.Last(0);
            _Simple_Trailing_Stop = Simple_Trailing_Stop(0, 0, _Trailing_Stop_Points, 10);
            _Trade_Exists         = Trade_Exists(0);
            _CCI_1 = i_CCI_1.Result.Last(1);

            //Step 2

            //Step 3
            _Sell_Signal = (!_Trade_Exists &&
                            _IsTime &&
                            (_CCI_1 > _CCI_Sell_Threshold) &&
                            (_CCI_0 < _CCI_Sell_Threshold) &&
                            (_MACD > 0));
            _Buy_Signal = (!_Trade_Exists &&
                           (_CCI_1 < _CCI_Buy_Threshold) &&
                           (_CCI_0 > _CCI_Buy_Threshold) &&
                           (_MACD < 0) &&
                           _IsTime);
            _Close_Long_Signal = (_Trade_Exists &&
                                  (_CCI_0 > _Upper_Close_Threshold));
            _Close_Short_Signal = (_Trade_Exists &&
                                   (_CCI_0 < _Lower_Close_Threshold));

            //Step 4
            if (_Close_Long_Signal)
            {
                _Close_All_Long_Trades = Close_All_Long_Trades(0);
            }
            if (_Close_Short_Signal)
            {
                _Close_All_Short_Trades = Close_All_Short_Trades(0);
            }
            _Buy_with_MM = (_Buy_Signal &&
                            _UseMM);
            _Buy_without_MM = (_Buy_Signal &&
                               !_UseMM);
            _Sell_with_MM = (_Sell_Signal &&
                             _UseMM);
            _Sell_without_MM = (_Sell_Signal &&
                                !_UseMM);

            //Step 5
            if (_Sell_with_MM)
            {
                _Sell_MM = Sell(0, ((Account.Equity / (1000)) * (_LotsPer1000)), 0, _Stop_Loss_Points, 0, _Take_Profit_Points, 5, _Max_Opened_Trades, _Max_Frequency_Minutes, "");
            }
            if (_Buy_with_MM)
            {
                _Buy_MM = Buy(0, ((Account.Equity / (1000)) * (_LotsPer1000)), 0, _Stop_Loss_Points, 0, _Take_Profit_Points, 5, _Max_Opened_Trades, _Max_Frequency_Minutes, "");
            }
            if (_Buy_without_MM)
            {
                _Buy_Fixed_Lot = Buy(0, _Lots, 0, _Stop_Loss_Points, 0, _Take_Profit_Points, 5, _Max_Opened_Trades, _Max_Frequency_Minutes, "");
            }
            if (_Sell_without_MM)
            {
                _Sell_Fixed_Lot = Sell(0, _Lots, 0, _Stop_Loss_Points, 0, _Take_Profit_Points, 5, _Max_Opened_Trades, _Max_Frequency_Minutes, "");
            }
        }