This simple robot has an error, where?

Created at 13 Jan 2016
cTrader Discord
tommy.994's avatar

tommy.994

Joined 11.01.2016

Status

Open


Budget

10.00 USD


Payment Method

Direct Payment

Job Description

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;

            Order1();

        }

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

        private void Order1()
        {
            if (Symbol.Spread < 0.006)
                ExecuteOrder(InitialVolume, GetRandomTradeType());

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

                Order1();
            }
        }

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

 

Comments
Log in to add a comment.
mindbreaker's avatar
mindbreaker · 9 years ago

Come on, one line you can not add?

add this

if (Symbol.Spread < Spr)

before

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

Line:

if (Symbol.Spread < Spr) ExecuteOrder((int)position.Volume * 2, position.TradeType);

tommy.994's avatar
tommy.994 · 9 years ago

I upgraded to the latest version but it does not change anything.

Moreover, the control of the spread is ignored by the new positions doubled from Martingale.

mindbreaker's avatar
mindbreaker · 9 years ago

It works for me (positions are opened).

install again calgo or ctrader.

tommy.994's avatar
tommy.994 · 9 years ago

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 :(


 

mindbreaker's avatar
mindbreaker · 9 years ago

https://en.wikipedia.org/wiki/Millisecond

milliseconds:

System.Threading.Thread.Sleep(Minute * 60 * 1000);

 

mindbreaker's avatar
mindbreaker · 9 years ago

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

        [Parameter("Spread", DefaultValue = 0.006)]
        public double Spr { get; set; }
        


        private Random random = new Random();


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

        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;

            Order1();

        }

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

        private void Order1()
        {
        Print(Symbol.Spread);
            if (Symbol.Spread < Spr){
                ExecuteOrder(InitialVolume, GetRandomTradeType());
                    }else{
                var stopwatch = Stopwatch.StartNew();
                System.Threading.Thread.Sleep(Minute * 6000);
                Print(stopwatch.ElapsedMilliseconds);
                Order1();
            }
        }

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