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

Probleme variable OnStateChange()

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

    Probleme variable OnStateChange()

    Hi,


    I have an issue to declare
    HTML Code:
    SetStopLoss(CalculationMode.Ticks, stopLossTicks)
    on OnStateChange() .


    The problem is that the stopLossTicks is not a fixed but obtained from the ATR(4)[0]. Every bar the value of stopLossTicks change.


    I've put the logic in OnBarUpdate() to get a dynamic StopLoss Modification to BreakEven and then TrailingStop based on the attached file.


    Do you have any idea how can i give the stopLossTicks variable into
    State.Configure?

    Code:
    protected override void OnStateChange()
       {
       if (State == State.SetDefaults)
       {
          Description   = ........
    
           AddPlot(new Stroke(Brushes.Lime, 2), PlotStyle.Hash, "ProfitTarget");
           AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.Line, "StopLoss");
         }
         else if (State == State.Configure)
          {                            
          SetStopLoss(CalculationMode.Ticks, stopLossTicks);
          }
     }
    
    
    protected override void OnBarUpdate()
     {
    int ATR_VAL                 =     (int)(ATR(4)[0]/ TickSize); 
    int stopLossTicks             =   (int)(ATR_VAL * 2.6);
    
    if ( Position.MarketPosition == MarketPosition.Flat 
          && Close[1] < Close[0])     
          {    
           EnterLongLimit(1, Close[0], "LongEntry");            
          }
              
           switch (Position.MarketPosition)
             {            
               case MarketPosition.Flat:
                        SetStopLoss(CalculationMode.Ticks, stopLossTicks);
                        previousPrice = 0;
                        stopPlot = 0;
                        break;               
                           
                 case MarketPosition.Long:                        
                        if (previousPrice == 0)
                        {
                         stopPlot = Position.AveragePrice - stopLossTicks * TickSize;  // initial stop plot level
                         }
                        
                         if (Close[0] > Position.AveragePrice + 5 * TickSize  && previousPrice == 0)
                        {
                            initialBreakEven = Position.AveragePrice + 3 * TickSize;
                            SetStopLoss(CalculationMode.Price, initialBreakEven);
                            previousPrice = Position.AveragePrice;
                            stopPlot = initialBreakEven;
                        }                    
                        else if (previousPrice    != 0 
                                 && GetCurrentAsk() > previousPrice + 7 * TickSize )
                        {
                            newPrice = previousPrice + 2 * TickSize;     /
                            SetStopLoss(CalculationMode.Price, newPrice);           
                            previousPrice = newPrice;                                
                            stopPlot = newPrice;                                      
                         }
                        
                         StopLoss[0]     = stopPlot;
    
                        break;
    
                    default:
                    break;
                }            
    
            }

    #2
    Originally posted by cbadr View Post
    Hi,


    I have an issue to declare
    HTML Code:
    SetStopLoss(CalculationMode.Ticks, stopLossTicks)
    on OnStateChange() .


    The problem is that the stopLossTicks is not a fixed but obtained from the ATR(4)[0]. Every bar the value of stopLossTicks change.


    I've put the logic in OnBarUpdate() to get a dynamic StopLoss Modification to BreakEven and then TrailingStop based on the attached file.


    Do you have any idea how can i give the stopLossTicks variable into
    State.Configure?

    Code:
    protected override void OnStateChange()
       {
       if (State == State.SetDefaults)
       {
          Description   = ........
    
           AddPlot(new Stroke(Brushes.Lime, 2), PlotStyle.Hash, "ProfitTarget");
           AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.Line, "StopLoss");
         }
         else if (State == State.Configure)
          {                            
          SetStopLoss(CalculationMode.Ticks, stopLossTicks);
          }
     }
    
    
    protected override void OnBarUpdate()
     {
    int ATR_VAL                 =     (int)(ATR(4)[0]/ TickSize); 
    int stopLossTicks             =   (int)(ATR_VAL * 2.6);
    
    if ( Position.MarketPosition == MarketPosition.Flat 
          && Close[1] < Close[0])     
          {    
           EnterLongLimit(1, Close[0], "LongEntry");            
          }
              
           switch (Position.MarketPosition)
             {            
               case MarketPosition.Flat:
                        SetStopLoss(CalculationMode.Ticks, stopLossTicks);
                        previousPrice = 0;
                        stopPlot = 0;
                        break;               
                           
                 case MarketPosition.Long:                        
                        if (previousPrice == 0)
                        {
                         stopPlot = Position.AveragePrice - stopLossTicks * TickSize;  // initial stop plot level
                         }
                        
                         if (Close[0] > Position.AveragePrice + 5 * TickSize  && previousPrice == 0)
                        {
                            initialBreakEven = Position.AveragePrice + 3 * TickSize;
                            SetStopLoss(CalculationMode.Price, initialBreakEven);
                            previousPrice = Position.AveragePrice;
                            stopPlot = initialBreakEven;
                        }                    
                        else if (previousPrice    != 0 
                                 && GetCurrentAsk() > previousPrice + 7 * TickSize )
                        {
                            newPrice = previousPrice + 2 * TickSize;     /
                            SetStopLoss(CalculationMode.Price, newPrice);           
                            previousPrice = newPrice;                                
                            stopPlot = newPrice;                                      
                         }
                        
                         StopLoss[0]     = stopPlot;
    
                        break;
    
                    default:
                    break;
                }            
    
            }
    1. Declare both of these as class variables, not local to OnBarUpdate():
    Code:
    int ATR_VAL                 =     0; 
    int stopLossTicks             =   0;
    2. Add an initialStopLossTicks value to use to reset when Flat
    Code:
    int initialStopLossTicks = 13 //say, or whatever value you prefer to use
    3. In State.Configure, set the initialStopLoss
    Code:
    ...
         else if (State == State.Configure)
          {                            
          SetStopLoss(CalculationMode.Ticks, initialStopLossTicks);
          }
    4. Update them on each pass in OnBarUpdate(), without redeclaring them:
    Code:
    ATR_VAL                 =     (int)(ATR(4)[0]/ TickSize); 
    stopLossTicks             =   (int)(ATR_VAL * 2.6);
    ...
    5. When Flat, reset the StopLossTicks:
    Code:
    ...
               case MarketPosition.Flat:
                        SetStopLoss(CalculationMode.Ticks, initialStopLossTicks);
                        previousPrice = 0;
                        stopPlot = 0;
                        break;

    Comment


      #3
      Thanks for the answer.
      When you say to declare in a class shall i put something like that just before OnStateChange():

      Code:
      private InnerClass inner = new InnerClass();          
      
      private class InnerClass
      {
      int ATR_VAL                   =   0;  
      int stopLossTicks           =   0; 
      int initialstopLossTicks   =   13; 
      }
      Last edited by cbadr; 06-05-2018, 05:40 AM.

      Comment


        #4
        Hello cbadr,

        I believe koganam just meant for you to declare the variables inside of the indicators class or at class level. You wouldn't need to make any new objects here, so like the following in your existing file:

        Code:
        	public class Test : Indicator
        {
        	int ATR_VAL                 =     0; 
        	int stopLossTicks             =   0;
        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by rhyminkevin, Today, 04:58 PM
        1 response
        35 views
        0 likes
        Last Post Anfedport  
        Started by iceman2018, Today, 05:07 PM
        0 responses
        4 views
        0 likes
        Last Post iceman2018  
        Started by lightsun47, Today, 03:51 PM
        0 responses
        7 views
        0 likes
        Last Post lightsun47  
        Started by 00nevest, Today, 02:27 PM
        1 response
        14 views
        0 likes
        Last Post 00nevest  
        Started by futtrader, 04-21-2024, 01:50 AM
        4 responses
        49 views
        0 likes
        Last Post futtrader  
        Working...
        X