incrément or step

Created at 05 Jul 2016, 18:57
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

incrément or step
05 Jul 2016, 18:57


Hello
I look for a formula that is less dangerous than a martingale.
I want to work incrementally.
when the position is closed (losing) the next trade is increased by the initial volume or step .... When he wins he comes back to the original volume etc ...
in my simplified example (even if the end result is positive), there is an error, because of the change of direction "buy and sell".
Does anyone have a formula (steadily increasing volume increments)
thanks in advance..

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 RSI : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Periods", DefaultValue = 14)]
        public int Periods { get; set; }

        [Parameter("InitialVolume", DefaultValue = 10000)]
        public int InitialVolume { get; set; }

        private RelativeStrengthIndex rsi;

        int Volume2;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);

            Positions.Closed += OnPositionClosed;
        }

        protected override void OnTick()
        {
            if (rsi.Result.LastValue < 30)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (rsi.Result.LastValue > 70)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }
        }

        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll("RSI", Symbol, tradeType))
                ClosePosition(position);
        }

        private void Open(TradeType tradeType)
        {

            var cBotPositions = Positions.FindAll("RSI");

            if (cBotPositions.Length >= 1)
                return;


            ExecuteMarketOrder(tradeType, Symbol, Volume2, "RSI");


            Volume2 = Volume2 + InitialVolume;

        }


        private void OnPositionClosed(PositionClosedEventArgs args)
        {

            Print("Closed");

            var position = args.Position;

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



            if (position.GrossProfit > 0)
                Volume2 = InitialVolume;

        }
    }
}

 


@tradermatrix
Replies

... Deleted by UFO ...

tradermatrix
07 Jul 2016, 13:18

lucian thank you
I have made a backtesting on 01 January and I have found a mistake (if opposite Close Signal)
but keeping part of your code, I managed to get what I wanted.
thank you again for your support on the forum.
 

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 RSI : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Periods", DefaultValue = 14)]
        public int Periods { get; set; }

        [Parameter("InitialVolume", DefaultValue = 10000)]
        public int InitialVolume { get; set; }

        [Parameter("Step_Volume", DefaultValue = 10000)]
        public int stepvolume { get; set; }

        private RelativeStrengthIndex rsi;

        public long Volume;

        protected override void OnStart()
        {

            rsi = Indicators.RelativeStrengthIndex(Source, Periods);

            Positions.Closed += OnPositionClosed;

            Volume = InitialVolume;
        }


        protected override void OnTick()
        {

            if (rsi.Result.LastValue < 30)
            {

                Open(TradeType.Buy);
                Close(TradeType.Sell);

            }
            else if (rsi.Result.LastValue > 70)
            {

                Open(TradeType.Sell);
                Close(TradeType.Buy);


            }
        }

        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll("RSI", Symbol, tradeType))
                ClosePosition(position);
        }

        private void Open(TradeType tradeType)
        {

            var cBotPositions = Positions.FindAll("RSI");

            if (cBotPositions.Length >= 1)
                return;


            ExecuteMarketOrder(tradeType, Symbol, Volume, "RSI");

        }


        private void OnPositionClosed(PositionClosedEventArgs args)
        {

            Print("Closed");

            var position = args.Position;

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

            if (position.GrossProfit > 0)
            {
                Volume = InitialVolume;

            }

            else if (position.GrossProfit < 0)
            {
                Volume = position.Volume + stepvolume;

            }
        }
    }
}



 


@tradermatrix