delay

Created at 06 May 2015, 18:32
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
TR

tradermatrix

Joined 24.07.2012

delay
06 May 2015, 18:32


hello
 I want to delay the cycle of my robot after each profit or loss

for that,I am looking for a formula that my robot starts one minute after it is turned on.

exemple:

   protected override void OnStart()
         {
  var sellOrderTargetPrice = Symbol.Bid - PipsAway * Symbol.PipSize; PlaceStopOrder(TradeType.Sell, Symbol, InitialVolume, sellOrderTargetPrice, Label, StopLoss, TakeProfit);

   var buyOrderTargetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;PlaceStopOrder(TradeType.Buy, Symbol, InitialVolume, buyOrderTargetPrice, Label, StopLoss, TakeProfit);
}

with this example StopOrders are instant to start the robot.
I would like if possible delay of one minute after startup.
cordially.

 


@tradermatrix
Replies

mindbreaker
07 May 2015, 08:39

RE:

tradermatrix said:

hello
 I want to delay the cycle of my robot after each profit or loss

for that,I am looking for a formula that my robot starts one minute after it is turned on.

exemple:

   protected override void OnStart()
         {
  var sellOrderTargetPrice = Symbol.Bid - PipsAway * Symbol.PipSize; PlaceStopOrder(TradeType.Sell, Symbol, InitialVolume, sellOrderTargetPrice, Label, StopLoss, TakeProfit);

   var buyOrderTargetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;PlaceStopOrder(TradeType.Buy, Symbol, InitialVolume, buyOrderTargetPrice, Label, StopLoss, TakeProfit);
}

with this example StopOrders are instant to start the robot.
I would like if possible delay of one minute after startup.
cordially.

 

http://www.dotnetperls.com/sleep

Thread.Sleep(60000);

https://msdn.microsoft.com/en-us/library/system.datetime.now%28v=vs.110%29.aspx

or use datatime.

if (timenow < timetostart){

print("waiting for triger");

}


@mindbreaker

mindbreaker
07 May 2015, 08:43

RE: RE:
while(timenow < timetostart){

print("waiting for triger");

}

 


@mindbreaker

tradermatrix
07 May 2015, 19:04

Hello and thank you ..
 thanks to you, I found this solution;

using System.Diagnostics;
using System.Threading;

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

   protected override void OnStart()

{

 var stopwatch = Stopwatch.StartNew();
            System.Threading.Thread.Sleep(Minute * 60000);
            Console.WriteLine(stopwatch.ElapsedMilliseconds);

......

it works well,

.but I am having a problem ...
I can not make Bactesting (endless), or optimization.

do you think that there is a solution?

I'll give you an example with sample martingale.
I have changed the code to delay between winning trades.


it works very well in demo or real, but backtesting impossible ...
 cordially

/////////////////////////////////////////////////////////////////////////////////////////////////////////

// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API sample.
//
//    This cBot is intended to be used as a sample and does not guarantee any particular outcome or
//    profit of any kind. Use it at your own risk
//
//    The "Sample Martingale cBot" creates a random Sell or Buy order. If the Stop loss is hit, a new 
//    order of the same type (Buy / Sell) is created with double the Initial Volume amount. The cBot will 
//    continue to double the volume amount for  all orders created until one of them hits the take Profit. 
//    After a Take Profit is hit, a new random Buy or Sell order is created with the Initial Volume amount.
//
// -------------------------------------------------------------------------------------------------

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 SampleMartingalecBot : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

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

        [Parameter("Take Profit", DefaultValue = 10)]
        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()
        {
            var stopwatch = Stopwatch.StartNew();
            System.Threading.Thread.Sleep(Minute * 60000);
            Console.WriteLine(stopwatch.ElapsedMilliseconds);


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


@tradermatrix

mindbreaker
07 May 2015, 19:29

RE:

Maybe change thread to:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace TimeStamp
{
    class Program
    {

        public static Int32 time = 0;

        static void Main(string[] args)
        {

                // Timestamp now unix format seconds
                time = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

                // timestamp + 20 seconds start after 20 sec
                int timeStart = time + 20;
            
            while (true) {
                // Timestamp now unix format seconds
                Int32 timenow = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

                if (timenow > timeStart) { Console.WriteLine(time); }

            }
        }
    }
}

 


@mindbreaker

mindbreaker
07 May 2015, 19:33

RE: RE:

Use < insert code>  button when You add cBot code:

// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API sample.
//
//    This cBot is intended to be used as a sample and does not guarantee any particular outcome or
//    profit of any kind. Use it at your own risk
//
//    The "Sample Martingale cBot" creates a random Sell or Buy order. If the Stop loss is hit, a new 
//    order of the same type (Buy / Sell) is created with double the Initial Volume amount. The cBot will 
//    continue to double the volume amount for  all orders created until one of them hits the take Profit. 
//    After a Take Profit is hit, a new random Buy or Sell order is created with the Initial Volume amount.
//
// -------------------------------------------------------------------------------------------------

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 SampleMartingalecBot : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

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

        [Parameter("Take Profit", DefaultValue = 10)]
        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()
        {
            var stopwatch = Stopwatch.StartNew();
            System.Threading.Thread.Sleep(Minute * 60000);
            Console.WriteLine(stopwatch.ElapsedMilliseconds);


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

 

 

 


@mindbreaker

tradermatrix
07 May 2015, 20:37

"Use < insert code>  button when You add cBot code:"

ok leader...

My code works but:

optimizer with my code is impossible....!!

 


@tradermatrix

mindbreaker
07 May 2015, 20:55

RE:

tradermatrix said:

"Use < insert code>  button when You add cBot code:"

ok leader...

My code works but:

optimizer with my code is impossible....!!

 

It looks nicer :D:D:D

I dont know, maybe support know it?

 


@mindbreaker

tradermatrix
07 May 2015, 22:19

RE: RE:

mindbreaker said:

tradermatrix said:

"Use < insert code>  button when You add cBot code:"

ok leader...

My code works but:

optimizer with my code is impossible....!!

 

It looks nicer :D:D:D

I dont know, maybe support know it?

 

ok I ask "admin" a solution.

I managed to include the code in other robots and c is good (except bascktesting)

thank you again


@tradermatrix