Trades executing late. Several bars after conditions are met. Can you tell me what i'm missing?

Created at 31 Jan 2023, 06:13
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!
DE

deeganpope

Joined 24.01.2023

Trades executing late. Several bars after conditions are met. Can you tell me what i'm missing?
31 Jan 2023, 06:13


I've been trying to tweak this for weeks but i can't figure out why trade execution is late.

I removed several things that i thought might be causing some lag, but it hasn't changed.  When Dema is > than EMA and EMA > SMA and the MACD>Signal it should open a long, and open a short when the opposite is true.  It however is often 3-5 bars late.  I'm using it on JPYUSD on a 3hr time frame.

 

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;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class JPow : Robot
    {
        private ExponentialMovingAverage EMA;
        private DoubleExponentialMovingAverage DEMA;
        private SimpleMovingAverage SMA;
        private MacdCrossOver MACD;
        private Double balancebefore;
        public Boolean shortOpen;
        public Boolean longOpen;
        [Parameter("Trailing Stop Step (pips)", DefaultValue = 25)]
        public double TrailingStopStep { get; set; }
        [Parameter("Percent Change", DefaultValue = 2)]
        public double PercentChange { get; set; }
        
        protected override void OnStart()
        {
            
            EMA = Indicators.ExponentialMovingAverage(Bars.MedianPrices , 9);
            DEMA = Indicators.GetIndicator<DoubleExponentialMovingAverage>(Bars.MedianPrices,9);
            SMA = Indicators.SimpleMovingAverage(Bars.MedianPrices, 21);
            MACD = Indicators.MacdCrossOver(Bars.MedianPrices, 26, 12, 9);
            balancebefore = Account.Balance;
          }
        
        protected override void OnBar()
        {
       

        
        }
        protected override void OnTick()
        {   

        
          if (((MACD.MACD.LastValue < MACD.Signal.LastValue) ) && longOpen == true){
            ClosePositions(TradeType.Buy);
            longOpen = false;
         }
          
         if (((MACD.MACD.LastValue > MACD.Signal.LastValue) ) && shortOpen == true){
            ClosePositions(TradeType.Sell);
            shortOpen = false;
         }
          if (CurrentTrend() == "up" && DEMA.Result.LastValue > EMA.Result.LastValue && EMA.Result.LastValue > SMA.Result.LastValue && (MACD.MACD.LastValue > MACD.Signal.LastValue) && DEMA.Result.IsRising() && EMA.Result.IsRising() && SMA.Result.IsRising() && (longOpen == false)){
            Print("Opening Long");
            longOpen = true;
            double volumePurchase =  (500000); 
            ExecuteMarketOrder(TradeType.Buy, Bars.SymbolName, volumePurchase, "myLabel",TrailingStopStep,null);
            ClosePositions(TradeType.Sell);
         }
         if (CurrentTrend() == "down" && DEMA.Result.LastValue < EMA.Result.LastValue && EMA.Result.LastValue < SMA.Result.LastValue && MACD.MACD.LastValue < MACD.Signal.LastValue && DEMA.Result.IsFalling() && EMA.Result.IsFalling() && SMA.Result.IsFalling() && (shortOpen == false)){
            Print("Opening Short");
            double volumePurchase =  (500000); 
            ExecuteMarketOrder(TradeType.Sell, Bars.SymbolName, volumePurchase, "myLabel",TrailingStopStep,null);
            ClosePositions(TradeType.Buy);
            shortOpen = true;
         }
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
            ClosePositions(TradeType.Buy);
            ClosePositions(TradeType.Sell);
        }
        
          private void ClosePositions(TradeType tradeType)
        {
            var positions = Positions.FindAll("myLabel", Bars.SymbolName, tradeType);
            foreach (var position in positions)
            {
                if (position.TradeType != tradeType) continue;
                ClosePosition(position);
            }
        }
        
        public string CurrentTrend()
        {
                double first = Bars.OpenPrices[^1];
                double last = Bars.ClosePrices[^1];
                double change = ((first-last)/first) *1000;
                if (change > PercentChange/100)
                    {
                    return "up";
                    }
                else if (change < -(PercentChange/100))
                    {
                    return "down";
                    }
                 else{
                    return "crabbing";
                 }
       }
    
    }
}


@deeganpope
Replies

firemyst
31 Jan 2023, 07:42

You're missing a screen capture showing us an example of where you think your bot has gotten in late.

That would be helpful


@firemyst

deeganpope
05 Feb 2023, 07:44 ( Updated at: 21 Dec 2023, 09:23 )


@deeganpope

deeganpope
05 Feb 2023, 07:48

RE:

firemyst said:

You're missing a screen capture showing us an example of where you think your bot has gotten in late.

That would be helpful

Here's another example of the trade initiating 1-2 bars after the trigger conditions are met.

 


@deeganpope

firemyst
05 Feb 2023, 12:01 ( Updated at: 21 Dec 2023, 09:23 )

RE: RE:

deeganpope said:

firemyst said:

You're missing a screen capture showing us an example of where you think your bot has gotten in late.

That would be helpful

Here's another example of the trade initiating 1-2 bars after the trigger conditions are met.

 

1) Looks like the exact same screen capture example as your previous post

2) The "sell 5.00 lots" covers up the DEMA so we can't see what it's doing there

3) have you tried putting PRINT statements in front of your conditions to see whether or not each condition is true or false? Or done any debugging in Visual Studio?

 

//Have you tried stuff like this?

Print ("Current Trend {0}", CurrentTrend());
Print ("DEMA.REsult.LastValue {0}, EMA.Result.LastValue {1}, DEMA.Result.LastValue < EMA.Result.LastValue {2}", DEMA.REsult.LastValue, EMA.Result.LastValue, DEMA.Result.LastValue < EMA.Result.LastValue);

//etc 
//etc
//etc
//Now you know what everything is evaluating to, so you can figure out why your "if" statement isn't evaluating the way you expect

 if (CurrentTrend() == "down" && DEMA.Result.LastValue < EMA.Result.LastValue && EMA.Result.LastValue < SMA.Result.LastValue && MACD.MACD.LastValue < MACD.Signal.LastValue && DEMA.Result.IsFalling() && EMA.Result.IsFalling() && SMA.Result.IsFalling() && (shortOpen == false)){

 


@firemyst