Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy with Position Sizing for Indirect Forex Rates Help

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

    Strategy with Position Sizing for Indirect Forex Rates Help

    Hi Guys, could somebody help me figure out what I am doing wrong in this strategy? Why am I getting the following error messages (see attached)?

    Code:
    namespace NinjaTrader.Strategy
    {
    [Description("")]
    public class aaaFXMMTest2 : Strategy
    {
        #region Variables
    
    	private double   stopLoss = 10;
    	private double   AccbalNewIR = 0;
    	private double   AccountSize = 100000;
    	private double   Riskf = 1;
    	private double   Psize = 0;
        private DataSeries lastProfit;
        private DataSeries cumulativeLastProfit;
    	
        #endregion
    
    protected override void Initialize()
    	{
    		CalculateOnBarClose = true;
            lastProfit = new DataSeries(this, MaximumBarsLookBack.Infinite);
            cumulativeLastProfit = new DataSeries(this, MaximumBarsLookBack.Infinite);
    	}
     
    protected override void OnBarUpdate()
    {    
        if (CrossAbove(SMA(10), SMA(20), 1))
        {
           PositionSize();
           EnterLong((int)Psize,"");
           SetStopLoss(CalculationMode.Ticks,stopLoss);
        }
    }
    
    #region PositionSizeFunction
    			void PositionSize()
    				{
    					Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count-1];
    					lastProfit.Set(lastTrade.ProfitCurrency * lastTrade.Quantity / lastTrade.Exit);
                        cumulativeLastProfit.Set(cumulativeLastProfit[1] + lastProfit[0]);
                        AccbalNewIR = AccountSize + cumulativeLastProfit;                                                              
    					Psize = Math.Round(AccbalNewIR * Riskf / 100 / stopLoss / TickSize * lastTrade.Exit);
    				}
                
    #endregion
    Attached Files

    #2
    sburtt, the snippet below should compile for you -

    One issue was not accessing any property of the lastTrade execution (.Price for example), the other trying to add a double to a dataseries, that would not work - you need to access particular double of the series and then you could add to it.

    Code:
    private void PositionSize()
    {
    	Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count-1];
    	lastProfit.Set(lastTrade.ProfitCurrency * lastTrade.Quantity / lastTrade.Exit.Price);
                 cumulativeLastProfit.Set(cumulativeLastProfit[1] + lastProfit[0]);
                 AccbalNewIR = AccountSize + cumulativeLastProfit[0];                                                              
    	Psize = Math.Round(AccbalNewIR * Riskf / 100 / stopLoss / TickSize * lastTrade.Exit.Price);
    }
    Last edited by NinjaTrader_Bertrand; 07-24-2013, 06:48 AM.

    Comment


      #3
      Originally posted by NinjaTrader_Bertrand View Post
      sburtt, the snippet below should compile for you -

      One issue was not accessing any property of the lastTrade execution (.Price for example), the other trying to add a double to a dataseries, that would not work - you need to access particular double of the series and then you could add to it.

      Code:
      private void PositionSize()
      {
      	[B]Trade lastTrade = Performance.RealtimeTradesPerformance.RealtimeTrades.Count-1];[/B]
      [B]	lastProfit.Set(lastTrade.ProfitCurrency * lastTrade.Quantity / lastTrade.Exit.Price);[/B]
                   cumulativeLastProfit.Set(cumulativeLastProfit[1] + lastProfit[0]);
                   AccbalNewIR = AccountSize + cumulativeLastProfit[0];                                                              
      	Psize = Math.Round(AccbalNewIR * Riskf / 100 / stopLoss / TickSize * lastTrade.Exit.Price);
      }
      Bertrand, I appreciate your help on this matter. It is still not compiling as I suspect something is wrong in the bold part of the code highlighted above. The compiler is still giving me an error there.

      In the strategy I need to access the last trade profit and last trade exit price, as I need to convert the profit in USD amount by dividing by the exit price and store this value in a database.

      Thanks for your help

      Comment


        #4
        Which exact error would you still get?

        The snippet I posted below will compile, were you attempted to reuse with the exact same syntax?

        Comment


          #5
          Originally posted by NinjaTrader_Bertrand View Post
          Which exact error would you still get?

          The snippet I posted below will compile, were you attempted to reuse with the exact same syntax?
          please see my first attached, i still get the 1st and 3rd error message.
          "Operator "/" cannot be applied to operands of type 'double' and 'NinjaTrader.Cbi.IExecution"
          "Operator "*" cannot be applied to operands of type 'double' and 'NinjaTrader.Cbi.IExecution"

          so exactly the second line of the highlighted bold code i posted earlier.

          Furthermore, I think that also the 1st line has an error as there is a ] that maybe should removed, please check

          Comment


            #6
            'please see my first attached, i still get the 1st and 3rd error message.'

            Sorry, I don't follow - I've posted a corrected, compiling snippet with changes in for you - why would you not use that one then?

            Comment


              #7
              Originally posted by NinjaTrader_Bertrand View Post
              'please see my first attached, i still get the 1st and 3rd error message.'

              Sorry, I don't follow - I've posted a corrected, compiling snippet with changes in for you - why would you not use that one then?
              Bertrand it's not compiling on my pc. Running through the code i see that you might be missing a square bracket, please see below:

              Trade lastTrade = Performance.RealtimeTradesPerformance.RealtimeTrad es.Count-1];

              Comment


                #8
                My apologies, please see my correct snippet in the post - there must have been one opening bracket missed copying this into the forums. Then I'm sure it would compile on your machine as well.

                Comment


                  #9
                  Originally posted by NinjaTrader_Bertrand View Post
                  My apologies, please see my correct snippet in the post - there must have been one opening bracket missed copying this into the forums. Then I'm sure it would compile on your machine as well.
                  unfortunately it's still not compiling properly on my end. This is the error I get:
                  "Operator "/" cannot be applied to operands of type 'double' and 'NinjaTrader.Cbi.IExecution"
                  "Operator "*" cannot be applied to operands of type 'double' and 'NinjaTrader.Cbi.IExecution"

                  and this is the part of the code where the issue occurs:
                  lastProfit.Set(lastTrade.ProfitCurrency * lastTrade.Quantity / lastTrade.Exit.Price);

                  Comment


                    #10
                    Please send me an email to support at ninjatrader dot com with the exact script you're using, as the snippet works fine here for me.

                    Comment


                      #11
                      Originally posted by NinjaTrader_Bertrand View Post
                      Please send me an email to support at ninjatrader dot com with the exact script you're using, as the snippet works fine here for me.
                      Bertrand, so basically now I am running this strategy, it compiles fine, but when I run this in StrategyAnalyser it doesn't generate any trade. This is weird as it should EnterLong on the SMA CrossAbove. I would appreciate if you could have time to look into this and tell me what you think could be the issue.
                      To help you I think it makes sense to explain you what I am trying to achieve with this strategy. I need the void PositionSize() to calculate the Quantity the strategy will Buy for each trade. This Quantity is function of the initial AccountSize(100,000USD) and the Cumulative Profit. However let’s assume I am trading USD/JPY, the Cumulative Profits calculated by NT7 are in JPY amount, not USD amount, hence I need to calculate the exact profit for each trade and divide it by the Exit.Price to convert that amount in USD, for that I have created the lastProfit and cumulativeLastProfit DataSeries to store this information. It all looks correctly coded to me, but I am no expert in programming and the strategy is not returning any trade in back testing, any idea why?

                      Code below:

                      Code:
                      Namespace NinjaTrader.Strategy
                      {
                      [Description("")]
                      public class aaaFXMMTest2 : Strategy
                      {
                          #region Variables
                      
                      	private double   AccbalNewIR = 0;
                      	private double   AccountSize = 100000;
                      	private double   Riskf = 1;
                      	private double   Psize = 0;
                          	private DataSeries lastProfit;
                          	private DataSeries cumulativeLastProfit;
                      	
                          #endregion
                      
                      protected override void Initialize()
                      	{
                      	CalculateOnBarClose = true;
                              	lastProfit = new DataSeries(this, MaximumBarsLookBack.Infinite);
                              	cumulativeLastProfit = new DataSeries(this, MaximumBarsLookBack.Infinite);
                      	}
                       
                      protected override void OnBarUpdate()
                      {    
                          if (CrossAbove(SMA(10), SMA(20), 1))
                          {
                             PositionSize();
                             EnterLong((int)Psize,"");
                             SetStopLoss(CalculationMode.Ticks,50);
                             SetProfitTarget(CalculationMode.Ticks,100);
                          }
                      }
                      
                      #region PositionSizeFunction
                      	private void PositionSize()
                      {
                      Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count-1];
                      lastProfit.Set(lastTrade.ProfitCurrency * lastTrade.Quantity / lastTrade.Exit.Price);
                       cumulativeLastProfit.Set(cumulativeLastProfit[1] + lastProfit[0]);
                       AccbalNewIR = AccountSize + cumulativeLastProfit[0];                                                              
                      Psize = Math.Round(AccbalNewIR * Riskf / 100 / stopLoss / TickSize * lastTrade.Exit.Price);
                      }
                                  
                      #endregion

                      Comment


                        #12
                        Thanks for getting in touch directly via email, I've replied to you already with my thoughts.

                        Comment


                          #13
                          Originally posted by NinjaTrader_Bertrand View Post
                          Thanks for getting in touch directly via email, I've replied to you already with my thoughts.
                          Thanks for taking the time to look into this.

                          1.
                          So basically you suggest that adding this in the OnBarUpdate() method should make the trick?

                          Code:
                          protected override void OnBarUpdate() 
                          { 
                              // Only run on real-time data
                              if (Historical) 
                                   return;
                          2.
                          Would I need to add also?

                          Code:
                          if (Performance.RealtimeTrades.Count < 1)
                          return;
                          3.
                          Finally, I am currently running the strategy for Back-test purpose, so not taking into account real time trade executed. Given this should I be using AllTrades rather than RealtimeTrades in my script?

                          Again thank you

                          Comment


                            #14
                            Your understanding is correct, you would need to have at least one realtime trade if using that snippet.

                            However for backtesting purposes that would not help you either, you need to work with AllTrades then.

                            Comment


                              #15
                              Originally posted by NinjaTrader_Bertrand View Post
                              Your understanding is correct, you would need to have at least one realtime trade if using that snippet.

                              However for backtesting purposes that would not help you either, you need to work with AllTrades then.
                              Unfortunately it's not working, given I need this for BT purpose, I've sent you an email with the code I am running, I am not receiving any error message, but running the code on 12 month 1 min data I get not 1 trade, it's very strange.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              648 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              369 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              108 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              572 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              573 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X