Topics
22 Nov 2018, 23:48
 1265
 6
24 Oct 2016, 18:08
 2115
 1
14 Oct 2014, 20:45
 2521
 3
Replies

9718853
22 Nov 2014, 15:10

RE:

Thank you Tradermatrix...

Both are set to m1 bars from Server (open prices)... Could it be that my robot uses Close prices for Moving Average calculations and that could be messing with results?!?

 

tradermatrix said:

optimization setting that you choose, data: tick data  or m1 bar from server ?
make sure to use the same choice for optimization and backtesting.

 


@9718853

9718853
22 Nov 2014, 12:20

Morning all,

 

If I optimise the parameters of my robot and then run a back test I get different results... How can this be? I've been through every parameter and setting to ensure that they match up...

Could the problem be in my code? 

Any ideas would be greatly appreciated...

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class BolliBot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Band Height (pips)", DefaultValue = 40.0, MinValue = 0)]
        public double BandHeightPips { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 1)]
        public int StopLossInPips { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 40, MinValue = 1)]
        public int TakeProfitInPips { get; set; }

        [Parameter("Volume", DefaultValue = 10000, MinValue = 1000)]
        public int Volume { get; set; }

        [Parameter("RSI High Trigger", DefaultValue = 60)]
        public int highRSI { get; set; }

        [Parameter("RSI Low Trigger", DefaultValue = 40)]
        public int lowRSI { get; set; }

        [Parameter("Bollinger Bands Deviations", DefaultValue = 2)]
        public double Deviations { get; set; }

        [Parameter("Bollinger Bands Periods", DefaultValue = 20)]
        public int Periods { get; set; }

        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter("Consolidation Periods", DefaultValue = 2)]
        public int ConsolidationPeriods { get; set; }

        //[Parameter("MA Periods", DefaultValue = 150)]
        //public int MAPeriods { get; set; }

        [Parameter("Ten MA Periods", DefaultValue = 150)]
        public int TenMAPeriods { get; set; }

        [Parameter("FortyFive MA Periods", DefaultValue = 150)]
        public int FortyFiveMAPeriods { get; set; }

        [Parameter("Daily MA Periods", DefaultValue = 150)]
        public int DailyMAPeriods { get; set; }

        [Parameter("True Range Limit", DefaultValue = 0.0005)]
        public double TrueRangeLimit { get; set; }

        [Parameter("Limit Positions", DefaultValue = 100)]
        public int LimitPositions { get; set; }

        [Parameter("SI Limit Move", DefaultValue = 12)]
        public int LimitMove { get; set; }

        private MarketSeries series10;
        private MarketSeries series45;
        private MarketSeries seriesDaily;


        private SwingIndex si;
        private TrueRange tri;
        private RelativeStrengthIndex rsi;
        //private MovingAverage ma;
        private MovingAverage ma10;
        private MovingAverage ma45;
        private MovingAverage madaily;
        BollingerBands bollingerBands;
        string label = "BolliBot cBot";
        int consolidation;

        protected override void OnStart()
        {
            bollingerBands = Indicators.BollingerBands(Source, Periods, Deviations, MAType);
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
            tri = Indicators.TrueRange();
            si = Indicators.SwingIndex(LimitMove);
            series10 = MarketData.GetSeries(TimeFrame.Minute10);
            series45 = MarketData.GetSeries(TimeFrame.Minute45);
            seriesDaily = MarketData.GetSeries(TimeFrame.Daily);


            //ma = Indicators.MovingAverage(MarketSeries.Close, MAPeriods, MAType);
            ma10 = Indicators.MovingAverage(series10.Close, TenMAPeriods, MAType);
            ma45 = Indicators.MovingAverage(series45.Close, FortyFiveMAPeriods, MAType);
            madaily = Indicators.MovingAverage(seriesDaily.Close, DailyMAPeriods, MAType);
        }


        protected override void OnBar()
        {

            var top = bollingerBands.Top.Last(1);
            var bottom = bollingerBands.Bottom.Last(1);
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
            var triL = tri.Result.Last(1);
            var siCurrent = si.Result.Last(0);
            var siLast = si.Result.Last(1);
            //var Ma = ma.Result.Last(0);
            var TenMa = ma10.Result.Last(0);
            var FortyFiveMa = ma45.Result.Last(0);
            var DailyMa = madaily.Result.Last(0);


            if (top - bottom <= BandHeightPips * Symbol.PipSize)
            {
                consolidation = consolidation + 1;
            }
            else
            {
                consolidation = 0;
            }

            if (consolidation >= ConsolidationPeriods)
            {

                if (Positions.Count < LimitPositions && Symbol.Ask > top && rsi.Result.LastValue < highRSI && triL > TrueRangeLimit && siCurrent > siLast && FortyFiveMa > DailyMa)
                {

                    Print("Long Position. Num positions: " + Positions.Count);
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLossInPips, TakeProfitInPips);

                    consolidation = 0;
                }

                else if (Positions.Count < LimitPositions && Symbol.Bid < bottom && rsi.Result.LastValue > lowRSI && triL > TrueRangeLimit && siCurrent < siLast && FortyFiveMa < DailyMa)
                {

                    Print("Short Position. Num positions: " + Positions.Count);
                    ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLossInPips, TakeProfitInPips);

                    consolidation = 0;
                }
            }




            if (top - bottom <= BandHeightPips * Symbol.PipSize)
            {
                consolidation = consolidation + 1;
            }
            else
            {
                consolidation = 0;
            }

            if (consolidation >= ConsolidationPeriods)
            {

                if (Positions.Count < LimitPositions && Symbol.Ask > top && rsi.Result.LastValue > highRSI && triL < TrueRangeLimit && siCurrent < siLast && FortyFiveMa < DailyMa)
                {
                    Print("Short Position 2. Num positions: " + Positions.Count);
                    ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label, StopLossInPips, TakeProfitInPips);

                    consolidation = 0;
                }

                else if (Positions.Count < LimitPositions && Symbol.Bid < bottom && rsi.Result.LastValue < lowRSI && triL < TrueRangeLimit && siCurrent > siLast && FortyFiveMa > DailyMa)
                {

                    Print("Long Position 2. Num positions: " + Positions.Count);
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLossInPips, TakeProfitInPips);

                    consolidation = 0;
                }
            }
        }


        protected override void OnTick()
        {

            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
            var TenMa = ma10.Result.Last(0);
            var FortyFiveMa = ma45.Result.Last(0);
            var DailyMa = madaily.Result.Last(0);



            if (shortPosition != null && FortyFiveMa > DailyMa)
            {
                foreach (var position in Positions)
                {
                    if (position.GrossProfit < 0)
                    {

                        Print("Closing All Losing Positions. Num positions: " + Positions.Count + "Due to Weakening Bearish Trend");
                        ClosePosition(position);
                    }
                }
            }

            if (longPosition != null && FortyFiveMa < DailyMa)
            {
                foreach (var position in Positions)
                {
                    if (position.GrossProfit < 0)
                    {
                        Print("Closing All Losing Positions. Num positions: " + Positions.Count + "Due to Weakening Bullish Trend");
                        ClosePosition(position);
                    }
                }
            }
        }
    }
}

 


