Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
Accessing value from custom indicator from strategy
Collapse
X
-
imported post
What do you mean by "returns a value"? The OnBarUpdate method of an indicator has no return value.
What you probably want to do is calculating a value which you want to make accessible in a strategy. You then need to define a plot series in the indicator wizard, change the OnBarUpdate method of the generated code. Your plot series then is accessible by a strategy.
I suggest strating of wizard generated code and not try to code free-hand.
-
imported post
Bad choice of words -> "returns a value" it doesn't actually RETURN a value.
I guess what I need to do is create a UserDefinedMethod(). Correct?
Here's the code from my indicator that I would like accessible.
protectedoverridevoid OnBarUpdate()
{
if (CurrentBar == 0){
Value.Set(0);
}else{
double d = 0;
d =Math.Abs((CCI(Close, T)[0] - CCI(Close, M)[0]));
Mplot.Set(d);
}
}
Comment
-
imported post
Ok, it is a custom indicator created with the wizard. It works fine. It's called MyDiffCCI(). Plots correctly in the DataBox(which is the whole reason I needed to create it as a seperate indicator). Now, when I try to access it via MyDiffCCI.Mplot[0]; I get this error:
'NinjaTrader.Strategy.Strategy.MyDiffCCI(int, int)' is a 'method', which is not valid in the given context
This is all I'm doing:
Print ("Mplot "+MyDiffCCI.Mplot[0]);
Comment
-
imported post
1. Ok, since I'm not really plotting anything but using the value in my strategy calculations, I see "Dummy" in the DataBox instead of "Mplot", which I would like to see. How do I correct that?
2. Also, in the indicator itself, can you set the "AutoScale", and which panel to display in via NinjaScript? Or is it only accessible via the indicator properties window?
ie:
AutoScale = false;
DisplayInDataBox = true;
Panel = 1;
etc.
Comment
-
imported post
1) As suggested below I recommend you go through the wizard which allows you to set the explicit plot name you want to see. Changing the code manually could be done but is prone to errors.
2) Set Overlay property to determine if the indicator should be displayed on the chart panel or on a seperate panel. The docs hold a good explanation for this property.
Comment
-
Need Help - Stop Loss and problem
Hallo.
I'm new Ninja user. I alredy developed my simple strategy and strategy works well until I tryed to modify stop loss price to breakeven. I used the sample shown here: http://www.ninjatrader-support.com/v...ead.php?t=3222
Now strategy when moves stop loss to breakeven and then reverse postition do not reset stop loss to original position and place stop order with the price from former turn. This price is over the market price what is case of error and strategy termination.
Can anybody help me to explan where the problem is???
Strategy has condition:
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, stoplossticks);
}
but looks it does not work????
Rgds
Czarek
My strategy below:
//
// Copyright (C) 2007, NinjaTrader LLC <www.ninjatrader.com>.
// NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
//
#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.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Sample strategy using StopLoss and ProfitTarget orders.
/// </summary>
[Description("Sample strategy using StopLoss and ProfitTarget orders.")]
public class SamplePriceModification : Strategy
{
#region Variables
private int stoplossticks = 20;
private int profittargetticks = 100;
#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()
{
/* There are several ways you can use SetStopLoss and SetProfitTarget. You can have them set to a currency value
or some sort of calculation mode. Calculation modes available are by percent, price, and ticks. SetStopLoss and
SetProfitTarget will submit real working orders unless you decide to simulate the orders. */
SetStopLoss(CalculationMode.Ticks, stoplossticks);
SetProfitTarget(CalculationMode.Ticks, profittargetticks);
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, stoplossticks);
}
// If a long position is open, allow for stop loss modification to breakeven
else if (Position.MarketPosition == MarketPosition.Long)
{
// Once the price is greater than entry price+50 ticks, set stop loss to breakeven
if (Close[0] > Position.AvgPrice + 50 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
}
}
// Condition set 1
if (Close[1] < Close[0]
&& Close[2] < Close[0]
&& Close[3] < Close[0]
&& Close[4] < Close[0]
&& Close[5] < Close[0]
&& Close[6] < Close[0]
&& Close[7] < Close[0]
&& Close[8] < Close[0]
&& Close[9] < Close[0]
&& Close[10] < Close[0]
&& Open[0] < Close[0]
&& Open[1] < Close[0]
&& Open[2] < Close[0]
&& Open[3] < Close[0]
&& Open[4] < Close[0]
&& Open[5] < Close[0]
&& Open[6] < Close[0]
&& Open[7] < Close[0]
&& Open[8] < Close[0]
&& Open[9] < Close[0]
&& Open[10] < Close[0])
{
EnterLong(1, "");
}
// Condition set 2
if (Close[1] > Close[0]
&& Close[2] > Close[0]
&& Close[3] > Close[0]
&& Close[4] > Close[0]
&& Close[5] > Close[0]
&& Close[6] > Close[0]
&& Close[7] > Close[0]
&& Close[8] > Close[0]
&& Close[9] > Close[0]
&& Close[10] > Close[0]
&& Open[0] > Close[0]
&& Open[1] > Close[0]
&& Open[2] > Close[0]
&& Open[3] > Close[0]
&& Open[4] > Close[0]
&& Open[5] > Close[0]
&& Open[6] > Close[0]
&& Open[7] > Close[0]
&& Open[8] > Close[0]
&& Open[9] > Close[0]
&& Open[10] > Close[0])
{
EnterShort(1, "");
}
}
#region Properties
/// <summary>
/// </summary>
[Description("Numbers of ticks away from entry price for the Stop Loss order")]
[Category("Parameters")]
public int StopLossTicks
{
get { return stoplossticks; }
set { stoplossticks = Math.Max(0, value); }
}
/// <summary>
/// </summary>
[Description("Number of ticks away from entry price for the Profit Target order")]
[Category("Parameters")]
public int ProfitTargetTicks
{
get { return profittargetticks; }
set { profittargetticks = Math.Max(0, value); }
}
#endregion
}
}
Comment
-
First you would need to address issues outlined in this tip: http://www.ninjatrader-support.com/v...ead.php?t=3170Josh P.NinjaTrader Customer Service
Comment
-
Link doesn't go anywhere.
I was trying to review the link referred to by Czarek in post #10, but it doesn't seem to go anywhere. Does anybody know if the link has changed?
Thank you,
Jeff
Comment
-
Go to NinjaTrader Support Forum > NinjaScript Educational Resources Reference Samples
Last topic:Strategy: Modifying the price of stop loss and profit target orders
Czarek
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by algospoke, Yesterday, 06:40 PM
|
2 responses
23 views
0 likes
|
Last Post
by algospoke
Today, 07:04 PM
|
||
Started by ghoul, Today, 06:02 PM
|
3 responses
14 views
0 likes
|
Last Post Today, 06:43 PM | ||
Started by jeronymite, 04-12-2024, 04:26 PM
|
3 responses
45 views
0 likes
|
Last Post
by jeronymite
Yesterday, 10:10 PM
|
||
Started by Barry Milan, Yesterday, 10:35 PM
|
7 responses
22 views
0 likes
|
Last Post Today, 06:16 PM | ||
Started by AttiM, 02-14-2024, 05:20 PM
|
10 responses
181 views
0 likes
|
Last Post
by jeronymite
Today, 06:11 PM
|
Comment