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

User input as condition for strategy entry

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

    User input as condition for strategy entry

    Hello,

    I've been trying to get this to work all day and have managed to produce all kinds of various errors, maybe this would be a good time to ask for some help.

    My goal is to make is so the user can specify long or short, and then the strategy uses that to decide direction.

    What is the proper way to include this? I thought something like this would work but it did not:
    //user input
    [NinjaScriptProperty]
    [Display(Name = "Long/Short", GroupName = "Long/Short", Order = 0)]
    public string LongShort { get; set; }

    And then in the entry logic, include this.

    For example:

    bool userLong = False;
    bool userShort = False;

    if(LongShort == "Long")
    {
    userLong;
    }else if(LongShort == "Short")
    {
    userShort;
    }

    protected override void OnBarUpdate()
    {
    if (Close[0] > Open[0] && userLong)
    {
    EnterLong("Enter Long");
    }

    if(Close[0] < Open[0] && userShort)
    {
    EnterShort("Enter Short");
    }
    }

    I've lost track of how many different ways I've tried this and all of the resulting errors.

    Can anyone please help point out the correct way to do this?

    Edit: I'm using NT8 and Windows 10
    Last edited by butt_toast; 07-18-2021, 12:48 PM.

    #2
    Tried to start from scratch and a new error is coming up. Googled it and it led back to this message board and the guy with this problem said: "If you're searching for an easy do-it-yourself type of answer - it's probably best to ping support for this one."

    Link: https://ninjatrader.com/support/foru...does-not-exist

    Seems very bizarre for another error to come up when I literally just tried getting a blank document to compile. Attaching a screenshot.

    Comment


      #3
      Tried the Ninjascript editor using the wizard. The only thing I did was include a string input.

      I hit "generate" then I tried to compile and I got errors.

      Very confusing.

      Edit:
      I removed NT7 to see if that would help, then restarted. I did this before this attempt in the screen shot.

      Comment


        #4
        Going to try to update this thread with the steps I've tried in case someone else comes across this.

        I've uninstalled NT7 and now I'm going to backup and reinstall NT8.

        Edit:
        Backed up > uninstalled > rebooted > reinstalled > no dice
        Last edited by butt_toast; 07-18-2021, 02:43 PM.

        Comment


          #5
          Used strategy builder on another computer and got this working code.

          If I wanted to use this code real time how would I do this?

          I test this code in market replay, but when I enable it, if an entry occurred earlier in the session I don't want that to stop me from being able to discretionarily enable the strategy.

          For example:
          You're monitoring the market and we approach resistance. You set the input for longshort to "SHORT" and enable the strat. What should happen here is the next down bar enters the trade but if the strat already thinks its in a trade because there's an earlier down bar that created a trade that you're still in. I changed the setting that seems related to this to "Immediately submit, synchronize" but that doesn't seem to be correct. Any advice?

          Edit:
          In case someone googles this question and come across it, just put this in the onbarupdate method: if(State==State.Historical) return;

          The 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 Jul18f : Strategy
          {
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your new custom Strategy here.";
          Name = "Jul18f";
          Calculate = Calculate.OnBarClose;
          EntriesPerDirection = 1;
          EntryHandling = EntryHandling.AllEntries;
          IsExitOnSessionCloseStrategy = true;
          ExitOnSessionCloseSeconds = 30;
          IsFillLimitOnTouch = false;
          MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
          OrderFillResolution = OrderFillResolution.Standard;
          Slippage = 0;
          StartBehavior = StartBehavior.WaitUntilFlat;
          TimeInForce = TimeInForce.Gtc;
          TraceOrders = false;
          RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
          StopTargetHandling = StopTargetHandling.PerEntryExecution;
          BarsRequiredToTrade = 20;
          // Disable this property for performance gains in Strategy Analyzer optimizations
          // See the Help Guide for additional information
          IsInstantiatedOnEachOptimizationIteration = true;
          LongShort = @"Neither";
          }
          else if (State == State.Configure)
          {
          SetStopLoss(@"short entry", CalculationMode.Ticks, 10, false);
          }
          }

          protected override void OnBarUpdate()
          {
          bool _oktolong = LongShort == "LONG";
          bool _oktoshort = LongShort == "SHORT";
          if (BarsInProgress != 0)
          return;

          if (CurrentBars[0] < 1)
          return;

          // Long
          if (Close[0] > Open[0] && _oktolong)
          {
          EnterLong(Convert.ToInt32(DefaultQuantity), @"long entry");
          }
          // Short
          if (Close[0] < Open[0] && _oktoshort)
          {
          EnterShort(Convert.ToInt32(DefaultQuantity), @"short entry");
          }

          }

          #region Properties
          [NinjaScriptProperty]
          [Display(Name="LongShort", Order=1, GroupName="Parameters")]
          public string LongShort
          { get; set; }
          #endregion

          }
          }
          Last edited by butt_toast; 07-18-2021, 06:08 PM.

          Comment


            #6
            Hello butt_toast,

            Are you generating new strategies (or new scripts in general) through the NinjaScript Editor by right-clicking the explorer and selecting New Strategy? (This generates the proper framework for a strategy or other script type).

            NinjaTrader 7 has nothing to do with NinjaTrader 8. Installing, uninstalling, re-installing, updating, either NinjaTrader 7 or NinjaTrader 8 would have no effect on the code in a script causing an error.

            The errors states the using statement 'using NinjaTrader.Custom.Addons' does not exist. The error is correct. There is no NinjaTrader.Custom namespace in NinjaTrader 8. Why was this using statement added? What are you trying to access in this non-existent namespace?

            To enable a strategy in real-time, connect to a real-time data feed, then add the strategy to a chart and enable it.
            The Strategy Builder 301 training video demonstrates.
            NinjaTrader's Strategy Builder empowers traders of all levels with point-and-click automated trading strategy development. Create your own advanced automated...



            A strategy placing an order in historical data will not prevent the strategy from placing orders in real-time.
            If you are referring to the Wait until flat start behavior not sending live orders until the strategy passes through flat see below.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello butt_toast,

              Are you generating new strategies (or new scripts in general) through the NinjaScript Editor by right-clicking the explorer and selecting New Strategy? (This generates the proper framework for a strategy or other script type).

              NinjaTrader 7 has nothing to do with NinjaTrader 8. Installing, uninstalling, re-installing, updating, either NinjaTrader 7 or NinjaTrader 8 would have no effect on the code in a script causing an error.

              The errors states the using statement 'using NinjaTrader.Custom.Addons' does not exist. The error is correct. The Addons class is in NinjaTrader.NinjaScript in NinjaTrader 8. Why was this using statement added? What are you trying to access in this non-existent namespace?

              To enable a strategy in real-time, connect to a real-time data feed, then add the strategy to a chart and enable it.
              The Strategy Builder 301 training video demonstrates.
              NinjaTrader's Strategy Builder empowers traders of all levels with point-and-click automated trading strategy development. Create your own advanced automated...



              A strategy placing an order in historical data will not prevent the strategy from placing orders in real-time.
              If you are referring to the Wait until flat start behavior not sending live orders until the strategy passes through flat see below.



              Last, I am providing a link to a forum post with helpful information about getting started with NinjaScript and C#.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Thank you for the reply!

                Are you generating new strategies (or new scripts in general) through the NinjaScript Editor by right-clicking the explorer and selecting New Strategy? (This generates the proper framework for a strategy or other script type).
                This is what I did:
                Control Center > New > Strategy Builder

                Do note that I did this on two different computers. On one computer (the one with the screen shots) it didn't work, on a laptop it did work. Both with NT8.

                NinjaTrader 7 has nothing to do with NinjaTrader 8. Installing, uninstalling, re-installing, updating, either NinjaTrader 7 or NinjaTrader 8 would have no effect on the code in a script causing an error.
                I uninstalled NT7 because this popped up after installing it on that computer, which was just a simple troubleshooting step because it was one of the few changes I'm aware of.

                The reinstalling etc was NT8, and I felt that reinstalling it was a reasonable move considering it worked on the laptop.

                The errors states the using statement 'using NinjaTrader.Custom.Addons' does not exist. The error is correct. The Addons class is in NinjaTrader.NinjaScript in NinjaTrader 8. Why was this using statement added? What are you trying to access in this non-existent namespace?
                I'm not suggesting the error is incorrect.

                Regarding why the statement was added, it was added by the strategy builder.

                To enable a strategy in real-time, connect to a real-time data feed, then add the strategy to a chart and enable it.
                The Strategy Builder 301 training video demonstrates.
                NinjaTrader's Strategy Builder empowers traders of all levels with point-and-click automated trading strategy development. Create your own advanced automated...


                A strategy placing an order in historical data will not prevent the strategy from placing orders in real-time.
                If you are referring to the Wait until flat start behavior not sending live orders until the strategy passes through flat see below.
                I was in market replay testing the code. The issue was that when I wanted to test discretionarily enabling the strategy at a specific point in time, it couldn't because of a trade that was still open from earlier in the session that was generated before the point I chose in market replay. This was resolved by including a line of code in onbarupdate().

                Last, I am providing a link to a forum post with helpful information about getting started with NinjaScript and C#.
                Thank you so much.

                Comment


                  #9
                  Hello butt_toast,

                  The Strategy you have posted a screenshot of in post #2 is not created with the Strategy Builder. The class declaration is wrong, the using statements are wrong. The Strategy Builder would not be able to create that strategy.

                  The error stated in your screenshot is from the using statement I have specified which is invalid. It is incorrect.

                  To confirm, if you make a new strategy, the using statement 'using NinjaTrader.Custom.Addons' is being automatically added?

                  Are you using NinjaTrader 8.0.24.3? (Help -> About)

                  If this can be reproduced, and that invalid using statement is being added, and the class declaration is not inheriting from : Strategy, I would have to schedule a call to observe you creating a new strategy from the Strategy Builder and confirm this.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_ChelseaB View Post
                    Hello butt_toast,

                    The Strategy you have posted a screenshot of in post #2 is not created with the Strategy Builder. The class declaration is wrong, the using statements are wrong. The Strategy Builder would not be able to create that strategy.

                    The error stated in your screenshot is from the using statement I have specified which is invalid. It is incorrect.

                    To confirm, if you make a new strategy, the using statement 'using NinjaTrader.Custom.Addons' is being automatically added?

                    Are you using NinjaTrader 8.0.24.3? (Help -> About)

                    If this can be reproduced, and that invalid using statement is being added, and the class declaration is not inheriting from : Strategy, I would have to schedule a call to observe you creating a new strategy from the Strategy Builder and confirm this.
                    Wow that is incredible support you guys are awesome.

                    I'll try again tomorrow to recreate this. I'll record my screen if you want, to save time. That is if I'm able to reproduce it which hopefully not the case.

                    Comment


                      #11
                      Originally posted by butt_toast View Post

                      Wow that is incredible support you guys are awesome.

                      I'll try again tomorrow to recreate this. I'll record my screen if you want, to save time. That is if I'm able to reproduce it which hopefully not the case.
                      Taking care of injured pet - on the first floor for today, will probably be trading from the first floor for the rest of the week and may need to wait until the weekend to mess around with the computer upstairs with the issues.

                      I did find that after renaming the NT folder in the documents folder and then reinstalling, that some of the strange behavior went away as I was able to compile something it wasn't before.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by alifarahani, Today, 09:40 AM
                      6 responses
                      36 views
                      0 likes
                      Last Post alifarahani  
                      Started by Waxavi, Today, 02:10 AM
                      1 response
                      17 views
                      0 likes
                      Last Post NinjaTrader_LuisH  
                      Started by Kaledus, Today, 01:29 PM
                      5 responses
                      14 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by Waxavi, Today, 02:00 AM
                      1 response
                      12 views
                      0 likes
                      Last Post NinjaTrader_LuisH  
                      Started by gentlebenthebear, Today, 01:30 AM
                      3 responses
                      17 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Working...
                      X