@9718853

9718853
12 Nov 2014, 11:49

Timer

Can you please provide an example of using the timer to start on sundays market open and close on Fridays market close? Every reference to the Timer Object seems to use it to trade quicker than on Tick....


@9718853

9718853
05 Nov 2014, 18:43

Hi all, the robot below should close all open positions if any of the TakeProfit levels are hit or one of the 2 active StopLoss (StopLoss2) levels are hit...

Can anyone please tell me why it isn't working?

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class RecoveryZone : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter()]
        public DataSeries SourceSeries { get; set; }

        [Parameter("Slow Periods", DefaultValue = 280)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", DefaultValue = 20)]
        public int FastPeriods { get; set; }

        [Parameter("Trigger", DefaultValue = -10)]
        public int Trigger { get; set; }

        [Parameter("Take Profit", DefaultValue = 50)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss", DefaultValue = 0)]
        public int StopLoss { get; set; }

        [Parameter("Stop Loss 2", DefaultValue = 10)]
        public int StopLoss2 { get; set; }

        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

        private Position longPosition = null;
        private Position shortPosition = null;
        private Position longHedge1 = null;
        private Position shortHedge1 = null;
        private Position longHedge2 = null;
        private Position shortHedge2 = null;

        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "RecoveryZone";

        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
        }

        private void closeAllPositions()
        {
            foreach (var position in Positions)
            {
                ClosePosition(position);
            }

            longPosition = null;
            shortHedge1 = null;
            longHedge1 = null;
            shortHedge2 = null;
            longHedge2 = null;

        }

        protected override void OnBar()
        {
            /*var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);*/

            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);

            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && Positions.Count == 0)
            {
                Print("Long Sequence Started");
                longPosition = ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume, label, StopLoss, TakeProfit).Position;
            }
            else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && Positions.Count == 0)
            {
                Print("Short Sequence Started");
                shortPosition = ExecuteMarketOrder(TradeType.Sell, Symbol, InitialVolume, label, StopLoss, TakeProfit).Position;
            }

            if (longPosition != null && longPosition.Pips <= Trigger && shortHedge1 == null)
            {
                Print("shortHedge1 Sell 20K");
                shortHedge1 = ExecuteMarketOrder(TradeType.Sell, Symbol, InitialVolume * 2, label, StopLoss, TakeProfit - 40).Position;
            }

            if (shortHedge1 != null && shortHedge1.Pips <= Trigger && longHedge1 == null)
            {
                Print("longHedge1 Buy 40K");
                longHedge1 = ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume * 4, label, StopLoss2, TakeProfit - 45).Position;
            }


            if (shortPosition != null && shortPosition.Pips <= Trigger && longHedge2 == null)
            {
                Print("longHedge2 Buy 20K");
                longHedge2 = ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume * 2, label, StopLoss, TakeProfit - 40).Position;
            }

            if (longHedge2 != null && longHedge2.Pips <= Trigger && shortHedge2 == null)
            {
                Print("shortHedge2 Sell 40K");
                shortHedge2 = ExecuteMarketOrder(TradeType.Sell, Symbol, InitialVolume * 4, label, StopLoss2, TakeProfit - 45).Position;
            }



            if ((longPosition != null && longPosition.Pips >= TakeProfit) || (shortHedge1 != null && shortHedge1.Pips >= TakeProfit))
            {

                if (longHedge1 != null && longHedge1.Pips <= StopLoss2)
                {
                    Print("Closing ALL in Long Sequence" + Positions.Count);
                    closeAllPositions();
                }

                if ((shortPosition != null && shortPosition.Pips >= TakeProfit) || (longHedge2 != null && longHedge2.Pips >= TakeProfit))
                {
                    if (shortHedge2 != null && shortHedge2.Pips <= StopLoss2)
                    {
                        Print("Closing ALL in Short Sequence" + Positions.Count);
                        closeAllPositions();
                    }
                }
            }
        }
    }
}

 

