NexusFi: Find Your Edge


Home Menu

 





ChartHelper in a Strategy


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one danjuma with 10 posts (1 thanks)
    2. looks_two Traderji with 5 posts (1 thanks)
    3. looks_3 sam028 with 4 posts (4 thanks)
    4. looks_4 gomi with 4 posts (19 thanks)
      Best Posters
    1. looks_one gomi with 4.8 thanks per post
    2. looks_two sam028 with 1 thanks per post
    3. looks_3 cory with 1 thanks per post
    4. looks_4 NJAMC with 1 thanks per post
    1. trending_up 11,676 views
    2. thumb_up 29 thanks given
    3. group 11 followers
    1. forum 27 posts
    2. attach_file 5 attachments




 
Search this Thread

ChartHelper in a Strategy

  #11 (permalink)
 
cory's Avatar
 cory 
virginia
 
Experience: Intermediate
Platform: ninja
Trading: NQ
Posts: 6,098 since Jun 2009
Thanks Given: 877
Thanks Received: 8,090


gomi View Post
I downloaded the NT7 version of Pepe package I could find on ET ( Forums - Software Used to Trade Jack Hershey Methods) and it reloaded up and down channels on chart recreation.

the channel in the package has a diff problem, sometimes when you hit delete key to delete a channel it deletes all channels and removes itself from a chart indicator list.

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
ZombieSqueeze
Platforms and Indicators
REcommedations for programming help
Sierra Chart
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
How to apply profiles
Traders Hideout
 
  #12 (permalink)
 danjuma 
London, UK
 
Experience: Beginner
Platform: NinjaTrader, IB TWS
Broker: IB, Kinetic
Trading: Stocks, Forex
Posts: 98 since Nov 2011
Thanks Given: 47
Thanks Received: 16

@ sam028, @ gomi, or anyone else,

What I am basically looking for is a way to be able to assign the price value I click on on a chart to a variable in an indicator. For example, if I have a variable in my custom indicator named entry price, then anytime I click on a price on the chart (for stocks or forex), this variable is assigned that price value. I could see from searching around that this involves some advance coding which is far beyond my very very basic understanding of NTScripts and C#. May I ask kindly if one you guys could assist with a code for achieving this in an indicator?

Many thanks
Dan

Reply With Quote
  #13 (permalink)
 danjuma 
London, UK
 
Experience: Beginner
Platform: NinjaTrader, IB TWS
Broker: IB, Kinetic
Trading: Stocks, Forex
Posts: 98 since Nov 2011
Thanks Given: 47
Thanks Received: 16



gomi View Post
Here's some code I wrote some time ago, it computes BarNum and Price using dichotomy on

ChartControl.GetXByBarIdx() and
ChartControl.GetYByValue()

I converted it to Strategy and looks OK

Ok, have managed to turn the above (with slight amendment) into an indicator and tested it and it does seem to do what I want - gets the price when I click on the chart. However, I can't seem to be able to figure out how to assign the price to a variable in a code I will eventually insert under "protected override void OnBarUpdate()". Any suggestions please? Thanks.



Quoting 
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using System.Windows.Forms;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;

namespace NinjaTrader.Indicator
{

public class TestGetMouse : Indicator
{
#region Variables
#endregion

/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = false;
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
}

protected override void OnStartUp()
{
this.ChartControl.ChartPanel.MouseUp += new MouseEventHandler(MouseEventH);
}

//declaring a delegate to functions we will dichotomize.
delegate int Function(int x);

//make this negative so the function is increasing
private int MinusGetY(int x)
{
return(-ChartControl.GetYByValue(BarsArray[0],x*this.TickSize));
}

private int GetX(int x)
{
return ChartControl.GetXByBarIdx(BarsArray[0],x);
}


//the dichotomy function, we look for yToFind using paramFunction, starting at xL and ending at xR/
private int IntegerDichotomy(int xL,int xR,Function paramFunction,int yToFind)
{


int yL=paramFunction(xL);
//xl matches
if (yL>=yToFind)
return(xL);

int yR=paramFunction(xR);
//xR matches
if (yR<=yToFind)
return(xR);

if (xL==xR)
return xL;

// if xR=xL+1, we're between 2 points. we choose the closest.
if ((xR-xL)==1)
if (Math.Abs((yToFind-yL))>Math.Abs((yR-yToFind)))
return xR;
else
return xL;

//else we split interval in 2 and start again.
int xM= (xL+xR)/2;
int yM= paramFunction(xM);

if (Math.Sign(yL-yToFind) == Math.Sign((yM-yToFind)))
return IntegerDichotomy(xM,xR,paramFunction,yToFind);
else
return IntegerDichotomy(xL,xM,paramFunction,yToFind);


}
private bool GetBarnumPriceFromMouseClick(ref int barnum, ref double price, int mousex, int mousey)
{
barnum=IntegerDichotomy( 0,CurrentBar,GetX,mousex);

price=IntegerDichotomy( Convert.ToInt32(MIN(CurrentBar)[0]/this.TickSize),Convert.ToInt32(MAX(CurrentBar)[0]/this.TickSize),MinusGetY,-mousey)*this.TickSize;

return true;


}




private void MouseEventH(object sender,MouseEventArgs e)
{
int barnum=0;
double price=0.0;
bool result;

result=GetBarnumPriceFromMouseClick(ref barnum,ref price,e.X,e.Y);

if (result)
DrawDiamond("mousebar", true, CurrentBar-barnum, price, Color.Blue);

DrawTextFixed("Price","Price is " + price, TextPosition.BottomLeft); //my code for now just to see price selected
}

protected override void OnTermination()
{
if (this.ChartControl != null)
{
this.ChartControl.ChartPanel.MouseUp -= MouseEventH;
}
base.Dispose();
}


#region Properties

#endregion
}
}


