Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Can't resolve issue with coding Trailing Stop
Collapse
X
-
Hello,
Thank you for your post.
The syntax for the OnOrderUpdate method is incorrect.
Code:protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment) { }
Please let us know if you have any further questions.
- Likes 1
Leave a comment:
-
Can't resolve issue with coding Trailing Stop
Here is a image of the error I keep getting at "line" 100: "Column" 33: I tried rearranging the order and I just can't get it to compile. Any help would be greatly appreciated. Below I provided the complete code. The code is basic, I am just trying to figure out how to code a trailing stop into the strategy.
Code:
region Using declarations
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.NinjaScript;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript.Strategies;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class BasicShortTrailingStop : Strategy
{
region User Defined Variables
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Trailing Bars Back", Order = 1, GroupName = "Parameters")]
public int TrailingBarsBack { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Initial Stop Ticks", Order = 2, GroupName = "Parameters")]
public int InitialStopTicks { get; set; }
#endregion
private Order stopOrder = null;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "BasicShortTrailingStop";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
BarsRequiredToTrade = 20;
TrailingBarsBack = 1;
InitialStopTicks = 20;
Description = "Enters a short trade on a down bar close and applies a trailing stop.";
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < TrailingBarsBack + 2)
return;
if (Position.MarketPosition == MarketPosition.Flat)
{
if (Close[0] < Open[0])
{
EnterShort("ShortEntry");
Print($"DEBUG: Entering short trade at {Close[0]} on bar {CurrentBar}");
}
}
else if (Position.MarketPosition == MarketPosition.Short)
{
if (stopOrder == null || stopOrder.OrderState == OrderState.Cancelled || stopOrder.OrderState == OrderState.Rejected)
{
double initialStopPrice = Position.AveragePrice + (InitialStopTicks * TickSize);
ExitShortStopMarket(Position.Quantity, initialStopPrice, "StopLoss", "ShortEntry");
Print($"DEBUG: Placed initial stop loss at {initialStopPrice}");
}
else
{
if (Close[TrailingBarsBack] < Open[TrailingBarsBack])
{
double newStopPrice = High[TrailingBarsBack] + TickSize;
if (stopOrder.OrderState == OrderState.Working && newStopPrice < stopOrder.StopPrice)
{
ChangeOrder(stopOrder, stopOrder.Quantity, newStopPrice, 0);
Print($"DEBUG: Updated trailing stop to {newStopPrice} on bar {CurrentBar}");
}
}
}
}
}
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity,
MarketPosition marketPosition, string orderId, DateTime time)
{
if (execution.Order.Name == "ShortEntry" && execution.Order.OrderState == OrderState.Filled)
{
Print($"DEBUG: Short entry filled at {price} on bar {CurrentBar}");
}
if (stopOrder != null && execution.Order == stopOrder && execution.Order.OrderState == OrderState.Filled)
{
stopOrder = null;
Print($"DEBUG: Stop loss hit at {price} on bar {CurrentBar}");
}
}
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity,
int filled, double averageFillPrice, OrderState orderState, string orderId, DateTime time)
{
if (order.Name == "StopLoss")
{
stopOrder = order;
}
}
}
}
Tags: None
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by herzogvladimir2, Today, 08:10 PM
|
0 responses
3 views
0 likes
|
Last Post
![]() |
||
Started by giogio1, 04-13-2025, 01:42 AM
|
2 responses
33 views
0 likes
|
Last Post
![]()
by giogio1
Today, 07:19 PM
|
||
Started by mmenigma, 01-23-2024, 09:37 AM
|
1 response
88 views
0 likes
|
Last Post
![]()
by Nin8aTrender
Today, 03:47 PM
|
||
Started by wbayne333, 02-22-2021, 01:18 PM
|
6 responses
410 views
0 likes
|
Last Post
![]()
by Nin8aTrender
Today, 03:44 PM
|
||
Started by gtheaded, 07-03-2020, 03:47 PM
|
3 responses
370 views
0 likes
|
Last Post
![]()
by Nin8aTrender
Today, 03:21 PM
|
Leave a comment: