• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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.
    JoshNinjaTrader 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
        	}
        JoshNinjaTrader 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)
            {
            	//execute logic intended for historical data
            }
            else if (State == State.Realtime) 
            {
            	//execute logic intended for live data
            }
            JoshNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by pecunianonolet, 11-28-2016, 03:34 AM
            4 responses
            3,135 views
            0 likes
            Last Post NinjaTrader_PaulH  
            Started by contrax, Today, 08:20 AM
            2 responses
            12 views
            0 likes
            Last Post contrax
            by contrax
             
            Started by brownsfan019, Today, 07:54 AM
            1 response
            9 views
            0 likes
            Last Post NinjaTrader_ChristopherJ  
            Started by Amitabha, Today, 04:50 AM
            1 response
            12 views
            0 likes
            Last Post NinjaTrader_Marco  
            Started by JulieG, Today, 12:40 AM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_ChrisSch  
            Working...
            X