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

Strategy builder - breakeven after certain ticks/price

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

    Strategy builder - breakeven after certain ticks/price

    Hello,
    I have looked all over a previous thread for help on BREAKEVEN after certain ticks/price. I found a zip file with a sample strategy but unfortunately when I try to copy the parameters from that strategy into mine, somehow my enter positions won't fill nor profit or stop loss. if anyone ever made it through this, PLEASE help a brother out!... My strategy is 99% ready but I'm stuck with the breakeven part...


    #2
    I can modifying the tick/price strategy ,'My strategy is 99% ready but I'm stuck with the breakeven part'. have you the code.
    Last edited by Emma1; 08-01-2021, 10:04 AM.

    Comment


      #3


      #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.Gui.Tools;

      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 BLUEMOON : Strategy

      {

      private MACD MACD1;

      private MACD MACD2;

      private EMA EMA1;




      protected override void OnStateChange()

      {

      if (State == State.SetDefaults)

      {

      Description = @"";

      Name = "BLUEMOON";

      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.ByStrategyPosition;

      BarsRequiredToTrade = 20;

      // Disable this property for performance gains in Strategy Analyzer optimizations

      // See the Help Guide for additional information

      IsInstantiatedOnEachOptimizationIteration = true;

      START = DateTime.Parse("08:00", System.Globalization.CultureInfo.InvariantCulture) ;

      END = DateTime.Parse("11:00", System.Globalization.CultureInfo.InvariantCulture) ;

      }

      else if (State == State.Configure)

      {

      }

      else if (State == State.DataLoaded)

      {

      MACD1 = MACD(Close, 10, 25, 9);

      MACD2 = MACD(Close, 10, 25, 9);

      EMA1 = EMA(Close, 8);

      MACD1.Plots[0].Brush = Brushes.DarkCyan;

      MACD1.Plots[1].Brush = Brushes.Crimson;

      MACD1.Plots[2].Brush = Brushes.Transparent;

      EMA1.Plots[0].Brush = Brushes.Goldenrod;

      AddChartIndicator(MACD1);

      AddChartIndicator(EMA1);

      SetStopLoss(CalculationMode.Ticks, 40);

      SetProfitTarget("", CalculationMode.Ticks, 60);

      }

      }




      protected override void OnBarUpdate()

      {

      if (BarsInProgress != 0)

      return;




      if (CurrentBars[0] < 1)

      return;




      // Set 1

      if ((Times[0][0].TimeOfDay >= START.TimeOfDay)

      && (Times[0][0].TimeOfDay <= END.TimeOfDay)

      && (CrossBelow(MACD1.Avg, MACD2.Avg, 1))

      && (Close[0] > EMA1[0]))

      {

      EnterLong(Convert.ToInt32(DefaultQuantity), "");

      }



      // Set 2

      if ((Times[0][0].TimeOfDay >= START.TimeOfDay)

      && (Times[0][0].TimeOfDay <= END.TimeOfDay)

      && (CrossBelow(MACD1.Avg, MACD2.Avg, 1))

      && (Close[0] < EMA1[0]))

      {

      EnterShort(Convert.ToInt32(DefaultQuantity), "");

      }



      // Set 3

      if (CrossBelow(MACD1.Avg, MACD2.Avg, 1))

      {

      ExitLong(Convert.ToInt32(DefaultQuantity), "", "");

      }



      // Set 4

      if (CrossAbove(MACD1.Avg, MACD2.Avg, 1))

      {

      ExitShort(Convert.ToInt32(DefaultQuantity), "", "");

      }



      }




      #region Properties

      [NinjaScriptProperty]

      [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKe y")]

      [Display(Name="START", Order=1, GroupName="Parameters")]

      public DateTime START

      { get; set; }




      [NinjaScriptProperty]

      [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKe y")]

      [Display(Name="END", Order=2, GroupName="Parameters")]

      public DateTime END

      { get; set; }

      #endregion




      }

      }

      Comment


        #4
        Update: We were posting at the same time. Looks like you already found it!

        My empower people by teaching people how to fish for themselves answer ..
        Here is an example Google query that you can use to quickly find good info on many NinjaTrader topics.
        https://www.google.com/search?q=Ninj...+Even&ei=OHgDY


        A more direct answer from Paul in the good link he just posted here.
        https://ninjatrader.com/support/foru...-1#post1165795

        HedgePlay

        Comment


          #5
          Sorry Emma, I don't know if this is what you meant but I appreciate your help!!!

          Comment


            #6
            Originally posted by hedgeplay View Post
            My empower people by teaching people how to fish for themselves answer ..
            Here is an example Google query that you can use to quickly find good info on many NinjaTrader topics.
            https://www.google.com/search?q=Ninj...+Even&ei=OHgDY


            A more direct answer from Paul in the good link he just posted here.
            https://ninjatrader.com/support/foru...-1#post1165795

            HedgePlay
            Since I'm new to the whole coding thing, I find Paul's answer very hard to follow,,, my apologies

            Comment


              #7
              Originally posted by rafael_delima86 View Post

              Since I'm new to the whole coding thing, I find Paul's answer very hard to follow,,, my apologies

              "my apologies"

              Don't do that. This is what the support forum is for, to help you as you work to figure things out.



              "Since I'm new to the whole coding thing, I find Paul's answer very hard to follow.."


              The short answer is that you incorporate code from the example Paul referenced, the example Chelsea wrote.


              Incorporate steps 1-5 in the code snip below below into your OnBarUpdate() code right after your current Step 4. So the steps below should become steps 5-9 in your code.


              If if is not clear how to do that just ask the NT support team staff are very helpful as people work to figure out how to use Builder. Just tell them how far you have gotten and ask questions to help you get past the next step. They welcome your questions and are very good at responding.

              HedgePlay



              Code:
              // Set 1
              if ((TriggerState >= 2)
              && (Position.MarketPosition == MarketPosition.Flat))
              {
              TriggerState = 0;
              }
              
              // Set 2
              if (Position.MarketPosition == MarketPosition.Flat)
              {
              TriggerState = 1;
              EnterLong(Convert.ToInt32(DefaultQuantity), @"entry");
              }
              
              // Set 3
              if ((TriggerState == 1)
              && (Position.MarketPosition == MarketPosition.Long))
              {
              TriggerState = 2;
              StopPrice = (Position.AveragePrice + (InitialStopDistance * TickSize)) ;
              TriggerPrice = (Position.AveragePrice + (BreakEvenTrigger * TickSize)) ;
              }
              
              // Set 4
              if ((TriggerState == 2)
              && (Close[0] >= TriggerPrice))
              {
              TriggerState = 3;
              StopPrice = Position.AveragePrice;
              Draw.Diamond(this, @"BreakEvenBuilderExample Diamond_1", true, 0, (High[0] + (2 * TickSize)) , Brushes.DarkCyan);
              }
              
              // Set 5
              if (TriggerState >= 2)
              {
              ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), StopPrice, @"exit", @"entry");
              }

              Comment


                #8
                Hello rafael_delima86,

                The general idea is to save the current price with an offset of the number of ticks of where you want the breakeven to be and then have a condition that compares the current price to that variable.

                Once the breakeven price is reached, the stop can be set to a new price.

                Please let me know if you have any questions about this, I am happy to assist.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  rafael_delima86 , you were reffering to prevoius example, i taught you had a clip of the questioned code.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by MarionCron, Yesterday, 11:43 PM
                  0 responses
                  4 views
                  0 likes
                  Last Post MarionCron  
                  Started by kaywai, Yesterday, 11:20 PM
                  0 responses
                  4 views
                  0 likes
                  Last Post kaywai
                  by kaywai
                   
                  Started by JodieThur, Yesterday, 11:11 PM
                  0 responses
                  5 views
                  0 likes
                  Last Post JodieThur  
                  Started by cryfgg, Yesterday, 06:01 PM
                  0 responses
                  19 views
                  0 likes
                  Last Post cryfgg
                  by cryfgg
                   
                  Started by Tremblinghandtrader, Yesterday, 04:56 PM
                  0 responses
                  35 views
                  0 likes
                  Last Post Tremblinghandtrader  
                  Working...
                  X