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

Simple strategy gives me error

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

    Simple strategy gives me error


    Hello, I am learning to program in NT. I want to apply a simple system, but I don't know why it gives me an error.


    The idea is the following. A system that goes long if the open of the current candle is higher than the close of the previous candle. On the contrary, it should go short if the opening of the current candle is lower than the closing of the previous candle. In both cases I want the trade to close at the close of the entry candle, as I use daily candles. I leave you my code, and if you can correct it, you would help me a lot.

    Thank you all.

    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "InApertura";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 2;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 0;
    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 = 0;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    if (CurrentBars[0] < 1)
    return;
    {
    ExitLong("","");
    }
    // Set 1
    if (Open[0] >= Close[1])
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }

    }

    #2
    Hello jalro,

    Thanks for your post.

    To enter a long position when the current bar Open is greater than the previous bar Close, you would create a condition that looks something like this, if (Open[0] > Close[1]), and then you would call EnterLong().

    To enter a short position when the current bar Open less than the previous bar Close, you would create a condition that looks something like this, if (Open[0] < Close[1]), and then you would call EnterShort().

    I see you are calling ExitLong() without any conditions. This would mean that since you are running OnBarClose, ExitLong would be called at the close of each bar. Something you could do is create exit order conditions and call ExitLong() to exit long orders and ExitShort() to exit short orders when specified conditions become true.

    See the help guide links below for more information about managed approach order methods.
    Managed Approach Order Methods: https://ninjatrader.com/support/help...d_approach.htm

    Here is a link to our publicly available training videos, 'Strategy Builder 301' and 'NinjaScript Editor 401', for you to view at your own convenience.

    Strategy Builder 301 — https://www.youtube.com/watch?v=_KQF2Sv27oE&t=13s

    NinjaScript Editor 401 - https://youtu.be/H7aDpWoWUQs?list=PL...We0Nf&index=14

    I am also linking you to the Educational Resources section of the Help Guide to help you get started with NinjaScript:
    https://ninjatrader.com/support/help..._resources.htm

    If you are new to C#, to get a basic foundation for the concepts and syntax used in NinjaScript I would recommend this section of articles in our help guide first:
    https://ninjatrader.com/support/help...g_concepts.htm

    And the MSDN (Microsft Developers Network) C# Language Reference.
    https://ninjatrader.com/support/help...erence_wip.htm

    Let us know if we may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3


      I do not know how to enter in my code that closes the position at the close of the candle, it gives me an error in the different ways that I try. Could you define what that part of the code would be like?

      Thank you.

      Comment


        #4
        Hello jalro,

        Thanks for your note.

        What does the error you are getting report? Please send a screenshot of the error so we may accurately assist.

        You would need to define exit conditions for your long orders and call ExitLong() to exit the long position. For example, to exit a long position when the current bar Close[0] is greater than the previous bar Close[1], the code would look something like this.

        if (Close[0] > Close[1])
        {
        ExitLong();
        }


        You would also need to define exit conditions for your short orders. However, instead of calling ExitLong(), you would call ExitShort().

        I recommend viewing the publicly available Strategy Builder 301 training video linked in my previous post regarding how to use the Strategy Builder to create conditions and action for a strategy.

        Let us know if we may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5

          Currently the code is like this, but it still does not work well. It does not always respect the entry rule. And the exit does not fulfill it, although I have marked that it closes the operation at the end of each candle, it does not. I have put a simple condition for him to close the long position, but he does not do it either.






          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your new custom Strategy here.";
          Name = "INApertura";
          Calculate = Calculate.OnPriceChange;
          EntriesPerDirection = 2;
          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 = 1;
          // Disable this property for performance gains in Strategy Analyzer optimizations
          // See the Help Guide for additional information
          IsInstantiatedOnEachOptimizationIteration = true;
          }
          else if (State == State.Configure)
          {
          }
          }

          protected override void OnBarUpdate()
          {
          if (BarsInProgress != 0)
          return;

          if (CurrentBars[0] < 1)
          return;

          // Set 1
          if (Open[0] >= Close[1])
          {
          EnterLongLimit(Convert.ToInt32(DefaultQuantity), Open[0], "");
          }

          // Set 2
          if (Close[0] != Open[0])
          {
          ExitLongLimit(Convert.ToInt32(DefaultQuantity), Close[0], "", "");
          }

          }
          }
          }

          Comment


            #6
            Hello jalro,

            Thanks for your note.

            Currently, in your strategy, you are checking if the current Open[0] is greater than or equal to the previous Close[1] and calling EnterLongLimit using the current Open[0] as your limit price. When testing your strategy, I see that the strategy does place orders when this condition becomes true.

            Then, you are checking if the current Close[0] does not equal the current Open[0] and are calling ExitLongLimit() with the current Close[0] as the limit price. When testing the strategy I am also seeing the exit order being placed when the condition becomes true. However, since you are submitting the order with a limit price of the current Close[0] and are running the strategy with Calculate.OnPriceChange, the order is immediately filled right after the entry order is placed. You could consider adding an offset to your limit price so that the order is not filled immediately after the entry order is submitted.

            You could also consider running the strategy with Calculate.OnBarClose, that way the strategy will process information at the close of each bar instead of for every change in price.

            Calculate: https://ninjatrader.com/support/help.../calculate.htm

            Ultimately, debugging steps would need to be taken to determine how your strategy is behaving.

            To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

            In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

            Below is a link to a forum post that demonstrates how to use prints to understand behavior.
            https://ninjatrader.com/support/foru...121#post791121

            Please let me know if I may further assist
            Brandon H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by futtrader, 04-21-2024, 01:50 AM
            4 responses
            41 views
            0 likes
            Last Post futtrader  
            Started by Option Whisperer, Today, 09:55 AM
            1 response
            11 views
            0 likes
            Last Post bltdavid  
            Started by port119, Today, 02:43 PM
            0 responses
            2 views
            0 likes
            Last Post port119
            by port119
             
            Started by Philippe56140, Today, 02:35 PM
            0 responses
            3 views
            0 likes
            Last Post Philippe56140  
            Started by 00nevest, Today, 02:27 PM
            0 responses
            2 views
            0 likes
            Last Post 00nevest  
            Working...
            X