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

Backtesting Strategy: Account Management + Position Sizing

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

    Backtesting Strategy: Account Management + Position Sizing

    I have this very basic strategy I am trying to backtest. If EMA8>SMA20 and EMA3 crosses EMA8 from below, go long and exit when EMA3 crosses EMA8 from above:

    public class TLine : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int fastEMA = 3; // Default setting for FastEMA
    private int mediumEMA = 8; // Default setting for MediumEMA
    private int slowSMA = 20; // Default setting for SlowSMA
    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(EMA(FastEMA), EMA(MediumEMA), 1)
    && EMA(MediumEMA)[0] > SMA(SlowSMA)[0])
    {
    EnterLong(DefaultQuantity, "");
    }

    // Condition set 2
    if (CrossBelow(EMA(FastEMA), EMA(MediumEMA), 1))
    {
    ExitLong("", "");
    }


    }

    #region Properties
    [Description("")]
    [GridCategory("Parameters")]
    public int FastEMA
    {
    get { return fastEMA; }
    set { fastEMA = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int MediumEMA
    {
    get { return mediumEMA; }
    set { mediumEMA = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int SlowSMA
    {
    get { return slowSMA; }
    set { slowSMA = Math.Max(1, value); }
    }
    #endregion
    }
    }

    I am having trouble with coding the account management & position sizing portion into this.
    I have the following parameters:
    1) Starting Account Size: 10,000
    2) % of Account Size at risk on each trade: 1%
    3) # of shares to purchase: the lower of current account size, or 1% / (((EMA3-EMA8)/EMA8)*2), or 10% of account size.

    I can't figure out how to code this in and have it update dynamically (say i reach the full account limit so no new trades until a position is exited and hence frees up capital.

    Any help will be appreciated!

    #2
    Hello,

    Welcome to our forums.

    We do not have any internal methods for tracking and calculation your position size dynamically as you seek, so you'll have to create your own variables and calculate it.

    Below is some pesudo code to help you get started.

    The basics of this concept is every time your strategy is flat, reset your variables based off the current performance

    Code:
    			if(Position.MarketPosition == MarketPosition.Flat)
    			{
    				
    				// after profit/loss has been calculated, adding to accountSize
    				accountSize += Performance.AllTrades.TradesPerformance.Currency.CumProfit;
    				
    				//set risk to 1% of account Size
    				risk = accountSize * 0.01;
    				
    				//
    				if(0.01 * (EMA3-EMA8)/EMA8)*2) < accountSize * 0.1)
    						shares = 0.01 * ((EMA3-EMA8)/EMA8)*2));
    					else shares = accountSize * 0.1;
    					
    			
    			}
    MatthewNinjaTrader Product Management

    Comment


      #3
      Is this how it would look?

      // Condition set 1
      if (CrossAbove(EMA(3), EMA(8), 1)
      && EMA(8)[0] > SMA(20)[0])

      if(Position.MarketPosition == MarketPosition.Flat)
      {
      // after profit/loss has been calculated, adding to accountSize
      accountSize += Performance.AllTrades.TradesPerformance.Currency.C umProfit;

      //set risk to 1% of account Size
      risk = accountSize * 0.01;

      //
      if(0.01 * ((EMA(3)-(EMA(8))/(EMA(8))*2) < accountSize * 0.1))
      shares = 0.01 * ((EMA(3)-(EMA(8))/(EMA(8))*2);
      else shares = accountSize * 0.1;
      }

      {
      EnterLong(shares, "");
      }

      Its giving me errors

      Comment


        #4
        Hello Kamisyed,

        What are the errors that you are receiving from the script?
        Cal H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DJ888, 04-16-2024, 06:09 PM
        4 responses
        12 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by terofs, Today, 04:18 PM
        0 responses
        11 views
        0 likes
        Last Post terofs
        by terofs
         
        Started by nandhumca, Today, 03:41 PM
        0 responses
        7 views
        0 likes
        Last Post nandhumca  
        Started by The_Sec, Today, 03:37 PM
        0 responses
        3 views
        0 likes
        Last Post The_Sec
        by The_Sec
         
        Started by GwFutures1988, Today, 02:48 PM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Working...
        X