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

  • mattlaguardia
    replied
    Thanks for the tip.

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    Hello mattlaguardia,

    Moving forward, create your strategy in the NinjaScript Editor. This will automatically create the correct framework with the correct namespace and will automatically have OnBarUpdate().

    Below is a link to a forum post with helpful resources on getting started with C# and NinjaScript. Please watch the two training videos.
    https://ninjatrader.com/support/foru...pts#post786040

    Leave a comment:


  • mattlaguardia
    replied
    On some further investigation, it looks like there might be a conflict between the last declaration of the Region line:

    using NinjaTrader.NinjaScript.Strategies;

    and the Namespace declaration that follows the Region block:

    namespace NinjaTrader.NinjaScript.Strategies

    Before compiling, there's an error that shows for the Region line:

    Extern alias declarations, using clauses, assembly/module attributes, or namespace/type declarations expected.

    If I compile the code, the error goes away, but the code still isn't quite working properly. It will attach to a chart now, but it still doesn't generate trades in the backtester, and it doesn't really produce much information in the way of logs or print messages.

    UPDATE: I solved the error by moving the Namespace declaration above the Region block.



    Attached Files
    Last edited by mattlaguardia; 02-05-2023, 04:42 PM.

    Leave a comment:


  • mattlaguardia
    replied
    ​OK, I'm not sure how I missed that critical line.

    I made the changes, sorted out a few CS errors, and got the code to compile.

    It shows up in the strategies menu, and I can add it to a chart.

    It still won't take a trade in the backtester, though​. The log shows:

    05-02-2023 15:04:33 Connection Using HDS (hds-us-nt-004.ninjatrader.com/31655)
    and
    05-02-2023 15:04:33 Connection Time to auto close position='00:00:00', Enabled=False

    Leave a comment:


  • KonstantinosNT
    replied
    After the #endregion , add:

    ​namespace NinjaTrader.NinjaScript.Strategies
    {
    public class CrudeOilOpeningRangeBreakoutStrategy : Strategy
    {​
    ......
    ...... your code here......
    ......
    }
    }
    Last edited by KonstantinosNT; 02-05-2023, 02:42 PM.

    Leave a comment:


  • mattlaguardia
    replied
    Just for reference, here's the most recent iteration of the code with print statements added:

    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 = double.MinValue;
    private double openingRangeLow = double.MaxValue;


    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Crude Oil Opening Range Breakout Strategy";
    Name = "CrudeOilOpeningRangeBreakoutStrategy";
    Calculate = Calculate.OnPriceChange;
    }
    else if (State == State.Realtime)
    {
    }
    base.OnStateChange();
    }
    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
    // Set opening range high and low
    if (Time[0].Hour == 8 && Time[0].Minute == 30)
    {

    openingRangeHigh = High[1];
    openingRangeLow = Low[1];

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

    Print("Opening Range High: " + openingRangeHigh);
    Print("Opening Range Low: " + openingRangeLow);
    Print("Stop Loss: " + stopLoss);
    Print("Profit Target: " + profitTarget);
    }
    {

    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 (marketDataUpdate.Price > 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 (marketDataUpdate.Price < openingRangeLow && !shortPositionOpened)
    {
    shortPositionOpened = true;
    EnterShort(Quantity, "Short");
    lastTradeTime = Time[0];

    // Reset trade flags and range at 2PM
    if (Time[0].Hour == 14);
    }}
    }

    Leave a comment:


  • mattlaguardia
    replied
    I'll add an OnBarUpdate() method and see if it solves the issue--NO LUCK.

    Edit: I think there is a conflict between OnMarketData and Calculate.OnEachTick. I want to calculate on price changes, so Calculate.OnPriceChange method + OnMarketData seems like the better combination? Calculate.OnEachTick would need to be used with the OnBarUpdate method.

    The issue though, is that neither method actually works. I still get no logs, no ability to add the strategy to a chart, and it doesn't ever take trades in the backtester.

    When I look in the folder after compiling the strategy, I notice that all the other strategies have the @ symbol in front of their name, but the strategy I build in the Editor does not have the @ symbol.
    Last edited by mattlaguardia; 02-05-2023, 01:50 PM. Reason: add thoughts.

    Leave a comment:


  • KonstantinosNT
    replied
    (I am just trying to help you finding a solution to your problem)

    I have to insist on the necessity of the OnBarUpdate() in your script.
    Don't confuse the OnBarUpdate() with the Calculate.OnBarClose.

    In NT we can have:
    Calculate.OnBarClose
    Calculate.OnEachTick
    Calculate.OnPriceChange

    In your script you can have Calculate.OnEachTick. That's fine, but you can't omit OnBarUpdate().
    Actually, a bar is updating in real time every moment a new tick is coming in, not only in bar close.

    Leave a comment:


  • mattlaguardia
    replied
    Thanks for the reply.

    The stop loss is here: stopLoss = openingRangeLow - (openingRangeHigh - openingRangeLow) * 0.5;

    There is no OnBarUpdate() call because price is not calculated OnBarUpdate(). It's calculated on tick data. If I wait for a bar to close before taking the position, it's often too late and the trade is missed. That's why I used the (MarketDataEventArgs marketDataUpdate) arguments.

    Maybe I should set an OnBarUpdate() = false; argument?
    Last edited by mattlaguardia; 02-05-2023, 01:01 PM. Reason: Add thought.

    Leave a comment:


  • KonstantinosNT
    replied
    Hello again mattlaguardia,

    The code in your first message must be a part of your script and not the full script, because I don't see any stop loss orders.

    Something I have noticed is that there is no "OnBarUpdate()" method anywhere in your script. ​
    You are only using OnMarketUpdate(), but in the NT Help it is clearly stated that:

    " The OnMarketData() method is expected to be called after OnBarUpdate()​ "

    Leave a comment:


  • mattlaguardia
    replied
    Thank you for the reply.

    It seems like your strategies are not yet compiled.
    I click the compile button inside the NinjaScript Editor, and they compile with no errors. Isn't that the process I need to go through? Am I missing a step?

    Also, there is another person who just posted here with the same issue:
    https://ninjatrader.com/support/foru...o-add-to-chart
    Last edited by mattlaguardia; 02-05-2023, 07:46 AM. Reason: Add information.

    Leave a comment:


  • KonstantinosNT
    replied
    Hello mattlaguardia,

    You said in the message #10
    "I am not able to apply the strategy to any chart. It does not show up in the list of available strategies when I right click and select 'add new strategy'."

    It's unlikely that this problem has anything to do with possibly wrong entry conditions in your strategy.
    It seems like your strategies are not yet compiled.
    Check that possibility.

    Leave a comment:


  • mattlaguardia
    replied
    The name follows the correct format, and the strategy shows up in the correct folder.

    I think the issue is that I haven't correctly defined the data condition used for entry? The code says to look at a bar close, but never defines the bar period. Instead it should be looking at individual tick data.

    Any thoughts there?​

    Leave a comment:


  • NinjaTrader_ChrisL
    replied
    Hello, thanks for the follow up. If you can not see the strategy in the Strategies menu, this typically means there is an initialization error with the strategy, an error message should pop up in the Log tab of the Control Center if this happens. If there is no message about the strategy, then the file may be in the wrong location. Check to make sure the strategy .cs file is within the following directory:

    Documents\NinjaTrader 8\bin\Custom\Strategies

    Also, double check the "Name" parameter of the strategy to confirm the name that will be seen in the Strategy menu.

    Leave a comment:


  • mattlaguardia
    replied
    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?​
    I am not able to apply the strategy to any chart. It does not show up in the list of available strategies when I right click and select 'add new strategy'.

    Nothing shows up in the strategies tab of the control center.

    There are no errors that show up in the log tab of the control center.

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by slightly, Today, 03:27 AM
0 responses
3 views
0 likes
Last Post slightly  
Started by llanqui, Yesterday, 04:09 PM
2 responses
18 views
0 likes
Last Post llanqui
by llanqui
 
Started by franatas, Today, 02:49 AM
0 responses
2 views
0 likes
Last Post franatas  
Started by Foxy1, Today, 12:59 AM
2 responses
17 views
0 likes
Last Post Foxy1
by Foxy1
 
Started by franatas, Yesterday, 11:51 PM
1 response
15 views
0 likes
Last Post franatas  
Working...
X