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 EB Worx, 04-04-2023, 02:34 AM
        7 responses
        161 views
        0 likes
        Last Post VFI26
        by VFI26
         
        Started by Mizzouman1, Today, 07:35 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by Radano, 06-10-2021, 01:40 AM
        20 responses
        616 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by i019945nj, 12-14-2023, 06:41 AM
        6 responses
        68 views
        0 likes
        Last Post i019945nj  
        Started by aa731, Today, 02:54 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Working...
        X