Reply With Quote
  #14 (permalink)
 
NJAMC's Avatar
 NJAMC 
Atkinson, NH USA
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader 8/TensorFlow
Broker: NinjaTrader Brokerage
Trading: Futures, CL, ES, ZB
Posts: 1,970 since Dec 2010
Thanks Given: 3,037
Thanks Received: 2,395


danjuma View Post
Ok, have managed to turn the above (with slight amendment) into an indicator and tested it and it does seem to do what I want - gets the price when I click on the chart. However, I can't seem to be able to figure out how to assign the price to a variable in a code I will eventually insert under "protected override void OnBarUpdate()". Any suggestions please? Thanks.

Hi,

A quick glance of the function:
 
Code
 
private void MouseEventH(object sender,MouseEventArgs e)
{ 
int barnum=0;
double price=0.0;
bool result;
 
result=GetBarnumPriceFromMouseClick(ref barnum,ref price,e.X,e.Y);
 
if (result)
DrawDiamond("mousebar", true, CurrentBar-barnum, price, Color.Blue);
 
DrawTextFixed("Price","Price is " + price, TextPosition.BottomLeft); //my code for now just to see price selected
}
It looks like "price" is what you are looking to use in your OnUpdate() function. What you should do is remove the local variable reference for price in this function and move it to your "Variables" regeon to make it global.

That should get access to the value, you will need to make sure the value is valid.

Nil per os
-NJAMC [Generic Programmer]

LOM WIKI: NT-Local-Order-Manager-LOM-Guide
Artificial Bee Colony Optimization
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #15 (permalink)
 danjuma 
London, UK
 
Experience: Beginner
Platform: NinjaTrader, IB TWS
Broker: IB, Kinetic
Trading: Stocks, Forex
Posts: 98 since Nov 2011
Thanks Given: 47
Thanks Received: 16


NJAMC View Post
Hi,

A quick glance of the function:
 
Code
 
private void MouseEventH(object sender,MouseEventArgs e)
{ 
int barnum=0;
double price=0.0;
bool result;
 
result=GetBarnumPriceFromMouseClick(ref barnum,ref price,e.X,e.Y);
 
if (result)
DrawDiamond("mousebar", true, CurrentBar-barnum, price, Color.Blue);
 
DrawTextFixed("Price","Price is " + price, TextPosition.BottomLeft); //my code for now just to see price selected
}
It looks like "price" is what you are looking to use in your OnUpdate() function. What you should do is remove the local variable reference for price in this function and move it to your "Variables" regeon to make it global.

That should get access to the value, you will need to make sure the value is valid.

Hi NJAMC,
Thanks for your suggestion. If I understand it correctly, I moved "double price=0.0;" to "Variables" region, and tested it out in a code under ' protected override void OnBarUpdate()', but it did not seem to work (just returning zero as price).
Another issue I forsee, even if I managed to figure out how to reference the price, is that for the code I want to use it for, the code has got a few calculations and so works bets with 'CalculateOnBarClose = true;". Unfortunately, it would appear this gomi's code I trying to use to get the price works with 'CalculateOnBarClose = false:". Tried it with 'CalculateOnBarClose = true;" and it did not seem to work properly. So, I have got a bit of problem there as well!

Cheers
Dan

Reply With Quote
Thanked by:
  #16 (permalink)
 Traderji 
Australia
 
Experience: Advanced
Platform: NinjaTrader, Multicharts
Trading: Spot Forex, Gold, Silver
Posts: 176 since Oct 2010
Thanks Given: 114
Thanks Received: 114


