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

Exit all position on the given time.

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

  • NinjaTrader_ChelseaB
    replied
    Hello DTSSTS,

    Time[0] is the time of the bar. This should be the first object printed in the print.

    Please watch the video 'Debugging using prints with the Strategy Builder' and 'NinjaScript Editor 401' for a demonstration how to use Print() to understand behavior.


    The print should have the time of the bar and all values used in the condition. Be sure there are labels for each value and comparison operator.

    Leave a comment:


  • DTSSTS
    replied
    Not sure what you mean


    Print the time of the bar first, <<-- what defines the first bar


    then print all values in the condition < what defines IN the Condition​

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    Hello DTSSTS,

    May I have you provide a saved text file of print output to understand the behavior?

    Print the time of the bar first, then print all values in the condition

    Leave a comment:


  • DTSSTS
    replied
    Using you First Example

    To close the position of the strategy at a specific time in OnBarUpdate():

    if (ToTime(Time[0]) == 90000)
    {
    if (Position.MarketPosition == MarketPosition.Long)
    ExitLong();
    if (Position.MarketPosition == MarketPosition.Short)
    ExitShort();
    }

    This would close an open position at 9:00 AM​

    I created a version with a variable time input, which I have watched it executed exits as expected except the time will be off about a 60 to 120 seconds

    Today It did not create my exits at 8:26 AM on 2 different strategies that were in positions prior to the Exit Time Settings

    Exit1TimeInput = DateTime.Parse("08:26", System.Globalization.CultureInfo.InvariantCulture) ;


    [NinjaScriptProperty]
    [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKe y")]
    [Display(Name="Exit 1 Time Input", Description="Use Time Exit 1 Time Input", Order=32, GroupName="Parameters")]
    public DateTime Exit1TimeInput
    { get; set; }


    MY STRATEGY RUNS ON A RENKO STYLE BAR, THE STRATEGY ALSO HAS A SECONDARY 1 MINUTE DATA
    SO I USED Times[1][0] to base the condition TimeOfDay on the 1 minute Secondary data

    /// TimeExit1
    if ((Position.MarketPosition == MarketPosition.Long)
    && (LongTrades == true)
    && (TimeExit1 == true)

    && (Times[1][0].TimeOfDay == Exit1TimeInput.TimeOfDay))

    {
    ExitLong(Convert.ToInt32(Position.Quantity), @"LTimeExit1", @"");
    PerBarExitONEmade = true;
    }

    SO I CANNOT FIGURE OUT WHY is NOT exact time set 08:26 AM and this may be why did not exit my positions today. The prior witness exits that were off 60-120 seconds may have been lagging more today for something in the condition using Times[1][0]

    Thanks for any help
    DTSSTS

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    Hello kazbek966,

    There is a reference sample for intra-bar granularity that has the basic setup, but no actions written for the added series.


    The AdvanceDecline and Spread indicators add multiple series with different instruments which you might find helpful, but these are complex.
    NYSE Advancing Issues/Declining Issues Uses historical tick data to calculate the ^ADV index minus the ^DECL index to give a simulation of the ^ADD index. Requires a data feed that supports indexes and provides data for the ^ADV (NYSE Advancing Issues) and ^DECL (NYSE Declining Issues). Update June 16th, 2020 &#8211; Corrected stroke attributes to [&#8230;]

    Plots the Spread of two instruments The first instrument is the primary chart data series. The secondary instrument is specified by the parameter "Symbol2". The Parameters "Qty1" and "Qty2" are multipliers which are applied to each instrument&apos;s price before adding the two; default values are "Qty1=1" and "Qty2=&dash;1" which will simply plot the difference between [&#8230;]

    Leave a comment:


  • kazbek966
    replied
    Thanks Chelsea, now a bit more clear. Are there any available examples of strategy working with multiple series so I did not reinvent bicycle.
    The theory is clear , but would be nice to have some examples to emulate.
    Cheers

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    Hello kazbek966,

    With the code at the top of OnBarUpdate:
    Code:
    if (BarsInProgress != 0)
    return;
    This return would stop evaluating any code unless the series is the primary series (BarsInProgress 0). Meaning no actions could happen on any other added series.

    BarsInProgress 0 is the primary chart series. BarsInProgress 1 would be the first series added with AddDataSeries(), BarsInProgress 2 would be the second series added with AddDataSeries(), etc..

    CurrentBar is the number of the bar (starting with bar 0 at the left of the chart) that is currently being updated in OnBarUpdate(). CurrentBars (plural) is a collection of CurrentBar values for each series added with AddDataSeries. The index is the BarsInProgress or BarsArray index.

    CurrentBars[0] would be the number of the bar being processed for the primary chart series. CurrentBars[1] would be the number of the bar being processed for the first added series with AddDataSeries().

    https://ninjatrader.com/support/help...urrentbars.htm
    Last edited by NinjaTrader_ChelseaB; 03-08-2022, 09:34 AM.

    Leave a comment:


  • kazbek966
    replied
    Good to know, Cheslea, that I 've been knocking to the closed door.
    That being said I explored suggested links and have a couple of questions related to suggested topics of dataseries and BarsInProgress.
    First of all there is obvious difference in syntax between script and Strategy Builder.
    While in script barsinprogress are defined as

    if (BarsInProgress == 0)

    In Strategy they are defined as

    if (BarsInProgress != 0)
    return;
    if (CurrentBars[0] < 1)
    return;

    In any case I presume these 4 lines specify dealing with main data series, and my task is to make sure that strategy understand that it needs to handle time based exit using 1 additional 1Min series. So I tried to improvise a bit and put this code together.

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

    if (CurrentBars[0] < 1)
    return;

    Print(string.Format("{0} | BIP: {1}, {2} {3} | (Position.MarketPosition: {4} == MarketPosition.Long) && ( (IsFalling(AuLLMA1.LLMA): {5} == true) || (Times[1][0].TimeOfDay: {6} == new TimeSpan(15, 0, 0): {7}) ) )", BarsInProgress, BarsArray[BarsInProgress].BarsPeriod.Value, BarsArray[BarsInProgress].BarsPeriod.BarsPeriodType, Position.MarketPosition, IsFalling(AuLLMA1.LLMA), Times[1][0].TimeOfDay, new TimeSpan(15, 0, 0) ));

    // Set 1
    if ((Position.MarketPosition == MarketPosition.Flat)
    && (IsRising(AuLLMA1.LLMA) == true)
    // TIME
    && ((Times[0][0].TimeOfDay >= new TimeSpan(17, 0, 0))
    || (Times[0][0].TimeOfDay < new TimeSpan(15, 0, 0))))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }

    // Set 2
    if ((Position.MarketPosition == MarketPosition.Long)
    && (IsFalling(AuLLMA1.LLMA) == true))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    }

    else if (BarsInProgress != 1)
    return;

    if (CurrentBars[1] < 1)
    return;

    // Set 3
    if ((Position.MarketPosition == MarketPosition.Long)
    && (Times[1][0].TimeOfDay == new TimeSpan(15, 0, 0)))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    }

    }

    The idea was to create separate condition designed to handle Time based exit on 1 MIN series while specifying that it is 1MIn series strategy must be looking for.
    It did not work as planned and thats where your input will be helpfull. By the way as seen in the script I put PRINT in its correct location and got exactly the same output as before.

    Finally while I do understand the meaning of "if (BarsInProgress != 0) " - work with main series , I am a bit unsure about meaning of "if (CurrentBars[0] < 1)".

    Cheers




    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    Hello kazbek966,

    That would be correct. The Strategy Builder is not able to trigger actions when a secondary series is processing in OnBarUpdate.

    The script must be unlocked and coded by hand.

    The BarsInProgress check is preventing the script from processing when BarsInProgress is 1 (the minute series) and this would need to be removed or modified.

    if (BarsInProgress != 0)
    return;

    A print would be placed one line above the condition set. This would print the time of the bar, BarsInProgress, as well as all values used in the condition set.

    Leave a comment:


  • kazbek966
    replied
    Hi Chelsea,

    Currently, I placed additional 1 MIN series according to Strategy Builder options given to me without delving into OnbarUpdate and BarsinProgress.

    }
    else if (State == State.Configure)
    {
    AddDataSeries("MNQ 03-22", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
    }
    else if (State == State.DataLoaded)
    {
    DATETIME = new Series<DateTime>(this);
    AuLLMA1 = AuLLMA(Median, 1, 1);
    }
    }

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

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if ((Position.MarketPosition == MarketPosition.Flat)
    && (IsRising(AuLLMA1.LLMA) == true)
    // Time
    && ((Times[0][0].TimeOfDay <= new TimeSpan(14, 55, 0))
    && (Times[0][0].TimeOfDay >= new TimeSpan(13, 30, 0))))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }

    // Set 2
    if ((Position.MarketPosition == MarketPosition.Long)
    // EXIT
    && ((IsFalling(AuLLMA1.LLMA) == true)
    || (Times[1][0].TimeOfDay == new TimeSpan(15, 0, 0))))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    }



    Am I correct to assume that current placing of 1 MIN Series is the culprit of observed behaviour ?
    By the way I put suggested PRINT in the { } after above mentioned portion of the script that resulted in the output I submitted as TXT file.
    Was the placement of the PRINT correct ?

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    Hello kazbek966,

    What doesn't work?

    The added 1 minute series is not updating OnBarUpdate with BarsInProgress 1?
    Have you added prints to determine this?

    The steps that would need to be taken to trigger an exit at exactly 15:00 would be a time based bar like 1 minute being added to the script, and logic to submit the exit order processed when OnBarUpdate is processing that BarsInProgress. A 1 minute bar would close every minute and would have a bar close at 15:00 (if the trading hours are still in session).





    If you want to know why the strategy is behaving as it is, it would be necessary to use prints to debug as previously suggested.
    You can print the time of the bar and BarsInProgress to confirm the 1 minute series is updating OnBarUpdate. You can print all values used in the condition to verify the condition evaluated as true.
    I am happy to assist with analyzing the output from prints if you provide these.

    Leave a comment:


  • kazbek966
    replied
    Hi Chelsea, indeed 8 min chart falls exactly on 15:00. When conducting same troubleshooting on 7 and 13 min series , exit on time was ignored as well and Exit on Session close was executed.
    So lets go back to square 1 and revisit basics. To the best of my understanding in the initial posting on current subject it was recommended to add 1 MIN additional series to primary series of our choice in order to exit on time within reasonable 1 MIN accuracy. As seen with RENKO , 7 and 13 min series this suggestion does not work. Mind you 15:00 exit time is being monitored on 1 MIN series so why this simple condition is being ignored is a bit of a mystery.
    So what steps need to be taken in order for EXIT order to recognize 15:00 time ?
    Cheers

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    Hello kazbek966,

    May I confirm you have added prints to print all value used in the conditions that submit the order?

    I am not seeing any output from Print() in the txt file you have provided.

    It's possible that on the 8 minute chart, a bar closed at 15:00, since it is a time based bar, and that on the renko chart no bar closed at exactly 15:00 since this is not a time based bar.

    Leave a comment:


  • kazbek966
    replied
    Hi Chelsea,
    I actually submitted prints to relevant times as an TXT file attachment . Did you receive them ? Just in case attaching them again.
    As far as I understood the Strategy builder function I instructed to Exit position starting at 15.00. That order was executed at 15:00:01 on 8 min chart

    1/24/2022 3:00:01 PM Strategy 'AuLLMAwTradingExiTimeLong/257591040': Entered internal SubmitOrderManaged() method at 1/24/2022 3:00:01 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''

    As for RENKO nothing was happening till our beloved 3:34 Exit on Session Close that also received order with 1 second delay. We can forgive it for this lack of punctuality.

    1/24/2022 1:59:01 PM Strategy 'AuLLMAwTradingExiTimeLong/257591041': Entered internal SubmitOrderManaged() method at 1/24/2022 1:59:01 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''

    1/24/2022 3:34:01 PM CancelAllOrders: BarsInProgress=0
    1/24/2022 3:34:01 PM CancelAllOrders: BarsInProgress=1
    1/24/2022 3:34:01 PM Strategy 'AuLLMAwTradingExiTimeLong/257591041': Entered internal SubmitOrderManaged() method at 1/24/2022 3:34:01 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Exit on session close' FromEntrySignal=''

    So again the question is why the Exit on Time order was received and properly executed on 8 MIn chart and completely missed on RENKO. The closure of bars in Both 8 MIN and RENKO charts is happening outside 15.00 marker and solely condition of exit is hitting 15:00 mark.
    Both of cases are using 1 MIN chart as a timer so it is inconceivable to me that timer is working on 1 MIN series in 8 MIN chart and suddenly decided not to function on RENKO. I believe some modification/specification in Exit order is needed. Please let me know whether you received an attachment.

    Cheers




    Attached Files
    Last edited by kazbek966; 03-07-2022, 09:21 AM.

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    Hello kazbek966,

    This likely indicates the condition set to submit the order is not evaluating as true.

    Note, that your conditions are looking for exactly 15:00. On a movement based chart, it would be unlikely for a bar to close at exactly 15:00 unless this is the end of the session.

    May we have the output from the prints so that we may further assist?

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by PaulMohn, Today, 03:49 AM
0 responses
2 views
0 likes
Last Post PaulMohn  
Started by inanazsocial, Today, 01:15 AM
1 response
6 views
0 likes
Last Post NinjaTrader_Jason  
Started by rocketman7, Today, 02:12 AM
0 responses
10 views
0 likes
Last Post rocketman7  
Started by dustydbayer, Today, 01:59 AM
0 responses
2 views
0 likes
Last Post dustydbayer  
Started by trilliantrader, 04-18-2024, 08:16 AM
5 responses
23 views
0 likes
Last Post trilliantrader  
Working...
X