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

EntriesPerDirection = 1

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

    EntriesPerDirection = 1

    Hello,

    I have a strategy who consiste to trade only one instrument with only one order (short or long). In the strategy analyser all it’s perfect and if I put the strategy in the chart all it’s ok to… But when I connect the strategy with my broker, the strategy can increase the orders quantity by taking many trades after time and I don’t know why. (if i’m already long, I don’t want another order long…)

    Someone have a idea?

    ...

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"";
    Name = “Stategy”;
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = false;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.AdoptAccountPosition;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = true;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    IsInstantiatedOnEachOptimizationIteration = true;
    IsAdoptAccountPositionAware = true;

    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 1);
    AddDataSeries(Data.BarsPeriodType.Minute, 2);
    AddDataSeries(Data.BarsPeriodType.Minute, 3);
    AddDataSeries(Data.BarsPeriodType.Minute, 4);

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1
    || CurrentBars[1] < 1
    || CurrentBars[2] < 1
    || CurrentBars[3] < 1
    || CurrentBars[4] < 1)
    return;

    ExitLong(Convert.ToInt32(DefaultQuantity), "ProfitTarget", "");
    ExitShort(Convert.ToInt32(DefaultQuantity), "ProfitTarget", "");
    EnterLong(Convert.ToInt32(DefaultQuantity), "EnterLong");
    EnterShort(Convert.ToInt32(DefaultQuantity), "EnterShort");

    #2
    You could just add limiting script to prevent entries.

    Code:
    if ((Position.MarketPosition == MarketPosition.Flat)
        && (LongEntryCondition))
    {
    EnterLong(.....)
    }
    Note: I think your entry conditions for long and short are not mutually exclusive, so the strategy will result in "extra" orders in the case where a position is closed and opened at exactly the same time, which will not be properly governed by exit conditions. If you want to prevent this, you need to make sure your entries can't happen simultaneously with your exits..

    Comment


      #3
      Hello Acrux,

      Thank you for your note.

      May I confirm you are attempting to submit this block of code on every bar update?

      Code:
      ExitLong(Convert.ToInt32(DefaultQuantity), "ProfitTarget", "");
      ExitShort(Convert.ToInt32(DefaultQuantity), "ProfitTarget", "");
      EnterLong(Convert.ToInt32(DefaultQuantity), "EnterLong");
      EnterShort(Convert.ToInt32(DefaultQuantity), "EnterShort");
      I see your strategy start behavior is set to AdoptAccountPosition, which is a bit more advanced and requires more consideration than some of the other start behaviors, have you specifically programmed your strategy to be able to adopt account positions?

      See links below on Start Behavior.



      I look forward to your reply.
      Alan P.NinjaTrader Customer Service

      Comment


        #4
        Hello Alan,

        Thank you for your post.

        It's a simple trend following strategy, ExitLong and ExitShort almost never meet the conditions so it's like I still have a position in the game.

        So when I need to reactivate the strategy, following a maintenance from my broker for exemple, I want it to be aware of my current position and deal with it.

        It's make sense?

        Comment


          #5
          Hello Acrux,

          May I ask what you're trying to achieve by calling the following on every bar update?

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

          I look forward to your reply.
          Alan P.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_AlanP View Post
            Hello Acrux,

            May I ask what you're trying to achieve by calling the following on every bar update?

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

            I look forward to your reply.
            If I have 1 long position and the trend change, I want sell my position to cover and then make 1 short position, ex :

            if ((Bar in timeframe 1 go up)
            && (Bar in timeframe 2 go up)
            && (etc...))
            {
            EnterLong(Convert.ToInt32(DefaultQuantity), "EnterLong");
            }
            if ((Bar in timeframe 1 go down)
            && (Bar in timeframe 2 go down)
            && (etc...))
            {
            EnterShort(Convert.ToInt32(DefaultQuantity), "EnterShort");
            }

            Is there a problem?

            Comment


              #7
              Hello Acrux,

              Without the full code we're unable to test on our end.

              If you'd like to upload the full code I can take a look and see if anything jumps out. Or if you'd prefer to email a copy, send to platformsupport[at]ninjatrader[dot]com with Attn: Alan P in the Subject line. Also within the email please include a link to this thread, and the files.

              I look forward to your reply.
              Alan P.NinjaTrader Customer Service

              Comment


                #8
                Where are the Exits located?
                NT_AP is right. Not enough information in your posts.

                Comment


                  #9
                  Originally posted by NinjaTrader_AlanP View Post
                  Hello Acrux,

                  Without the full code we're unable to test on our end.

                  If you'd like to upload the full code I can take a look and see if anything jumps out. Or if you'd prefer to email a copy, send to platformsupport[at]ninjatrader[dot]com with Attn: Alan P in the Subject line. Also within the email please include a link to this thread, and the files.

                  I look forward to your reply.
                  Hi guys,

                  Thanks to helping me.

                  Like I said, I don't really use the Exits, it append almost never. All it's working fine for me with this, but sometimes (not every day), the strategy take a second position in the same direction and I never want to have more then 1 position. It append just when my strategy is connected with the broker. It may be just a glitch sometimes...

                  Let me know if you see something wrong :

                  #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 MyStrategy : Strategy
                  {
                  private MyIndicator MyIndicator60;
                  private MyIndicator MyIndicator120;
                  private MyIndicator MyIndicator180;
                  private RSI RSI5;
                  private RSI RSI180;

                  protected override void OnStateChange()
                  {
                  if (State == State.SetDefaults)
                  {
                  Description = @"";
                  Name = "MyStrategy";
                  Calculate = Calculate.OnBarClose;
                  EntriesPerDirection = 1;
                  EntryHandling = EntryHandling.AllEntries;
                  IsExitOnSessionCloseStrategy = false;
                  ExitOnSessionCloseSeconds = 30;
                  IsFillLimitOnTouch = false;
                  MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                  OrderFillResolution = OrderFillResolution.Standard;
                  Slippage = 0;
                  StartBehavior = StartBehavior.AdoptAccountPosition;
                  TimeInForce = TimeInForce.Gtc;
                  TraceOrders = true;
                  RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                  StopTargetHandling = StopTargetHandling.PerEntryExecution;
                  BarsRequiredToTrade = 20;
                  IsInstantiatedOnEachOptimizationIteration = true;
                  IsAdoptAccountPositionAware = true;
                  DefaultQuantity = 1;
                  }
                  else if (State == State.Configure)
                  {
                  AddDataSeries(Data.BarsPeriodType.Minute, 5);
                  AddDataSeries(Data.BarsPeriodType.Minute, 60);
                  AddDataSeries(Data.BarsPeriodType.Minute, 120);
                  AddDataSeries(Data.BarsPeriodType.Minute, 180);
                  }
                  else if (State == State.DataLoaded)
                  {
                  MyIndicator60 = MyIndicator(Closes[2]);
                  MyIndicator120 = MyIndicator(Closes[3]);
                  MyIndicator180 = MyIndicator(Closes[4]);
                  RSI5 = RSI(Closes[1], 14, 3);
                  RSI180 = RSI(Closes[4], 14, 3);
                  }
                  }

                  protected override void OnBarUpdate()
                  {
                  if (BarsInProgress != 0)
                  return;

                  if (CurrentBars[0] < 1
                  || CurrentBars[1] < 1
                  || CurrentBars[2] < 1
                  || CurrentBars[3] < 1
                  || CurrentBars[4] < 1)
                  return;


                  // Exit Long

                  if ((RSI5.Default[0] > 90)
                  && (RSI180.Default[0] > 50))
                  {
                  ExitLong(Convert.ToInt32(DefaultQuantity), "ProfitTarget", "");
                  }

                  // Exit Short

                  if ((RSI5.Default[0] < 10)
                  && (RSI180.Default[0] < 50))
                  {
                  ExitShort(Convert.ToInt32(DefaultQuantity), "ProfitTarget", "");
                  }

                  // Enter Long

                  if ((RSI5.Default[0] < 50)
                  && (MyIndicator60.Open[0] > MyIndicator60.Close[0])
                  && (MyIndicator120.Open[0] > MyIndicator120.Close[0])
                  && (MyIndicator180.Open[0] > MyIndicator180.Close[0]))
                  {
                  EnterLong(Convert.ToInt32(DefaultQuantity), "EnterLong");
                  }

                  // Enter Short

                  if ((RSI5.Default[0] > 50)
                  && (MyIndicator60.Open[0] < MyIndicator60.Close[0])
                  && (MyIndicator120.Open[0] < MyIndicator120.Close[0])
                  && (MyIndicator180.Open[0] < MyIndicator180.Close[0]))
                  {
                  EnterShort(Convert.ToInt32(DefaultQuantity), "EnterShort");
                  }
                  }
                  }
                  }
                  Last edited by Acrux; 01-04-2018, 06:03 PM.

                  Comment


                    #10
                    Is it possible, for example that

                    1) you have EnterShort on the 1 set of conditions. (SELL)

                    2) Your other set of conditions fires an ExitLong (SELL).

                    Your broker would score this as 2 SHORT positions. I don't think EntriesPerDirection would block this.

                    EntriesPerDirection blocks you from EnterShort EnterShort EnterShort as your enter condition
                    keeps firing on every incoming tick.

                    That's the only thing that makes sense to me at the moment without seeing exactly what is going on, and explains your "it only happens sometimes but not everyday".

                    For other reasons, when I execute EnterShort, I usually set a variable to say I have entered short. In your case, I would use this variable to prevent ExitLong from firing.

                    And the opposite would need done for EnterLong.

                    Comment


                      #11
                      Originally posted by sledge View Post
                      Is it possible, for example that

                      1) you have EnterShort on the 1 set of conditions. (SELL)

                      2) Your other set of conditions fires an ExitLong (SELL).

                      Your broker would score this as 2 SHORT positions. I don't think EntriesPerDirection would block this.

                      EntriesPerDirection blocks you from EnterShort EnterShort EnterShort as your enter condition
                      keeps firing on every incoming tick.

                      That's the only thing that makes sense to me at the moment without seeing exactly what is going on, and explains your "it only happens sometimes but not everyday".

                      For other reasons, when I execute EnterShort, I usually set a variable to say I have entered short. In your case, I would use this variable to prevent ExitLong from firing.

                      And the opposite would need done for EnterLong.
                      Hi sledge,

                      It's not really possible because the ExitLong need to be overbought in uptrend and the EnterShort need to be downtrend for minimum 3 hours... But anyway, if it append I will see it in the chart with the plot executions.

                      It happened again today, I maked a EnterLong manually and then start the strategy.Two hours later, a second EnterLong has taken.

                      I think this is the pattern, when I make a trade by my self and then start the strategy, even if the position is recongnize, it seems that a second position can be taken...?
                      If I Iet the strategy alone and never take a trade manually, I believe it never happens.
                      Last edited by Acrux; 01-05-2018, 08:21 AM.

                      Comment


                        #12
                        Hello Acrux,

                        Thank you for your response.

                        Can you send us your log and trace files so we may review the last time you ran the strategy on real-time data? Please provide the date, time, and instrument you ran the strategy on as well.

                        You can do this by going to the Control Center-> Help-> Email Support. Ensuring 'Log and Trace Files' is checked will include these files. This is checked by default.

                        Please list 'ATTN: Patrick H' in the subject line and reference this thread in the body of the email.

                        I look forward to your response.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by andrewtrades, Today, 04:57 PM
                        1 response
                        4 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Started by chbruno, Today, 04:10 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post chbruno
                        by chbruno
                         
                        Started by josh18955, 03-25-2023, 11:16 AM
                        6 responses
                        436 views
                        0 likes
                        Last Post Delerium  
                        Started by FAQtrader, Today, 03:35 PM
                        0 responses
                        6 views
                        0 likes
                        Last Post FAQtrader  
                        Started by rocketman7, Today, 09:41 AM
                        5 responses
                        19 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Working...
                        X