Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Auto-Break-Even

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

    Auto-Break-Even

    I want to code a auto-break-even in a strategy. I know, that you can't do this with the strategy-builder.
    I tried to put your "sample price modification" code into my strategy, but there seems to be something that I'm doing wrong.
    Could you please be so kind and post a code snippet for a auto-break-even?
    It should behave like this:

    If unrealized win bigger than VARIABLE set Stop to entry-price plus 1

    (Somehow this should be different for long and short orders).
    Thanks for your help!

    #2
    Hello moon_121,

    Thank you for your post.

    Using the SamplePriceModification example is a great way to start. For your sepecific example you could use the following code:

    Code:
    if (Position.GetProfitLoss(Close[0], PerformanceUnit.Points) > yourVariable)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 1 * TickSize);
    }
    For information on GetProfitLoss please visit the following link: http://www.ninjatrader.com/support/h...profitloss.htm

    Please let me know if I may be of further assistance.

    Comment


      #3
      I put the in an sample strategy (simple SMA cross). I could compile it, but do not get the desired results. Could you please check my code and tell me what I did wrong?
      Code:
      #region Using declarations
      using System;
      using System.ComponentModel;
      using System.Diagnostics;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Data;
      using NinjaTrader.Indicator;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Strategy;
      #endregion
      
      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
          /// <summary>
          /// Enter the description of your strategy here
          /// </summary>
          [Description("Enter the description of your strategy here")]
          public class SMAcrossBreakeven : Strategy
          {
              #region Variables
              // Wizard generated variables
              private int fast = 1; // Default setting for Fast
              private int slow = 1; // Default setting for Slow
              private int breakeven = 1; // Default setting for Breakeven
              // User defined variables (add any user defined variables below)
              #endregion
      
              /// <summary>
              /// This method is used to configure the strategy and is called once before any strategy method is called.
              /// </summary>
              protected override void Initialize()
              {
                  Add(SMA(Close, Fast));
                  Add(SMA(Median, Slow));
                  Add(SMA(Close, Fast));
                  Add(SMA(Median, Slow));
      
                  CalculateOnBarClose = true;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
                  // Condition set 1
                  if (CrossAbove(SMA(Close, Fast), SMA(Median, Slow), 1))
                  {
                      EnterLong(DefaultQuantity, "");
                  }
      
                  // Condition set 2
                  if (CrossBelow(SMA(Close, Fast), SMA(Median, Slow), 1))
                  {
                      EnterShort(DefaultQuantity, "");
                  }
      
                  // Beginning of Auto-Break-Even
                  // If not flat print our unrealized PnL
                    if (Position.MarketPosition != MarketPosition.Flat)
                    Print("Open PnL: " + Position.GetProfitLoss(Close[0], PerformanceUnit.Points));
              
                  if (Position.GetProfitLoss(Close[0], PerformanceUnit.Points) > breakeven)
                  {
                  SetStopLoss(CalculationMode.Price, Position.AvgPrice + 1 * TickSize);
                  }
              
              
              }
      
              #region Properties
              [Description("")]
              [GridCategory("Parameters")]
              public int Fast
              {
                  get { return fast; }
                  set { fast = Math.Max(1, value); }
              }
      
              [Description("")]
              [GridCategory("Parameters")]
              public int Slow
              {
                  get { return slow; }
                  set { slow = Math.Max(1, value); }
              }
      
              [Description("")]
              [GridCategory("Parameters")]
              public int Breakeven
              {
                  get { return breakeven; }
                  set { breakeven = Math.Max(1, value); }
              }
              #endregion
          }
      }

      Comment


        #4
        Hello moon_121,

        Thank you for your response.

        Please send me your log and trace files for today so that I may look into what occurred.

        You can do this by going to the Control Center-> Help-> Mail to Support.

        Please place 'ATTN: Patrick' in the subject line and reference this thread in the body of the e-mail: http://www.ninjatrader.com/support/f...ad.php?t=53668

        I look forward to your response.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by arvidvanstaey, Today, 02:19 PM
        4 responses
        10 views
        0 likes
        Last Post arvidvanstaey  
        Started by samish18, 04-17-2024, 08:57 AM
        16 responses
        56 views
        0 likes
        Last Post samish18  
        Started by jordanq2, Today, 03:10 PM
        2 responses
        8 views
        0 likes
        Last Post jordanq2  
        Started by traderqz, Today, 12:06 AM
        10 responses
        18 views
        0 likes
        Last Post traderqz  
        Started by algospoke, 04-17-2024, 06:40 PM
        5 responses
        47 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X