Код:
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Enter the description of your strategy here")]
public class ATMstrategyVstrategy : Strategy
{
#region Variables
// Wizard generated variables
private int _contracts = 1; // Default setting for Contracts
private string _instrumentName = @"CL 12-15"; // Default setting for Instrument
private int fast = 10;
private int slow = 25;
private string atmStrategyId = string.Empty; // Variable to hold the atmStrategyId
private string orderId = string.Empty; // Variable to hold the orderId
private double realizedProfitLoss = 0; // Variable to hold a specific ATM strategy's profit/loss
private double unrealizedProfitLoss= 0; // Variable to hold a strategy's unrealized profit/loss
private double totalPL = 0; // Variable to hold the running total of realized profit/loss
private int timeStart = 100000; // Default setting for TimeStart
private int timeEnd = 230000; // Default setting for TimeEnd
private double dayTakeProfit = 1000;
private double dayStopLoss = 1000;
private bool CheckTime()
{
// Проверка диапазона времени
bool rez=false;
if (timeStart < timeEnd
&& ToTime(Time[0]) >= timeStart
&& ToTime(Time[0]) < timeEnd) rez=true;
if(timeStart > timeEnd) rez=true;
return rez;
}
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
Add( _instrumentName,PeriodType.Minute,1);
SMA(Fast).Plots[0].Pen.Color = Color.Orange;
SMA(Slow).Plots[0].Pen.Color = Color.Green;
Add(SMA(Fast));
Add(SMA(Slow));
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
//На первом тике начала сессии запоминаем суммарный профит(убыток)
if (Bars.FirstBarOfSession && FirstTickOfBar)
priorTradesCumProfit = Performance.AllTrades.TradesPerformance.Currency.CumProfit;
//Сравниваем разницу профита(убытка) на текущий момент и профит(убыток) на начало сессии
if (Performance.AllTrades.TradesPerformance.Currency.CumProfit - priorTradesCumProfit >= dayTakeProfit)
return;
if (Performance.AllTrades.TradesPerformance.Currency.CumProfit - priorTradesCumProfit <= -dayStopLoss)
return;
/* HELP DOCUMENTATION REFERENCE: Please see the Help Guide section "Using ATM Strategies"
Make sure this strategy does not execute against historical data */
if (Historical)
return;
/* Submits an entry limit order at the current low price to initiate an ATM Strategy if both order id and strategy id are in a reset state
**** YOU MUST HAVE AN ATM STRATEGY TEMPLATE NAMED 'AtmStrategyTemplate' CREATED IN NINJATRADER (SUPERDOM FOR EXAMPLE) FOR THIS TO WORK ****/
if (orderId.Length == 0 && atmStrategyId.Length == 0 && Close[0] > Open[0])
{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "ATMstrategyVstrategy", atmStrategyId);
}
// Check for a pending entry order
if (orderId.Length > 0)
{
string[] status = GetAtmStrategyEntryOrderStatus(orderId);
// If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
if (status.GetLength(0) > 0)
{
// If the order state is terminal, reset the order id value
if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
orderId = string.Empty;
}
} // If the strategy has terminated reset the strategy id after doing some profit/loss calculations.
else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
{
/* Retrieve the profit or loss from the just-closed ATM strategy and round it to the nearest tick. This only updates after all targets or stops are filled (the position is flat).
GetAtmStrategyRealizedProfitLoss can return values such as 99.9999999998 or 100.00000000012, which would be rounded to 100. */
realizedProfitLoss = Instrument.MasterInstrument.Round2TickSize(GetAtmStrategyRealizedProfitLoss(atmStrategyId));
// Sum the profit to the running total.
totalPL = totalPL + realizedProfitLoss;
// Reset the strategy id and unrealized profit/loss variables because the strategy is now terminated.
atmStrategyId = string.Empty;
unrealizedProfitLoss = 0;
}
// Make sure atmStrategyId contains a value or GetAtmStrategyUnrealizedProfitLoss() will throw an error.
if (atmStrategyId.Length > 0)
{
unrealizedProfitLoss = Instrument.MasterInstrument.Round2TickSize(GetAtmStrategyUnrealizedProfitLoss(atmStrategyId));
}
// Concatenate all the information and then draw it onto the chart. '\r\n' basically just means new line.
string textToDraw = "Unrealized P/L: " + unrealizedProfitLoss.ToString() + "\r\nRealized P/L: " + totalPL.ToString() + "\r\nLast Strategy's P/L: " + realizedProfitLoss.ToString();
DrawTextFixed("P/L information", textToDraw, TextPosition.BottomRight);
{
if (CrossAbove(SMA(Fast), SMA(Slow), 1))
EnterLong();
else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
EnterShort();
}
{
if(!CheckTime()) return;
}
}
#region Properties
/// <summary>
/// </summary>
[Description("Period for fast MA")]
[GridCategory("Parameters")]
public int Fast
{
get { return fast; }
set { fast = Math.Max(1, value); }
}
/// <summary>
/// </summary>
[Description("Period for slow MA")]
[GridCategory("Parameters")]
public int Slow
{
get { return slow; }
set { slow = Math.Max(1, value); }
}
[Description("Время начала работы робота")]
[GridCategory("Parameters")]
[Gui.Design.DisplayName("\t\t\t\t\t\tTimeStart")]
public int TimeStart
{
get { return timeStart; }
set { timeStart = Math.Max(0, value); }
}
[Description("Время окончания работы робота")]
[GridCategory("Parameters")]
[Gui.Design.DisplayName("\t\t\t\t\t\tTimeEnd")]
public int TimeEnd
{
get { return timeEnd; }
set { timeEnd = Math.Max(0, value); }
}
[Description("Default setting for Instrument")]
[Category("Parameters")]
public string Instrument
{
get { return _instrumentName; }
set { _instrumentName = value; }
}
[Description("The number of contracts to trade")]
[Category("Parameters")]
public int Contracts
{
get { return _contracts; }
set { _contracts = Math.Max(1, value); }
}
[Description("Ограничить профитом в день")]
[GridCategory("parameters")]
public double DayTakeProfit
{
get { return dayTakeProfit; }
set { dayTakeProfit = Math.Max(1, value); }
}
[Description("Ограничить размером потерь в день")]
[GridCategory("parameters")]
public double DayStopLoss
{
get { return dayStopLoss; }
set { dayStopLoss = Math.Max(1, value);}
}
#endregion
}
}
2 ошибки
piorTradesCumProfit CS0103
string for MasterInstrument CS1061
жду ваших советов и поправок
Спасибо.