Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

My strategy is triggering SHORT when it should be LONG

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

    My strategy is triggering SHORT when it should be LONG

    Hey NTCS,

    I have a strategy where I am looking at the 9:50AM using the 1 MIN bar on NQ.

    I ran a backtest and the strategy on Mon 18 March '24 should trigger bullish as shown in the small picture below.

    Instead, it triggers short. Help!

    I'm using this code to establish direction, then using IF / ELSE IF to enter the next 1 minute bar.

    isBarLong = Close[0] > Open[0]);
    isBarShort = Close[0] < Open[0]);

    if ((Time[0].TimeOfDay).Equals(entryTime))
    {
    // If the bar is long, go short
    if (isBarLong)
    {
    EnterShort("ShortPosition");
    Print ("Short" + (entryPrice = Close[0]));
    SetStopLoss("ShortPosition", CalculationMode.Ticks, 40, false);
    SetProfitTarget("ShortPosition", CalculationMode.Ticks, 60);
    Print ("Triggered Short");
    }

    // If the bar is short, go long
    else if (isBarShort)
    {
    EnterLong("LongPosition");
    Print ("Long" + (entryPrice = Close[0]));
    SetStopLoss("LongPosition", CalculationMode.Ticks, 40, false);
    SetProfitTarget("LongPosition", CalculationMode.Ticks, 60);
    Print ("Triggered Long");
    }
    }



    Click image for larger version

Name:	image.png
Views:	122
Size:	7.4 KB
ID:	1299058

    #2
    Hello unknowncaller007,

    Thank you for your post.

    If the expected trade(s) are not appearing, this would indicate that the condition to place the order is not evaluating as true and the order is not being submitted, or the order is being ignored for other reasons, or the order is being cancelled.

    To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.

    The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

    I am happy to assist you with analyzing the output from the output window.

    Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

    Below is a link to a forum post that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.


    Please let me know if I may further assist with analyzing the output or if you need any assistance creating a print or enabling TraceOrders.​

    Comment


      #3
      Hey Gaby,

      I am looking for help with a strategy where the output shows the opposite than what is expected.

      Here's the screenshot from PRINT using NinjaTrader Output Window.

      The <>***<> is the beginning for that particular time.

      Click image for larger version

Name:	image.png
Views:	63
Size:	19.0 KB
ID:	1299215

      Comment


        #4
        It was actually the Tuesday March 19 that didn't trigger correctly. Below is the output using TraceOrders

        Click image for larger version