danjuma View Post
Hi NJAMC,
Thanks for your suggestion. If I understand it correctly, I moved "double price=0.0;" to "Variables" region, and tested it out in a code under ' protected override void OnBarUpdate()', but it did not seem to work (just returning zero as price).
Another issue I forsee, even if I managed to figure out how to reference the price, is that for the code I want to use it for, the code has got a few calculations and so works bets with 'CalculateOnBarClose = true;". Unfortunately, it would appear this gomi's code I trying to use to get the price works with 'CalculateOnBarClose = false:". Tried it with 'CalculateOnBarClose = true;" and it did not seem to work properly. So, I have got a bit of problem there as well!

Cheers
Dan


Maybe a simpler solution might be to add a draw object, like Horizontal Line, to the chart at the price you want. For example press F6 and click on the chart to add a Horizontal Line at whatever price you want. Then it is easy enough to just look at the Y-axis value of that drawing object to get the price.

 
Code
foreach (IDrawObject draw in DrawObjects)
{
	if (draw.DrawType == DrawType.HorizontalLine && draw.UserDrawn)
	{
		IHorizontalLine hLine = (IHorizontalLine) draw;
		yvalue = Math.Round(hLine.Y, 5);    // This is Price value
	}
}


Reply With Quote
Thanked by:
  #17 (permalink)
 danjuma 
London, UK
 
Experience: Beginner
Platform: NinjaTrader, IB TWS
Broker: IB, Kinetic
Trading: Stocks, Forex
Posts: 98 since Nov 2011
Thanks Given: 47
Thanks Received: 16


Traderji View Post
Maybe a simpler solution might be to add a draw object, like Horizontal Line, to the chart at the price you want. For example press F6 and click on the chart to add a Horizontal Line at whatever price you want. Then it is easy enough to just look at the Y-axis value of that drawing object to get the price.

 
Code
foreach (IDrawObject draw in DrawObjects)
{
    if (draw.DrawType == DrawType.HorizontalLine && draw.UserDrawn)
    {
        IHorizontalLine hLine = (IHorizontalLine) draw;
        yvalue = Math.Round(hLine.Y, 5);    // This is Price value
    }
}


Sometimes one post replies on this forum and for some reason it does not show. I posted a reply to this post yesterday to say thank you very much Traderji, as your suggestion seems to have addressed my needs. I am most grateful. Also thanks for helping out with a request on NT forum (as rabcnesbit).

Reply With Quote
  #18 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,463 since Jun 2009
Thanks Given: 33,236
Thanks Received: 101,658


danjuma View Post
Sometimes one post replies on this forum and for some reason it does not show. I posted a reply to this post yesterday to say thank you very much Traderji, as your suggestion seems to have addressed my needs. I am most grateful. Also thanks for helping out with a request on NT forum (as rabcnesbit).

You are probably previewing instead of submitting.

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #19 (permalink)
 danjuma 
London, UK
 
Experience: Beginner
Platform: NinjaTrader, IB TWS
Broker: IB, Kinetic
Trading: Stocks, Forex
Posts: 98 since Nov 2011
Thanks Given: 47
Thanks Received: 16

Hello again Traderji,

A favour please if I may ask. Like I mentioned before, your suggestion/code serves my purpose. Only issue is that if I change stocks on the chart, if the new stock's price is not within the price range of the old stock's price, the new chart (all the bars) get compressed, if you know what I mean. I then have to draw a new line within the price range of the new stock and reload (F5) for the chart to come back to normal. Below is attached two charts with different stocks for illustration. Chart on the left is the old stock, and chart on the right is how the chart looks after switching to a new stock. Any suggestions as to how one could get it to adjust automatically after switching to a new stock, please?

Many thanks
Dan.

Attached Thumbnails
Click image for larger version

Name:	Example.jpg
Views:	163
Size:	137.6 KB
ID:	66178   Click image for larger version

Name:	Example2.jpg
Views:	121
Size:	116.4 KB
ID:	66179  
Reply With Quote
  #20 (permalink)
 Traderji 
Australia
 
Experience: Advanced
Platform: NinjaTrader, Multicharts
Trading: Spot Forex, Gold, Silver
Posts: 176 since Oct 2010
Thanks Given: 114
Thanks Received: 114



danjuma View Post
Hello again Traderji,

A favour please if I may ask. Like I mentioned before, your suggestion/code serves my purpose. Only issue is that if I change stocks on the chart, if the new stock's price is not within the price range of the old stock's price, the new chart (all the bars) get compressed, if you know what I mean. I then have to draw a new line within the price range of the new stock and reload (F5) for the chart to come back to normal. Below is attached two charts with different stocks for illustration. Chart on the left is the old stock, and chart on the right is how the chart looks after switching to a new stock. Any suggestions as to how one could get it to adjust automatically after switching to a new stock, please?

Many thanks
Dan.


You could have your code remove the horizontal line(s) from the chart if the instrument changes or once it has obtained the price (and replace it with a text marker).

Reply With Quote




Last Updated on March 18, 2012


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts