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

Building my first strategy...it doesn't execute orders, help!

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

  • NinjaTrader_ChelseaB
    replied
    Hello mattlaguardia,

    Is it possible there's an issue in the OnStateChange() section that's causing this odd behavior?
    Yes, it is possible there is an issue in OnStateChange().

    Please provide the information requested in post # 7.

    "Importantly, are there errors appearing on the Log tab of the Control Center?"


    Regarding the strategy not placing orders, print the time of the bar, and all values used in the condition that submits the entry order. Include labels for each value and comparison operator. Save the output to a text file. Include the text file with your reply and we will be happy to assist with analyzing the output. Post # 4 includes a link to a forum post with directions on using prints to understand behavior and includes a video.
    Last edited by NinjaTrader_ChelseaB; 02-02-2023, 09:04 AM.

    Leave a comment:


  • mattlaguardia
    replied
    Hello Chelsea, thanks so much for the reply.

    If the strategy is applied to a chart, is there data with new bars appearing on the chart the Strategy is applied to?​
    I'm unable to apply it to a chart because it doesn't show up in the list of available strategies when I right click on the chart.

    This would indicate there is an error in OnStateChange(). This error would be appearing on the Log tab of the Control Center.

    From a strategy still locked for editing with the Strategy Builder, this is typically due to using series information (like indicators or prices) with stops and targets​
    That's very strange--the strategy isn't locked within the Strategy Builder. It compiles with no errors. I can run it in the Backtester, but it won't take trades.

    Is it possible there's an issue in the OnStateChange() section that's causing this odd behavior?

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    Hello mattlaguardia,

    If the strategy is applied to a chart, is there data with new bars appearing on the chart the Strategy is applied to?

    Is the strategy showing as enabled on the Strategies tab of the Control Center?

    Importantly, are there errors appearing on the Log tab of the Control Center?

    It doesn't show up in the strategies when I right click and bring up the menu.
    This would indicate there is an error in OnStateChange(). This error would be appearing on the Log tab of the Control Center.

    From a strategy still locked for editing with the Strategy Builder, this is typically due to using series information (like indicators or prices) with stops and targets

    Leave a comment:


  • mattlaguardia
    replied
    OK, I've added print statements to the code, and changed to HighestBar/LowestBar.

    I also watched the video...something very interesting I discovered is that I can't attach this strategy to a chart. It doesn't show up in the strategies when I right click and bring up the menu.

    I can load it into the backtester and run it, but there is no output in the terminal even with the print statements added.

    Leave a comment:


  • mattlaguardia
    replied
    Thank you so much guys! I will try these suggestions tonight and report back.

    Leave a comment:


  • NinjaTrader_Emily
    replied
    Hello mattlaguardia,

    Thank you for your post and welcome to the NinjaTrader forums! Thank you waltFX and KonstantinosNT for your input as well.

    For anyone getting started with NinjaScript, I like to share the following post that has a lot of helpful resources listed in one place:
    In this case, I do agree that you should add print statements to check the values of your variables. If the openingRangeHigh and openingRangeLow values are not what you expect, you could also look into using HighestBar and LowestBar as suggested. We have more information about using prints to debug a script, as well as a video showing how to add prints in the Strategy Builder, at the following link:Please don't hesitate to reach out with any additional questions or concerns.

    Leave a comment:


  • KonstantinosNT
    replied
    Add a "print" statement to check the values of your variables.

    Leave a comment:


  • waltFX
    replied
    Just a quick comment; I would use the "HighestBar" & "LowestBar" functions to set your "openingRangeHigh" & "openingRangeLow" values...

    Leave a comment:


  • Building my first strategy...it doesn't execute orders, help!

    I'm teaching myself to code strategies in the Strategy Builder. I've built my first application, and it compiles correctly. But when I run the backtester, it never takes a trade! I know the backtester is configured correctly because I can run other strategies successfully.

    This is an opening range strategy, so there's a condition met nearly every day. I'm sure I did something wrong, but I can't tell what. I need a fresh set of eyes.

    The code is supposed to look at the first 30 minutes of market open, and establish a range. After the first 30min, if the range is broken it takes a trade with a target of 1/2 the range and a stop loss of 1/2 the range. It's designed to take either one long or one short trade each day, and it resets at 2PM.

    Here's the code, can anyone help me spot what I've missed?

    region Using declarations

    using System.Linq;

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.ComponentModel.DataAnnotations;

    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;

    using NinjaTrader.NinjaScript.Strategies;

    #endregion

    class CrudeOilOpeningRangeBreakoutStrategy : Strategy

    {

    private DateTime lastTradeTime = DateTime.MinValue;

    private double stopLoss;

    private double profitTarget;

    private int Quantity = 1;

    private bool longPositionOpened = false;

    private bool shortPositionOpened = false;

    private double openingRangeHigh;

    private double openingRangeLow;

    protected override void OnStateChange()

    {

    if (State == State.SetDefaults)

    {

    Description = "Crude Oil Opening Range Breakout Strategy";

    Name = "CrudeOilOpeningRangeBreakoutStrategy";

    Calculate = Calculate.OnEachTick;

    }

    else if (State == State.Realtime)

    {

    }
    base.OnStateChange();

    }
    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)

    {

    // Check if it's outside the allowed trading time

    if (Time[0].Hour < 8 || Time[0].Hour >= 14) return;

    if (Time[0].Hour == 8 && Time[0].Minute < 30) return;

    if (Time[0].Hour == 14 && Time[0].Minute >= 0) return;

    // Set opening range high and low

    if (Time[0].Hour == 8 && Time[0].Minute == 30)

    {

    double highValue = High[1];

    double lowValue = Low[1];

    openingRangeHigh = Math.Max(highValue, openingRangeHigh);

    openingRangeLow = Math.Min(lowValue, openingRangeLow);

    stopLoss = openingRangeLow - (openingRangeHigh - openingRangeLow) * 0.5;

    profitTarget = openingRangeHigh + (openingRangeHigh - openingRangeLow) * 0.5;

    }

    // Reset trade flags and range at 2PM

    if (Time[0].Hour == 14)

    {

    longPositionOpened = false;

    shortPositionOpened = false;

    openingRangeHigh = 0;

    openingRangeLow = 0;

    stopLoss = 0;

    profitTarget = 0;
    }
    // Place a long trade if price rises above the opening range high and no long trade has been placed

    if (Close[0] > openingRangeHigh && !longPositionOpened)

    {

    longPositionOpened = true;

    EnterLong(Quantity, "Long");

    lastTradeTime = Time[0];

    }

    // Place a short trade if price falls below the opening range low and no short trade has been placed

    if (Close[0] < openingRangeLow && !shortPositionOpened)

    {

    shortPositionOpened = true;

    EnterShort(Quantity, "Short");

    lastTradeTime = Time[0];

    }

    }}

Latest Posts

Collapse

Topics Statistics Last Post
Started by Rapine Heihei, Today, 08:19 PM
1 response
5 views
0 likes
Last Post NinjaTrader_Manfred  
Started by Rapine Heihei, Today, 08:25 PM
0 responses
5 views
0 likes
Last Post Rapine Heihei  
Started by f.saeidi, Today, 08:01 PM
1 response
4 views
0 likes
Last Post NinjaTrader_Manfred  
Started by Rapine Heihei, Today, 07:51 PM
0 responses
6 views
0 likes
Last Post Rapine Heihei  
Started by frslvr, 04-11-2024, 07:26 AM
5 responses
97 views
1 like
Last Post caryc123  
Working...
X