using System;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.Strategies;
public class reveralsbottest4 : Strategy
{
private double accountSize = 500;
private double commission = 1.50;
private double maxAccountLoss = 0.1;
private double trailingStopPercent = 0.2;
private Order buyOrder;
private Order sellOrder;
private Pivots pivots;
private Pivots PivotsRange;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "reveralsbottest4";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.UniqueEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 2;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
BarsRequiredToTrade = 20;
}
else if (State == State.Configure)
{
AddDataSeries(BarsPeriodType.Minute, 10);
}
else if (State == State.DataLoaded)
{
double value = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).Pp[0];
AddChartIndicator(pivots);
}
}
protected override void OnBarUpdate()
{
if (CurrentBars[0] < BarsRequiredToTrade)
return;
if (BarsInProgress != 0)
return;
if (Position.MarketPosition == MarketPosition.Flat)
{
if (Close[0] > pivots.R1[0])
{
buyOrder = EnterLongStopMarket(Close[0] + TickSize);
}
else if (Close[0] < pivots.S1[0])
{
sellOrder = EnterShortStopMarket(Close[0] - TickSize);
}
}
if (Position.MarketPosition != MarketPosition.Flat)
{
double profit = Position.Quantity * (Close[0] - Position.AveragePrice) * (Position.MarketPosition == MarketPosition.Long ? 1 : -1) * Bars.Instrument.MasterInstrument.PointValue - commission;
double maxLoss = -accountSize * maxAccountLoss;
if (profit > 0)
{
SetStopLoss(CalculationMode.Percent, trailingStopPercent);
}
else if (profit <= maxLoss)
{
ExitLong();
ExitShort();
}
}
}
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
if (execution.Order == buyOrder)
{
buyOrder = null;
}
else if (execution.Order == sellOrder)
{
sellOrder = null;
}
}
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice,int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
{
if (order.OrderState == OrderState.Rejected)
{
Log(string.Format("Order Rejected: {0}", order.ToString()), LogLevel.Error);
}
}
}
Comment