Replies

brokerof.net@gmail.com
30 Mar 2017, 14:44

RE:

Ok, I incorporated it as you said and changed the lines 19 and 58 with the latest update. Builds just fine.

Now I started to run optimizations on it and noticed that several best passes may sometimes differ in Daily Target parameters ranging from 0.25 to 3.5

So I was wondering whether the logic above stops the bot after current balance exceeds the Daily Target just once? I mean, if the DT is say 1% and on Monday the profit reaches that, then on Tuesday it seems to still check whether the profit is 1% higher than the initial weekly balance (which is true because the day before we reached the value and the bot stopped).

// sets new daily net target on each Monday
            if (Time >= nextMonday)
            {
                weeklyStartingBalance = Account.Balance;
                dailyNetTarget = weeklyStartingBalance * DailyTargetPercentage / 100;
            }

The idea was if Monday hit the Daily Target, bot stops to go on Tuesday in order to try to make another 1% and then stop until Wednesday, etc.

So in the line

dailyNetTarget = weeklyStartingBalance * DailyTargetPercentage / 100;

I guess I should multiply DailyTargetPercentage by the day number (e.g 1 for Mon, 2 for Tue, etc)? Or it is easier to redefine the weeklyStartingBalance once the Daily Target is met and the bot is awaiting new day?

 

P.S.: appreciate ur help very much, tmc! Already with your previous code update the line graphs now become more edgy, which is a good thing I guess, because it stops from excessive trading and risk. So if I find the "holy grail", you will be the first to know)) http://dl4.joxi.net/drive/2017/03/30/0015/2663/1018471/71/5e0c17967e.jpg 

 

tmc. said:

Yes or you can put both between brackets.

if (!isPausedTillNextDay)
{
    if (shortSignal)
    {
        EnterInPosition(TradeType.Sell);
    }
    else if (longSignal)
    {
        EnterInPosition(TradeType.Buy);
    }
}

One more thing that should be updated in case the cBot would be started on Sunday.

Line 19: DateTime lastMonday = Time.AddDays(1 - (Time.DayOfWeek == 0 ? 7 : (int)Time.DayOfWeek)).Date

Line 58: nextMonday = Time.AddDays(8 - (Time.DayOfWeek == 0 ? 7 : (int)Time.DayOfWeek)).Date;

Please keep in mind I haven't tested it so there might be more to fix.

 


@brokerof.net@gmail.com

brokerof.net@gmail.com
29 Mar 2017, 06:42

RE:

Wow, tmc! You're a real cAlgo shaman!) Thanks for being so elaborate!

Didn't realize my "simple" idea involves that much code)) o_O

Great you made the Daily Target as a variable parameter, now I can run it thru optimization and see which number works best (if any:)

Now let me make sure I add it correctly into the core logic. So I'm copying the code you provided into corresponding segments of the existing bot (like OnStart, OnTick sections) and then I have the opening positions part which looks like this:

                     if (shortSignal)
                    {
                        EnterInPosition(TradeType.Sell);
                    }
                    else if (longSignal)
                    {
                        EnterInPosition(TradeType.Buy);
                    }

If I get it correctly, I should be adding in both of the if/else-if brackets something like && !isPausedTillNextDay expression, right?

 

 

tmc. said:

Updated code.

using System;
using System.Linq;
using cAlgo.API;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NotTestedSample : Robot
    {
        [Parameter("Daily Target %", DefaultValue = 1)]
        public double DailyTargetPercentage { get; set; }

        private DateTime nextMonday, nextDay;
        private double weeklyStartingBalance, dailyNetTarget;
        private bool isPausedTillNextDay;

        protected override void OnStart()
        {
            DateTime lastMonday = Time.AddDays(-(int)Time.DayOfWeek + 1).Date;
            HistoricalTrade trade = History.LastOrDefault(x => x.ClosingTime <= lastMonday); // finds last historical trade before this week

            weeklyStartingBalance = trade != null ? trade.Balance : Account.Balance; // if there is historical trade it uses its balance for better accuracy
            dailyNetTarget = weeklyStartingBalance * DailyTargetPercentage / 100;

            nextMonday = Time.AddDays(8 - (int)Time.DayOfWeek).Date;
            nextDay = Time.AddDays(1).Date;

            Positions.Closed += OnPositionClosed;
        }

        private void OnPositionClosed(PositionClosedEventArgs obj)
        {
            double dailyNet = History.Where(x => x.ClosingTime.Date == Time.Date).Sum(x => x.NetProfit); // sums net profit of current day

            // pauses core logic if daily net target was exceeded
            if (dailyNet >= dailyNetTarget)
            {
                isPausedTillNextDay = true;
            }
        }

        protected override void OnTick()
        {
            // unpauses core logic if it's new day
            if (Time >= nextDay)
            {
                isPausedTillNextDay = false;

                nextDay = Time.AddDays(1).Date;
            }

            // sets new daily net target on each Monday
            if (Time >= nextMonday)
            {
                weeklyStartingBalance = Account.Balance;
                dailyNetTarget = weeklyStartingBalance * DailyTargetPercentage / 100;

                nextMonday = Time.AddDays(8 - (int)Time.DayOfWeek).Date;
            }

            // passes if core logic isn't paused
            if (!isPausedTillNextDay)
            {
                // Put your core logic here
            }
        }
    }
}

 

 


@brokerof.net@gmail.com