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

Strategy Help - MACD Cross / Selective Orders / Order Reversals / EnterExit Positions

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

    Strategy Help - MACD Cross / Selective Orders / Order Reversals / EnterExit Positions

    Good day,

    I have a number of questions and ideas in regards to what I would like to implement into the Strategy testing I am doing and hoping someone can help me figure them out.

    1. MACD Crossovers:

    I am trying to utilize one of my strategies to enter long/short based on how the MACD line crosses over the average line, but there are instances where a trade might occur 3 bars in a row (each getting stopped out in the same bar they began in), because the MACD line is just barely breaching the average line, either barely breaking below to go short, or barely breaking above to go long. There have been instances where this has happened for 3 bars in a row, where the MACD line never truly crosses over the average line, yet I get stopped out of 3 different short positions because of it. Is there any way to avoid this without changing the way the strategy/MACD is calculated to "bar close"? I am trying to have everything be calculated by "each tick" to be as accurate and precise as possible.


    2. Selective Orders:

    Following up the situation from above, is there a way to selectively remove orders from happening if the MACD line is effectively moving in a flat manner? i.e If the slope or gain/fall of the MACD line is near 0%, can I use this parameter in some way to eliminate these trades from going through? Ideally, I would like only the trades that are clearly breaking through the average line to be taken, and none where the MACD line is essentially flatlining.


    3. Order Reversals:

    Is there a way to set up in Strategy Builder where if a stop loss is hit, a reverse position is immediately entered, but perhaps at a different profit/stop loss target? I.e, if I enter a long position with a profit goal of 8 ticks and a stop loss of 12 ticks, if I get stopped out, is there a way for a reverse order to be placed immediately with say a profit goal of 4 ticks and stop loss of 6 ticks for example?


    4. Enter+Exit Positions:

    I've also been compiling a different strategy that essentially looks at the MACD's momentum or current trajectory and acts upon it sooner without having to wait for any form of crossover. I've attempted this by asking the strategy to enter a long position if the MACD value 0 bars ago (i.e current bar) is greater than the MACD value 1 bar ago. On the other side, I have a LongExit condition set that links to my LongEnter position, and exits when the MACD value 0 bars ago is less than the MACD value 1 bar ago, i.e when the momentum of the MACD is swinging in the other direction.

    Conversely, I've attempted the same strategy with short positions, where I have the strategy execute a short if the MACD value 0 bars ago is less than the MACD value 1 bar ago and vice versa for the exit strategy. Oddly enough, no matter what I try, when I enable the strategy, only the long positions ever execute. Why is this or how can I fix it?

    Also, is there a better, or more accurate way of achieving what I am attempting here in regards to capturing the momentum swings? As you can see from the screenshot I attached, in the first Long Entry, where the Long position is initially exited should not have happened until 5 bars later seeing how the MACD line never decreased during that time span. Also, during the second Long Exit, based on my conditions stating that when the MACD (0) is less than MACD (1), this second long exit happens a full bar late based on how the MACD peaked on that last green bar and dropped immediately in the next bar.

    How can I better capture this movement within the strategy builder or am I using the "bars ago" function incorrectly? It just seems that often the orders are delayed by a full bar from where I believe they should enter/exit based on the conditions I have set. And of course, you can notice that after the second Long Exit, that no short position was entered for whatever reason.


    5. Target Order + Runner:

    If I wanted to combine my above enter+exit strategy with some profit/stop losses, how could I submit an order for say 2 contracts, one with a target of 20 points for example and a set stop loss of 10 points, with the second contract either having a much larger target of lets say 50 points and a stop of 10, or just setting the second contract with a trailing stop of 10-15 points? I've tried inputting another "do action" under my Enter Long set of conditions, but the problem I'm having, is when an entry triggers, and I am now in a position with 2 contracts, if one contract hits its target or stop loss, I would like there to be only the one remaining contract open until it is closed. What I am finding, is if one hits its target or stop, and the conditions are right, a 3rd contract might open while the initial "runner" is still active. Is there a way to limit this such that one my initial entry of two contracts begins, that no other entries may occur until they have both exited?

    ****

    I know there is a lot of information here and a lot of questions, but I really appreciate anyone who takes the time to help me out here. Spending my evenings now just tinkering with this Strategy Builder and running backtests and feel I can get a decently profitable strategy going if I can figure out these quirks.

    Thanks again
    Attached Files
    Last edited by The_Righteous_One; 01-27-2022, 10:24 PM. Reason: Question 5 details

    #2
    Hello The_Righteous_One,

    Thank you for your post.

    Originally posted by The_Righteous_One View Post
    Good day,

    I have a number of questions and ideas in regards to what I would like to implement into the Strategy testing I am doing and hoping someone can help me figure them out.

    1. MACD Crossovers:

    I am trying to utilize one of my strategies to enter long/short based on how the MACD line crosses over the average line, but there are instances where a trade might occur 3 bars in a row (each getting stopped out in the same bar they began in), because the MACD line is just barely breaching the average line, either barely breaking below to go short, or barely breaking above to go long. There have been instances where this has happened for 3 bars in a row, where the MACD line never truly crosses over the average line, yet I get stopped out of 3 different short positions because of it. Is there any way to avoid this without changing the way the strategy/MACD is calculated to "bar close"? I am trying to have everything be calculated by "each tick" to be as accurate and precise as possible.
    If you want to control the number of bars before the strategy enters again, you can use Bars Since Exit (under Misc in the Strategy Builder). This will return the number of bars that have elapsed since the last exit OR if no prior exits have been made it will return a -1. So you can add a condition group to your conditions for entry (use the Group button under the Conditions section) and use If Any in the upper right corner to link them with OR statements and say, for example if Bars since exit on the left is greater than say 3 on the right OR if Bars since Exit on the left is equal to -1 on the right and add that to your entry conditions, it would not allow a reentry within 3 bars of an exit. The problem there becomes situations in which you wish the position to actually be reversed based on the crossover because from the Strategy Builder there's not a way for you to monitor fills in OnOrderUpdate so you can't know if the prior exit occured because it was stopped out or reversed or what have you.

    2. Selective Orders:

    Following up the situation from above, is there a way to selectively remove orders from happening if the MACD line is effectively moving in a flat manner? i.e If the slope or gain/fall of the MACD line is near 0%, can I use this parameter in some way to eliminate these trades from going through? Ideally, I would like only the trades that are clearly breaking through the average line to be taken, and none where the MACD line is essentially flatlining.
    You could certainly take a look at the slope of the MACD line by using Slope under Misc, using the MACD's MACD line as its input and comparing that to an numeric value in your conditions for entry. If the slope is between, say, 0.10 and -0.10, that's going to indicate a pretty flat MACD line. You would want to look at the values you get for the slope and decide for yourself exactly what values to compare it to. I'm attaching an indicator below that can use the MACD as an input that will display the current MACD slope over the last 10 bars' value on a chart so you can get a better idea of this. I'm also attaching an example indicator that uses the slope of an EMA to define entry conditions.


    3. Order Reversals:

    Is there a way to set up in Strategy Builder where if a stop loss is hit, a reverse position is immediately entered, but perhaps at a different profit/stop loss target? I.e, if I enter a long position with a profit goal of 8 ticks and a stop loss of 12 ticks, if I get stopped out, is there a way for a reverse order to be placed immediately with say a profit goal of 4 ticks and stop loss of 6 ticks for example?
    This is going to be most likely pretty complicated for the Builder to handle with the Set Stop Loss and Set Profit Target methods on the Conditions and Actions screen, but theoretically it could be done with a number of different entry and exit sets linked with Signal Names. You could think about using a bool called something like SubmitReversal and setting that to false. You could then create a condition set to check the current price against the Average Entry Price + or minus the same number of ticks you used for the stop and set the bool to true if it hits that price. Then, if that bool is true and the position is flat, submit a reversal order with linked stops and targets for a different number of ticks for the target and set the bool back to false. I'd also check in your regular entries that the bool is false so you don't end up trying to submit one of those when you're trying to reverse instead.


    Code:
    4. Enter+Exit Positions:
    
    I've also been compiling a different strategy that essentially looks at the MACD's momentum or current trajectory and acts upon it sooner without having to wait for any form of crossover. I've attempted this by asking the strategy to enter a long position if the MACD value 0 bars ago (i.e current bar) is greater than the MACD value 1 bar ago. On the other side, I have a LongExit condition set that links to my LongEnter position, and exits when the MACD value 0 bars ago is less than the MACD value 1 bar ago, i.e when the momentum of the MACD is swinging in the other direction.
    
    Conversely, I've attempted the same strategy with short positions, where I have the strategy execute a short if the MACD value 0 bars ago is less than the MACD value 1 bar ago and vice versa for the exit strategy. Oddly enough, no matter what I try, when I enable the strategy, only the long positions ever execute. Why is this or how can I fix it?
    
    Also, is there a better, or more accurate way of achieving what I am attempting here in regards to capturing the momentum swings? As you can see from the screenshot I attached, in the first Long Entry, where the Long position is initially exited should not have happened until 5 bars later seeing how the MACD line never decreased during that time span. Also, during the second Long Exit, based on my conditions stating that when the MACD (0) is less than MACD (1), this second long exit happens a full bar late based on how the MACD peaked on that last green bar and dropped immediately in the next bar.
    
    How can I better capture this movement within the strategy builder or am I using the "bars ago" function incorrectly? It just seems that often the orders are delayed by a full bar from where I believe they should enter/exit based on the conditions I have set. And of course, you can notice that after the second Long Exit, that no short position was entered for whatever reason.
    First, if you're running the strategy OnBarClose it means your logic is evaluated only once per bar, when that bar closes. It would be expected that any orders that are then triggered would be submitted at the open of the following bar.

    As far as not seeing the orders when you would expect, the first thing we would suggest is to turn on TraceOrders:

    Strategy Builder > Default Properties > More Properties > Trace Orders, or:

    if (State == State.SetDefaults)
    {
    TraceOrders = true;
    }

    Once you then recompile the strategy, you can open a new NinjaScript Output window under New > NinjaScript Output. This will print a log of any orders submitted by the strategy during while it's running, along with any ignored orders. You can then look through and see what may be occurring.

    Here is a link to our help guide that goes into more detail on tracing orders:

    https://ninjatrader.com/support/help...aceorders2.htm

    Trace orders alone may not give you the full picture of whether or not a trade should have been entered on a given bar, so adding prints to your strategy that will show in the NinjaScript Output window, with information on what the variables you're using for your conditions are on a particular bar, can be helpful.

    This forum post goes into great detail on how to use prints to help figure out where issues may stem from — this should get you going in the correct direction. You can even add these using the Strategy Builder.

    https://ninjatrader.com/support/foru...ns-not-working

    If you run into issues like you're describing here, the above information will allow you to print out all values used in the condition in question that may be evaluating differently. With the printout information you can assess what is different between the two.


    5. Target Order + Runner:

    If I wanted to combine my above enter+exit strategy with some profit/stop losses, how could I submit an order for say 2 contracts, one with a target of 20 points for example and a set stop loss of 10 points, with the second contract either having a much larger target of lets say 50 points and a stop of 10, or just setting the second contract with a trailing stop of 10-15 points? I've tried inputting another "do action" under my Enter Long set of conditions, but the problem I'm having, is when an entry triggers, and I am now in a position with 2 contracts, if one contract hits its target or stop loss, I would like there to be only the one remaining contract open until it is closed. What I am finding, is if one hits its target or stop, and the conditions are right, a 3rd contract might open while the initial "runner" is still active. Is there a way to limit this such that one my initial entry of two contracts begins, that no other entries may occur until they have both exited?
    What I would suggest is checking the position is flat prior to entry, setting your entries per direction to 2 and your Entry Handling to Unique Entries, and submit two separate entry orders each tied specifically by Signal name to their own exits. Since you are checking the position is flat prior to submitting both entries, you would have to exit both of the entered orders in order to return to a flat position and enter again.

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


    Attached Files
    Kate W.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by emillieany, Today, 01:28 AM
    0 responses
    2 views
    0 likes
    Last Post emillieany  
    Started by GilbeCarra, Today, 01:22 AM
    0 responses
    3 views
    0 likes
    Last Post GilbeCarra  
    Started by oprahwinfreyketogummies, Today, 01:20 AM
    0 responses
    3 views
    0 likes
    Last Post oprahwinfreyketogummies  
    Started by ntbone, Today, 01:02 AM
    0 responses
    1 view
    0 likes
    Last Post ntbone
    by ntbone
     
    Started by nikholasjain, Today, 12:52 AM
    0 responses
    2 views
    0 likes
    Last Post nikholasjain  
    Working...
    X