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

Strategy Not Working As Coded

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

    Strategy Not Working As Coded

    This is the coded part. Basically trying to sell when above Oscillator is above 75 then crosses below 78 and buy when crossabove 26. I'm not worrying as much about buying at the moment as I'm just trying to get going short working properly.

    Within the attached photo you will see that the strategy should have entered short at the begging of the blue line and exited at the end. My question is:
    why in the world is this not working at coded? Is there a maximum number of if statements then it doesn't respond as well? It's as if it only reads 25% of my if-statements or something.

    #region Variables
    // Wizard generated variables
    private int my_TakeProfit = 19; // Default setting for My_TakeProfit
    // User defined variables (add any user defined variables below)
    private int bars_Ago = 0;
    private int ulte_Fast = 7;
    private int ulte_Int = 14;
    private int ulte_Slow = 28;
    private int oB_Num = 78;
    private int oS_Num = 26;
    #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()
    {
    DefaultQuantity = 10000;
    Add(UltimateOscillator(Ulte_Fast, Ulte_Int, Ulte_Slow));
    SetStopLoss("", CalculationMode.Ticks, 200, false);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Short Section
    #region Short


    // If UO goes above OB_Num then Crosses below the OB_Num it will then finally enter short.
    if ( UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow)[Bars_Ago] >= 75
    && CrossBelow(UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow), OB_Num, 1))
    {
    EnterShort(DefaultQuantity, "Shorting D1");
    }
    // If our position is open short and the market then goes back above the OB_Num we exit the position taking our loss as soon as possible.
    else if ( Position.MarketPosition == MarketPosition.Short
    && CrossAbove(UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow), OB_Num, 1) )
    {
    ExitShort("", "Exiting D1 S");
    }

    else if ( Position.MarketPosition == MarketPosition.Short
    && UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow)[Bars_Ago] <= (OB_Num +1))
    {
    ExitShort("", "Exit Short Profit");
    }

    else if ( Position.MarketPosition == MarketPosition.Short
    && UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow)[Bars_Ago] <= 50 )
    {
    if ( CrossAbove(UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow), 40, 1) )
    {
    ExitShort("","Exit Short Profit");
    }
    }
    #endregion




    #region Long
    else if( Position.MarketPosition == MarketPosition.Long
    && CrossAbove(UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow), OB_Num, 1) )
    {
    ExitLong("", "Exit Long Profit");
    }
    // OverSold Section
    // If UO is lessthan or equal to OS_Num we proceed.
    else if ( UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow)[Bars_Ago] <= (OS_Num +1) )
    {
    // If we are under oversold number then crossabove it we will then enter long
    if ( CrossAbove(UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow), OS_Num, 1) )
    {
    EnterLong(DefaultQuantity, "Long D1");
    }
    }
    // If position is == long and the UO falls below the oversold number we exit long position to take our loss.
    else if ( Position.MarketPosition == MarketPosition.Long
    && CrossBelow(UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow), OS_Num, 1) )
    {
    ExitLong("", "Exiting D1 L");
    }

    #endregion
    Attached Files

    #2
    Originally posted by andyshear View Post
    This is the coded part. Basically trying to sell when above Oscillator is above 75 then crosses below 78 and buy when crossabove 26. I'm not worrying as much about buying at the moment as I'm just trying to get going short working properly.

    Within the attached photo you will see that the strategy should have entered short at the begging of the blue line and exited at the end. My question is:
    why in the world is this not working at coded? Is there a maximum number of if statements then it doesn't respond as well? It's as if it only reads 25% of my if-statements or something.

    #region Variables
    // Wizard generated variables
    private int my_TakeProfit = 19; // Default setting for My_TakeProfit
    // User defined variables (add any user defined variables below)
    private int bars_Ago = 0;
    private int ulte_Fast = 7;
    private int ulte_Int = 14;
    private int ulte_Slow = 28;
    private int oB_Num = 78;
    private int oS_Num = 26;
    #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()
    {
    DefaultQuantity = 10000;
    Add(UltimateOscillator(Ulte_Fast, Ulte_Int, Ulte_Slow));
    SetStopLoss("", CalculationMode.Ticks, 200, false);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Short Section
    #region Short


    // If UO goes above OB_Num then Crosses below the OB_Num it will then finally enter short.
    if ( UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow)[Bars_Ago] >= 75
    && CrossBelow(UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow), OB_Num, 1))
    {
    EnterShort(DefaultQuantity, "Shorting D1");
    }
    // If our position is open short and the market then goes back above the OB_Num we exit the position taking our loss as soon as possible.
    else if ( Position.MarketPosition == MarketPosition.Short
    && CrossAbove(UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow), OB_Num, 1) )
    {
    ExitShort("", "Exiting D1 S");
    }

    else if ( Position.MarketPosition == MarketPosition.Short
    && UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow)[Bars_Ago] <= (OB_Num +1))
    {
    ExitShort("", "Exit Short Profit");
    }

    else if ( Position.MarketPosition == MarketPosition.Short
    && UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow)[Bars_Ago] <= 50 )
    {
    if ( CrossAbove(UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow), 40, 1) )
    {
    ExitShort("","Exit Short Profit");
    }
    }
    #endregion




    #region Long
    else if( Position.MarketPosition == MarketPosition.Long
    && CrossAbove(UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow), OB_Num, 1) )
    {
    ExitLong("", "Exit Long Profit");
    }
    // OverSold Section
    // If UO is lessthan or equal to OS_Num we proceed.
    else if ( UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow)[Bars_Ago] <= (OS_Num +1) )
    {
    // If we are under oversold number then crossabove it we will then enter long
    if ( CrossAbove(UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow), OS_Num, 1) )
    {
    EnterLong(DefaultQuantity, "Long D1");
    }
    }
    // If position is == long and the UO falls below the oversold number we exit long position to take our loss.
    else if ( Position.MarketPosition == MarketPosition.Long
    && CrossBelow(UltimateOscillator(ulte_Fast, ulte_Int, ulte_Slow), OS_Num, 1) )
    {
    ExitLong("", "Exiting D1 L");
    }

    #endregion
    Starting from this thread, and reading the links therein might tell you why. After all, you are using a Renko chart.

    ref: http://ninjatrader.com/support/forum...d.php?p=446249

    Comment


      #3
      Hello andyshear,

      I would recommend adding a print to your script that prints the values of the variables used in the if statement.

      Below is a link to a video that demonstrates.
      Dive into manipulating C# code from within an unlocked NinjaScript strategy using the NinjaScript Editor.NinjaTrader 7 is an award winning end to end online ...


      What was the value of the UltimateOscillator?
      Is this equal to or below 75 in the print?

      What was the value of the UltimateOscillator on the previous bar?
      What was the value of the OB_Num variable on the previous bar?
      What was the value of the UltimateOscillator on the current bar?
      What was the value of the OB_Num variable on the current bar? (With the included code this value would remain the same, but this is good to print for a clear comparison)


      As a tip, to export a NinjaTrader 7 NinjaScript do the following:
      1. Click File -> Utilities -> Export NinjaScript
      2. Enter a unique name for the file in the value for 'File name:'
      3. Select the strategy from the objects list on the left -> click the right facing arrow ">" to add the strategy to the export
      4. Click the 'Export' button -> click 'yes' to add any referenced indicators to the export -> click OK to clear the export location message

      By default your exported file will be in the following location:
      • (My) Documents\NinjaTrader 7\bin\Custom\ExportNinjaScript\<export_file_name.z ip>

      Below is a link to the help guide on Exporting NinjaScripts.
      http://www.ninjatrader.com/support/h...nt7/export.htm
      Chelsea B.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by trilliantrader, 04-18-2024, 08:16 AM
      5 responses
      22 views
      0 likes
      Last Post trilliantrader  
      Started by Davidtowleii, Today, 12:15 AM
      0 responses
      3 views
      0 likes
      Last Post Davidtowleii  
      Started by guillembm, Yesterday, 11:25 AM
      2 responses
      9 views
      0 likes
      Last Post guillembm  
      Started by junkone, 04-21-2024, 07:17 AM
      9 responses
      68 views
      0 likes
      Last Post jeronymite  
      Started by mgco4you, Yesterday, 09:46 PM
      1 response
      12 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Working...
      X