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

Reversing Trades

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

    Reversing Trades

    I have a trading strategy that is kind of working but not executing 100%. It seems to skip once in a while. What I want it to do is reverse an order.

    Scenario 1: when the time is decided to change and the order didn't hit the target exit position AND set a new limit order the opposite direction;
    Scenario 2: if the order wasn't filled Cancel the order AND set a new limit order the opposite direction.

    What I am finding out it can cancel the order but skips setting up the reversal order. I think the code doesn't think it is flat.



    if (Position.MarketPosition == MarketPosition.Short)
    {
    if (nTarget1Contracts != 0)
    ExitShort("Short1");

    if (nTarget2Contracts != 0)
    ExitShort("Short2");

    if (nTarget3Contracts != 0)
    ExitShort("Short3");
    }
    else
    {
    if (oEntryOrder1 != null)
    {
    oEntryOrder1 = GetRealtimeOrder(oEntryOrder1);
    CancelOrder(oEntryOrder1);
    }

    if (oEntryOrder2 != null)
    {
    oEntryOrder2 = GetRealtimeOrder(oEntryOrder2);
    CancelOrder(oEntryOrder2);
    }

    if (oEntryOrder3 != null)
    {
    oEntryOrder3 = GetRealtimeOrder(oEntryOrder3);
    CancelOrder(oEntryOrder3);
    }
    }

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    {
    if (nTarget1Contracts != 0 && bTradeEnabled == true)
    {

    SetProfitTarget("Long1", CalculationMode.Price, dPrice1, true);
    SetStopLoss("Long1", CalculationMode.Price, dPrice2, false);

    if (bUseMar****rder == true)
    EnterLong(nTarget1Contracts, "Long1");
    else
    EnterLongLimit(0, true, nTarget1Contracts, dPrice3, "Long1");
    }
    }

    #2
    Hello ballboy11,

    You are very likely correct in your assessment here, the position will not be updated until the next OnBarUpdate call. Calling the CancelOrder will not allow the Position check to evaluate true immediately after.

    To confirm that is happening you could use a Print inside your condition and wait for the time when you see this happen, if the print didn't happen your logic was not called which indicates it was not flat.
    Code:
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    Print("Here");

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by andrewtrades, Today, 04:57 PM
    1 response
    5 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by chbruno, Today, 04:10 PM
    0 responses
    3 views
    0 likes
    Last Post chbruno
    by chbruno
     
    Started by josh18955, 03-25-2023, 11:16 AM
    6 responses
    436 views
    0 likes
    Last Post Delerium  
    Started by FAQtrader, Today, 03:35 PM
    0 responses
    6 views
    0 likes
    Last Post FAQtrader  
    Started by rocketman7, Today, 09:41 AM
    5 responses
    19 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Working...
    X