Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

My strategy is not working as desired

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

    My strategy is not working as desired

    Hello.
    I am a complete beginner and to learn, I am trying to develop the stupidest strategy possible: When a new candle appears, we take a sale if the previous one is bullish and a purchase if the previous one is bearish.
    When I test with the strategy analyzer, everything works.
    When I put it live on my demo account, a first trade is taken. It is not closed on the new candle but later, whatever the time frame used and it does not resume a trade before many candles.
    Could you tell me what is wrong with my code because I think the logic is respected.
    But it may also be the parameters more than the logic.
    Thank you all.​
    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class SimpleCandleStrategy : Strategy
        {
            private double lastBarOpen = 0;
            private double lastBarClose = 0;
            private int lastBarIndex = -1;
            
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"SimpleCandleStrategy  SimpleCandleStrategy  SimpleCandleStrategy ";
                    Name                                        = "SimpleCandleStrategy";
                    Calculate                                    = Calculate.OnEachTick;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 4;
                    StartBehavior                                = StartBehavior.ImmediatelySubmit;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = false;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 20;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration    = true;
    
                }
                else if (State == State.Configure)
                {
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0)
                            return;
    
                if (CurrentBar < 2) return;
                
                if (CurrentBar != lastBarIndex)
                {
                    lastBarIndex = CurrentBar;
    
                    double previousOpen = lastBarOpen;
                    double previousClose = lastBarClose;
    
                    lastBarOpen = Open[0];
                    lastBarClose = Close[1];
    
                    if (Position.MarketPosition == MarketPosition.Flat)
                    {
                        if (previousClose > previousOpen)
                        {
                            EnterShort();
                        }
                        else if (previousClose < previousOpen)
                        {
                            EnterLong();
                        }
                    }
                    else if (Position.MarketPosition == MarketPosition.Long)
                    {
                        if (previousClose > previousOpen)
                        {
                            ExitLong();
                            EnterShort();
                        }
                    }
                    else if (Position.MarketPosition == MarketPosition.Short)
                    {
                        if (previousClose < previousOpen)
                        {
                            ExitShort();
                            EnterLong();
                        }
                    }
                }
            }
        }
    }
    ​

    #2
    Hello StephDeNice,

    Thank you for your post.

    This code will call an entry and an exit on the same bar, which can result in an overfill. If you want to reverse the position, simply call the Enter() method in the opposite direction without calling an Exit() method.

    An order with the name 'Close position' is automatically sent by NinjaTrader when calling an entry order in the opposite direction of the position. Calling an exit order and an entry order at the same time will result in 3 orders being submitted. The Exit order, the Close position order, and the Entry order.

    This will double the position in the opposite direction or cause an overfill. If this is your strategy you are programming, and if you are trying to reverse the position, call the entry in the opposite direction without calling an exit order.

    Please see this forum post for a detailed explanation:



    If you have any other questions, please let me know.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    56 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
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X