How can I repeat a script

Created at 13 Nov 2020, 01:49
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!
andreicristianro's avatar

andreicristianro

Joined 16.07.2020

How can I repeat a script
13 Nov 2020, 01:49


Hi! I want to make this repeat in backtesting please :D

 

 

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 korakodmyX : Robot
    {
        [Parameter("Initial Quantity (Lots)", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double InitialQuantity { get; set; }
        [Parameter("Pips Down", DefaultValue = 30)]
        public int pips { get; set; }
        [Parameter("Profit $", DefaultValue = 2)]
        public int Profit { get; set; }

        [Parameter("Label", DefaultValue = "korakodmy")]
        public string Label { get; set; }
        public int lastposID;


        protected override void OnStart()
        {

            ExecuteOrder(InitialQuantity, TradeType.Buy);
        }
        protected override void OnTick()
        {
            if (CalculateProfit(TradeType.Buy) > Profit)
                foreach (var pos in Positions.FindAll(Label))
                    if (pos.TradeType == TradeType.Buy)
                    {
                        ClosePosition(pos);
                    }
            foreach (var pos in Positions.FindAll(Label))
            {
                if (pos.Id == lastposID && pos.Pips <= -pips)
                {
                    ExecuteOrder(pos.Quantity * 2, TradeType.Buy);
                }
            }
        }
       

        private void ExecuteOrder(double quantity, TradeType tradeType)
        {
            var volumeInUnits = Symbol.QuantityToVolume(quantity);
            var result = ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, Label);

            if (result.IsSuccessful)
                lastposID = result.Position.Id;
        }
        private double CalculateProfit(TradeType tradetype)
        {
            double _sum = 0;
            foreach (var pos in Positions)
            {
                if ((pos.SymbolCode == Symbol.Code) && (pos.Label == Label) && pos.TradeType == tradetype)
                {
                    _sum += pos.NetProfit;
                }
            }
            return _sum;

        }

    }
}

@andreicristianro
Replies

PanagiotisCharalampous
13 Nov 2020, 15:25

Hi andreicristianro,

You need to be more specific. To repeat what? What does the cBot do? What should it do instead?

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous