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

Doesn't Place any Trades

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

    Doesn't Place any Trades

    Hey there, I try to make a really simple strategy but it's my first time working with time so I think the issue is based off the time calculation.

    Thesis is simple, You take High and Low of 4PM Candle and once we get a 2Min candle close above or below the 4PM Candle, it should enter.

    Would appreciate any help to fix this issue!


    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 MyStrategy : Strategy
        {
            private bool tradeExecuted = false;
    
            protected override void OnBarUpdate()
            {
                // Check if it's the last bar and 4 PM EST
                if (Bars.Count > 0 && CurrentBar == Bars.Count - 1 && Time[0].Hour == 16 && Time[0].Minute == 0)
                {
                    // Get the high and low of the 2-minute candle at 4 PM EST
                    double high = Highs[1][0];
                    double low = Lows[1][0];
    
                    // Calculate stop loss and take profit levels
                    double stopLoss = low - 10 * TickSize;
                    double takeProfit = high + 10 * TickSize;
    
                    // Check if the closing price is below the low of the 4 PM candle
                    if (Close[0] < low && !tradeExecuted)
                    {
                        // Go short with stop loss and take profit
                        EnterShort();
                        SetStopLoss(CalculationMode.Price, stopLoss);
                        SetProfitTarget(CalculationMode.Price, takeProfit);
                        tradeExecuted = true;
                    }
                    // Check if the closing price is above the high of the 4 PM candle
                    else if (Close[0] > high && !tradeExecuted)
                    {
                        // Go long with stop loss and take profit
                        EnterLong();
                        SetStopLoss(CalculationMode.Price, stopLoss);
                        SetProfitTarget(CalculationMode.Price, takeProfit);
                        tradeExecuted = true;
                    }
                }
                else
                {
                    tradeExecuted = false;
                }
            }
        }
    }​​

    #2
    Hello Lones,

    The code you have shown will only work in realtime as it is checking the CurrentBar == Bars.Count - 1, the subtraction of 1 would also mean the script has to be set to Calculate.OnEachTick.

    I don't see your OnStateChange override, you will also need that in addition to AddDataSeries to use Highs[1] and Lows[1].

    I would suggest using a print here to Print the values of your first condition, for example:

    Code:
    Print(Bars.Count + " " + CurrentBar + " " + Time[0].Hour + ":" +Time[0].Minute);
    if (Bars.Count > 0 && CurrentBar == Bars.Count - 1 && Time[0].Hour == 16 && Time[0].Minute == 0)
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse,

      thank you for the fast response.

      I removed the realtime check part and replaced it so it works with historical data but now it won't show in Strategy Analyzer at all.
      Any idea how I can fix this?

      Thank you!

      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 TestNew : Strategy
          {
              private bool tradeExecuted = false;
      
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      // Set the strategy properties here
                      Calculate = Calculate.OnEachTick; // Ensure calculations happen on each tick
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  // Check if it's the last bar and 4 PM EST
                  if (BarsInProgress == 0 && Bars.Count > 0 && CurrentBar == Bars.Count - 1 && Time[0].Hour == 15 && Time[0].Minute == 0)
                  {
                      // Get the high and low of the 2-minute candle at 4 PM EST
                      double high = Highs[1][0];
                      double low = Lows[1][0];
      
                      // Calculate stop loss and take profit levels
                      double stopLoss = low - 10 * TickSize;
                      double takeProfit = high + 10 * TickSize;
      
                      // Check if the closing price is below the low of the 4 PM candle
                      if (Close[0] < low && !tradeExecuted)
                      {
                          // Go short with stop loss and take profit
                          EnterShort();
                          SetStopLoss(CalculationMode.Price, stopLoss);
                          SetProfitTarget(CalculationMode.Price, takeProfit);
                          tradeExecuted = true;
                      }
                      // Check if the closing price is above the high of the 4 PM candle
                      else if (Close[0] > high && !tradeExecuted)
                      {
                          // Go long with stop loss and take profit
                          EnterLong();
                          SetStopLoss(CalculationMode.Price, stopLoss);
                          SetProfitTarget(CalculationMode.Price, takeProfit);
                          tradeExecuted = true;
                      }
                  }
                  else
                  {
                      tradeExecuted = false;
                  }
              }
          }
      }
      ​
      Last edited by Lones; 02-27-2024, 04:37 AM.

      Comment


        #4
        Hello Lones,

        The new code appears to have the same condition and you are also missing AddDataSeries which is needed to use Highs[1] and Lows[1].



        After adding the necessary data you would also need to remove

        CurrentBar == Bars.Count - 1

        to be able to use that code in a backtest. In a backtest CurrentBar starts at 0 and goes all the way through the Bars.Count so that condition won't become true because it does not enter realtime in a backtest.

        I would still suggest using a print here to Print the values of your first condition, for example:

        Code:
        Print(BarsInProgress + " " + Bars.Count + " " + CurrentBar + " " + Time[0].Hour + ":" +Time[0].Minute);
        if (BarsInProgress == 0 && Bars.Count > 0 && CurrentBar == Bars.Count - 1 && Time[0].Hour == 16 && Time[0].Minute == 0)​
        That will let you output what the script is doing so you can get a better idea of why its not trading.

        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by rhyminkevin, Today, 04:58 PM
        3 responses
        49 views
        0 likes
        Last Post Anfedport  
        Started by iceman2018, Today, 05:07 PM
        0 responses
        5 views
        0 likes
        Last Post iceman2018  
        Started by lightsun47, Today, 03:51 PM
        0 responses
        7 views
        0 likes
        Last Post lightsun47  
        Started by 00nevest, Today, 02:27 PM
        1 response
        14 views
        0 likes
        Last Post 00nevest  
        Started by futtrader, 04-21-2024, 01:50 AM
        4 responses
        50 views
        0 likes
        Last Post futtrader  
        Working...
        X