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_PatrickH
    replied
    Hello newuser,

    Thank you for your response.

    For the second item, set the EntriesPerDirection and EntryHandling as needed to allow for the number of signals you wish to see. You can find details on EntryHandling at the following link: http://ninjatrader.com/support/helpG...ryhandling.htm

    For the first item, I would like to recreate this on my end.
    Can you send me the strategy and a screenshot of the settings used in the Strategy Analyzer?

    Please send the strategy and screenshot in an email to platformsupport[at]ninjatrader[dot]com with 'ATTN: Patrick H - 1564110' in the subject line.

    You can attach your strategy to your email by going to File > Utilities > Export NinjaScript > Export selected source files > select your strategy > select the right arrow > Export. The file will be located under (My) Documents\NinjaTrader 7\bin\Custom\ExportNinjaScript.

    Leave a comment:


  • newuser
    replied
    I've been looking through the results of the backtest and I've noticed two issues (for reference I have run the test on the AUDUSD forex pair, using a 60 minute chart).

    The first issue is that orders are being triggered even though the market isn't reaching the price level as set by the EnterLongLimit(). I noticed for example that my code generates a signal at 10am on 2 Sep 2016 which triggers. When I look at the executions tab in the strategy analyser window it says the entry price is 0.0000, but on the trades tab it is listed as 0.7559. I have confirmed that it is a valid entry and the entry price should be 0.7559, but the market never reaches that price point so the trade should never trigger. I'm at a loss to explain why that has occurred.

    The second issue (which might be related to the first but I'm not sure) is that trades are also being ignored due to the signal limit being exceeded. I know this because I set traceorders=true; in my code and I looked through the output (sample pasted below). I haven't specified any sort of limit on the maximum number of entries so presumably there is a default value that I am butting up against?

    18/08/2016 6:00:00 PM Ignored PlaceOrder() method at 18/08/2016 6:00:00 PM: Action=Buy OrderType=Limit Quantity=1 LimitPrice=0.0000 StopPrice=0 SignalName='Buy' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'

    Leave a comment:


  • NinjaTrader_ZacharyG
    replied
    Hello newuser,

    The logic you have provided will submit a long limit order. The stop loss and profit target values will be modified as long as BarsSinceEntry() == 0 and they'll be submitted to the market whenever your order is filled.

    As a sidenote, I would like to note that a limit order submitted above the current market price will be fill immediately as it would be a better price than you have specified.

    Leave a comment:


  • newuser
    replied
    I realized after reading your reply that I had made an error in the SetProfitTarget logic, thank you. What I had previously was just calculating the tick value for twice the initial risk, but I forgot to add the bit of code for "entry price plus."

    I amended the code to reflect your previous post and I only had one trade returned in the backtest. I surmised that including the EnterLongLimit() code after the if statement was blocking any further entries from being placed, so I put the entry code above it (as follows) and it looks like it has worked =)

    Can you see anything wrong with that or does it make sense?

    {
    EnterLongLimit(DefaultQuantity, High[0] + 2 * TickSize,"");
    if (BarsSinceEntry()==0)
    {
    //stop loss and profit target logic
    }
    }

    Leave a comment:


  • NinjaTrader_ZacharyG
    replied
    Hello newuser,

    I see that you have placed a semicolon ( ; ) after if (BarsSinceEntry() == 0). The logic below the if won't be affected by if the condition is true or not; it will run regardless.

    You'll want to do this:

    Code:
    if (BarsSinceEntry() == 0)
    {
         // logic
    }
    What steps have you done to debug your code?

    I would suggest checking to see if the values you are using for your stop loss and profit target are where you are expecting to be placed at. You can do this by using prints.

    Testing on my end, your profit targets are being placed below the entry. Ensure that the values you're trying to calculate are actually what you're expecting.

    The stop loss and profit target are not going to be submitted until the entry fills. It has nothing to do with if BarsSinceEntry() == 0 or not.

    Leave a comment:


  • newuser
    replied
    Am I correct in thinking that even with a value of 0 BarsSinceEntry will not apply the stop loss until after the first candle of the open trade has actually closed?

    If that is the case then that won't work for me, as there's always a chance that the market could trigger my position and then move so far against my position that by the time the stop is placed the market has already moved past it.

    Leave a comment:


  • newuser
    replied
    Sorry what I meant was that if CurrentBar is based off the first (left most) bar on the chart being 0 then I wasn't sure how I would go about coding that to append to a signal name.

    So given that I always want to set the initial stop for a buy order at the low of the setup minus 2 ticks I tried adding a line: if BarsSinceEntry equal to 0 (my logic being that it should only set the stop loss at that point when the trade first triggers).

    I notice in the backtest that all trades seem to end in 1 bar so something didn't quite work, but I did also notice that the time filter was now working properly. Can you take a look at the order management code below please and let me know what I have done wrong.

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

    }

    Leave a comment:


  • NinjaTrader_ZacharyG
    replied
    Hello newuser,

    I'm not quite sure by what you mean by "CurrentBar might cause me problems if it based off the position of the chart?"

    All CurrentBar is going to return is the index value of the bar your strategy is currently running on. This should not cause any problems unless you're having multiple entries occur on the same bar (as CurrentBar wouldn't change between the two entries).

    If you're using the BarsSinceEntry() overload that asks for an entriesAgo parameter, you'll use 0 for the last entry, 1 for the previous, so on and so forth.

    Leave a comment:


  • newuser
    replied
    Thanks, that's a good point about Performance.Longtrades.Count that I hadn't thought about.

    CurrentBar might cause me problems if it based off the position of the chart?

    What about if I used BarsSinceEntry() instead? If I've understood the help guide correctly I can use it to specify a condition for both how many bars a given trade has been open and also whether I want the most recent trade (entriesAgo = 0), the second trade (entriesAgo =1) or any other number in the sequence?

    Leave a comment:


  • NinjaTrader_ZacharyG
    replied
    Hello newuser,

    I would say that if you do not know how many entries your strategy is going to have, then, yes, the best approach would be to use a counter to to append to your signal name.

    I would like to note that a trade is considered as an entry and exit. Performance.LongTrades.Count will not increment until a long trade (entry and exit) has occurred. This probably would not be the value you want to use.

    You could use CurrentBar. CurrentBar returns the bar index of the bar the script is currently processing. The first bar on your chart (the left most bar) will be 0, the next would be 1, so on and so forth.

    Leave a comment:


  • newuser
    replied
    I was thinking about what you said re specifying a signal name for my entry and then using a different signal name for any additional entry/ies. The solution I envisage is some sort of iterative count function as it is possible that I might even find myself with more than 2 trades open at once. Given that I have no way to predict how many open trades I might have open simultaneously I figure this is the best way?

    I've been doing some searching around the forum and the help guide and stumbled across Performance.LongTrades.Count Presumably I could use this count as a way of differentiating trades when setting the stop loss at the order stage?

    Leave a comment:


  • NinjaTrader_ZacharyG
    replied
    Hello newuser,

    You will want to specify a signal name for your entry and not pass an empty string.

    For your additional entry, I would suggest the use of a different signal name than the first order. As long as your stop loss for that second order is not prevented from being modified on the next call of OnBarUpdate(), the stop loss will continue to be modified to your desired price.

    I would highly suggest taking a look at this reference sample on our support forum detailing how to modify stop loss and profit target orders. The example provides a sample of how to modify the stop loss to break even as well: http://ninjatrader.com/support/forum...ead.php?t=3222

    Leave a comment:


  • newuser
    replied
    Hi Zachary_G,

    So given that I'm using the following code:

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

    for entry am I correct in thinking I need to insert a string in the "" at the end of the code above so I can then call that signal name for the stop loss?

    I also have another question around your statement You are able to have multiple SetStopLoss() calls in your strategy with different fromEntrySignal parameters.

    I am using a strategy that rides trends, and what that means is when I get a signal I will often get several consecutive signals (which may or may not trigger depending on if new highs are set in a bullish run). So it is entirely possible that I will have 2 trades open at once, based on the same condition set or in the very least I will have 1 trade open and want to set a stop for another order. How would I go about handling that, noting that (for bullish setups) I will always want to calculate the stop loss at Low[0] - 2 * TickSize?

    The added complication is that I will want to add a breakeven strategy, but if it's simpler to sort out the multiple stop handling first based on the scenario I have described above then that's fine. For the record what I would like to do is set the stop to breakeven if after the first candle closes the current price is > entry price (to clarify when I say the first candle I do mean the very candle that the order is triggered on).

    Many, many thanks once again for all the help and advice.

    Leave a comment:


  • NinjaTrader_ZacharyG
    replied
    Hello newuser,

    Without specifying a signal name in the SetStopLoss(), it's going to apply to all entries.

    You'll notice that SetStopLoss() has an overload that takes in a string parameter, fromEntrySignal:

    Code:
    SetStopLoss(string fromEntrySignal, CalculationMode mode, double value, bool simulated)
    You'll want to ensure that you're passing the signal name of the specific entry to the SetStopLoss() method call. You are able to have multiple SetStopLoss() calls in your strategy with different fromEntrySignal parameters.

    I would suggest the use of booleans to prevent the strategy from calling your stop losses multiple times. For example, once the stop loss is set for your first entry, set a boolean to true/false to prevent the stop loss from being modified the next iteration of OnBarUpdate().

    Example:
    Code:
    if (stopLossOneSet == false)
    {
         SetStopLoss(....);
         stopLossOneSet = true;
    }
    On the next call of OnBarUpdate(), SetStopLoss() won't be called as the boolean is true.

    Leave a comment:


  • newuser
    replied
    Hi ZacharyG,

    Ah that's filled in a piece of the puzzle. To answer your question no, I have not prevented the strategy from updating the stop loss on every call of OnBarUpdate() so that's definitely an issue.

    In the example code you provide: what would happen if I had more than one trade open at a time? As in how would the code handle the stops on two simultaneous trades?

    Let's say I had one trade open and second one triggers. Presumably I would need to add another few lines of code to account for where Position.MarketPosition == MarketPosition.Long?

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by ezrollin, Today, 08:51 PM
1 response
24 views
0 likes
Last Post bltdavid  
Started by Rubix, Today, 09:31 PM
0 responses
15 views
0 likes
Last Post Rubix
by Rubix
 
Started by ark321, Today, 05:12 PM
1 response
22 views
0 likes
Last Post NinjaTrader_EricB  
Started by futurenow, Today, 09:08 PM
0 responses
4 views
0 likes
Last Post futurenow  
Started by fafafdenis, Today, 04:56 PM
1 response
5 views
0 likes
Last Post NinjaTrader_EricB  
Working...
X