Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problems with Automated Strategy

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

    Problems with Automated Strategy

    Hi,

    Just downloaded NT and am trying to use it for Forex and Futures. Having lots of issues.

    Problems:
    1. I tried running my strategy and it complained about trying to set stop loss above current price or something. I cancelled all open orders and such. Now, when I try to rerun my strategy with some tweaks, it keeps thinking I have open positions even though I am using a simulated account against a real IB data feed. So, now, my strategy doesnt kick in at all because from the get go it thinks I am long instead of flat.

    2. For whatever reason the Print statements at the top of my OnBarUpdate event keep getting printed twice each time the event is fired. So, I am beginning to feel, that event is fired once for the bar that just completed, and once for the new bar. Is there anyway to make it fire just once at the close of a bar?

    3. I tried running the code with CalculateOnBarClose with True and False and not much difference. With False, the OnBarUpdate keeps firing a lot and with true, only at the end of bar update. But like I said in #2, it fires in pairs regardless of this setting.

    My code is below just for reference:
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// 3 Bar Up or Down, go long/short
    /// </summary>
    [Description("3 Bar Up or Down, go long/short")]
    public class RB3BarSetupV2 : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int qty = 1; // Default setting for Qty
    // 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( PeriodType.Minute, 1);
    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {

    //Long signal
    if (CurrentBar > 3) {
    Print("Close 1:"+Close[1].ToString());
    Print("Close 2:"+Close[2].ToString());
    Print("Close 3:"+Close[3].ToString());
    Print("Market position:"+Position.MarketPosition.ToString());

    if ((Close[1] > Close[2] && Close[2] > Close[3]) && Position.MarketPosition == MarketPosition.Flat) {
    EnterLong(Qty);
    Print("Stop:"+MIN(Low,3)[0]);
    //SetStopLoss(CalculationMode.Price,MIN(Low,3)[0]);
    ExitLongStop(MIN(Low,3)[0]);
    } else if ((Close[1] < Close[2] && Close[2] < Close[3]) && Position.MarketPosition == MarketPosition.Flat) {
    EnterShort(Qty);
    Print("Current Price:"+Close[0]);
    Print("Stop:"+MIN(High,3)[0]);
    //SetStopLoss(CalculationMode.Price,MAX(High,3)[0]);
    ExitShortStop(MAX(High,3)[0]);
    } else if ((Close[1] > Close[2] && Close[2] > Close[3]) && Position.MarketPosition == MarketPosition.Short) {
    ExitLong();
    } else if ((Close[1] < Close[2] && Close[2] < Close[3]) && Position.MarketPosition == MarketPosition.Long) {
    ExitShort();
    }

    }
    }

    #region Properties
    [Description("")]
    [Category("Parameters")]
    public int Qty
    {
    get { return qty; }
    set { qty = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #2
    Hello,

    >>1. I tried running my strategy and it complained about trying to set stop loss above current price or something. I cancelled all open orders and such. Now, when I try to rerun my strategy with some tweaks, it keeps thinking I have open positions even though I am using a simulated account against a real IB data feed. So, now, my strategy doesnt kick in at all because from the get go it thinks I am long instead of flat.

    This error occurs when you place an order on an illegal price for that type of order. For example you cannot place a standard limit buy order above the last traded price.

    For the open positions issue please review this link for details on how to rectify it:


    >>2. For whatever reason the Print statements at the top of my OnBarUpdate event keep getting printed twice each time the event is fired. So, I am beginning to feel, that event is fired once for the bar that just completed, and once for the new bar. Is there anyway to make it fire just once at the close of a bar?

    The OnBarUpdate() event only happens once per bar. There must be something going on in your code to produce this. I suggest commenting out one section of code at a time to see what is causing this.

    You also may want to add Time[0] in your prints to determine when it is being printed exactly.


    >>3. I tried running the code with CalculateOnBarClose with True and False and not much difference. With False, the OnBarUpdate keeps firing a lot and with true, only at the end of bar update. But like I said in #2, it fires in pairs regardless of this setting.

    Please see above.
    DenNinjaTrader Customer Service

    Comment


      #3
      Originally posted by stocastix View Post
      } else if ((Close[1] > Close[2] && Close[2] > Close[3]) && Position.MarketPosition == MarketPosition.Short) {
      ExitLong();
      } else if ((Close[1] < Close[2] && Close[2] < Close[3]) && Position.MarketPosition == MarketPosition.Long) {
      ExitShort();
      if you haven't solved this already, i think you might want to switch either marketposition.short/long OR exitlong/short

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by ghoul, Today, 06:02 PM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by jeronymite, 04-12-2024, 04:26 PM
      3 responses
      44 views
      0 likes
      Last Post jeronymite  
      Started by Barry Milan, Yesterday, 10:35 PM
      7 responses
      20 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by AttiM, 02-14-2024, 05:20 PM
      10 responses
      180 views
      0 likes
      Last Post jeronymite  
      Started by DanielSanMartin, Yesterday, 02:37 PM
      2 responses
      13 views
      0 likes
      Last Post DanielSanMartin  
      Working...
      X