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

Position sizing rule for backtesting and live trading in the same strategy

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

    Position sizing rule for backtesting and live trading in the same strategy

    Hi
    I'm working on position sizing rule in a strategy. The rule is simple, I want to risk 2% of my capital on every trade.
    So, following the guideline on this forum

    /*
    Using an example of USD/JPY

    1st Step: 100,000*114.28 = 11,428,000
    100,000*114.23 = 11,423,000
    5,000 difference between bid and ask

    2nd Step: Determine Midpoint between bid and ask
    114.28+114.23 = 228.51/2 = 114.255

    3rd Step: Divide the Difference in Japanese yen by the midpoint of the bid/offer
    5000/114.255 = 43.76

    4th Step: Divide the total US dollar difference by the number of pips to attain the average pip value at the current value.
    43.76/5 = $8.75

    Using these steps you can figure the pip value and then determine how many units to trade based on a percentage of your account.
    */

    This involves getting Bid, Ask prices, spread, and midpoint.
    But in my case, I want to develop position sizing rule separately for backtesting and live trading in the same strategy.

    Here is my code
    Code:
            private double histBid = 0;
            private double histAsk = 0;
            private double curBid = 0;
            private double curAsk = 0;
            private double spread = 0;
            private double midPoint = 0;
    
    protected override void OnStateChange()
    
    if (State == State.Historical)
            {
                spread = Math.Abs(100000 * (histBid - histAsk));
                midPoint = Math.Abs (histBid - histAsk)/2;
            }
                        
            else if (State == State.Realtime)
            {
                spread = Math.Abs(100000 * (curBid - curAsk));
                midPoint = Math.Abs(curBid - curAsk)/2;
            }
    
    protected override void OnBarUpdate()
    
    // Get historical Bid, Ask for backtesting
    
            histBid = Bars.GetBid(CurrentBar);
            histAsk = Bars.GetAsk(CurrentBar);
    
    // Get realtime Bid, Ask for live trading
    
            curBid = GetCurrentBid();
            curAsk = GetCurrentAsk();
    How would you suggest the best solution?

    Thank you

    #2
    Hello thebigarch,
    Thanks for your post.

    Just to clarify- are you trying to write you strategy so that it performs differently on a backtest vs running it live?

    Backtests are only on historical data so anything that is written explicitly for State.Realtime would never be used during the backtest.
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Hi Josh
      Just to clarify- are you trying to write you strategy so that it performs differently on a backtest vs running it live?
      Yes I want my strategy to
      1. get historical Bid, Ask prices from historical data in backtesting
      2. get real-time Bid, Ask prices from real-time data feed in live trading

      Thank you

      Comment


        #4
        You could just create a bool switch that is accessible from inside your strategies parameters. You could use it as a condition whether or not you were backtesting and then place your appropriate logic inside those conditions.

        Something like the following snippet is what I mean:

        Code:
        protected override void OnStateChange()
        {
        	if (State == State.SetDefaults)
        	{
        		IsBacktest	= false;
        	}
        }
        protected override void OnBarUpdate()
        {
        	if (IsBacktest == true)
        	{
        		//Backtest Logic Can Go Here
        	}
        	else if (IsBacktest ==false)
        	{
        		//Real-Time Logic Can Go Here
        	}
        Josh G.NinjaTrader Customer Service

        Comment


          #5
          Josh
          Thanks for your solution.
          I'm a newbie as a programmer and totally new to NT.
          I'm just wondering. What if I'd rather automate the process than having to manually switch between backtest and live trading? What's the best way to set up automated different behavior for a strategy in backtest vs live trading? (The key being automated)

          Thank you.
          Regards.
          Last edited by thebigarch; 04-25-2018, 10:11 PM.

          Comment


            #6
            Hello thebigarch,

            I suppose if you did not want to use a switch you could check if you are on historical or live data and separate your logic that way. Something like the following.

            Code:
            if (State == State.Historical)
            {
            	[COLOR="Green"]//execute logic intended for historical data[/COLOR]
            }
            else if (State == State.Realtime) 
            {
            	[COLOR="Green"]//execute logic intended for live data[/COLOR]
            }
            Josh G.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by yertle, Yesterday, 08:38 AM
            7 responses
            28 views
            0 likes
            Last Post yertle
            by yertle
             
            Started by bmartz, 03-12-2024, 06:12 AM
            2 responses
            21 views
            0 likes
            Last Post bmartz
            by bmartz
             
            Started by funk10101, Today, 12:02 AM
            0 responses
            5 views
            0 likes
            Last Post funk10101  
            Started by gravdigaz6, Yesterday, 11:40 PM
            1 response
            9 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by MarianApalaghiei, Yesterday, 10:49 PM
            3 responses
            11 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Working...
            X