Announcement

Collapse
No announcement yet.

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.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        75 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        146 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        79 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        50 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        54 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X