How to avoid onTick() running 'too quick' to place 'duplicate orders'

Created at 04 Mar 2021, 18:27
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!
Capt.Z-Partner's avatar

Capt.Z-Partner

Joined 09.05.2020

How to avoid onTick() running 'too quick' to place 'duplicate orders'
04 Mar 2021, 18:27


Hello,

I have a simple cBots here, to place a market sell order when the market price moving up every 2 spread/pips. But I found a tech problem that, onTick() running 'too quick' to place 'duplicate orders'. I suspect it is because that the key variable 'BidAnchor' is not updated according to the newly built position. Please help correct the code to make it run as my original purpose.

Thanks,

Lei

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 AutoSellEvery2Spreads : Robot
    {   
        [Parameter("Start Sell Position:", Group = "SELL Position", DefaultValue = 1.2100 )]
         public double BidAnchor{ get; set; }        
        
        protected override void OnStart(){}

        protected override void OnTick()
        {
            var shortpositions = Positions.FindAll("", Symbol.Name, TradeType.Sell);
            foreach (var position in shortpositions)
            {
                if (position.EntryPrice > BidAnchor)
                {
                    BidAnchor = position.EntryPrice + 0.0002;  
                    //Define next entry price to sell a position
                }
            }

            if (Symbol.Bid >= BidAnchor) 
            { 
                ExecuteMarketOrder(TradeType.Sell, Symbol.Name, 1000);  

                /*
                 * Problem is: when market price moving quickly, BidAnchor has not 
                 * been updated according to a new position. While another OnTick has been 
                 * already on the way, which makes 1.2102 or above, eg. 1.21021 another 
                 * position would be added.
                 *
                 * Question is: how to avoid the 'Too quick' running of OnTick() before 
                 * variable BidAnchor's updates.
                 *
                 * Thank you.
                 */

            }
        } 

        protected override void OnStop(){}

    }
}

 


@Capt.Z-Partner
Replies

DonaldD
05 Mar 2021, 09:18 ( Updated at: 05 Mar 2021, 10:24 )

Hi Delphima, 

I am not sure if this the case, because I cannot easily reproduce it, but if it is, a way to solve it is to keep your own counter of expected open positions and execute an order only when the count of positions meets that counter e.g.

            if (Symbol.Bid >= BidAnchor && positionsCounter == Positions.Count) 
            { 
               positionsCounter++;
.
.
.

of course you will need to manage the counter accordingly e.g. decrease it when a position is closed.

 


@DonaldD

Capt.Z-Partner
05 Mar 2021, 17:53 ( Updated at: 21 Dec 2023, 09:22 )

RE:

DonaldD said:

Hi Delphima, 

I am not sure if this the case, because I cannot easily reproduce it, but if it is, a way to solve it is to keep your own counter of expected open positions and execute an order only when the count of positions meets that counter e.g.

            if (Symbol.Bid >= BidAnchor && positionsCounter == Positions.Count) 
            { 
               positionsCounter++;
.
.
.

of course you will need to manage the counter accordingly e.g. decrease it when a position is closed.

 

Hi Donald,

Thanks for your idea. Yes, it is easier to reproduce the situation when the market price moving quickly. As my screenshot shows below:

I set 1.1921 as the entry price when the price bounced up passing quickly, there're 3 OnTick() run in a very short time, and BidAnchor has not updated yet, so 3 positions have been opened. After a few seconds, as I suppose, BidAnchor has been updated, and while the price passed 1.1923 again very quickly, 2 orders were sent...

 

Well, I just find a temporary solution, if we update BidAnchor just after sending the order, it will surely quicker than the next OnTick(), and it also make sure the next sell price 2 pips higher than this order send e.g Symbol.Bid. 

        if (Symbol.Bid >= BidAnchor) 
            { 
                ExecuteMarketOrder(TradeType.Sell, Symbol.Name, 1000);  
                BidAnchor = Symbol.Bid + 0.0002;
             ...

As another screenshot shows below: entry at 1.1915, every 2 pips added one order, some order's gap is higher than 2 pips, due to market orders executed according to the market price.

Thanks.


@Capt.Z-Partner

... Deleted by UFO ...

mahrenal203
29 Mar 2021, 15:58

RE: RE:

A new name for the trader is fit for the approval of the goals for humans. The rule of the trade and coursework writing service is the volume for the things. An account is opened for Madoff the theories for the suggestions for all people for the game.

 


@mahrenal203