region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class TakeThree : Strategy
{
private double openingPrice; // Declare openingPrice at the class level
region Strategy Parameters
[Range(1, double.MaxValue), NinjaScriptProperty]
public double Target { get; set; } = 80; // Default target in ticks
[Range(1, double.MaxValue), NinjaScriptProperty]
public double StopLoss { get; set; } = 40; // Default stop loss in ticks
#endregion
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"The";
Name = "TakeThree";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
}
else if (State == State.Configure)
{
AddDataSeries("MES 03-24", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] == 0)
{
// Get the opening price of the current bar
openingPrice = Open[0];
}
}
protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
{
// Ensure we are processing market data for the primary instrument
if (marketDataUpdate.MarketDataType != MarketDataType.Last || marketDataUpdate.Instrument != Instrument)
return;
// Get the current price
double currentPrice = marketDataUpdate.Price;
// Calculate the first 10s place above and below the opening price
double first10sPlaceAbove = Math.Ceiling(openingPrice / 10) * 10;
double first10sPlaceBelow = Math.Floor(openingPrice / 10) * 10;
// Check if we have already placed orders
if (Position.MarketPosition == MarketPosition.Flat)
{
// Place a buy order (long position) if the price reaches the first 10s place below
if (currentPrice <= first10sPlaceBelow)
{
EnterLong(Convert.ToInt32(DefaultQuantity));
SetProfitTarget(CalculationMode.Ticks, Target);
SetStopLoss(CalculationMode.Ticks, StopLoss);
}
// Place a sell order (short position) if the price reaches the first 10s place above
else if (currentPrice >= first10sPlaceAbove)
{
EnterShort(Convert.ToInt32(DefaultQuantity));
SetProfitTarget(CalculationMode.Ticks, Target);
SetStopLoss(CalculationMode.Ticks, StopLoss);
}
}
}
}
}
Thank you
Comment