Thank you in advance, Ian


@9718853

9718853
30 Oct 2014, 00:19

Great, thank you....


@9718853

9718853
18 Oct 2014, 23:15

I'm actually trying to make the robot below, simple stuff but I'm new to coding... Any advice would be much appreciated..

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class HedgeBot : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 100000, MinValue = 0)]
        public int InitialVolume { get; set; }

        [Parameter("Take Profit", DefaultValue = 50)]
        public int TakeProfit { get; set; }
        
        [Parameter("Stop Loss", DefaultValue = 20)]
        public int StopLoss { get; set; }        

        private const string label = "HedgeBot";

        protected override void OnStart()
        {
            ExecuteMarketOrder(InitialVolume, TradeType.Sell);
            ExecuteMarketOrder(InitialVolume, TradeType.Buy);
        }   
                      
        protected override void Ontick()
        { 
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
            { 
                if (Longposition.GrossProfit > 10);
        }
            ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume * 2);    
            }
                else if (Shortposition.GrossProfit > 10);
            {
                ExecuteMarketOrder (TradeType.Sell, Symbol, InitialVolume * 2);
            }
        }
            ExecuteMarketOrder(tradeType, Symbol, volume, StopLoss, TakeProfit);
            }   
        }
    }
}

Thank you in advance, Ian


@9718853

9718853
16 Oct 2014, 00:53

Hi Emeeder, Thank you for commenting...

I've yet to come across such statements in CAlgo, I'll investigate in the morning... If you have any example code with these statements in use it would be great to see a snippet?

My robot is very simple: Whenever a 14 period Exponential moving average is above a 100 period EMA it creates a short position, and the opposite for a long position.... I've placed a 5 pip delay on entry to prevent over trading and the results are very good in consolidating markets...

The robot is profitable (without a stop loss) as the gains during consolidation are greater than the losses when trending... 

Now I simply need to reverse the trade temporarily when trend develops to reduce the drawdown and increase profit...

I'll post back here if I can find the solution tomorrow...

Thanks again..


@9718853

9718853
15 Oct 2014, 11:17

Thank you so much for this Tradermatrix...

After backtesting it seems that the stop loss is effective but a new trade is immediately placed... 

We really need to make the bot wait until the Moving Averages cross again to trigger entry, is this possible? 

Many Thanks,

Ian


@9718853

9718853
14 Oct 2014, 20:40

Excellent... Thank you....


@9718853