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 Traderji with 0.2 thanks per post
    1. trending_up 11,395 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

  #21 (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
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).

Hello Traderji,

Sorry I was not able to reply sooner. Been working for a living and doing some unsociable hours Thanks for your reply and suggestion. I am not too sure how to do this, as my NTScript/C# skills are limited to just nicking snippets of codes here and there and trial and error If you do have the time to come up with a sample code to do what you have suggested above, I would be most grateful. If you can't, no problem, I am still very grateful for your assistance so far.

Many thanks
Dan

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Deepmoney LLM
Elite Quantitative GenAI/LLM
Better Renko Gaps
The Elite Circle
NexusFi Journal Challenge - April 2024
Feedback and Announcements
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
Exit Strategy
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
59 thanks
Funded Trader platforms
37 thanks
NexusFi site changelog and issues/problem reporting
23 thanks
GFIs1 1 DAX trade per day journal
22 thanks
The Program
19 thanks
  #22 (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 Traderji,

Sorry I was not able to reply sooner. Been working for a living and doing some unsociable hours Thanks for your reply and suggestion. I am not too sure how to do this, as my NTScript/C# skills are limited to just nicking snippets of codes here and there and trial and error If you do have the time to come up with a sample code to do what you have suggested above, I would be most grateful. If you can't, no problem, I am still very grateful for your assistance so far.

Many thanks
Dan


I have basic C# skills, just enough to be dangerous. But I'll point you in the right direction.

This code should remove all the user drawn horizontal lines if the instrument changes. I haven't tried to run it or compile it and it might be all horribly wrong so good luck.


 
Code
//Variables
string chartInstrument;

// On Bar Update
protected override void OnBarUpdate()
{
     if (chartInstrument == null)
     {
          chartInstrument = Instrument.FullName;
      }
      else
      {
          if ( chartInstrument != Instrument.FullName)
          {
                foreach (IDrawObject draw in DrawObjects)
               {
	            if (draw.DrawType == DrawType.HorizontalLine && draw.UserDrawn)
	            {
		            IHorizontalLine hLine = (IHorizontalLine) draw;
		            RemoveDrawObject(hLine);
	            }
                }
                chartInstrument = Instrument.FullName;
            }
      }
  }

Reply With Quote
  #23 (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
I have basic C# skills, just enough to be dangerous. But I'll point you in the right direction.

This code should remove all the user drawn horizontal lines if the instrument changes. I haven't tried to run it or compile it and it might be all horribly wrong so good luck.


 
Code
//Variables
string chartInstrument;

// On Bar Update
protected override void OnBarUpdate()
{
     if (chartInstrument == null)
     {
          chartInstrument = Instrument.FullName;
      }
      else
      {
          if ( chartInstrument != Instrument.FullName)
          {
                foreach (IDrawObject draw in DrawObjects)
               {
                if (draw.DrawType == DrawType.HorizontalLine && draw.UserDrawn)
                {
                    IHorizontalLine hLine = (IHorizontalLine) draw;
                    RemoveDrawObject(hLine);
                }
                }
                chartInstrument = Instrument.FullName;
            }
      }
  }

Hi Traderji,

Thanks for the reply. I have tried your code above, but it didn't seem to work. I am switching instruments via Market Analyser, if that makes any difference. Thanks
Dan

Reply With Quote
  #24 (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 Traderji,

Thanks for the reply. I have tried your code above, but it didn't seem to work. I am switching instruments via Market Analyser, if that makes any difference. Thanks
Dan


I just realised that when I change instruments on my charts NT does not carry over any drawing objects from the previous instrument to the new instrument. So if I have a marker in EURUSD and I switch to AUDUSD that marker will go away automatically. So I don't know how come your horizontal line objects are persisting even after you have changed instruments on the chart.

Reply With Quote
  #25 (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
I just realised that when I change instruments on my charts NT does not carry over any drawing objects from the previous instrument to the new instrument. So if I have a marker in EURUSD and I switch to AUDUSD that marker will go away automatically. So I don't know how come your horizontal line objects are persisting even after you have changed instruments on the chart.

Hi Traderji
Thanks for your reply. Below is my code. The only thing I have added is your suggested code for getting the price under protected override void OnStartUp(). Basically, my code without your suggested code does not seem to have this problem, so not sure what the problem might be. I am using NT version 7.0.1000.8
Anyway, I am still grateful for your code and assistance, and I will just have to live with having to manually remove the horizontal lines anytime I change instruments, if I can't come up with a solution.

Thanks
Dan




Quoting 
protected override void Initialize()
{
Overlay = false;


}
protected override void OnStartUp()
{

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

}


}

