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

How to pass Primary DataSeries to Custom Method

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

  • aventeren
    replied
    Originally posted by NinjaTrader_ChelseaB View Post
    Hi aventeren,

    If an order is submitted when a bar closes, the time of the order is going to be the start time of the new bar. This is why an order that is triggered on one bar shows on the next.

    The order in your output that was cancelled was expired. That means that the order was not created with the liveUntilCancelled bool as true. When the liveUntilCancelled bool is not used, by default it is false and this means that any working order such as a limit or stop will be cancelled on the next bar.

    To place an order with liveUntilCancelled as true try:
    EnterLongLimit(0, true, 1, Low[0]-2*TickSize, "myOrder");
    EnterLongLimit(int barsInProgressIndex, bool liveUntilCancelled, int quantity, double limitPrice, string signalName)
    http://www.ninjatrader.com/support/h...rlonglimit.htm


    If you would like to place an order intrabar, you can set the strategy to Calculate on bar close as false which will cause OnBarUpdate to run every tick.
    http://www.ninjatrader.com/support/h...onbarclose.htm

    Use if (FirstTickOfBar) { /* execute code */ } to detect when a bar has closed.
    http://www.ninjatrader.com/support/h...ttickofbar.htm

    You can also use intrabar granularity and process during BarsInProgress 1 but use the primary data series for calculations (BarsArray[0]).
    Thanks for the help on why the orders were being cancelled. I'll do some research on what you've provided.

    So to have a backtest get us closer to an intrabar backtest, the only way to do this is by placing the orders with the secondary series?

    If the answer is yes, a secondary series must be used to get closer to an intrabar backtest, can you also please help me understand the following?

    As a follow up, then, the chart that I have shown is on a ES Daily chart, but let's assume that it was on 60 min chart with a bar time of 12:05pm, which would be the bar's closing time, which would further mean that the bar would have opened at 12:00:01pm. My question if I load the 1 minute series in: Given that the primary bar (in this case) were to exist in between the times noted, what would what be the first secondary bar referenced? Would it be the 12:01pm bar, which would mean that the bar opened at 12:00:01pm and will close at 12:00pm? And as another follow up, would I just continue calling the entry command using BarsInProgress == 1 until the order is either cancelled (like it was in the above example) or executed by the secondary series?
    Thanks!

    Aventeren

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    Hi aventeren,

    If an order is submitted when a bar closes, the time of the order is going to be the start time of the new bar. This is why an order that is triggered on one bar shows on the next.

    The order in your output that was cancelled was expired. That means that the order was not created with the liveUntilCancelled bool as true. When the liveUntilCancelled bool is not used, by default it is false and this means that any working order such as a limit or stop will be cancelled on the next bar.

    To place an order with liveUntilCancelled as true try:
    EnterLongLimit(0, true, 1, Low[0]-2*TickSize, "myOrder");
    EnterLongLimit(int barsInProgressIndex, bool liveUntilCancelled, int quantity, double limitPrice, string signalName)
    http://www.ninjatrader.com/support/h...rlonglimit.htm


    If you would like to place an order intrabar, you can set the strategy to Calculate on bar close as false which will cause OnBarUpdate to run every tick.
    http://www.ninjatrader.com/support/h...onbarclose.htm

    Use if (FirstTickOfBar) { /* execute code */ } to detect when a bar has closed.
    http://www.ninjatrader.com/support/h...ttickofbar.htm

    You can also use intrabar granularity and process during BarsInProgress 1 but use the primary data series for calculations (BarsArray[0]).

    Leave a comment:


  • aventeren
    replied
    Still having secondary DataSeries to ATR issue...

    Chelsea--

    I figured that I would hold this question until I got things working, but now that I'm moving in the right direction I figured that I needed to come back to it.

    If you go to the GetATR() Method and change the code to this:

    Code:
    #region GetATR()
    		private double GetATR()
    		{
    			double	methodATR;
    			methodATR	=	ATR(triggerDataSeries, PeriodATR)[0];
    			return methodATR;
    		}
    		#endregion GetATR()
    and then recompile the code and reload it in the chart, you'll see that the ATR value drops to sub 11 (again, I'm looking at a daily ES 03-14 chart loaded with 180 days of data...I think previously I may have said it was a 60 min chart...it's a daily chart--my bad). Clearly this is off.

    If you track through the triggerDataSeries Set points, the are:

    1. If CurrentBar less than the periods, it's Set to Close[0]

    2. Within FindTriggerPrice() Method, where it's Set to various values until the trigger occurs, at which point FindTriggerMethod() Returns--which should leave the triggerDataSeries[0] equal to the trigger price.

    3. At the end of OnBarUpdate.

    I then installed a quick for loop within the GetATR() Method to track what the recorded Close and triggerDataSeries values were, and the for loop shows that all of the historical bars always match, while the current triggerDataSeries[0] generated after the FindTriggerPrice() Method is set to the trigger price instead of the Close price of the bar, so that is even right--because in using the ATR(triggerDataSeries, PeriodATR) I was trying to calculate the ATR as it would have been when the trigger occurred. Yet within the same for loop I also calculated the ATR using the primary DataSeries (by ATR(PeriodATR) and the triggerDataSeries (by ATR(triggerDataSeries, PeriodATR), and the results are wildly different.

    So this is weird because the values are correct, but I just can't see how the ATRs would be that different, when 13 of the 14 period ATR are equal while the final (trigger) bar's difference between Close and the trigger price during this period were only 4 to 15 points different. So something weird is going on with NT's calculation of the ATR using a secondary data series--and I can't figure out what it is.

    To test and recreate this problem, I've attached V003 of the strategy. Make sure you open the Output Window because I printed the Close and tiggerDataSeries historical and ATR values from the GetATR() Method for loop so you can see the differences.

    Hopefully we can figure this out.

    I'm standing by with your questions and comments.

    Thanks,

    Aventeren
    Attached Files

    Leave a comment:


  • aventeren
    replied
    Originally posted by NinjaTrader_ChelseaB View Post
    Hi aventeren,

    Take a look at the prints in your output window and take a look at the go short code.

    The price from FindTriggerPrice() and set to priceMethod is 1646.75.
    The price that comes back from GetStop() is 1662.75.

    You then add these together when setting the stop.
    SetStopLoss("short limit", CalculationMode.Price, priceMethod + priceStop, false);
    1646.75 + 1662.75 = 3309.5

    and thats exactly what happens in your output file:

    10/9/2013 9:00:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='short limit' Mode=Price Value=3309.5 Currency=0 Simulated=False

    For the profit target you are subtracting priceMethod - pricePT.
    1649.75 - 1614.75 = 32


    In other words, the stop loss and profit target are both being set exactly where you have told them. Its just really really far away from the current price.
    Well I'll be diggity damned if that didn't do it. Wow. I knew it had to be something easy. Thanks for the second set of eyes!

    Okay so now what do we have here? We have orders being executed at the price that actually caused the cross, but we the order is not executed until after:

    1. After the close of the trigger (signal) bar, and
    2. If the bar(s) following the trigger (signal) trade through the limit price.

    We can see the 1 bar delay by the fact that the diamond is printed on top of the trigger bar but the order arrow is not placed until the next bar (assuming that the next bar traded through the entry price, of course).

    I found an example of a trade working correctly and an example of a trade that found the trigger price but then expired because price did not trade through the entry price again. Here is the chart showing these two cases.

    Click image for larger version

Name:	Unfilled Order Chart, 2.28.14, 1206pm.jpg
Views:	1
Size:	62.8 KB
ID:	869950

    And here is a screenshot of the Output Window showing that the trigger price was found and everything was set to go long, but then the order was cancelled.

    Click image for larger version

Name:	Output Window Showing Expired Order, 2.28.14, 1207pm.png
Views:	1
Size:	242.2 KB
ID:	869951

    So to have the order placed intrabar, we must use a secondary series?

    If we must use a secondary series, let's assume that I load in a 1 minute series. I would then need to place all of what I have done thus far within BarsInProgress == 0, however I would need to place the order within BarsInProgress == 1. Correct?

    As a follow up, then, the chart that I have shown is on a ES Daily chart, but let's assume that it was on 60 min chart with a bar time of 12:05pm, which would be the bar's closing time, which would further mean that the bar would have opened at 12:00:01pm. My question if I load the 1 minute series in: Given that the primary bar (in this case) were to exist in between the times noted, what would what be the first secondary bar referenced? Would it be the 12:01pm bar, which would mean that the bar opened at 12:00:01pm and will close at 12:00pm? And as another follow up, would I just continue calling the entry command using BarsInProgress == 1 until the order is either cancelled (like it was in the above example) or executed by the secondary series?

    Is there any way to simulate a primary intrabar entry?

    Finally, do the stop and profit targets also behave in this same manner? Is there first a "stop" signal bar and "profit target" signal bar that must first close and then have price trade through them to close the trade or are they assumed to have executed within the stop/profit target signal bar?

    Thanks for your continued help, Chelsea. I really appreciate it.

    All best,

    Aventeren

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    Hi aventeren,

    Take a look at the prints in your output window and take a look at the go short code.

    The price from FindTriggerPrice() and set to priceMethod is 1646.75.
    The price that comes back from GetStop() is 1662.75.

    You then add these together when setting the stop.
    SetStopLoss("short limit", CalculationMode.Price, priceMethod + priceStop, false);
    1646.75 + 1662.75 = 3309.5

    and thats exactly what happens in your output file:

    10/9/2013 9:00:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='short limit' Mode=Price Value=3309.5 Currency=0 Simulated=False

    For the profit target you are subtracting priceMethod - pricePT.
    1649.75 - 1614.75 = 32


    In other words, the stop loss and profit target are both being set exactly where you have told them. Its just really really far away from the current price.

    Leave a comment:


  • aventeren
    replied
    Thanks for responding, Chelsea. Here are my responses to your questions.

    Originally posted by NinjaTrader_ChelseaB View Post
    Are there any errors in the Log tab of the Control Center.
    No

    You also have TraceOrders set to true. Do you have the output window open?

    When the SetStopLoss gets called do you see the print in the output window about it?
    Here is an Output window screenshot.

    Click image for larger version

Name:	Output Window, 2.28.14, 906am.png
Views:	1
Size:	163.5 KB
ID:	869943

    I modified the code with a bunch of Print statements to navigate through the various Methods. I hope they provide some insight.

    Also, here is a screen grab of the chart showing that the "short limit" was placed "1 @ 1646.75". The chart also shows the Position.MarketPosition states (i.e. the F (Flat), L (Long) and S (Short) letters). Plus, you can see the DarkRed diamond that a drew on the chart from within OnBarUpdate.

    Click image for larger version

Name:	Chart Image Showing First Trade Entry, 2.28.14, 9_09am.jpg
Views:	1
Size:	61.0 KB
ID:	869944

    Some observations from the Output Window:

    1. The stop and profit target "Price Value=" values do not match the passed and verified values. Why?

    2. The entry's "PriceValue=1646.75" appears to be right, but the Position.AvgPrice = 0.00. Why?

    If you open chart trader to the same instrument you can see working orders. After entering a position do you see the profit target appear? If you increase the scaling do you see the stop loss?
    When I open a new ES 03-14 daily chart with Chart Trader open, I do not see the orders. I tried to see the orders on both of my Sim accounts plus my live account, and nothing. Any ideas?

    I have attached the updated strategy with the new print statements.

    This has to be something really basic because it seems like my logic is right. Clearly I must be missing something really fundamental about order placement and execution. In any event, I look forward to your comments, questions and ideas.

    All best,

    Aventeren

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    Hi Aventeren,

    Are there any errors in the Log tab of the Control Center.

    You also have TraceOrders set to true. Do you have the output window open?

    When the SetStopLoss gets called do you see the print in the output window about it?

    If you open chart trader to the same instrument you can see working orders. After entering a position do you see the profit target appear? If you increase the scaling do you see the stop loss?

    Leave a comment:


  • aventeren
    replied
    Can't get Stop Loss to execute; Strategy Attached; Any thoughts?

    So I took the SampleMA example strategy and modified it as the attached strategy.

    I first test to see if the cross happened on the primary DataSeries (I'm using a 60 min ES 03-14 chart) by calling a custom EMACross Method, and then if the cross happened within that bar I call a custom Method called FindTriggerPrice that hunts down the actual price that caused the cross to happen--at which point FindTriggerPrice passes the trigger price back into OnBarUpdate.

    After a trigger price is found, I then pass that trigger price (called "priceOrder") to one of two custom Methods called GoLong and GoShort that set a stop, set a profit target and then enter with a limit order at the trigger price.

    The stops are set by calling a custom GetStop Method, and the stop is a multiple of ATR, which is also found by calling a custom GetATR Method.

    The profit targets are also set by a custom GetPT Method, and they are also multiples of ATR, which is also found by calling the custom GetATR Method.

    I've put in Print statements and DrawTexts at a variety of places to confirm the values being passed to the various Methods and immediately prior to and after key commands. I can get the first order to execute, but for some reason the stop is not being executed, so the first position stays open the entire time. So I'm stuck and need some help.

    The exact chart I have up is a 60 min ES 03-14 chart on a 24/7 session template that starts on 2.27.14 and extents back 180 days (ie, DataSeries UI settings).

    Thanks for any help provided. I'm just confused why the stop is not executing correctly.

    Finally, from a housekeeping perspective, I've known shifted gears from Indicators to Strategy Development, and as such if you'd like to move this thread to Strategy Development that is your call.

    Thanks,

    Aventeren
    Attached Files

    Leave a comment:


  • aventeren
    replied
    Originally posted by koganam View Post
    What error do you get when you try to pass a DataSeries to the ATR? I do not get any error: that is eminently passable. Maybe if you wrote the instruction that you are using?

    Did I misunderstand? Are you trying to pass ATR to a method, or a DataSeries to ATR?
    I had been trying to pass a DataSeries to ATR. As it turns out I was able to eventually pass a DataSeries to ATR. It must have been something else in my code.

    Thanks for checking in, koganam. I really appreciate it.

    Leave a comment:


  • koganam
    replied
    Originally posted by aventeren View Post
    So I ran into the same problem with the ATR indicator. As it turns out, the ATR will only take an IDataSeries parameter input and not a custom DataSeries parameter. Therefore, it doesn't look like we can pass a custom DataSeries (i.e., like myDataSeries below) to ATR.

    Does anyone have any workarounds for how to pass a secondary DataSeries to a custom method or indicator that only takes an IDataSeries parameter input?

    Clearly I could just build a custom ATR that would take a DataSeries parameter (instead of an IDataSeries parameter), but man that's going to get difficult when I try and scale into other more complex indicators.

    Does anyone have any thoughts?
    What error do you get when you try to pass a DataSeries to the ATR? I do not get any error: that is eminently passable. Maybe if you wrote the instruction that you are using?

    Did I misunderstand? Are you trying to pass ATR to a method, or a DataSeries to ATR?

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    Hi aventeren,

    I've made a sample to show how to pass a custom data series to the ATR.

    Follow these steps to import the NinjaScript:
    1. Download the script to your desktop, keep it in the compressed .zip file.
    2. From the Control Center window select the menu File > Utilities > Import NinjaScript
    3. Select the downloaded .zip file
    4. NinjaTrader will then confirm if the import has been successful.


    Critical *Note that on any files that say "File already exists on your PC" that start with an "@" symbol are the ones that came preloaded inside of NinjaTrader so you would say "No" so that you do not override those files.
    Attached Files

    Leave a comment:


  • aventeren
    replied
    Same Problem with ATR

    So I ran into the same problem with the ATR indicator. As it turns out, the ATR will only take an IDataSeries parameter input and not a custom DataSeries parameter. Therefore, it doesn't look like we can pass a custom DataSeries (i.e., like myDataSeries below) to ATR.

    Does anyone have any workarounds for how to pass a secondary DataSeries to a custom method or indicator that only takes an IDataSeries parameter input?

    Clearly I could just build a custom ATR that would take a DataSeries parameter (instead of an IDataSeries parameter), but man that's going to get difficult when I try and scale into other more complex indicators.

    Does anyone have any thoughts?
    Last edited by aventeren; 02-27-2014, 09:53 AM.

    Leave a comment:


  • koganam
    replied
    Originally posted by aventeren View Post
    When I attempt to not pass a DataSeries to myMethod by doing this:

    Code:
    myMethod();
    I'm getting a No Overload for myMethod takes 0 arguments error.

    I then go back to trying to pass myMethod the Close DataSeries by doing this:

    Code:
    myMethod(Close);
    But then I get an error that says that I cannot convert from the 'NinjaTrader.Data.IDataSeries' to 'NinjaTrader.Data.DataSeries'. So it looks like Close is an IDataSeries type and the secondary series is defined as a DataSeries type. Is it possible to pass the primary DataSeries Close values as a NinjaTrader.Data.DataSeries type instead of as a .IDataSeries type?
    A few ways to resolve your issue come to mind, but the simplest way is simply to duplicate your method, but in the second copy, pass an IDataSeries instead of a DataSeries. The code will know which copy of the method to call, depending on what parameter is passed to it.

    Leave a comment:


  • aventeren
    replied
    I ended up changing the myMethod parameter to a string called dataseries like this:

    Code:
    myMethod(string dataseries)
    Then when I needed to pass the secondary data series to myMethod is just used:

    Code:
    myMethod("secondary");
    or the following for the primary dataseries:

    Code:
    myMethod("primary");
    Within myMethod, I then ran a bit of logic to determine whether "primary" or "secondary" was passed to "dataseries"--and then based on this I used either Close or myDataSeries as the DataSeries input in the indicator.

    So I think it's working, but now I need to start tracing values down to make sure everything is happening correctly.

    Thanks for your help, ChelseaB. You're always right on top of our questions, and I for one appreciate it.

    All best,

    Aventeren
    Last edited by aventeren; 02-27-2014, 08:40 AM.

    Leave a comment:


  • aventeren
    replied
    Originally posted by NinjaTrader_ChelseaB View Post
    Hello aventeren,

    The message you got is likely because you have your method set to accept a parameter.

    Try removing this.
    So would I have to create a second custom Method to process the secondary DataSeries?

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by GLFX005, Today, 03:23 AM
0 responses
0 views
0 likes
Last Post GLFX005
by GLFX005
 
Started by XXtrader, Yesterday, 11:30 PM
2 responses
11 views
0 likes
Last Post XXtrader  
Started by Waxavi, Today, 02:10 AM
0 responses
6 views
0 likes
Last Post Waxavi
by Waxavi
 
Started by TradeForge, Today, 02:09 AM
0 responses
11 views
0 likes
Last Post TradeForge  
Started by Waxavi, Today, 02:00 AM
0 responses
2 views
0 likes
Last Post Waxavi
by Waxavi
 
Working...
X