• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Please help me understand some strategy codings

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

    Please help me understand some strategy codings

    Hi - I'm trying to code my strategy for the first time, so I have some questions in order to gain a good understanding how the strategy works. Would you please help me with the following questions?

    1. The strategy codes have 2 methods under it: OnStateChange() and OnBarUpdate(). From reading the help guides, it seems that everything in OnStateChange is run only once when strategy is started. I notice that indicator instances are coded under State.DataLoaded, where it's updated only once after data series are loaded. I don't understand how does the strategy run real-time calculations on the indicators, since they are not updated with bar updates?

    I need to understand the executing order of when OnStateChange() and OnBarUpdate(), for below reason:
    a) I need to move stop loss value with current price change. SetStopLoss() is under State.Dataloaded by default. I'm not sure if I need to move the code to be under OnBarUpdate(), since my strategy needs to make real-time decision to update stop loss value when price goes up & down. Not sure if SetStopLoss() is under State.Dataloaded will work.

    b) I need to calculate real-time moving average difference for different data series. For example, I have 2 instances: EMA1 = EMA(Close, 21); EMA2 = EMA(Close, 60); I need to create a new variable Diff: Diff = EMA1 - EMA2; This will be used in my conditions. I'm not sure where I should put it. In OnBarUpdate() or State.Dataloaded?

    2. Question about ATR indicator. My strategy runs on 1 minute data series. I added addtional 30 minute data series. When I call ATR on 30 minute, what should the input be? For example, I know when I call EMA, it's EMA(Closes[1], 60), since EMA calculates based on close price. But ATR is based on several price series for calculation, so what should the method input parameter be?

    I know this is a long question. I really appreciate it if you can elaborate in details, so that I can have a good idea of how everything works.

    Thank you very much!

    #2
    Hello HiddenPhilosopher,

    Below is a link to a forum post with helpful information about getting started with NinjaScript. I highly recommend watching the Strategy Builder 301 and NinjaScript Editor 401 training videos.
    https://ninjatrader.com/support/foru...040#post786040

    OnStateChange() is run every time the State changes. This will go from State.SetDefaults when opening the Strategy window to State.Configure when the strategy is added, to State.DataLoaded once the strategy is started and has loaded historical data, to State.Historical when processing historical data in OnBarUpdate(), to State.Real-time when processing real-time data.
    https://ninjatrader.com/support/help...tatechange.htm

    OnBarUpdate is run for every historical bar on the chart and every real-time bar that closes.
    https://ninjatrader.com/support/help...nbarupdate.htm
    https://ninjatrader.com/support/help...currentbar.htm

    Below is a link to an example that moves a stop loss.
    https://ninjatrader.com/support/foru...269#post802269

    And a link to an example created with the Strategy Builder that moves exit orders.
    https://ninjatrader.com/support/foru...596#post806596

    SetStopLoss() can be called in OnBarUpdate() in an unlocked strategy.

    Logic and calculations should be in OnBarUpdate() and run any time the bar is updating.

    The ATR can also be called with Closes[1] or BarsArray[1].
    https://ninjatrader.com/support/help..._range_atr.htm

    if (BarsArray.Count > 1 && CurrentBars[1] > 0)
    Print(ATR(Closes[1], 20)[0];

    https://ninjatrader.com/support/help...dataseries.htm
    https://ninjatrader.com/support/help...urrentbars.htm
    https://ninjatrader.com/support/help...inprogress.htm
    https://ninjatrader.com/support/help.../barsarray.htm
    https://ninjatrader.com/support/help...nt8/closes.htm
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by hedgeplay, Today, 08:04 PM
    0 responses
    4 views
    0 likes
    Last Post hedgeplay  
    Started by DTSSTS, Today, 07:02 PM
    0 responses
    6 views
    0 likes
    Last Post DTSSTS
    by DTSSTS
     
    Started by arguru, Today, 04:07 PM
    1 response
    11 views
    0 likes
    Last Post NinjaTrader_ShawnB  
    Started by lakersfan, Today, 06:02 PM
    0 responses
    13 views
    0 likes
    Last Post lakersfan  
    Started by hobah99, Today, 04:12 PM
    1 response
    5 views
    0 likes
    Last Post hobah99
    by hobah99
     
    Working...
    X