View Full Version : Accessing value from custom indicator from strategy
funk101
04-15-2007, 06:49 PM
I have a custom indicator that returns a value. How do I access that value from my strategy?
NinjaTrader_Dierk
04-15-2007, 07:31 PM
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.
funk101
04-16-2007, 02:02 AM
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.
protected override void 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);
}
}
NinjaTrader_Dierk
04-16-2007, 02:25 AM
Please be as specific as possible when you report an problem: What is the problem you get? Do you get an error message? Where?
Speculating: If you have generated "Mplot" by the indicator wizard, then you can access the value by <YourIndicatorsName>.Mplot[0].
funk101
04-16-2007, 02:50 AM
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]);
NinjaTrader_Dierk
04-16-2007, 02:55 AM
Your indicator has 2 integer parameters which you need to apply:
Print ("Mplot "+MyDiffCCI(<param1>, <param2>).Mplot[0]);
You need to set actual values for these parameters.
funk101
04-16-2007, 03:10 AM
Ok, I got it. Thanks.
funk101
04-16-2007, 03:28 AM
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.
NinjaTrader_Dierk
04-16-2007, 03:35 AM
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.
Czarek
07-26-2008, 02:42 AM
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 (http://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
}
}
NinjaTrader_Josh
07-26-2008, 04:14 AM
First you would need to address issues outlined in this tip: http://www.ninjatrader-support.com/vb/showthread.php?t=3170
Jeff 15
08-19-2008, 07:50 AM
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
http://www.ninjatrader-support.com/v...ead.php?t=3222
Czarek
08-19-2008, 08:41 AM
Go to NinjaTrader Support Forum (http://www.ninjatrader-support.com/vb/index.php) > NinjaScript Educational Resources (http://www.ninjatrader-support.com/vb/forumdisplay.php?f=29) Reference Samples
Last topic:Strategy: Modifying the price of stop loss and profit target orders (http://www.ninjatrader-support.com/vb/showthread.php?t=3222)
Czarek