【问题标题】:How to get lot size script to work with NinjaTrader如何让手数脚本与 NinjaTrader 一起使用
【发布时间】:2020-11-11 18:50:00
【问题描述】:

我不是一个很高级的程序员,只是在学习。

如果最后一笔交易是输家,我已经成功地获得了一个将交易规模从 1 份合约增加到 2 份合约的脚本。

我通过声明以下变量来做到这一点

private int OrderQuantity = 1;
    private int OrderQuantityMultiplier1 = 2;`

然后我写了下面的规则

int orderQuantity = OrderQuantity;
            
            if( SystemPerformance.AllTrades.Count > 0 )
        
                if( SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count-1].ProfitTicks < 0 ) // last trade is a loss
                    orderQuantity = Math.Max(1, Convert.ToInt32(orderQuantity * OrderQuantityMultiplier1));
                       EnterLongLimit(orderQuantity, (Low[0] + (1 * TickSize)) ,"");;}

而且效果很好。但后来我想说,如果最后一笔交易是输家,则从 1 手变为 2 手。如果最后 2 笔交易是亏损的,则从 2 手增加到 4 手,依此类推。

我创建了这些变量

private int OrderQuantityMultiplier2 = 4;
    private int OrderQuantityMultiplier3 = 8;
    private int OrderQuantityMultiplier4 = 16;`

并写了这段代码

int orderQuantity = OrderQuantity;
            
            if( SystemPerformance.AllTrades.Count > 0 )
        
                if( SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count-1].ProfitTicks < 0 ) // last trade is a loss
                    orderQuantity = Math.Max(1, Convert.ToInt32(orderQuantity * OrderQuantityMultiplier1));
                       EnterLongLimit(orderQuantity, (Low[0] + (1 * TickSize)) ,"");;}
        
        if( SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count-2].ProfitTicks < 0 ) // last trade is a loss
                    orderQuantity = Math.Max(1, Convert.ToInt32(orderQuantity * OrderQuantityMultiplier2));
                       EnterLongLimit(orderQuantity, (Low[0] + (1 * TickSize)) ,"");;}

但我收到以下错误

NinjaScript 文件错误代码行列 Intraday1.cs 当前上下文中不存在名称“orderQuantity” CS0103 105 7

我认为这样做可能比写出 If last 2 trades lossers 和 if last 3 trades losters 一遍又一遍地写出更快的方法。

【问题讨论】:

    标签: c# trading algorithmic-trading


    【解决方案1】:

    我不确定您是否在询问如何消除错误或其他问题,所以我假设这就是您所需要的。

    您收到的错误看起来像是变量范围的问题,尽管很难说,因为您没有告诉我们您在策略中的哪个位置包含了所显示的代码。尽管如此,您的问题很可能会通过按照我在以下示例中显示的方式声明变量来解决。

    我包含了 NinjaTrader 策略的框架。希望对你有帮助:

    #region Using declarations
    .
    .
    .
    #endregion
    
    //This namespace holds Strategies in this folder and is required. Do not change it. 
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class myStrategy : Strategy
        {
            // STEP 1: Declare here any variables you want to make available for the entire strategy.
            // So, put your variables here like this:
            private int OrderQuantity = 1;
            private int OrderQuantityMultiplier1 = 2;
            private int OrderQuantityMultiplier2 = 4;
            private int OrderQuantityMultiplier4 = 16;
            private int OrderQuantityMultiplier3 = 8;
            // This is the place to declare objects like indicators and Series for example:
            private SMA SMA1;
            private EMA EMA1;
            private Series<double> mInnerMA;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    .
                    .
                    .
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {               
                    // Instatiate Series here:
                    mInnerMA = new Series<double>(this);
                    SMA1 = SMA(Close, Convert.ToInt32(SMA_Period));
                    EMA1 = EMA(Close, Convert.ToInt32(EMA_Period));
                }
            }
    
            protected override void OnBarUpdate()
            {
                // Makes sure we have data for the indicators
                if (BarsInProgress != 0) 
                    return;
    
                if (CurrentBars[0] < 20)
                    return;
    
    
                // Here you develop your strategy and all variables are going to be available
                .
                .
                .   // STEP 2: Make use of any variables defined on STEP 1
    
            }
            
            // This is the place for functions
            private void Function1(int parameter1, double parameter2, ...)
            {
                // This is the place to declare variables that are local for this particular function
                // These variables are not visible for the rest of the code, not even for other functions, 
                // only for this particula function
                int var1;
                string var2;
                .
                .   // STEP 3: Even here you can use the variables defined on STEP 1
                .   // although not a pretty habit, but it will not produce errors
                .
                return (var1);      // This is the only value that will go out from Function1
            }
    
            #region Properties
            // NinjaTrader generated code
            .
            .
            .
            #endregion
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多