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 exit on a reverse signal and enter again

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

    how to exit on a reverse signal and enter again

    Hi,
    How do i have stop and reverse logic? I want to get in short on a cross over and if there is long cross over i want to exit my short and enter long..... And reverse for Shorts. I am a bit confused how to deal with ExitLong stop markets and limits.... With logic below when in long, on crossShort it doesnt reverses position


    EnterLong(PositionSize, "Cross Long");
    if ((Position.MarketPosition == MarketPosition.Long && EMACrossShort == true) )
    {
    ExitLong();
    EnterShort(PositionSize, "Cross Short");
    }​

    if(Position.MarketPosition == MarketPosition.Long)


    ExitLongStopMarket(0, true, Position.Quantity, Position.AveragePrice - (TickSize * StopLossTicks), "SLL", "Cross Long");

    ExitLongLimit(0, true, Position.Quantity, Position.AveragePrice + (TickSize * ProfitTargetTicks), "PTL", "Cross Long");​
    Last edited by tkaboris; 07-03-2023, 06:52 AM.

    #2
    Hello tkaboris,

    Because you are using targets you would need to either have those targets fill first and become flat before entering into the opposite direction or you would need to cancel them and then enter the opposite direction after the cancellation is complete. When using the managed approach you can have an automatic reversal if you call the opposite entry method while in a position, that both exits the position and enters the opposite. With what you have here you will run into the managed approach order handling rules.

    Another item is that you cannot call Exit methods at the same time as entry methods, The code you have shown will cause a duplicate exit and lead to a duplicate reversal, you need to remove the exit order you have before EnterShort. If you are in a long position and call entershort that will reverse the position.

    You can read the general rules of orders in the following page: https://ninjatrader.com/support/help...antedPositions

    To know when you are hitting a rule you can enable TraceOrders https://ninjatrader.com/support/help...ub=TraceOrders
    JesseNinjaTrader Customer Service

    Comment


      #3
      Ok got it. I simply used ExitLong() ExitShort() before making an apposite trade. however
      during backtesting market replay i sometimes get this messed up screen. Its like taking one of mine EMAs cyan and ploting it to 0 values.. Like tests run fine for couple of hours, taking all trades but suddenly occasioanll i get this chart

      Click image for larger version

Name:	image.png
Views:	134
Size:	609.7 KB
ID:	1258717

      Comment


        #4
        Hello tkaboris,

        I wouldnt be able to say what may be causing that from the image, you would have to add prints into the script and see what may be happening there. If thats an EMA plotting at 0 that wouldn't be related to your trading question, I would suggest to break that into a seperate post based on what you find after adding prints.

        One note on your comment about using exits before an opposing trade, if you are doing the following that will result in a double reversal. The following is not valid:

        Code:
        if ((Position.MarketPosition == MarketPosition.Long && EMACrossShort == true) )
        {
        ExitLong();
        EnterShort(PositionSize, "Cross Short");
        }​​​
        this would be valid:

        Code:
        if ((Position.MarketPosition == MarketPosition.Long && EMACrossShort == true) )
        {
        EnterShort(PositionSize, "Cross Short");
        }​
        You would only see a problem when running the script in realtime, it would exit the position then try to reverse which also exts the position leading to 2 in the opposite direction. ​
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by patrickmlee007, Today, 09:33 AM
        2 responses
        17 views
        0 likes
        Last Post patrickmlee007  
        Started by magnatauren, 08-15-2020, 02:12 PM
        5 responses
        206 views
        0 likes
        Last Post RaddiFX
        by RaddiFX
         
        Started by rene69851, 05-02-2024, 03:25 PM
        1 response
        21 views
        0 likes
        Last Post rene69851  
        Started by ETFVoyageur, Yesterday, 07:05 PM
        5 responses
        45 views
        0 likes
        Last Post ETFVoyageur  
        Started by jpeep, 08-16-2020, 08:31 AM
        13 responses
        487 views
        0 likes
        Last Post notenufftime  
        Working...
        X