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

Using a time filter

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

  • NinjaTrader_ZacharyG
    replied
    Hello newuser,

    Are you ensuring that the stop loss is not being modified to being the current low - 2 ticks on every call of OnBarUpdate()?

    For example:
    Code:
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    	SetStopLoss(CalculationMode.Price, Low[0] - 2 * TickSize);
    	EnterLong();
    }
    In my example above, the stop loss is going to be set to the price of Low[0] - 2 * TickSize only when my strategy is still in a flat position. As soon as the strategy enters, the stop loss price is not going to be modified on the next call of OnBarUpdate() and will maintain its initial position.

    Leave a comment:


  • newuser
    replied
    Unfortunately I'm still having issues with trying to get the entry, stop and profit target to work properly. As per my previous post I am trying to calculate the stop based on the current bar, so I tried using the following code in the OnBarUpdate()

    }
    SetStopLoss(CalculationMode.Price, (Low[0]) - (2 * TickSize));
    EnterLongLimit(DefaultQuantity, High[0] + 2 * TickSize, "");
    {

    It compiles and it runs in the backtest but when I look at the results it's not performing as expected. I have the 8am to 11pm time filter for example and if I remove all the order management code and replace it with DrawDot I get exactly what I'm after, but the order stuff seems to be causing issues.

    I also tried creating a stop based on the current bar using the wizard but it didn't seem to work either. In that instance what I did was create a user variable which was equal to the Low[0] with a 2 tick offset, and then create a stop loss with CalculationMode of Ticks and set the value as the user variable.

    Any help would be much appreciated.

    Leave a comment:


  • NinjaTrader_ZacharyG
    replied
    Hello newuser,

    Thank you for the clarification.

    In the case that you have mentioned, it would be better to use CalculationMode.Price as you have outlined in your post.

    Please, let us know if we may be of further assistance.

    Leave a comment:


  • newuser
    replied
    Hi ZacharyG,

    Thanks for the quick reply and the explanatory link.

    I probably should've explained in my previous post that I want to set the stop based on the current bar. Basically for a long order I want to set the stop a few ticks below the low.

    Is there a way to do that using CalculationMode.Ticks or would I better off using this line of code below in the OnBarUpdate()

    SetStopLoss(CalculationMode.Price, Low[0] - 2 * TickSize);

    Incidentally further to my original post about trades triggering outside of the time filter I deleted the code I had for entry, stop and take profit and simply used DrawDot when all conditions were met and everything worked just fine (so at least know I where the problem is now).

    Leave a comment:


  • NinjaTrader_ZacharyG
    replied
    Hello newuser,

    You would be unable to access the TickSize property from Initialize(), as per the help guide: https://ninjatrader.com/support/help.../?ticksize.htm

    If you wish for your stop loss and profit target to be based on ticks, you can call SetStopLoss() and SetProfitTarget() with CalculationMode set to CalculationMode.Ticks.


    Leave a comment:


  • newuser
    replied
    Hi PatrickH,

    I had a look at the log file and I found the following error message

    ERROR: Failed to call method 'Initialize' for strategy: 'TickSize' property can't be accessed from within 'Initialize' method in OnUnhandledThreadException

    which I attribute to the code I used to set the stop and profit target in the Initialize() section. I am assuming this is if not the cause of the problem then at least something I need to fix? If so, where should I be putting the code for the stop and take profit? Below the EnterLongLimit line?

    Leave a comment:


  • NinjaTrader_PatrickH
    replied
    Hello newuser,

    Thank you for your patience.

    The TimeInForce is likely not the cause. Using Day would mean the orders would only be active during the Session selected for the Session Template. Usign GTC would mean the orders are active until cancelled.

    So likely it is that the orders are still active outside the times you wish and thus can fill outside those hours. You would likely wish to look into CancelOrder() and IOrder objects to cancel orders when the end time of your filter is hit.

    Please let me know if you have any questions.

    Leave a comment:


  • newuser
    replied
    Hi NinjaTrader_PatrickH,

    Thank you for the prompt reply.

    If the time filter code is correct then I must have a problem somewhere else because when I ran a backtest on the AUDUSD forex pair I got trades that were outside the specified time limits. I can think of two possible issues at this point:

    The first is that the backtest properties asks for an order 'time in force' and only offers two options - GTC and Day. I tried running the test twice using either option but I couldn't understand how either good till cancelled or day would be an appropriate choice?

    The second issue might be the entry order code. I have used

    EnterLongLimit(DefaultQuantity, High[0] + 2 * TickSize, "");

    and set the tick size for the AUDUSD pair to 0.0001 as I would like orders triggered two pips above the high.

    Do you think either of these could be the issue or is it something else?

    Apologies again if I'm asking silly questions but I'm a little baffled as to where I'm going wrong.

    Leave a comment:


  • NinjaTrader_PatrickH
    replied
    Hello newuser,

    Thank you for your post.

    You are correct, ToTime would use a 24 hour clock. This clock would use your time zone from your local PC clock time.

    Leave a comment:


  • newuser
    started a topic Using a time filter

    Using a time filter

    Apologies if I have posted this in the wrong area but I'm new to all this and finding my way around.

    On this post http://ninjatrader.com/support/forum...ead.php?t=3226 I found an explanation (and sample code) describing how to incorporate a time filter into the trading strategy.

    If I wanted to restrict trading between say 8am and 11pm, would the line of code below do the trick? (I have assumed a 24 hour clock is used but I could be wrong).

    (ToTime(Time[0]) >= 80000 && ToTime(Time[0] <= 230000)

    What time zone does the code actually check against? Does it automatically default to the local time of the machine?

    Any help/advice much appreciated.

Latest Posts

Collapse

Topics Statistics Last Post
Started by Aviram Y, Today, 05:29 AM
0 responses
1 view
0 likes
Last Post Aviram Y  
Started by quantismo, 04-17-2024, 05:13 PM
3 responses
25 views
0 likes
Last Post NinjaTrader_Gaby  
Started by ScottWalsh, 04-16-2024, 04:29 PM
7 responses
34 views
0 likes
Last Post NinjaTrader_Gaby  
Started by cls71, Today, 04:45 AM
0 responses
6 views
0 likes
Last Post cls71
by cls71
 
Started by mjairg, 07-20-2023, 11:57 PM
3 responses
216 views
1 like
Last Post PaulMohn  
Working...
X