Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy Help

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

    Strategy Help

    I'm trying to get a strategy to execute properly but when I review the chart in Strategy Analyzer I see trades that are not conforming to the logic I'm trying to code. I have attached a screen shot of an executed trade in Strategy Analyzer that should occur later, only after rising 2 ticks above the green "HighestHigh" line. Any help is much appreciated.

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class OpeningRangeUnlocked : Strategy
    {
    private HighLowByTimeRange HighLowByTimeRange1;
    private Stochastics Stochastics1;
    private int priorTradesCount = 0;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"";
    Name = "OpeningRangeUnlocked";
    Calculate = Calculate.OnEachTick;
    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;



    }
    else if (State == State.Configure)
    {
    SetProfitTarget(@"LongMarket", CalculationMode.Ticks, 8);
    SetStopLoss(@"LongMarket", CalculationMode.Ticks, 8, false);
    SetProfitTarget(@"ShortMarket", CalculationMode.Ticks, 8);
    SetStopLoss(@"ShortMarket", CalculationMode.Ticks, 8, false);
    HighLowByTimeRange1 = HighLowByTimeRange(8, 30, 8, 50);
    HighLowByTimeRange1.Plots[0].Brush = new SolidColorBrush(Colors.Chartreuse);
    HighLowByTimeRange1.Plots[1].Brush = new SolidColorBrush(Colors.Red);
    AddChartIndicator(HighLowByTimeRange1);
    Stochastics1 = Stochastics(7, 14, 3);
    Stochastics1.Plots[0].Brush = new SolidColorBrush(Colors.IndianRed);
    Stochastics1.Plots[1].Brush = new SolidColorBrush(Colors.LimeGreen);
    AddChartIndicator(Stochastics1);

    }
    }

    protected override void OnBarUpdate()
    {
    // Make sure there are enough bars.
    if (CurrentBars[0] < 1) return;



    if (CurrentBar < BarsRequiredToTrade) return;
    // At the start of a new session
    if (Bars.IsFirstBarOfSession)
    {
    // Store the strategy's prior cumulated realized profit and number of trades
    priorTradesCount = SystemPerformance.AllTrades.Count;

    /* NOTE: Using .AllTrades will include both historical virtual trades as well as real-time trades.
    If you want to only count profits from real-time trades please use .RealtimeTrades. */
    }

    /* Prevents further trading if the current session's realized profit exceeds $1000 or if realized losses exceed $400.
    Also prevent trading if 10 trades have already been made in this session. */
    if (SystemPerformance.AllTrades.Count - priorTradesCount > 0)
    {
    /* TIP FOR EXPERIENCED CODERS: This only prevents trade logic in the context of the OnBarUpdate() method. If you are utilizing
    other methods like OnOrderUpdate() or OnMarketData() you will need to insert this code segment there as well. */

    // Returns out of the OnBarUpdate() method. This prevents any further evaluation of trade logic in the OnBarUpdate() method.
    return;
    }


    // Set 1 - Long Entry
    if ((Time[0].TimeOfDay > new TimeSpan(8, 50, 01))
    && (Time[0].TimeOfDay < new TimeSpan(10, 30, 00))
    && ((High[0] + (2 * TickSize)) >= HighLowByTimeRange1.HighestHigh[0])
    && (Stochastics1.K[0] > Stochastics1.D[0]))

    {EnterLong(Convert.ToInt32(DefaultQuantity), @"LongMarket");}

    // Set 2 - Short Entry
    if ((Time[0].TimeOfDay > new TimeSpan(8, 50, 01))
    && (Time[0].TimeOfDay < new TimeSpan(10, 30, 00))
    && ((Low[0] + (2 * TickSize)) <= HighLowByTimeRange1.LowestLow[0])
    &&(Stochastics1.K[0] < Stochastics1.D[0]))

    {EnterShort(Convert.ToInt32(DefaultQuantity), @"ShortMarket");}
    }
    }
    }
    Attached Files

    #2
    Hello user_456,

    Thanks for your post.

    What we recommend is that you use general debugging techniques to break your conditions code down and evaluate each condition to ensure that is true/false or is meeting the value checks. Typically this would be done with Print statements.

    Specifically here you would want to examine the values of HighLowByTimeRange1.HighestHigh[0] and High[0] + (2 * TickSize)) as it would seem to conflict between what you see and what you are expecting.

    Where your enterlong statement is, you may want to include a Draw.Dot() and place the dot below the low of the bar so you know what bar the condition set is true verses where the order actually occurred.

    To aid with debugging we have put together some tips here: http://ninjatrader.com/support/forum...ead.php?t=3418
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      I created this strategy in Strategy Builder because I am not a coder. After the strategy wasn't creating trades I contacted support. After 3 days of working with support it was determined that even though I created the strategy in Strategy Builder, it was inserting code in the wrong section.

      Here are the comments from the issue:
      "This behavior (the strategy builder adding secondary data series information to State.DataLoaded) was confirmed on our end, and is scheduled to be repaired in Beta 13. Please keep an eye on the NinjaTrader 8 Release Notes page for updates and bugfixes."

      I have been informed that this bug will be fixed in build 13, so in the meantime I have to proceed programmatically. This is why I submitted my code for assistance. If I could debug it I could probably code it correctly, so your suggestions don't help me a lot.

      What I can tell you is that the indicator is painting correctly on the chart. So we're left with why the order is not being placed in the right location of the chart.

      Your comment - "Specifically here you would want to examine the values of HighLowByTimeRange1.HighestHigh[0] and High[0] + (2 * TickSize)) as it would seem to conflict between what you see and what you are expecting.", is a restatement of the problem I'm submitting here on the forum.

      I know it conflicts, that's why I submitted the issue on the forum. The long market order should be only placed 2 ticks outside of the highest high of the opening range indicator or if going market short, two ticks below the lowest low of the indicator. The opening range indicator is painting correctly and so if I added two ticks to that highest high and it doesn't match where the order shows up in Strategy Analyzer then I'm stuck.

      Given that this bug mentioned above is the result of correctly using the Strategy Builder not creating the correct code, I would hope that I could get help correcting the code.

      I welcome yours or anyone else's assistance in this regard.

      Comment


        #4
        Hello user_456,

        Please send me your strategy source file. It can be found in Documents>NinjaTrader8>bin>Custom>strategies.

        Attach the cs file in an email to PlatformSupport[at]NinjaTrader[dot]com, with atten Paul and a link to this thread.,
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          && ((High[0] + (2 * TickSize)) >= HighLowByTimeRange1.HighestHigh[0])
          The code filter did exactly as you wrote, even if that is not what you intended. You say, in English: "if the High of this bar plus 2 ticks is equal to or greater than the highest high that is the marker, go Long."

          What you really intended is: "... that should occur later, only after rising 2 ticks above the green "HighestHigh" line."

          That is the exact opposite mathematical logic statement from what you wrote. In other words, " + (2 * TickSize)" is on the wrong side of the inequality. Move it to the other side.

          Comment


            #6
            Hello koganam,

            Thanks for your post.

            I did not report back here but this was resolved off line with user_456 and that was the issue identified..

            Thanks for your effort.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              That's right and thank you Paul for the one-on-one assistance.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by mattbsea, Today, 05:44 PM
              0 responses
              3 views
              0 likes
              Last Post mattbsea  
              Started by RideMe, 04-07-2024, 04:54 PM
              6 responses
              31 views
              0 likes
              Last Post RideMe
              by RideMe
               
              Started by tkaboris, Today, 05:13 PM
              0 responses
              2 views
              0 likes
              Last Post tkaboris  
              Started by GussJ, 03-04-2020, 03:11 PM
              16 responses
              3,282 views
              0 likes
              Last Post Leafcutter  
              Started by WHICKED, Today, 12:45 PM
              2 responses
              20 views
              0 likes
              Last Post WHICKED
              by WHICKED
               
              Working...
              X