bot fails to open traders upon signal

Created at 05 Nov 2022, 03:17
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!
SE

sejdinimarkelian

Joined 18.11.2020

bot fails to open traders upon signal
05 Nov 2022, 03:17


this is my code , basically what happens is in backtest orders fills after 1 h candle closes, but majority of the time in live trading brooker fails to fill at the exact time and price level is there any way to improve it to retry 1s or 2 later ? 

 

plsss helpp

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class XAUUSD : Robot
    {
    //atr indicator
    private AverageTrueRange atr;
    
    private HistoricalVolatility hvv;
    private ExponentialMovingAverage emmahvv;
    
    private WilliamsPctR wpctr;
    private ExponentialMovingAverage wpctrema;
    
    private LinearRegressionForecast lrf;
    private ExponentialMovingAverage lrfema;
    
    private OnBalanceVolume obv;
    private ExponentialMovingAverage obvema;
    
    private TradeVolumeIndex tvi;
    private ExponentialMovingAverage tviema;
    
    private Vidya vidyaa;
    private ExponentialMovingAverage vidyaema;
    
    private Aroon aroon;
    
    private AverageDirectionalMovementIndexRating admir;
    
    private DirectionalMovementSystem  dms;
    
    private AcceleratorOscillator accelero;
    
    private AwesomeOscillator aweso;
    
    private MacdCrossOver macdco;
    
    private MacdHistogram macdh;
    
    private BollingerBands bb;
    
    private KeltnerChannels kch;
    
    private IchimokuKinkoHyo ichimokuu;
    
    
    
        //risk parameters
        [Parameter("volume", DefaultValue = 0.05)]
        public double riskpct { get; set; }
        
        //atr periods
        [Parameter("Atr Periods", DefaultValue = 14)]
        public int atrprds { get; set; }
        
        //atr multiplier
        [Parameter("Atr multiplier", DefaultValue = 1.5)]
        public double atrmltp { get; set; }

        
       
        
        //historical volatility
        [Parameter("historical volatil 1", DefaultValue = 20)]
        public int hv1 { get; set; }
        [Parameter("historical volatil 2", DefaultValue = 252)]
        public int hv2 { get; set; }
        [Parameter("historical volatil ema", DefaultValue = 21)]
        public int hvema { get; set; }

        protected override void OnStart()
        {
            //indicators loading
            atr = Indicators.AverageTrueRange(atrprds,MovingAverageType.Exponential);
            
            wpctr= Indicators.WilliamsPctR(14);
            wpctrema= Indicators.ExponentialMovingAverage(wpctr.Result,21);
            
            hvv=Indicators.HistoricalVolatility(Bars.ClosePrices,hv1,hv2);
            emmahvv=Indicators.ExponentialMovingAverage(hvv.Result,hvema);
            
            lrf= Indicators.LinearRegressionForecast(Bars.ClosePrices,9);
            lrfema= Indicators.ExponentialMovingAverage(lrf.Result,21);
            
            
            
        }

        protected override void OnBar()
        {
        
            //variables
            
            var prevatr = Math.Round(atr.Result.Last(1) / Symbol.PipSize);
            var tradeamount = (Account.Equity *riskpct) / (1.5*prevatr *Symbol.PipValue);
            tradeamount = Symbol.NormalizeVolumeInUnits(tradeamount, RoundingMode.Down);
            
            var price = Bars.ClosePrices.Last(1);
            var prevprice = Bars.ClosePrices.Last(2);
            
            var longpst = Positions.Find("long");
            var shortpst = Positions.Find("short");
            
            var emahv=emmahvv.Result.Last(1);
            var hv=hvv.Result.Last(1);
            
            var wpctrr=wpctr.Result.Last(1);
            var prevwpctrr=wpctr.Result.Last(2);
            var wpctrremaa=wpctrema.Result.Last(1);
            var prevwpctrremaa=wpctrema.Result.Last(2);
            
            var lrff=lrf.Result.Last(1);
            var prevlrff=lrf.Result.Last(2);
            var lrffema=lrfema.Result.Last(1);
            var prevlrffema=lrfema.Result.Last(2);
            
            

            // trade entry
            {
            if (Positions.Count<1)
            if (wpctrr>wpctrremaa)
            if (lrff>lrffema)
            if (hv>emahv)
  
            ExecuteMarketOrder(TradeType.Buy , Symbol.Name ,tradeamount, "long" ,atrmltp*prevatr ,0);
            }
            
            
            {
            if (Positions.Count<1)
            if (wpctrr<wpctrremaa)
            if (lrff<lrffema)
            if (hv>emahv)

            ExecuteMarketOrder(TradeType.Sell , Symbol.Name ,tradeamount , "short" ,atrmltp*prevatr ,0);
            }
            
            
            
            
            {
            
            if (shortpst !=null &lrff>lrffema&prevlrff<prevlrffema)
            ClosePosition(shortpst);
            
            }
            
            {
            
            if (longpst !=null &lrff<lrffema&prevlrff>prevlrffema)
            ClosePosition(longpst);
            
            }
            
           
            
        
}
        protected override void OnStop()
        {
            // Handle cBot stop here
            }
    }
}


@sejdinimarkelian
Replies

PanagiotisChar
07 Nov 2022, 12:33

Hi there.

but majority of the time in live trading brooker fails to fill at the exact time and price level is there any way to improve it to retry 1s or 2 later ? 

Can you explain what do you mean with this and what actually happens?

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


 


@PanagiotisChar

sejdinimarkelian
07 Nov 2022, 16:39

Yes i can explain

Basically how the bot works is this. It opens a trade when signal alighn only after 1 hour candle close. So what happens is this, the signal tells the bot to make an order but sometimes doesnt get fill at the time and price that the bot asked for, so now bot doesnt retry until next candle close. What i need help with is making sure when broker doesnt fill the order at the specific price range the bot should retry to reenter at the earliest time posible so it doesnt miss moves waiting for next 1 h candle close


@sejdinimarkelian

PanagiotisChar
08 Nov 2022, 09:24

Hi there,

 the signal tells the bot to make an order but sometimes doesnt get fill at the time and price that the bot asked for

Can you be more specific? Does the order get filled at the wrong price or is it rejected?

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar

sejdinimarkelian
08 Nov 2022, 14:01

Yes it gets rejected sometimes.

So i want the bot to retry after that asap


@sejdinimarkelian