Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy doesn't work somehow

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

    Strategy doesn't work somehow

    Hi,

    I just created a strategy. Maybe someone can take a look, if there is a mistake in the source code, since it doesn't generate a single trade if I run the strategy analyser. This is how it is supposed to work:

    I look at the High and the low of an initial period between 09.30 to 10.00. The distance between that high and low should be at least 20 points (not ticks), which is calculated by Variable0. If this is true, Variable5 gets the Value 1 and the strategy is supposed to trade for the day.

    After 10, I place two orders whether to buy at the low of that initial period or to sell short at the high of that period. I don't enter any position after 11, and once I am in a position (Variable9 gets the Value 1), I don't open another one.

    If I am long by 11, I try to sell on a limit, which is the high of the initial period minus 10 ticks (Limitprice should be denoted by Variable3). If I am short, I try to buy on a limit, which is the low plus 10 ticks (Limitprice should be denoted by Variable4).

    If I am still in a position by 12, I exit via a market order.

    Here's the source code:

    // Condition set 1
    if (ToTime(Time[0]) == ToTime(10, 00, 0))
    {
    Variable1 = High[30];
    Variable2 = Low[30];
    Variable3 = High[30] - 10 * TickSize;
    Variable4 = Low[30] + 10 * TickSize;
    Variable0 = (High[30] - Low[30]);
    Variable9 = 0;
    }
    // Condition set 2
    if (Variable0 >= 20)
    {
    Variable5 = 1;
    }
    // Condition set 3
    if (Variable5 == 1
    && ToTime(Time[0]) < ToTime(11, 00, 0)
    && ToTime(Time[0]) > ToTime(10, 00, 0)
    && Position.MarketPosition != MarketPosition.Short
    && Variable9 != 1)
    {
    EnterLongLimit(DefaultQuantity, Variable2, "long");
    }
    // Condition set 4
    if (Variable5 == 1
    && ToTime(Time[0]) < ToTime(11, 00, 0)
    && ToTime(Time[0]) > ToTime(10, 00, 0)
    && Position.MarketPosition != MarketPosition.Long
    && Variable9 != 1)
    {
    EnterShortLimit(DefaultQuantity, Variable1, "short");
    }
    // Condition set 5
    if (Position.MarketPosition == MarketPosition.Long)
    {
    ExitLongStop(Variable3, "exitlong", "long");
    Variable9 = 1;
    }
    // Condition set 6
    if (Position.MarketPosition == MarketPosition.Short)
    {
    ExitShortStop(Variable4, "exitshort", "short");
    Variable9 = 1;
    }
    // Condition set 7
    if (ToTime(Time[0]) == ToTime(12, 00, 0)
    && Position.MarketPosition == MarketPosition.Long)
    {
    ExitLong("", "long");
    }
    // Condition set 8
    if (ToTime(Time[0]) == ToTime(12, 00, 0)
    && Position.MarketPosition == MarketPosition.Short)
    {
    ExitShort("", "short");
    }

    #2
    Hi Gordon_Gekko,

    You are going to have to debug your code.


    Under brief examination it is most likely due to your second condition set.
    Code:
    if (Variable0 >= 20)
    {
        Variable5 = 1;
    }
    For the price of an instrument to actually span 20 in price within one bar is extremely rare. Like on the ER2 you normally only see high-low disparities of maybe 2 or 3, not 20.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hi and thanks for the fast reply!

      This: Variable0 = (High[30] - Low[30]); should give me the span of High and low of the previous 30 bars, right? So if I have 1 Min Bars, I will examine the last 30 Mins?

      Did I write the time conditions in a right way? So does this e.g.

      if (
      ToTime(Time[0]) < ToTime(11, 00, 0)
      && ToTime(Time[0]) > ToTime(10, 00, 0)
      ...

      really only make action between 10:00 and 11:00 or is there something wrong with this code?

      Tnx again!

      Comment


        #4
        No, High[30] gives the high price for the 30th bar back from the present.

        You probably want to use the Max() method. See: http://www.ninjatrader-support.com/H...aximumMAX.html

        Comment


          #5
          Hi Gordon Gekko,

          Your current code
          Code:
          High[30] - Low[30]
          Subtracts the high of the bar 30 minutes ago with the low of that same bar. Can you be more specific as to which value you would like to subtract from each other? I think you are trying to do the highest high of the past 30 minutes and the lowest low from the past 30 minutes. If that is the case you can try this:
          Code:
          Variable0 = (MAX(High, 30)[0] - MIN(Low, 30)[0]);
          Also your code for the time condition is correct. You are checking to make sure that the current bar's time is less than 11am AND greater than 10am at the same time.
          Josh P.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          646 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          367 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          107 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          569 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          573 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X