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

SetStopLoss

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

    SetStopLoss

    Hi,

    All I did was change my code from:

    Code:
    [FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]
    [COLOR=black][COLOR=blue]if[/COLOR] (Close[0] > ShortStopLoss)[/COLOR]
    [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=black]{[/COLOR]
    [/SIZE][/FONT][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=black] Print("Close > ShortStopLoss");[/COLOR]
    [/SIZE][/FONT][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=black] ExitShort();[/COLOR]
    [/SIZE][/FONT][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][COLOR=black]}[/COLOR]
    [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
    to

    Code:
    [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]
    if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000] (((ShortStopLoss-Position.AvgPrice)*[/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]100000[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000]) < ShortStopLossAlt)[/COLOR]
    {
    SetStopLoss(CalculationMode.Price, ShortStopLoss);
    }
    [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]else
    [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]{
    SetStopLoss(CalculationMode.Ticks, [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]12[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]);
    }
    [/SIZE][/FONT][/SIZE][/FONT]
    and my strategy went haywire! There are many occasions now where I get stopped out as soon as I enter the trade and at the same price. Could someone please help?

    Regards

    Kay Wai

    #2
    Hi Kay, are you sure you attempt this when you would have a position, so the Position.AvgPrice would have a meaningful value to calculate with?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi Bertrand,

      Well before I introduced the SetStopLoss, I had a value for every Position.AvgPrice. I even had a value for my LongStopLoss and ShortStopLoss.

      But once I changed the code as per below, this didn't happen anymore. There are occassions where I am unable to get a price for Position.AvgPrice but a value is nevertheless reflected on the chart and that position is stopped out immediately and I am unable to print the value....

      It is truly frustrating especially when you feel you are getting somewhere and this happens.

      Regards

      Kay Wai

      Comment


        #4
        I just realised that I added a flag after EnterLong() and changed the code after "else if (Oisition.Market.Position == MarketPosition.Long" from

        if (Position.AvgPrice >0)
        {
        Do something
        }

        to

        if (flag)
        {
        Do Something
        }

        The "Do Something" in both cases are the same.

        Regards

        Kay Wai

        Comment


          #5
          Kay, I'm sorry I do not follow you 100% here - what would be needed is printing the variables involved to understand what you would need to change in your code structure, there would be only a Position Price as the MarketPosition updated to the new value.

          Here are helpful guidelines how to debug your NinjaScript code with Print() statements :

          BertrandNinjaTrader Customer Service

          Comment


            #6
            Hi Bertrand,

            Printing the variables is to help me debug. But I can't figure out why it is getting stopped out at the same bar when there are occasions where a trade is working properly for the same set of instructions.

            And there is no suggestion from the code that something is wrong. What other ways are they to debug?

            Regards

            Kay Wai

            Comment


              #7
              Hi Bertrand,

              I took my code apart and attempted to put it piece by piece together again. I even removed the condition to determine the stoploss value and replaced it with SetStopLoss(CalculationMode.Ticks, 12). Everything was working fine.

              So I re-inserted the following code:-

              Code:
              [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]
              if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000] (PotentialShortLoss < ShortStopLossAlt)[/COLOR]
              {
              SetStopLoss(CalculationMode.Price, ShortStopLoss);
              }
              [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]else
              [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]{
              SetStopLoss(ShortStopLossAlt);
              }
              [/SIZE][/FONT][/SIZE][/FONT]
              And my problems surfaced again. Some positions get stopped out immediately and there was also 50% more trades! Any thoughts on what could be wrong?

              Regards

              Kay Wai

              Comment


                #8
                Just to follow on from the previous thread, I get these printouts when my position is flat and about to initiate a position:-

                5/18/2009 8:15:00 PM
                hhssbar = 1
                myHHShortSignal = True
                5/18/2009 8:15:00 PM
                1.3509
                1.348
                0.0029
                1.3495
                5/18/2009 8:15:00 PM Entered internal PlaceOrder() method at 5/18/2009 8:15:00 PM: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=0.1M LimitPrice=0 StopPrice=0 SignalName='MyHHShort' FromEntrySignal=''
                justenteredshort = True
                And when my position is filled, these values are printed:-
                5/18/2009 8:18:00 PM
                1.3495
                0.483
                1.3504
                justenteredshort = False
                PSL = 90.00
                ShortStopLossAlt =120
                When the "issue" occurs, I am only getting the part when I am about to enter a position. The printout ends here:-
                5/18/2009 11:42:00 PM
                hhssbar = 4
                mySPLongSignal = True
                1.3517
                1.3495
                0.0022
                1.3496
                5/18/2009 11:42:00 PM Entered internal PlaceOrder() method at 5/18/2009 11:42:00 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=0.1M LimitPrice=0 StopPrice=0 SignalName='MySpecialLong' FromEntrySignal=''
                justenteredlong = True
                From the output window, it would seem that the trade has been initiated. But there is no indication of an exit at all.

                Regards

                Kay Wai

                Comment


                  #9
                  Attached is the chart of the first 3 trades...all got stopped out.

                  The first 2 at the correct spots but the 3 at the point of entry.
                  Attached Files

                  Comment


                    #10
                    Kay, is there any stripped down example we could give a run here and check into? I'm not sure why you would see no set / exit order related traces here - are you resetting the stop-loss then in your script to a default value when you're flat?
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Hi Bertrand,

                      1) Doesn't the stop loss reset itself once the position is exited?
                      2) How do I reset a stop loss? I do not have SetStopLoss in Initialize()
                      3) If I have positions which go from long straight to short and vice versa, how would I reset the stop loss?

                      This might be just the reason!

                      Regards

                      Kay Wai

                      Comment


                        #12
                        Kay, even on a reverse there's a short 'flat' state so the reset could trigger, it would be detailed in this reference sample here - http://www.ninjatrader.com/support/f...ead.php?t=3222
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          Hi Bertrand,

                          From what I see from the script in the provided link, I need to place a SetStopLoss instruction where it is unlikely to be hit upon entering a new position and that I need to place it inside this block of code.

                          if (Position.MarketPosition == MarketPosition.Flat)
                          {

                          }

                          Is that correct?

                          Regards

                          Kay Wai

                          Comment


                            #14
                            Correct, just put it before the other order logic at the OnBarUpdate() start setting the stoploss to a default tick offset when you're flat.
                            BertrandNinjaTrader Customer Service

                            Comment


                              #15
                              Hi Bertrand,

                              I've included the code as per below as I intend to only trade from 19:57 to 00:03:-

                              Code:
                              [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]
                              [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]if (Position.MarketPosition == MarketPosition.Flat)
                              {
                              [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (ToTime(Time[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]]) >= [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]195700[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] || ToTime(Time[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]]) <= [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]00300[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
                              [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
                              [SIZE=2][FONT=Courier New]SetStopLoss(CalculationMode.Ticks, [/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]12[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
                              [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
                              [/SIZE][/FONT]}
                              It works fine except when I'm going from a Long to Short position like
                              Code:
                              if[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000] (Position.MarketPosition == MarketPosition.Long)[/COLOR][/SIZE][/FONT]
                              [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
                              [SIZE=2][FONT=Courier New]if (xx condition is true)[/FONT][/SIZE]
                              [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
                              [SIZE=2][FONT=Courier New]EnterShort();[/FONT][/SIZE]
                              [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
                              [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
                              [/SIZE][/FONT]
                              I get stopped out at the same price.

                              Here is an excerpt from the output window:-

                              5/25/2009 11:30:00 PM Entered internal PlaceOrder() method at 5/25/2009 11:30:00 PM: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=0.1M LimitPrice=0 StopPrice=0 SignalName='MyHHXShort' FromEntrySignal=''
                              justenteredshort = True
                              5/25/2009 11:30:00 PM Cancelled pending exit order, since associated position is closed: Order='NT-00025/Backtest' Name='Stop loss' State=Working Instrument='$EUUS' Action=Sell Limit price=0 Stop price=1.3984 Quantity=0.1M Strategy='MyCustomFXStrategy2v7alt' Type=Stop Tif=Gtc Oco='NT-00022-1225' Filled=0 Fill price=0 Token='3fe8ae70973b425d8ca7b231e63236ec' Gtd='12/1/2099
                              How do I resolve this? I've also attached a chart for your reference

                              Regards

                              Kay Wai
                              Attached Files
                              Last edited by kaywai; 09-21-2011, 10:23 PM. Reason: attaching chart

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by lollers, Today, 03:26 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post lollers
                              by lollers
                               
                              Started by aliyahany, Today, 03:16 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post aliyahany  
                              Started by mariajvillarrealr, Today, 02:57 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post mariajvillarrealr  
                              Started by marcus2300, Yesterday, 10:21 AM
                              6 responses
                              20 views
                              0 likes
                              Last Post marcus2300  
                              Started by skorpn, Today, 01:28 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post skorpn
                              by skorpn
                               
                              Working...
                              X