首页 > 解决方案 > 如何让手数脚本与 NinjaTrader 一起使用

问题描述

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

如果最后一笔交易是输家,我已经成功地获得了一个脚本,将交易规模从 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

我认为这样做可能比写出如果最后 2 笔交易失败者,如果最后 3 笔交易失败者一遍又一遍地写出更快的方法。

标签: c#tradingalgorithmic-trading

解决方案


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

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

我包含了 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
    }
}

推荐阅读