Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy Not Being Called...

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

    Strategy Not Being Called...

    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Just seeing how this stuff works!!!
    /// </summary>
    [Description("Just seeing how this stuff works!!!")]
    public class RivTestNumber1 : Strategy
    {
    #region Variables
    // Wizard generated variables
    private bool closeAboveMidBand = false; // Default setting for CloseAboveMidBand
    private bool openBelowMidBand = false; // Default setting for OpenBelowMidBand
    private bool lowBelowBottomBand1 = false; // Default setting for LowBelowBottomBand1
    private bool lowBelowBottomBand2 = false; // Default setting for LowBelowBottomBand2
    private int initialSimpleMovingAverage = 1; // Default setting for InitialSimpleMovingAverage
    private int justOne = 1; // Default setting for JustOne

    private bool tradeFinalized = false;
    private bool strategyActive = false;
    private bool lookForLows = false;

    private int lowCount = 0;
    private int barChangeCount = 0;
    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    Print( "In Initialize()" );
    //set period to 3 minutes (3 minute chart, instead of every tick change)
    //Add(PeriodType.Minute, 3);

    //set initial SMA to current SMA (used to determine when OPEN and/or CLOSE and/or HIGH and/or LOW
    //are above/below it (for current run of strategy)
    InitialSimpleMovingAverage = SMA(14)[0];
    Print("InitialSimpleMovingAverage = SMA(14)[0]: " + SMA(14)[0] );

    //set trafeFinalized to false, once a successful sell/buy is executed, this will be set to true
    tradeFinalized = false;
    Log("tradeFinalized = false", LogLevel.Information);

    //set runStrategy to false, indicating that the OPEN/CLOSE/HIGH/LOW are not all above the SMA
    strategyActive = false;
    Log("strategyActive = false", LogLevel.Information);

    lookingForLows = false;
    Log("lookingForLows = false", LogLevel.Information);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    //only process if this is a change in the primary bar data
    //if (BarsInProgress != 0)
    // return;

    //if onBarUpdate the tradeFinalized bool is true, need to call Initialize() again
    if( tradeFinalized )
    {
    Initialize();
    Log("call Initialize from OnBarUpdate", LogLevel.Information);
    }

    //if this strategy is not yet active, check to see if we want to make it active
    if( !strategyActive )
    {
    Log("strategyActive check was false", LogLevel.Information);

    //make strategy active when the entire candlestick is completely above the SMA
    if( High[0]>InitialSimpleMovingAverage && Low[0]>InitialSimpleMovingAverage && Open[0]>InitialSimpleMovingAverage && Close[0]>InitialSimpleMovingAverage )
    {
    strategyActive = true;
    PlaySound(@"C:\Program Files\NinjaTrader 6.5\sounds\Alert4.wav");

    Log("NOW ACTIVE - candlestick body below the SMA", LogLevel.Information);
    }
    }

    //run the strategy logic only if it is active
    if( strategyActive )
    {
    Log("strategyActive = true", LogLevel.Information);

    //only want to run this piece of code if the candlestick has not dropped below the
    //SMA for the first time
    if( !lookForLows )
    {
    Log("not looking for lows yet", LogLevel.Information);

    //look for the entire body of the candlestick to drop below SMA
    if( Open[0] < SMA(14)[0] && Close[0] < SMA(14)[0] && Low < SMA(14)[0] )
    {
    //now we are looking for 2 lows to be below the bottom of the bollinger band so we
    //can buy
    lookForLows = true;
    Log("NOW we are looking for lows!!!", LogLevel.Information);
    }

    return;
    }

    //once we enter into this code, we are looking for 2 lows below bollinger low
    //(when close < open) in 3 changes
    if( lookForLows )
    {
    //only counting lows that go through bollinger low if they have a close < high
    if( Close[0] < Open[0] )
    {
    //increment for every bar change
    barChangeCount++;

    //increment the bar changes when the low is less than bollinger low
    if( Low[0] < Bollinger(2, 14).Lower[0] )
    {
    lowCount++;
    }
    }

    //complete strategy by selling
    //only
    if( lowCount == 2 && barChangeCount <= 3 )
    {
    //enter a short (single contract)
    EnterShort( 1 );

    //set profit target
    SetProfitTarget( CalculationMode.Ticks, 8 );

    //set stop loss
    SetTrailStop("Short", CalculationMode.Ticks, 3, true);

    //set tradeFinalized to true
    tradeFinalized = true;
    }
    else if( barChangecount > 3 )
    {
    Initialize();
    }
    }
    }
    }

    #region Properties
    [Description("Ensures current low is above mid band")]
    [Category("Parameters")]
    public bool CloseAboveMidBand
    {
    get { return closeAboveMidBand; }
    set { closeAboveMidBand = value; }
    }

    [Description("Marks the move from the top of bollinger band to the bottom")]
    [Category("Parameters")]
    public bool OpenBelowMidBand
    {
    get { return openBelowMidBand; }
    set { openBelowMidBand = value; }
    }

    [Description("Looks for first low below absolute bottom band")]
    [Category("Parameters")]
    public bool LowBelowBottomBand1
    {
    get { return lowBelowBottomBand1; }
    set { lowBelowBottomBand1 = value; }
    }

    [Description("Looks for second low below absolute bottom band")]
    [Category("Parameters")]
    public bool LowBelowBottomBand2
    {
    get { return lowBelowBottomBand2; }
    set { lowBelowBottomBand2 = value; }
    }

    [Description("Grab the initial SMA so we can determine when we drop below it")]
    [Category("Parameters")]
    public int InitialSimpleMovingAverage
    {
    get { return initialSimpleMovingAverage; }
    set { initialSimpleMovingAverage = Math.Max(1, value); }
    }

    [Description("")]
    [Category("Parameters")]
    public int JustOne
    {
    get { return justOne; }
    set { justOne = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #2
    My Goal...

    I suppose I should describe my goal of this strategy...

    Bar#1
    I want it to become active when all parts (open, close, high, low) of the candlestick are above the middle of the bollinger band.

    Bar#2
    Then, when the open, close, and low are below that middle band (and the close is less than the open) I want to start looking for lows that fall below the low band.

    Bars#3-5
    If 2 of the next 3 bands have lows that fall below the low band, I want to go short.



    Other Questions
    ----------------
    - If I set a trailing band, can I still set a profit target so that trailing will only apply once that profit target is met?

    - I am used to OO programmig, bub now I am thinking for this that I should set my profit target and stops, etc. prior to the EnterShort( 1 ) line.....is that right???


    Thanks again.

    Comment


      #3
      Jordan,

      I will not be able to evaluate your code line by line, but for the most part to do what you want you could probably use some temp variables.

      On bar #1 if your condition is true, set temp1 = true.
      On bar #2 if temp1=true and condition=true, temp2 = true and temp2BarNum=CurrentBar. Else temp1=false.
      On bar #3 if temp2=true and CurrentBar < temp2BarNum + 3 and condition=true, temp3 = true
      Same for bar 4 and 5.

      If you have at least two trues on temp3, 4, 5 then you can go trade.

      If you want to submit a trail only after certain amount in profit you will need to just use something like ExitShortStop() and mimic the trail stop in your own code.

      You would call the Set() methods before the Enter() method to get those orders submitted as Enter() goes in.
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        Thanks for the reply Josh.

        I understand not being able to evaluate my code. The real issue is not the logic at this point, though it may need some tweaking.

        The problem is, even my Print statement in the Initialize() method is not showing up in the output window...so I don't believe any part of the strategy is being put into place.

        I am not certain why this is.

        Comment


          #5
          Oh yeah...

          I tried running the SampleMaCrossover and it was called. How come the icon for the strategy is different....

          Is it because mine is user defined?

          Also, how come my new strategies dont want to show up in the strategy list when I want to apply them....is there some way for me to update this list?

          Thanks

          Comment


            #6
            You should check the Control Center logs for run time errors. Also, you should not call Initialize() from OnBarUpdate(). If you have any logic that you want you should not put it in Initialize() and make your own method to contain it.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              If I can't call Initialize() from OnBarUpdate, how can I have this strategy happen more than once throughout the day?

              Make more other methods to reset everything?

              Once this is running, it runs continuously right?

              Thanks

              Comment


                #8
                Not sure what you mean. OnBarUpdate triggers every single time you have a bar update. Every single bar throughout the day triggers OnBarUpdate and the strategy processes code in that block for each event. There should never be a reason for you to need to reinitialize and in fact doing so may have unknown adverse effects.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Ok

                  Ok, thanks a bunch man. I will rework the code and let you know how it works out.

                  Jordan

                  Comment


                    #10
                    Startegy Creation Problem

                    I am also unable to see any new strategies I create using the wizard (whether I unlock the code or not)...

                    Each time I get the error 'error on generating strategy' and the debug window error does not make any sense to me...

                    'The namespace 'NinjaTrader.Strategy' already contains a definition for 'SampleMACrossOver' on line 26, but line 26 is a variable declaration/initialization: private int myInput0 = 1;

                    Thanks
                    [IMG]file:///C:/Documents%20and%20Settings/Rich%20Ninja/My%20Documents/My%20Pictures/strategy.creation.error.JPG[/IMG]

                    Comment


                      #11
                      You can't create a new strategy with the same name as SampleMACrossOver as that already exists.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        Right, but I am using a different name each time. The wizard still creates the file by the new name in the strategies folder, but perhaps this has something to do with why I cannot chose/see the new strategies in the list of strategies when I try to put it into practice.

                        Comment


                          #13
                          Jordan,

                          It sounds like your script environment is messed up. You will need to go through your files and find the conflict where it has two definitions and remove the duplicate. Alternatively you can just uninstall, delete everything in Documents\NinjaTrader 6.5 and reinstall to get a clean environment.
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            Well, I figured it out. A couple issues were present...some because I didn't know better, and some due to who knows why.

                            1. A second strategy in the folder (like you said) had the same name as another. How it happened I don't know, because I didn't try to name two the same, and it clearly won't let me if I did want to. Oh well.

                            2. I assumed (the dumb part) that when I saved a strategy that it would be compiled and be ready for use. It must have been compiled somewhere along the line, but the previous error (dup name) didn't allow compilation to take place (since when one change is made, everything gets compiled). That is why no new strategies were showing up! So now I hit F5 when I make a change and it compiles everything. My bad.

                            So, now my logging entries are working and my code runs. Just have to fine tune it now.


                            Thanks for your help Josh.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by xiinteractive, 04-09-2024, 08:08 AM
                            5 responses
                            13 views
                            0 likes
                            Last Post NinjaTrader_Erick  
                            Started by swestendorf, Today, 11:14 AM
                            2 responses
                            5 views
                            0 likes
                            Last Post NinjaTrader_Kimberly  
                            Started by Mupulen, Today, 11:26 AM
                            0 responses
                            2 views
                            0 likes
                            Last Post Mupulen
                            by Mupulen
                             
                            Started by Sparkyboy, Today, 10:57 AM
                            1 response
                            6 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by TheMarlin801, 10-13-2020, 01:40 AM
                            21 responses
                            3,918 views
                            0 likes
                            Last Post Bidder
                            by Bidder
                             
                            Working...
                            X