Why this robot doesn't respond to the command to operate only in minimum spread?

Created at 13 Jan 2016, 16:48
tommy.994's avatar

tommy.994

Joined 11.01.2016

Why this robot doesn't respond to the command to operate only in minimum spread?
13 Jan 2016, 16:48


This is a simple Martingala system with the command to open a trade only if the spread is less than "0,006" (0.6 in USD/JPY for example), but it don't work!

The code seems correct, I can't find the problem!

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


namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HEREHEREHERE : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 1000, MinValue = 0)]
        public int InitialVolume { get; set; }

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

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

        [Parameter("Delay minute", DefaultValue = 1)]
        public int Minute { get; set; }



        private Random random = new Random();



//-----------------------------------------------------------

        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;
            if (Symbol.Spread < 0.006)
            {
                Positions.Closed += OnPositionsClosed;

                Order1();
            }
            if (Symbol.Spread > 0.006)
            {
                var stopwatch = Stopwatch.StartNew();
                System.Threading.Thread.Sleep(Minute * 600);
                Console.WriteLine(stopwatch.ElapsedMilliseconds);
                OnStart();



            }

        }

//-----------------------------------------------------------


        private void Order1()
        {
            ExecuteOrder(InitialVolume, GetRandomTradeType());


        }

//-----------------------------------------------------------
        private void ExecuteOrder(long volume, TradeType tradeType)
        {



            var result = ExecuteMarketOrder(tradeType, Symbol, volume, "Martingale", StopLoss, TakeProfit);

            if (result.Error == ErrorCode.NoMoney)
                Stop();

        }
//-----------------------------------------------------------
        private void OnPositionsClosed(PositionClosedEventArgs args)
        {

            Print("Closed");
            var position = args.Position;


            {

                if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code)
                    return;

                if (position.Pips > 0)
                    Order1();

                if (position.GrossProfit > 0)
                {
                    ExecuteOrder(InitialVolume, GetRandomTradeType());
                }
                else
                {

                    ExecuteOrder((int)position.Volume * 2, position.TradeType);

                }
            }
        }


        private TradeType GetRandomTradeType()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
        }
    }
}

 


@tommy.994
Replies

Spotware
13 Jan 2016, 16:50

Dear Trader,

We do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You can contact one of our Partners or post a job in Development Jobs section for further coding assistance.


@Spotware

trend_meanreversion
14 Jan 2016, 13:39

RE:

tommy.994 said:

This is a simple Martingala system with the command to open a trade only if the spread is less than "0,006" (0.6 in USD/JPY for example), but it don't work!

The code seems correct, I can't find the problem!

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


namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HEREHEREHERE : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 1000, MinValue = 0)]
        public int InitialVolume { get; set; }

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

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

        [Parameter("Delay minute", DefaultValue = 1)]
        public int Minute { get; set; }



        private Random random = new Random();



//-----------------------------------------------------------

        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;
            if (Symbol.Spread < 0.006)
            {
                Positions.Closed += OnPositionsClosed;

                Order1();
            }
            if (Symbol.Spread > 0.006)
            {
                var stopwatch = Stopwatch.StartNew();
                System.Threading.Thread.Sleep(Minute * 600);
                Console.WriteLine(stopwatch.ElapsedMilliseconds);
                OnStart();



            }

        }

//-----------------------------------------------------------


        private void Order1()
        {
            ExecuteOrder(InitialVolume, GetRandomTradeType());


        }

//-----------------------------------------------------------
        private void ExecuteOrder(long volume, TradeType tradeType)
        {



            var result = ExecuteMarketOrder(tradeType, Symbol, volume, "Martingale", StopLoss, TakeProfit);

            if (result.Error == ErrorCode.NoMoney)
                Stop();

        }
//-----------------------------------------------------------
        private void OnPositionsClosed(PositionClosedEventArgs args)
        {

            Print("Closed");
            var position = args.Position;


            {

                if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code)
                    return;

                if (position.Pips > 0)
                    Order1();

                if (position.GrossProfit > 0)
                {
                    ExecuteOrder(InitialVolume, GetRandomTradeType());
                }
                else
                {

                    ExecuteOrder((int)position.Volume * 2, position.TradeType);

                }
            }
        }


        private TradeType GetRandomTradeType()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
        }
    }
}

 

 

Are you trying to run it on USDJPY ? The spread on USDJPY must be of order of 0.6 , 0.8 types so it is indeed never less than "0.006"  so it is not falling in your "IF" condition block. Always use condition like this 

If( Symbol.Spread < 6 * Symbol.Pipsize ) {

// do something

 


@trend_meanreversion

tommy.994
14 Jan 2016, 16:53

It's the same thing.

If I active the robot when the spread is high it does nothing, okay, that's good, but in the moment that the spread becomes low the order doesn't start :(


@tommy.994