Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Help by CCI/Kama Strategy

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

    Help by CCI/Kama Strategy

    Hello,

    iam new in C# and after 5 Days triying to realise a simple Strategy with no sucess i search help

    For my first Strategy i use only Draw.ArropUl/Down and no Entryshort/longs, to see what happens.

    What i want is:

    1. Draw Backgroundcolor of Chart Green if CCI > 50 until CCI < -50
    2. Draw Backgroundcolor of Chart Red if CCI < -50 until CCI > 50
    3. If Chart is Green then search for a vaildate Longretest (point 4) and if Chart Red then search for a validate Shortretest (Point 5)
    4. When Close < Kama then Longrestest = true and when on an other Bar Close > Kama then Draw.ArrowUp (see Picture)
    5. When Close > Kama then Shortretest = true and when on an other Bar Close < Kama then Draw ArrowDown (see Picture)

    Thats all, but for me to hard to programm first time

    What i have:

    Code:
    #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 ArFTest : Strategy
        {
            private int CCI_Wert;
            private bool LongRetest;
            private bool ShortRetest;
    
            private CCI CCI1;
            private KAMA KAMA1;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "ArFTest";
                    Calculate                                    = Calculate.OnPriceChange;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.Infinite;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 0;
                    StartBehavior                                = StartBehavior.WaitUntilFlat;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = true;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 50;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration    = true;
                    //CCI_Wert                    = 0;
                    LongRetest                    = false;
                    ShortRetest                    = false;
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {                
                    CCI1                = CCI(Close, 21);
                    KAMA1                = KAMA(Close, 2, 12, 30);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0)
                    return;
    
                if (CurrentBars[0] < 1)
                    return;
    
                 // Hintergrund_Farbe soll je nach CCI Richtung eingestellt werden
                //Long
                if ((CCI1[0] > 50) || (CCI_Wert == 1) )
                {    
                    CCI_Wert = 1;
                    BackBrushAll = Brushes.Olive;
                }
    
                 // Short
                if ((CCI1[0] < -50) || (CCI_Wert == -1) )
                {
                    CCI_Wert = -1;
                    BackBrushAll = Brushes.Salmon;
                }
    
    
    
                 // Set 3
                if ((CCI_Wert == 1) &&  (CrossBelow(CCI1, KAMA1, 1))  )
                {
                    LongRetest = true;
                }
    
                 // Set 4
                if ((CCI_Wert == -1) && (Close[1] > KAMA1[1]) && (Close[2] < KAMA1[2]))
                {
                    ShortRetest = true;
                }
    
                 // Set 5
                if ((LongRetest == true) && (Close[1] > KAMA1[1]))
                {
                    Draw.ArrowUp(this, @"ArFTest Arrow up_1", true, 1, Low[1], Brushes.Black);
                    LongRetest = false;
                    ShortRetest = false;
                }
    
                 // Set 6
                if ((ShortRetest == true) && (Close[1] < KAMA1[1]))
                {
                    Draw.ArrowDown(this, @"ArFTest Arrow down_1", false, 1, High[1], Brushes.Red);
                    ShortRetest = false;
                    LongRetest = false;
                }
    
    
            }
        }
    }
    ​
    Can anybody help me to realize my first C#?

    Many Thanks and have i nice Day.
    Attached Files

    #2
    Hello Dirales,

    Thanks for your post.

    I have tested the code that you shared and it seems that the script is behaving as expected.

    I see the chart background color is switching from an Olive color to a Salmon color when your conditions are true. I also see a Draw object being placed on the chart, such as Draw.ArrowDown().

    If you would like to have more than one arrow object drawn on the chart, you could make the tag name unique. One way of doing this is by adding CurrentBar to the tag name. For example, Draw.ArrowDown(this, @"ArFTest Arrow down_1" + CurrentBar, false, 1, High[1], Brushes.Red);.

    From the Draw.ArrowUp() and Draw.ArrowDown() help guide pages: "If you pass in a value of "myTag", each time this tag is used, the same draw object is modified. If unique tags are used each time, a new draw object will be created each time."

    Draw.ArrowUp(): https://ninjatrader.com/support/help...aw_arrowup.htm
    Draw.ArrowDown(): https://ninjatrader.com/support/help..._arrowdown.htm

    What specifically in your script is not working as you expect it to?

    Please let me know if I may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hello Brandon,

      thanks for your time.

      Yes, it drawing ArrowUps & Downs, but every time!

      But when Chart is Green, it can only gives a ArrowUp when a Longretest = true.

      And this dont work in my Strategy.

      Best Regards

      Comment


        #4
        Hello Dirales,

        Thanks for your note.

        The Draw objects will be drawn on the chart anytime the condition to draw the objects becomes true.

        If the strategy is not behaving as expected, debugging prints should be added to the script to understand how it is behaving and drawing objects on the chart.

        Add debugging prints one line above the condition to draw the object that prints out each value being used in the condition along with the Time[0]/

        An example of adding prints would look something like this:
        Code:
        Print("LongRetest is " + LongRetest + " Close[0] is " + Close[0] + " KAMA1[0] is "  + KAMA1[0] + "  " + Time[0]);
        if ((LongRetest == true) && (Close[1] > KAMA1[1]))
        {
              Draw.ArrowUp(this, @"ArFTest Arrow up_1", true, 1, Low[1], Brushes.Black);
              LongRetest = false;
              ShortRetest = false;
        }​
        Below is a link to a forum post that demonstrates how to use prints to understand behavior.
        https://ninjatrader.com/support/foru...121#post791121

        Please let me know if I may assist with creating a print or analyzing the Output window.​
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Hello Brandon,

          many thanks for your help. I found the Output and see the Value from Close etc.

          I will read your link und when i have more questions i write here

          Best Regards

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Jonafare, 12-06-2012, 03:48 PM
          5 responses
          3,985 views
          0 likes
          Last Post rene69851  
          Started by Fitspressorest, Today, 01:38 PM
          0 responses
          2 views
          0 likes
          Last Post Fitspressorest  
          Started by Jonker, Today, 01:19 PM
          0 responses
          2 views
          0 likes
          Last Post Jonker
          by Jonker
           
          Started by futtrader, Today, 01:16 PM
          0 responses
          7 views
          0 likes
          Last Post futtrader  
          Started by Segwin, 05-07-2018, 02:15 PM
          14 responses
          1,792 views
          0 likes
          Last Post aligator  
          Working...
          X