Topics
01 Aug 2019, 20:55
 1230
 6
29 Jul 2019, 16:20
 1015
 2
08 Sep 2018, 13:30
 3217
 6
06 Sep 2018, 16:18
 3421
 9
Replies

useretinv
19 Nov 2022, 12:40

RE:

admin said:

The code executes only within a specific time interval of the day. The input parameters for the start and stop time are in hours. For instance, if the Start Hour is 10 and the Stop Hour is 12, the robot will only execute when the server time is between 10am and 12am.

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot]
    public class SampleTradingTime2 : Robot
    {
        private DateTime _startTime;
        private DateTime _stopTime;
        private BollingerBands _bollingerBands;

        [Parameter("Start Hour", DefaultValue = 10.0)]
        public double StartTime { get; set; }

        [Parameter("Stop Hour", DefaultValue = 12.0)]
        public double StopTime { get; set; }


        protected override void OnStart()
        {
            // Start Time is the same day at 22:00:00 Server Time
            _startTime = Server.Time.Date.AddHours(StartTime);

            // Stop Time is the next day at 06:00:00
            _stopTime = Server.Time.Date.AddHours(StopTime);

            Print("Start Time {0},", _startTime);
            Print("Stop Time {0},", _stopTime);

            _bollingerBands = Indicators.BollingerBands(MarketSeries.Close, 20, 2, MovingAverageType.Simple);
        }

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

            var currentHours = Server.Time.TimeOfDay.TotalHours;
            bool tradeTime = StartTime < StopTime
                ? currentHours > StartTime && currentHours < StopTime
                : currentHours < StopTime || currentHours > StartTime;

            if (!tradeTime)
                return;

            if (Positions.Count != 0) return;

            var top = _bollingerBands.Top.LastValue;
            var bottom = _bollingerBands.Bottom.LastValue;

            if (Symbol.Ask > top)                
                ExecuteMarketOrder(TradeType.Buy, Symbol, 10000);
            else if (Symbol.Bid < bottom)
                ExecuteMarketOrder(TradeType.Sell, Symbol, 10000);
        }
    }
}

 

 

i just need to integrate that code to Sample Martingale cBot for purpose to trading it at spasific times 

 

 

// -------------------------------------------------------------------------------------------------
//
//    This code is a cTrader Automate API example.
//
//    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.
//    
//    All changes to this file might be lost on the next application update.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
//    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 cAlgo.API;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleMartingalecBot : Robot
    {
        [Parameter("Initial Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double InitialQuantity { get; set; }

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

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


        private Random random = new Random();

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

            ExecuteOrder(InitialQuantity, GetRandomTradeType());
        }

        private void ExecuteOrder(double quantity, TradeType tradeType)
        {
            var volumeInUnits = Symbol.QuantityToVolumeInUnits(quantity);
            var result = ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "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.SymbolName != SymbolName)
                return;

            if (position.GrossProfit > 0)
            {
                ExecuteOrder(InitialQuantity, GetRandomTradeType());
            }
            else
            {
                ExecuteOrder(position.Quantity * 2, position.TradeType);
            }
        }

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

 


@useretinv

useretinv
16 Dec 2019, 14:35

RE:

PanagiotisCharalampous said:

Hi useretinv,

I am not sure what do you mean when you say.

to apply new Multi-Symbol Back testing code

This cBot is not a multisymbol cBot. It just trades one symbol. Do you want this cBot to trade on many symbols at the same time?

Best Regards,

Panagiotis 

 yes , i want this code to trade on many symbols at the same time .


@useretinv

useretinv
02 Aug 2019, 19:20

Can i add symbols i need to the code? I couldn't make the back test on multi symbol, back test available only for ever pair separately
@useretinv

useretinv
02 Aug 2019, 19:08

Very important
Multi symbols backtest is critical for me now to continue using ctrader. Please do it ASAP. it is very important.
@useretinv

useretinv
02 Aug 2019, 13:33

Thank you, But X refers to what?
@useretinv

useretinv
10 Sep 2018, 16:17

OK , just sample code to start and stop cbot  within  specified hours , plz

and maximum volume that cbot can use from account

 

thank you 


@useretinv

useretinv
10 Sep 2018, 15:31

RE:

Panagiotis Charalampous said:

Hi useretinv,


In this forum we are trying to help programmers to use cTrader and cAlgo by addressing questions related to the API and the usage of the platform and by providing suggestions in development questions.

If you wish me to provide a sample on how to do what you have asked and then adjust the code yourself, I am more than happy to do so.

But understanding what a 300 lines cBot does and making the change you request is a task that could take some hours and can be considered custom development service. In this case, you should contact a professional.

Let me know if want me to provide you with a code sample.

Best Regards,
Panagiotis

 

 

yes, i just want a sample code to close all position when max positions is reached , i just posted the full code cause i thought that will make it easy to help if you see the full code . that's all 

 Similarly here

https://ctrader.com/forum/cbot-support/13700 

 


@useretinv

useretinv
10 Sep 2018, 13:49

Great help !!

cBots API Help

Great Fourm Name !!! 


@useretinv

useretinv
08 Sep 2018, 11:58

  . Many thanks Mr.Panagiotis 

 .IT WORKS

 

 PLZ help me to ADD server hours to Martingle sample code , Open and close  cbot Automatically at specified hours

  i tryied to put

( if (Server.Time.Hour >= 7 && Server.Time.Hour < 10 

 ( as you advised in ( https://ctrader.com/forum/cbot-support/13565 

 but it's not working with Sample Martingle Code .plz help

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

Also i want to Add a simple logic to the code to prevent it opening new positions if the the first position hits the stop loss in the current bar .

 for Example : if the code was working on 30min bar chart , and the first position hits a stop loss. don't open new position at the same bar (30min bar ) i want the cbot to wait 

another 2 or 3 bars latter to open the new position with the  Initial Volume .

 

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

Also plz i want to add Maximum volume to prevent cbot to doubling position volume with no limit , just add parameter of maximum orders or maximum volume that cbot can use  from Account to open position , after reaching the maximum orders , make cbot start over withthe  Initial Volume  .

 

ALL IN Martingle Sample code Plz

 

Many many Thanks in advance, Mr.Panagiotis

 

// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API sample.
//
//    This robot 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
//
//    All changes to this file will be lost on next application start.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
//    The "Sample Martingale Robot" 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 robot 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;
 
namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleMartingaleRobot : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }
 
        [Parameter("Stop Loss", DefaultValue = 40)]
        public int StopLoss { get; set; }
 
        [Parameter("Take Profit", DefaultValue = 40)]
        public int TakeProfit { get; set; }
         
        private Random random = new Random();
 
        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;
 
            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.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;
        }
    }
}

 

 

 


@useretinv