Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

strategy distance of openprice.

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

    strategy distance of openprice.

    Good morning, I would like to create a strategy that allows me to enter long if the price drops by 50 points from the opening price and enter short if the price rises by 50 points from the opening price (0001) , the stop loss and take profit of the operations are 50 points and 60 points, both the distance of points from the opening price and the take profit and stop loss I would like to be able to set it in the inputs from time to time. . I tried to create it with artificial intelligence but it does not work .... could you help me? or is there something ready? thanks


    #2
    Hello Dugez,

    That is possible, it would be most simple to do in manual coding. I would suggest using Ticks instead of points as that would simplify the equation. For example if you wanted to check if a price is less than 50 ticks from the open of the bar it would be:

    if(Close[0} < Open[0] - 50 * TickSize)
    {

    }

    You could check how many ticks is the equivalent of 50 points and replace that number in the statement.

    Comment


      #3
      He created this for me, I tried but it doesn't work, where is the error?

      using NinjaTrader.Gui.Tools;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Data;
      using NinjaTrader.Gui;
      using System.Windows.Media;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using NinjaTrader.Cbi;
      using NinjaTrader.NinjaScript.DrawingTools;

      //This namespace holds Strategies in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class OpeningPriceStrategy : Strategy
      {
      private double openingPrice;
      private double upperEntryPrice;
      private double lowerEntryPrice;

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = "A strategy based on the Opening Price Indicator, entering at a specific distance from the opening price.";
      Name = "OpeningPriceStrategy";
      Calculate = Calculate.OnEachTick;
      IsOverlay = false;
      IsUnmanaged = false;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      BarsRequiredToTrade = 20;

      // Strategy parameters
      ProfitTargetTicks = 200;
      StopLossTicks = 200;
      EntryOffsetTicks = 120;
      }
      }

      protected override void OnBarUpdate()
      {
      // Ensure we have enough bars to begin trading
      if (CurrentBars[0] < BarsRequiredToTrade)
      return;

      // Set the opening price
      openingPrice = Open[0];
      upperEntryPrice = openingPrice + EntryOffsetTicks * TickSize;
      lowerEntryPrice = openingPrice - EntryOffsetTicks * TickSize;

      // Draw entry levels on the chart for visual reference
      Draw.Line(this, "UpperEntryLine", false, 0, upperEntryPrice, 10, upperEntryPrice, Brushes.Red, DashStyleHelper.Dash, 2);
      Draw.Line(this, "LowerEntryLine", false, 0, lowerEntryPrice, 10, lowerEntryPrice, Brushes.Green, DashStyleHelper.Dash, 2);

      // Debugging: Print the current values to see why orders might not be triggering
      Print($"Current Close: {Close[0]}, Opening Price: {openingPrice}, Upper Entry Price: {upperEntryPrice}, Lower Entry Price: {lowerEntryPrice}");

      // Entry Condition: Short if the price rises above the opening price + offset
      if (High[0] >= upperEntryPrice && Position.MarketPosition == MarketPosition.Flat)
      {
      Print("Entering Short Position");
      EnterShort("ShortAtUpperLevel");
      }

      // Entry Condition: Long if the price drops below the opening price - offset
      if (Low[0] <= lowerEntryPrice && Position.MarketPosition == MarketPosition.Flat)
      {
      Print("Entering Long Position");
      EnterLong("LongAtLowerLevel");
      }

      // Set profit target and stop loss for all entries
      if (Position.MarketPosition == MarketPosition.Long)
      {
      SetProfitTarget("LongAtLowerLevel", CalculationMode.Ticks, ProfitTargetTicks);
      SetStopLoss("LongAtLowerLevel", CalculationMode.Ticks, StopLossTicks, false);
      }

      if (Position.MarketPosition == MarketPosition.Short)
      {
      SetProfitTarget("ShortAtUpperLevel", CalculationMode.Ticks, ProfitTargetTicks);
      SetStopLoss("ShortAtUpperLevel", CalculationMode.Ticks, StopLossTicks, false);
      }
      }

      region Properties

      [Range(1, int.MaxValue), NinjaScriptProperty]
      [Display(Name = "ProfitTargetTicks", Description = "Number of ticks for profit target", Order = 1, GroupName = "Parameters")]
      public int ProfitTargetTicks { get; set; }

      [Range(1, int.MaxValue), NinjaScriptProperty]
      [Display(Name = "StopLossTicks", Description = "Number of ticks for stop loss", Order = 2, GroupName = "Parameters")]
      public int StopLossTicks { get; set; }

      [Range(1, int.MaxValue), NinjaScriptProperty]
      [Display(Name = "EntryOffsetTicks", Description = "Number of ticks away from opening price for entry", Order = 3, GroupName = "Parameters")]
      public int EntryOffsetTicks { get; set; }

      #endregion
      }
      }

      Comment


        #4
        Hello Dugez,

        While we cannot assist with AI generated code we can try to answer any questions you may have or provide links to learning resources.

        If you are seeing a compile error you would need to post the details about that so we know what kind of error you are having. If you otherwise are seeing an error when running it we would need the details of what errors you see in the output window.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        58 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        133 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        73 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        45 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        50 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X