Name:	image.png
Views:	64
Size:	117.7 KB
ID:	1299220

        Comment


          #5
          Hello,

          Unfortunately, these prints aren't descriptive enough to be able to tell why this is happening. The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

          A more descriptive print based on the code you shared would be something like this:

          Print(Times[0][0] + " = entryTime: " + entryTime);
          Print(Close[0] " + Close[0] + " Open[0]: " + Open[0]);
          Print("isBarLong: " + isBarLong + " or isBarShort: " + isBarShort);

          Please include more descriptive prints to be able to analyze the output with more accuracy.

          Comment


            #6
            Hey Gaby,

            I'm confused. Seems to me the prints I used were similar.

            The question I have is WHY? is the strategy trigger SHORT when in fact the bar on 9:50:00 at 1 min is bearish which should create a bullish trigger for the 9:51:00 entry.

            Here's the print which includes your prints.

            <>*1*<>
            strategyTime1 09:50:00
            Time [0] 3/19/2024 9:50:00 AM
            True
            False
            <>*2*<>
            3/19/2024 9:51:00 AM = entryTime: 09:51:00
            Close[0] 18073.25 Open[0]: 18080
            isBarLong: True or isBarShort: False
            3/19/2024 9:51:00 AM Strategy 'MacroScalpv2/-1': Entered internal SubmitOrderManaged() method at 3/19/2024 9:51:00 AM: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='ShortPosition' FromEntrySignal=''
            Short18073.25
            3/19/2024 9:51:00 AM Strategy 'MacroScalpv2/-1': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='ShortPosition' Mode=Ticks Value=40 IsSimulatedStop=False IsMarketIfTouched=False
            3/19/2024 9:51:00 AM Strategy 'MacroScalpv2/-1': Entered internal SetStopTarget() method: Type=Target FromEntrySignal='ShortPosition' Mode=Ticks Value=60 IsSimulatedStop=False IsMarketIfTouched=False
            Triggered Short
            3/19/2024 9:51:00 AM Strategy 'MacroScalpv2/-1: Cancelled pending exit order, since associated position is closed, orderId='NT-00167-102' account='Backtest' name='Profit target' orderState=Working instrument='NQ 06-24' orderAction=BuyToCover orderType='Limit' limitPrice=18058 stopPrice=0 quantity=1 tif=Gtc oco='NT-00110-102' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2024-03-19 09:51:00' gtd='2099-12-01' statementDate='2024-04-13'
            3/19/2024 9:51:00 AM Strategy 'MacroScalpv2/-1': Cancelled OCO paired order: BarsInProgress=0, orderId='NT-00167-102' account='Backtest' name='Profit target' orderState=Cancelled instrument='NQ 06-24' orderAction=BuyToCover orderType='Limit' limitPrice=18058 stopPrice=0 quantity=1 tif=Gtc oco='NT-00110-102' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2024-03-19 09:51:00' gtd='2099-12-01' statementDate='2024-04-13'

            Comment


              #7
              Hello unknowncaller007,

              I've added the code you have suggested in post # 1 to a test script.
              I had to modify the code to correct the parenthesis mistake, and to add prints.

              I am not able to reproduce.

              The output appears as:
              4/11/2024 9:51:00 AM | Close[0]: 5217.25 < Open[0]: 5218, isBarLong: False
              4/11/2024 9:51:00 AM | Close[0]: 5217.25 > Open[0]: 5218, isBarShort: True
              4/11/2024 9:51:00 AM Strategy 'Unknowncaller007Test/323442677': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='LongPosition' Mode=Ticks Value=40 IsSimulatedStop=False IsMarketIfTouched=False
              4/11/2024 9:51:00 AM Strategy 'Unknowncaller007Test/323442677': Entered internal SetStopTarget() method: Type=Target FromEntrySignal='LongPosition' Mode=Ticks Value=60 IsSimulatedStop=False IsMarketIfTouched=False
              4/11/2024 9:51:00 AM Strategy 'Unknowncaller007Test/323442677': Entered internal SubmitOrderManaged() method at 4/11/2024 9:51:00 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='LongPosition' FromEntrySignal=''
              Long5217.25
              Triggered Long
              4/11/2024 10:14:00 AM Strategy 'Unknowncaller007Test/323442677: Cancelled pending exit order, since associated position is closed, orderId='NT-00004-238' account='Sim101' name='Stop loss' orderState=Working instrument='ES 06-24' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=5207.5 quantity=1 tif=Gtc oco='NT-00002-238' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2024-04-11 09:51:00' gtd='2099-12-01' statementDate='2024-04-15'
              4/11/2024 10:14:00 AM Strategy 'Unknowncaller007Test/323442677': Cancelled OCO paired order: BarsInProgress=0, orderId='NT-00004-238' account='Sim101' name='Stop loss' orderState=Cancelled instrument='ES 06-24' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=5207.5 quantity=1 tif=Gtc oco='NT-00002-238' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2024-04-11 09:51:00' gtd='2099-12-01' statementDate='2024-04-15'
              4/12/2024 9:51:00 AM | Close[0]: 5168.5 < Open[0]: 5165.25, isBarLong: True
              4/12/2024 9:51:00 AM | Close[0]: 5168.5 > Open[0]: 5165.25, isBarShort: False
              4/12/2024 9:51:00 AM Strategy 'Unknowncaller007Test/323442677': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='ShortPosition' Mode=Ticks Value=40 IsSimulatedStop=False IsMarketIfTouched=False
              4/12/2024 9:51:00 AM Strategy 'Unknowncaller007Test/323442677': Entered internal SetStopTarget() method: Type=Target FromEntrySignal='ShortPosition' Mode=Ticks Value=60 IsSimulatedStop=False IsMarketIfTouched=False
              4/12/2024 9:51:00 AM Strategy 'Unknowncaller007Test/323442677': Entered internal SubmitOrderManaged() method at 4/12/2024 9:51:00 AM: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='ShortPosition' FromEntrySignal=''
              Short5168.5
              Triggered Short
              ​​

              Note, it is always helpful to provide an export of a reduced test script that shows the behavior.

              Using the attached exported test script without making any modifications to it, is the output you are getting different than the output I'm showing?
              Unknowncaller007Test_NT8.zip
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Today, 05:17 AM
              0 responses
              44 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              124 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              65 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              42 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              46 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X