Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CCI sell at market is not working properly

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

    CCI sell at market is not working properly

    I'm not great at C# and NinjaScript is new to me. I'm just messing around with strategies and backtesting trying to hone my skills.

    I have a strategy that for all intents and purposes will sell at market when the previous bar closes while the CCI is over 200. I would like to add the converse and buy market when below -200 but i cant even get the first to work.

    it will buy the first order and from what I'm seeing it seems to work for the first one as my 1 minute chart shows every market open with the CCI over 200 because its just been that way this week. but there's always another sell, on the very next candle close, after every profit trigger or stop loss thereafter all session. its just trading nonstop even when the CCI is below 200 it still triggers a sell regardless. i am going to paste my code below I hope it formats right. also I made my own CCI but its just different colors so any code calling for "MyCCI" should be changed to "CCI" if you want to test it for me and help figure out what's wrong. unless its blatant just by looking at my code and I cant see why.

    also "Market Open" is coming up as "Mar****pen" i dont know why but its not that way in my script. thers also not a space but i cant get it to show up otherwise.


    region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class CaseyCCIUnlocked : Strategy
    {
    private CCI CCI1;
    private CCI CCI2;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "CaseyCCIUnlocked";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    TakeProfit = 20;
    StopLoss = 48;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    AddChartIndicator(MyCCI(20));

    SetProfitTarget(@"CaseyCCIUnlocked", CalculationMode.Ticks, TakeProfit);
    SetStopLoss(@"CaseyCCIUnlocked", CalculationMode.Ticks, StopLoss, true);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    //Check if regular trading hours
    bool mar****pen = ToTime(Time[0]) >= 063000 && ToTime(Time[0]) <= 130000;
    double cci20 = MyCCI(20)[0];

    /************************************************** ************************************************** ***************************/

    if (mar****pen)
    {

    /*
    // Long
    if (CrossBelow(CCI(20), -200, 1));
    {
    //EnterLong(Convert.ToInt32(DefaultQuantity), @"CaseyCCIUnlocked");
    EnterLong();
    }

    */



    // Short
    if (CrossAbove(MyCCI(20), 200, 0));
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), @"CaseyCCIUnlocked");

    }

    /************************************************** ************************************************** *******************************/


    }

    // Exit Positions
    if(!mar****pen)
    {
    ExitLong();
    ExitShort();
    }

    }

    region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="TakeProfit", Order=1, GroupName="Parameters")]
    public int TakeProfit
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="StopLoss", Order=2, GroupName="Parameters")]
    public int StopLoss
    { get; set; }
    #endregion

    }
    }

    #2
    Hello ccagle,

    It looks like you have semi colons on the if conditions which terminates them meaning they are not used .

    if (CrossBelow(CCI(20), -200, 1));

    The semi colon at the end needs removed

    if (CrossBelow(CCI(20), -200, 1))

    That would need to be done for both of your if statements.

    Comment


      #3
      Jesse... I am quite embarrassed lol. But I am super thankful for your reply. It has been too long and the semi-colons have always plagued me. I tried that and realized I had the lookback period still set to 0 from my earlier troubleshooting too haha. But that solved my issue and again I'm super appreciative of your help and feedback. I'm just a mess. I will try harder in the future to not bog you guys down with such a stupid errors. Thanks again
      -Casey

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      111 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      60 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      38 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      43 views
      0 likes
      Last Post TheRealMorford  
      Started by Mindset, 02-28-2026, 06:16 AM
      0 responses
      79 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Working...
      X