Take only one trade per day.

Created at 05 Aug 2022, 18:21
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!
AL

Alfie

Joined 23.06.2022

Take only one trade per day.
05 Aug 2022, 18:21


Hello all, 

Is there any way to take only one trade per day.?


@Alfie
Replies

firemyst
06 Aug 2022, 07:59

Yes.

 


@firemyst

Alfie
06 Aug 2022, 09:45

RE:

firemyst said:

Yes.

 

Good ,
Do you have the code? or logic?


@Alfie

firemyst
06 Aug 2022, 11:34

RE: RE:

Alfie said:

firemyst said:

Yes.

 

Good ,
Do you have the code? or logic?

Set a global bot variable called "CountOfTradesToday" = 0;

Set two date/time variables of the "Start time" and "end time" for your day.

Inside your bot's logic:

* check if CountOfTradesToday == 0

* if so, place your trade and increment CountOfTradesToday += 1

* if not, just skip

At end of your defined day, set CountOfTradesToday == 0 again to reset

 


@firemyst

Alfie
07 Aug 2022, 19:47

RE: RE: RE:

firemyst said:

Alfie said:

firemyst said:

Yes.

 

Good ,
Do you have the code? or logic?

Set a global bot variable called "CountOfTradesToday" = 0;

Set two date/time variables of the "Start time" and "end time" for your day.

Inside your bot's logic:

* check if CountOfTradesToday == 0

* if so, place your trade and increment CountOfTradesToday += 1

* if not, just skip

At end of your defined day, set CountOfTradesToday == 0 again to reset

 

Can You provide the code?


@Alfie

paolo.panicali
12 Aug 2022, 13:23 ( Updated at: 12 Aug 2022, 13:24 )

a little issue

Hi, well the problem with what Firemyst suggests is that if along the day for any reason you stop and restart the bot, the counter will go to zero, so for instance if the bot takes a trade in the morning and at noon you have to stop the bot, then in the afternoon when you restart the bot if it gets a signal it will take another trade.

Also, if you have more than one bot working but you only want to take one trade a day, well the counter will count one trade per bot, so that trades will be from 0-4 at least.

I would personally just check the History and Positions of the day,.

So if there are not open positions nor positions closed that had been opened today on this symbol or using this Bot(check using the label when you open a trade) or in total including all symbols and all bots active then I take a new trade whenever I get a valid signal.

Doing so if you have to stop and restart the bot or use multiple bots or the vps crashes or whatever you will always get one trade a day.

Hope that helps

 

https://ctrader.com/api/reference/history

 


@paolo.panicali

paolo.panicali
12 Aug 2022, 15:11 ( Updated at: 21 Dec 2023, 09:22 )

an example

this is the Log tab output and the following is the actual today s history from the history tab.

Now you have the number of the trades taken today, happy ending.

Of course you also need to check for open positions...so it will be a foreach var position in Positions and check how many trades with a certain position.Label or position.SymbolName and with Today s entry time there are....

 

//trade a day, how many trades today?? august 2022

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 OneTradeADay : Robot
    {
        protected override void OnStart()
        {
            var LastTrades = History.OrderByDescending(iTrade => iTrade.EntryTime).Take(100).ToArray();

            int TradesToday = 0, TradesTodayThisSymbol = 0;

            for (int i = 0; i < LastTrades.Length; i++)
            {
                if (LastTrades[i].EntryTime.Day == Bars.OpenTimes.LastValue.Day && LastTrades[i].ClosingTime.Month == Bars.OpenTimes.LastValue.Month && LastTrades[i].ClosingTime.Year == Bars.OpenTimes.LastValue.Year)
                {
                    Print("Symbol : {0}  ClosingTime : {1}", LastTrades[i].SymbolName, LastTrades[i].ClosingTime);
                    TradesToday++;
                }
            }

            Print("---------------------------------------------------------------------------------------");

            for (int i = 0; i < LastTrades.Length; i++)
            {
                if (LastTrades[i].SymbolName == Symbol.Name && LastTrades[i].EntryTime.Day == Bars.OpenTimes.LastValue.Day && LastTrades[i].ClosingTime.Month == Bars.OpenTimes.LastValue.Month && LastTrades[i].ClosingTime.Year == Bars.OpenTimes.LastValue.Year)
                {
                    Print("Symbol : {0}  ClosingTime : {1}", LastTrades[i].SymbolName, LastTrades[i].ClosingTime);
                    TradesTodayThisSymbol++;
                }
            }
        }

    }
}


@paolo.panicali