Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Why is sell being triggered at point not called in code?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Why is sell being triggered at point not called in code?

    Hey there... I'm working with a strategy, and I see in the backtest chart, some of the positions are closing when I don't expect... I have one condition check that can call ExitLong(), and that is if(Close[0]<SMA(5)[0]... my positions are exiting with the close over the sma line. The whole method is below, and an image is attached(The 5per sma is the blue dashed line). Any help would be great...

    Code:
    protected override void OnBarUpdate()
            {
                if(CurrentBar<85)
                {
                    Print("Not enough bars. Current bar count: " + CurrentBar);
                    return;
                }
                else
                {
                    if(Close[0]>SMA(5)[0])
                        if(Volume[0]>=(SMA(Volume,10)[1])*2)
                            if(Close[0]>(MAX(High,10)[1]))
                                if(Slope(SMA(50),11,1)<0)
                                    if(CrossAbove(SMA(10),SMA(20),20))
                                        if(CountIf(delegate {return Close[0]>SMA(20)[0];},10)>4)
                                                if(CountIf(delegate {return (SMA(10)[0]<SMA(20)[0])&&(SMA(20)[0]<SMA(50)[0]);},30)>9)
                                                    EnterLong();
                    
                    if(Close[0]<SMA(5)[0])
                        ExitLong();
                }
            }
    Attached Files

    #2
    Hello,

    If you're checking that all these conditions are true, please put them all in the same statement. You can separate the conditions using the && operator.

    Code:
    			 if(CurrentBar<85)
                {
                    Print("Not enough bars. Current bar count: " + CurrentBar);
                    return;
                }
                else
                {
                    if(Close[0]>SMA(5)[0] &&
                        Volume[0]>=(SMA(Volume,10)[1])*2 &&
                            Close[0]>(MAX(High,10)[1]) &&
                                Slope(SMA(50),11,1)<0 &&
                                    CrossAbove(SMA(10),SMA(20),20) &&
                                        CountIf(delegate {return Close[0]>SMA(20)[0];},10)>4 &&
                                                CountIf(delegate {return (SMA(10)[0]<SMA(20)[0])&&(SMA(20)[0]<SMA(50)[0]);},30)>9)
                                                    EnterLong();
                    
                    if(Close[0]<SMA(5)[0])
                        ExitLong();
    			}
    MatthewNinjaTrader Product Management

    Comment


      #3
      Syntax issue?

      Thanks Matthew. Is the syntax incorrect? I did that for speed of execution to not have to check all conditions if an early condition is false. Is the code structure related to my sell issue?

      Comment


        #4
        I believe it is related to the structure. Current EnterLong is only called when the last condition is true.
        MatthewNinjaTrader Product Management

        Comment


          #5
          Thanks..

          Thanks again. My understanding of C# is that when you eliminate curly braces, only one statement immediately following the conditional check can be executed. One for the .NET gurus...

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by RideMe, 04-07-2024, 04:54 PM
          5 responses
          28 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by f.saeidi, Today, 08:13 AM
          1 response
          4 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by DavidHP, Today, 07:56 AM
          1 response
          6 views
          0 likes
          Last Post NinjaTrader_Erick  
          Started by kujista, Today, 06:23 AM
          3 responses
          9 views
          0 likes
          Last Post kujista
          by kujista
           
          Started by Mindset, Yesterday, 02:04 AM
          2 responses
          18 views
          0 likes
          Last Post NinjaTrader_RyanS  
          Working...
          X