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

What is wrong with this simple strategy?

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

    What is wrong with this simple strategy?

    I'm pulling my hair out trying to solve what should be a simple problem but I'm at a loss. Put simply, I'm using a public bool from an indicator as a trigger to enter short or long within my strategy. I'm using the code straight from here https://ninjatrader.com/support/foru...t-plots?t=4991 so my code looks something like the following.



    #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.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 SampleBoolSeriesStrategy : Strategy
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Sample strategy demonstrating how to call an exposed BoolSeries object";
    Name = "SampleBoolSeriesStrategy";
    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;
    }
    else if (State == State.Configure)
    {
    AddChartIndicator(MyCustomIndicator());

    }
    }

    protected override void OnBarUpdate()
    {

    Print(
    MyCustomIndicator().BullIndication[0]);
    Print(
    MyCustomIndicator().BearIndication[0]);


    //entry conditions
    if (MyCustomIndicator().BullIndication[0])
    {
    EnterLong();
    }


    if (MyCustomIndicator().BearIndication[0])
    {
    EnterShort();
    }
    }
    }
    }




    If I try to backtest the strategy the first 3-5 entries make sense (i.e. a long position is entered when a bool is true). However, after this I start to get erroneous entries. Trades are entered when both bools are false (confirmed in the output window with the Print() statements). Am I missing something obvious that would cause this? Any help is appreciated!

    #2
    Hello Trader1001,

    Thank you for the post.

    Without also seeing what the indicator is doing, this would be difficult to determine what is happening in contrast to what you have provided so far. The only items I can note from this sample would be that adding the Time[0] to your prints as that may be helpful in matching prints with specific bars to confirm the conditions. Also, I see you have used the "Exposing indicator values that are not plots" which is a different process than just Plotting. Are you using Update(); in your indicators property?

    If the indicator has plots, this could potentially be that you are not calling the indicator from OnBarUpdate but you are directly calling the sub-property, if it also has plots you may try adding the following before your prints:

    Code:
    double dummyValue = MyCustomIndicator()[0];

    If your prints are both false but you are still seeing entries, there may be something else going on that is not apparent from this sample. Would you be able to export the strategy test along with the supporting indicator so that I can test the same code you are? If not, would you be able to create a simple sample indicator that you could provide that shows how the indicator is setting its values?

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Attached is a simplified version of my code. My intent in this example is to have the indicator determine if a candle is red or green. If the candle is green AND the body of the candle is > 7.5, set the bool bullishBody = true and draw a green arrow beneath the bar. Bool bullishBody is then passed to the strategy as bool BullishBody. If BullishBody = true a long entry is executed.

      I've also attached a screenshot of the results I'm getting. As you can see the first entry is correct but the other three entries are not (one is entered with a false buy signal (i.e. the body < 7.5) and the other two are entered without any signal at all.

      Take a look and let me know what I'm missing.

      Thanks.




      Attached Files
      Last edited by Trader1001; 10-28-2018, 10:36 PM.

      Comment


        #4
        Hello Trader1001,

        Thank you for providing a more complete sample.

        In this case, it looks like this is due to how the indicator is coded. Although you are correctly setting the values, you are never resetting the variables used leaving them true for more than one bar. You are also not setting a value for the series on each bar leaving invalid data points in the series.

        Is the intention to have conditions become true only on specific bars, or is the intention to leave the variables set to true until the opposite condition occurs?

        If the intention is specific bars for the conditions to become true and only at that time, you could just reset the variables you have used before your conditions:

        redCandle = false;
        greenCandle = false;
        bullishBody[0] = false;
        bearishBody[0] = false;

        This ensures a valid data point is set for the series and the conditions are ready to be checked again.

        If the intention is to leave the condition true, you would likely need to just reset the plots to make sure they have valid points:

        bullishBody[0] = false;
        bearishBody[0] = false;

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by techgetgame, Yesterday, 11:42 PM
        0 responses
        8 views
        0 likes
        Last Post techgetgame  
        Started by sephichapdson, Yesterday, 11:36 PM
        0 responses
        2 views
        0 likes
        Last Post sephichapdson  
        Started by bortz, 11-06-2023, 08:04 AM
        47 responses
        1,613 views
        0 likes
        Last Post aligator  
        Started by jaybedreamin, Yesterday, 05:56 PM
        0 responses
        10 views
        0 likes
        Last Post jaybedreamin  
        Started by DJ888, 04-16-2024, 06:09 PM
        6 responses
        20 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Working...
        X