protected override void OnBarUpdate()
{

if (CurrentBar < 1)
return;

double StopLossP, StopLossPATR, StopLossP2, StopLossPATR2, SharesMxS, SharesMxS2, SharesATR, SharesATR2, SharesPC, SharesPC2;


if (yvalue > Close[0])
{
StopLossP = yvalue - LossPerUnit;
StopLossPATR = Math.Round(yvalue - ATR(14)[0],2);
SharesATR = Math.Round(Math.Abs(LossPerTrade/(yvalue - StopLossPATR)),2);
SharesPC = Math.Round(Math.Abs(LossPerTrade/(yvalue - Low[0])),2);
SharesMxS = Math.Round(Math.Abs(LossPerTrade/(yvalue - StopLossP)),2);
DrawHorizontalLine("Previous candle stop", true, Low[0], Color.Yellow, DashStyle.Dot, 2);
DrawHorizontalLine("Maximum stop", true, StopLossP, Color.Silver, DashStyle.Solid, 2);
DrawHorizontalLine("ATR stop", true, StopLossPATR, Color.Silver, DashStyle.Dash, 2);
DrawTextFixed("Stop Values", "MaxStop " + StopLossP + " (" + SharesMxS + ")" + " ATRStop " + StopLossPATR + " (" + SharesATR + ")" + " Prior CandleStop " + Low[0] + " (" + SharesPC + ")" + " Entry Price " + yvalue, TextPosition.BottomLeft);

}
if (yvalue < Close[0])
{
StopLossP2 = yvalue + LossPerUnit;
StopLossPATR2 = Math.Round(yvalue + ATR(14)[0],2);
SharesATR2 = Math.Round(Math.Abs(LossPerTrade/(yvalue - StopLossPATR2)),2);
SharesPC2 = Math.Round(Math.Abs(LossPerTrade/(yvalue - High[0])),2);
SharesMxS2 = Math.Round(Math.Abs(LossPerTrade/(yvalue - StopLossP2)),2);
DrawHorizontalLine("Previous candle stop", true, High[0], Color.Yellow, DashStyle.Dot, 2);
DrawHorizontalLine("Maximum stop", true, StopLossP2, Color.Silver, DashStyle.Solid, 2);
DrawHorizontalLine("ATR stop", true, StopLossPATR2, Color.Silver, DashStyle.Dash, 2);
DrawTextFixed("Stop Values", "MaxStop " + StopLossP2 + " (" + SharesMxS2 + ")" + " ATRStop " + StopLossPATR2 + " (" + SharesATR2 + ")" + " Prior Candle Stop " + High[0] + " (" + SharesPC2 + ")" + " Entry Price " + yvalue, TextPosition.BottomLeft);

}


Reply With Quote
  #26 (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

Ok, I think it's probably to do with with calculations in my formula, as even when I manually remove the horizontal lines, same thing happens. I think it's to do with my price value defaulting to 0, if the price range of the new instrument is not within the price range of the last instrument on the chart when the horizontal line was drawn and from which the price value got.

Cheers
Dan

Reply With Quote
  #27 (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
Ok, I think it's probably to do with with calculations in my formula, as even when I manually remove the horizontal lines, same thing happens. I think it's to do with my price value defaulting to 0, if the price range of the new instrument is not within the price range of the last instrument on the chart when the horizontal line was drawn and from which the price value got.

Cheers
Dan


quite easy to fix. Move my code to OnBarUpdate instead of OnStartUp. Also add something to the horizontallines you are drawing in your calculation code so that it does not grab yvalues from those. For example

 
Code
DrawHorizontalLine("DONOTUSEPrevious candle stop", true, Low[0], Color.Yellow, DashStyle.Dot, 2);
This way you can check and exclude any hline which has been drawn by your calculations


 
Code
foreach (IDrawObject draw in DrawObjects)
{
if (draw.DrawType == DrawType.HorizontalLine && draw.UserDrawn && !(draw.Tag.Contains("DONOTUSE")) {
IHorizontalLine hLine = (IHorizontalLine) draw; yvalue = Math.Round(hLine.Y, 2); // This is Price value RemoveDrawObject(hLine);
}
}

Reply With Quote
  #28 (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

Hi Traderji,

Thank you for your response, time and efforts, I am most grateful. I tried your suggestion above (first code below), but it didn't seem to make any difference. So working on my theory that the cause is probably due to the price value defaulting to zero when I change instruments, and getting some ideas from your previous codes, I came up with the following code (second code below), and it seems to work.

First code

Quoting 
protected override void Initialize()
{
Overlay = false;


}
protected override void OnStartUp()
{


}

protected override void OnBarUpdate()
{

if (CurrentBar < 1)
return;

foreach (IDrawObject draw in DrawObjects)
{
if (draw.DrawType == DrawType.HorizontalLine && draw.UserDrawn && !draw.Tag.Contains("DONOTUSE"))
{
IHorizontalLine hLine = (IHorizontalLine) draw;
yvalue = Math.Round(hLine.Y, 2); // This is Price value
RemoveDrawObject(hLine);
}

}

double StopLossP, StopLossPATR, StopLossP2, StopLossPATR2, SharesMxS, SharesMxS2, SharesATR, SharesATR2, SharesPC, SharesPC2;


if (yvalue > Close[0])
{
StopLossP = yvalue - LossPerUnit;
StopLossPATR = Math.Round(yvalue - ATR(14)[0],2);
SharesATR = Math.Round(Math.Abs(LossPerTrade/(yvalue - StopLossPATR)),2);
SharesPC = Math.Round(Math.Abs(LossPerTrade/(yvalue - Low[0])),2);
SharesMxS = Math.Round(Math.Abs(LossPerTrade/(yvalue - StopLossP)),2);
DrawHorizontalLine("DONOTUSEPrevious candle stop", true, Low[0], Color.Yellow, DashStyle.Dot, 2);
DrawHorizontalLine("DONOTUSEMaximum stop", true, StopLossP, Color.Silver, DashStyle.Solid, 2);
DrawHorizontalLine("DONOTUSEATR stop", true, StopLossPATR, Color.Silver, DashStyle.Dash, 2);
DrawTextFixed("Stop Values", "MaxStop " + StopLossP + " (" + SharesMxS + ")" + " ATRStop " + StopLossPATR + " (" + SharesATR + ")" + " Prior CandleStop " + Low[0] + " (" + SharesPC + ")" + " Entry Price " + yvalue, TextPosition.BottomLeft);

}
if (yvalue < Close[0])
{
StopLossP2 = yvalue + LossPerUnit;
StopLossPATR2 = Math.Round(yvalue + ATR(14)[0],2);
SharesATR2 = Math.Round(Math.Abs(LossPerTrade/(yvalue - StopLossPATR2)),2);
SharesPC2 = Math.Round(Math.Abs(LossPerTrade/(yvalue - High[0])),2);
SharesMxS2 = Math.Round(Math.Abs(LossPerTrade/(yvalue - StopLossP2)),2);
DrawHorizontalLine("DONOTUSEPrevious candle stop", true, High[0], Color.Yellow, DashStyle.Dot, 2);
DrawHorizontalLine("DONOTUSEMaximum stop", true, StopLossP2, Color.Silver, DashStyle.Solid, 2);
DrawHorizontalLine("DONOTUSEATR stop", true, StopLossPATR2, Color.Silver, DashStyle.Dash, 2);
DrawTextFixed("Stop Values", "MaxStop " + StopLossP2 + " (" + SharesMxS2 + ")" + " ATRStop " + StopLossPATR2 + " (" + SharesATR2 + ")" + " Prior Candle Stop " + High[0] + " (" + SharesPC2 + ")" + " Entry Price " + yvalue, TextPosition.BottomLeft);

}

Second code

Quoting 
protected override void Initialize()
{
Overlay = false;


}
protected override void OnStartUp()
{

if (chartInstrument == null)
{
chartInstrument = Instrument.FullName;
yvalue = Close[0];
}

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

}

}

protected override void OnBarUpdate()
{

if (CurrentBar < 1)
return;


double StopLossP, StopLossPATR, StopLossP2, StopLossPATR2, SharesMxS, SharesMxS2, SharesATR, SharesATR2, SharesPC, SharesPC2;


if (yvalue > Close[0])
{
StopLossP = yvalue - LossPerUnit;
StopLossPATR = Math.Round(yvalue - ATR(14)[0],2);
SharesATR = Math.Round(Math.Abs(LossPerTrade/(yvalue - StopLossPATR)),2);
SharesPC = Math.Round(Math.Abs(LossPerTrade/(yvalue - Low[0])),2);
SharesMxS = Math.Round(Math.Abs(LossPerTrade/(yvalue - StopLossP)),2);
DrawHorizontalLine("Previous candle stop", true, Low[0], Color.Yellow, DashStyle.Dot, 2);
DrawHorizontalLine("Maximum stop", true, StopLossP, Color.Silver, DashStyle.Solid, 2);
DrawHorizontalLine("ATR stop", true, StopLossPATR, Color.Silver, DashStyle.Dash, 2);
DrawTextFixed("Stop Values", "MaxStop " + StopLossP + " (" + SharesMxS + ")" + " ATRStop " + StopLossPATR + " (" + SharesATR + ")" + " Prior CandleStop " + Low[0] + " (" + SharesPC + ")" + " Entry Price " + yvalue, TextPosition.BottomLeft);

}
if (yvalue < Close[0])
{
StopLossP2 = yvalue + LossPerUnit;
StopLossPATR2 = Math.Round(yvalue + ATR(14)[0],2);
SharesATR2 = Math.Round(Math.Abs(LossPerTrade/(yvalue - StopLossPATR2)),2);
SharesPC2 = Math.Round(Math.Abs(LossPerTrade/(yvalue - High[0])),2);
SharesMxS2 = Math.Round(Math.Abs(LossPerTrade/(yvalue - StopLossP2)),2);
DrawHorizontalLine("Previous candle stop", true, High[0], Color.Yellow, DashStyle.Dot, 2);
DrawHorizontalLine("Maximum stop", true, StopLossP2, Color.Silver, DashStyle.Solid, 2);
DrawHorizontalLine("ATR stop", true, StopLossPATR2, Color.Silver, DashStyle.Dash, 2);
DrawTextFixed("Stop Values", "MaxStop " + StopLossP2 + " (" + SharesMxS2 + ")" + " ATRStop " + StopLossPATR2 + " (" + SharesATR2 + ")" + " Prior Candle Stop " + High[0] + " (" + SharesPC2 + ")" + " Entry Price " + yvalue, TextPosition.BottomLeft);

}


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