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

Disconnecting from data interrupts strategy?

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

    Disconnecting from data interrupts strategy?

    Hi,
    I trade only between certain hours (7am to 5pm). As such, I generally connect the data feed in NT8 in the morning, and then disconnect again at night.

    If I trade on the hourly chart, and my strategy requires 60 hours to pass before a trade alert is generated, does this mean an Alert will never ever be generated?

    I/e Do I need to leave myself connected to the data feed 24/7?

    Code below:

    // Look for a trigger for the strong trade
    if (Position.MarketPosition == MarketPosition.Flat
    && Notrade
    && SMA(movav)[0]<SMA(movavshort)[0]
    )

    { Triggerbar=CurrentBar;
    Notrade=false;
    GoLong=true;
    }

    // Switch off the potential trade if the SMA short breaks below the longer term SMA

    if (Position.MarketPosition == MarketPosition.Flat
    && GoLong
    && SMA(movav)[0]>SMA(movavshort)[0]
    )

    { Triggerbar=0;
    Notrade=true;
    GoLong=false;
    }

    // Switch off the potential trade if the close breaks above the longer term SMA

    if (Position.MarketPosition == MarketPosition.Flat
    && GoLong
    && Close[0]<SMA(movav)[0]
    )

    { Triggerbar=0;
    Notrade=true;
    GoLong=false;
    }

    if (Position.MarketPosition == MarketPosition.Flat
    && ToTime(Time[0])>70000 && ToTime(Time[0])<170000
    && SMA(movav)[0]<SMA(movavshort)[0]
    && Low[0]<SMA(movavshort)[0]
    && GoLong
    && CurrentBar-Triggerbar>60

    {Enter trade;}

    #2
    Hello lancasterstephen,

    Thanks for the post.

    Note this reference sample that shows how to limit trading hours using a time filter, this does not rely on runtime variables:



    To use your approach, would be better to use the Time[] array to mark an epoch that can be checked later. NinjaTrader needs to be running with an active data feed for the OnBarUpdate function to be called.

    A good way to check that enough time has passed would be to write down the time in a text file, then read that file on the next run of your script to check that at least 60 hours have passed.

    This reference sample shows how to write to a text file:


    The former approach would be recommended over this though.

    Please let me know if I can assist further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris

      Sorry, I don't follow your response.

      As an example, if CurrentBar-Triggerbar = 48 at 5pm and I disconnected my data feed for the day.

      If the conditions continued to be met through the evening, at 7am the next morning when I reconnected to the data feed, CurrentBar-Triggerbar = 62 and therefore a trade should trigger.

      Would Ninjatrader recognise this and enter a trade, or because I was not connected to the datafeed for the entire 62 bars would it not enter a trade?

      Many thanks
      STeve

      Comment


        #4
        Hello Steve,

        Thanks for the reply.

        If your session template is set to something other than default 24/7, then counting the bars would not work because the bars would stop being built at the end of the session and begin again at the next session start, excluding all the hours in between. Try this idea with the default 24/7 template, that does seem like it could work even when NinjaTrader is shut down. Using the time filter example would be the recommended way of limiting your trading hours though.

        I look forward to hearing of your results.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hi Chris

          So, if my Session Template is set to default 24/7, then when I switch off the data feed, and reconnect in the morning, then it should take into account the fact that bars 49 through to 62 have occurred?

          How do I check my Session Template sessions please? Tools-Trading Hours-Forex looks to be set to 24 /7.

          Start End
          Sunday 1700 Monday 1700
          Monday 1700 Tues 1700


          Does this mean I'm ok for what I'm trying to achieve? Doesn't seem to be generating Alerts/Trades in real time.

          I can get results Backtesting the strategy

          Thanks for the advice on how to more appropriately limit my trading hours, I'll take a look and compare to how I've currently done it.

          Comment


            #6
            Hello Steve,

            Thanks for the reply.

            Use Print statements to output values that make your condition true, then view the output through a NinjaScript output window - this is available through the New menu. Print statements help to see why a condition is not becoming true.

            EX:

            Code:
            Print("Evaluating Alert Condition");
            Print(Position.MarketPosition);
            Print(Time[0]);
            Print(SMA(movav)[0]);
            Print(SMA(movavshort)[0]);
            Print(Low[0]);
            Print(GoLong);
            Print(CurrentBar);
            Print(Triggerbar);
            if (Position.MarketPosition == MarketPosition.Flat
            && ToTime(Time[0])>70000 && ToTime(Time[0])<170000
            && SMA(movav)[0]<SMA(movavshort)[0]
            && Low[0]<SMA(movavshort)[0]
            && GoLong
            && CurrentBar-Triggerbar>60)
            https://ninjatrader.com/support/help...-us/?print.htm - Print()

            Please let me know if I can assist further.
            Chris L.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by jaybedreamin, Today, 05:56 PM
            0 responses
            3 views
            0 likes
            Last Post jaybedreamin  
            Started by DJ888, 04-16-2024, 06:09 PM
            6 responses
            18 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by Jon17, Today, 04:33 PM
            0 responses
            1 view
            0 likes
            Last Post Jon17
            by Jon17
             
            Started by Javierw.ok, Today, 04:12 PM
            0 responses
            6 views
            0 likes
            Last Post Javierw.ok  
            Started by timmbbo, Today, 08:59 AM
            2 responses
            10 views
            0 likes
            Last Post bltdavid  
            Working...
            X