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

Only place Long and ExitLong order once?!

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

    Only place Long and ExitLong order once?!


    Why my codes only place Long order and ExitLong once? I want the strategy continue to place Long order/Exit Long when the condition is met.


    My codes:
    if (hiLoTrend == 1) // up
    {
    strDirection = string.Format("\tHiLoTrend UpTrend !!");
    PrintLog(strDirection);
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"leTMA");
    PrintLog("Enter Long-leTMA");
    }
    }
    else if (hiLoTrend == -1)
    {
    strDirection = string.Format("\tHiLoTrend DownTrend !!");
    PrintLog(strDirection);
    if (Position.MarketPosition == MarketPosition.Long)
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), @"LeExitTMA", @"leTMA");
    PrintLog("Exit Long - LeExitTMA");
    }
    }​


    Logs:
    me:2023-10-11 14:19:10.000 HiLoTrend DownTrend !!
    10/11/2023 2:19:09 PM Strategy 'AlgoTechNQTrendMA/307965668': Entered internal SubmitOrderManaged() method at 10/11/2023 2:19:09 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='LeExitTMA' FromEntrySignal='leTMA'
    10/11/2023 2:19:09 PM Strategy 'AlgoTechNQTrendMA/307965668': Ignored SubmitOrderManaged() method at 10/11/2023 2:19:09 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='LeExitTMA' FromEntrySignal='leTMA' Reason='There already is a matching order with same prices and quantity'
    Time:2023-10-11 14:19:10.000 Exit Long - LeExitTMA
    Time:2023-10-11 14:19:20.000 MarketPosition.Long Pos:1| Avg:15291.25
    Time:2023-10-11 14:19:20.000 Entered OnBarUpdate()
    Time:2023-10-11 14:19:20.000 HiLoTrend DownTrend !!
    10/11/2023 2:19:19 PM Strategy 'AlgoTechNQTrendMA/307965668': Entered internal SubmitOrderManaged() method at 10/11/2023 2:19:19 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='LeExitTMA' FromEntrySignal='leTMA'
    10/11/2023 2:19:19 PM Strategy 'AlgoTechNQTrendMA/307965668': Ignored SubmitOrderManaged() method at 10/11/2023 2:19:19 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='LeExitTMA' FromEntrySignal='leTMA' Reason='There already is a matching order with same prices and quantity'
    Time:2023-10-11 14:19:20.000 Exit Long - LeExitTMA​


    #2
    Hello sysmatrix,

    It looks like your script is running on each tick or on price change so your condition to exit is becoming true multiple times in a row, you can tell based on multiple exists being submitted with the same FromEntrySignal in the log along with the same exit name.

    10/11/2023 2:19:09 PM Strategy 'AlgoTechNQTrendMA/307965668': Entered internal SubmitOrderManaged() method at 10/11/2023 2:19:09 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='LeExitTMA' FromEntrySignal='leTMA'
    10/11/2023 2:19:09 PM Strategy 'AlgoTechNQTrendMA/307965668': Ignored SubmitOrderManaged() method at 10/11/2023 2:19:09 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='LeExitTMA' FromEntrySignal='leTMA'​
    Reason='There already is a matching order with same prices and quantity'
    It also appears this logic is happening all at one based on the timestamps so you would need to re evaluate how this logic is running.

    To have a strategy continue to enter and exit your conditions to enter and exit need to alternate. The prints show the HiLoTrend DownTrend !! continues to be true so it wont enter again until the tHiLoTrend UpTrend !! is true again.






    JesseNinjaTrader Customer Service

    Comment


      #3
      This strategy base on price change and codes are in OnBarUpdate() . hiLoTrend returns up or down. Are you saying that Position.MarketPosition can't be used in threaded condition ? Should I use nOrderUpdate() to check order condition? or anyway to prevent it ? Thank you !!

      Comment


        #4
        Hello sysmatrix,

        You mentioned "threaded condition", if you are using C# threads you need to remove those items. NinjaScript does not support custom threading and will cause issues with how your logic is executed.

        When you submit an order it is not filled exactly at that point, there is some delay so if your conditions remain true while the order is being processed and filled it will continue to do that order submission Generally you need to wait at least 1 OnBarUpdate event to see the position updated however when you use OnEachTick or OnPriceChange in a fast market you may have multiple updates before the order fills.

        Your prints show that is happening because you are getting the warning based on each of the second exit orders that you are submitting. On some brokers there is no logic to check for duplicate orders so in that situation you would have actually got two exits filled leading to an accidental reversal.

        If you are using OnPriceChange or OnEachTick in realtime you want to ensure your entry an exit conditions become true 1 time per entry or exit to avoid that. You can add additional logic like a bool variable to indicate you submitted the order so that you don't try and resubmit it if the position has not changed yet or the condition remains true for multiple updates.

        In regard to the hiLoTrend your print shows that it remained as -1 for a period of time. While I cannot see where all of your prints are we can see that the HiLoTrend UpTrend !! print did not happen during this time so it should not have tried to re enter but only exit. I do see a print where the position changed to long in your output which leads me to believe that the prints have been edited or not all of the print data for this time was included. Alternatively if you are using C# threads that would make the prints appear out of order.

        me:2023-10-11 14:19:10.000 HiLoTrend DownTrend !! //hiLoTrend is -1
        10/11/2023 2:19:09 PM Strategy 'AlgoTechNQTrendMA/307965668': Entered internal SubmitOrderManaged() method at 10/11/2023 2:19:09 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='LeExitTMA' FromEntrySignal='leTMA'
        10/11/2023 2:19:09 PM Strategy 'AlgoTechNQTrendMA/307965668': Ignored SubmitOrderManaged() method at 10/11/2023 2:19:09 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='LeExitTMA' FromEntrySignal='leTMA' Reason='There already is a matching order with same prices and quantity'
        Time:2023-10-11 14:19:10.000 Exit Long - LeExitTMA

        Time:2023-10-11 14:19:20.000 MarketPosition.Long Pos:1| Avg:15291.25

        Time:2023-10-11 14:19:20.000 Entered OnBarUpdate()

        Time:2023-10-11 14:19:20.000 HiLoTrend DownTrend !! //hiLoTrend is -1
        10/11/2023 2:19:19 PM Strategy 'AlgoTechNQTrendMA/307965668': Entered internal SubmitOrderManaged() method at 10/11/2023 2:19:19 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='LeExitTMA' FromEntrySignal='leTMA'
        10/11/2023 2:19:19 PM Strategy 'AlgoTechNQTrendMA/307965668': Ignored SubmitOrderManaged() method at 10/11/2023 2:19:19 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='LeExitTMA' FromEntrySignal='leTMA' Reason='There already is a matching order with same prices and quantity'
        Time:2023-10-11 14:19:20.000 Exit Long - LeExitTMA​​
        Last edited by NinjaTrader_Jesse; 10-11-2023, 01:44 PM.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thank you Jesse. I found that was because I placed my codes in OnBarupdate() and it is fixed.

          I have some questions..

          1. What is the best way to fix/remove any pending orders in the Orders tab.. I often see there are several orders left and can not be cancelled manually.. I have to do system database reset. Is any programable way to address this issue or other quick fix.

          2. EnterLong() , ExitLong() or other Long/.Short method, do you suggest to keep unique signal name for each order? or just one signal name in all trading hours ?

          3. When a strategy just starts, what there is 1 in the Position column in the strategy tab. I don't see any position showing up in the Positions tab. Is any old order records left in NT database that the strategy still tried to loop up ?

          Comment


            #6
            Hello sysmatrix,

            If you are getting stuck orders on a sim account you can reset the account using the accounts tab. https://ninjatrader.com/support/help...01_account.htm
            If that is happening for a demo account or live account please use the platforms help -> email support option when this happens next so that our support team can review the log files and your account to better understand what may have occurred.

            Using unique signal names would be helpful for reading performance reports, that lets you know which specific order you are looking at in the results. The signal name is also used for tying exits to entries so in that situation you would want to use unique names so you can tie the correct targets with the entry.

            When you strategy is enabled it first does a historical backtest over the data you selected. If it enters a historical position the strategy position will show the position and the account will not because that is a virtual order. If you are using wait until flat as the start behavior the strategy will be listed as yellow as it waits to exit that historical position. Once it exits that position it will begin working in realtime.
            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by AaronKoRn, Today, 09:49 PM
            0 responses
            11 views
            0 likes
            Last Post AaronKoRn  
            Started by carnitron, Today, 08:42 PM
            0 responses
            10 views
            0 likes
            Last Post carnitron  
            Started by strategist007, Today, 07:51 PM
            0 responses
            11 views
            0 likes
            Last Post strategist007  
            Started by StockTrader88, 03-06-2021, 08:58 AM
            44 responses
            3,980 views
            3 likes
            Last Post jhudas88  
            Started by rbeckmann05, Today, 06:48 PM
            0 responses
            9 views
            0 likes
            Last Post rbeckmann05  
            Working...
            X