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

Logic Error

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

    Logic Error

    I'm trying to increase my position size after winning trades. Do you have any idea why the position size won't increase with this code?

    Code:
    public class MyStrategy: Strategy
    {
    private int winloss = 0;
    
            protected override void Initialize()
            { CalculateOnBarClose = true;  }
    
    protected override void OnBarUpdate()
            {
                int lotsize;
                if (winloss > 0)
                {
                    lotsize = Convert.ToInt32(Math.Round(Lots + (Lots * Math.Log(1+winloss))));
                }
                else
                {
                    lotsize = Lots;    
                }
    
    // code to enter long goes here
    
    
                // Condition set 3
                if (an exit long is signaled)
                {
                    winloss = UpdateWL(winloss);
                    ExitLong("", "");
                }
    
            private int UpdateWL(int wl)
            {
                    if (Position.GetProfitLoss(Open[0], PerformanceUnit.Percent) <= 0) // a losing trade
                    { 
                        if (wl  <= 0) { wl--; }
                        else { wl  = -1; }
                    }
                    else // a winning trade
                    { 
                        if (wl >= 0) { wl++;  }
                        else { wl = 1; }
                    }
                    return(wl);
            }
    }

    #2
    In case it's relevant, I'm using the GAIN feed. I took account to round up to whole numbers since that feed supports a minimum unit size of 1.

    Comment


      #3
      texasnomad, I would suggest printing the values to the ouput window (like your winloss and lotsize) to check they actually return what you would expect based on your code logic - http://www.ninjatrader-support2.com/...ead.php?t=3418
      BertrandNinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by MrAdamSK, 04-02-2024, 11:25 PM
      3 responses
      41 views
      0 likes
      Last Post NinjaTrader_Zachary  
      Started by lakman184, 03-24-2024, 01:30 PM
      7 responses
      35 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by DawnTreader, 05-08-2024, 05:58 PM
      20 responses
      80 views
      0 likes
      Last Post DawnTreader  
      Started by llanqui, Yesterday, 10:29 AM
      5 responses
      26 views
      0 likes
      Last Post llanqui
      by llanqui
       
      Started by lorem, 04-25-2024, 09:18 AM
      14 responses
      62 views
      0 likes
      Last Post lorem
      by lorem
       
      Working...
      X