NexusFi: Find Your Edge


Home Menu

 





Why is my code using historical data?


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one ShruggedAtlas with 2 posts (1 thanks)
    2. looks_two Quick Summary with 1 posts (0 thanks)
    3. looks_3 traderwerks with 1 posts (3 thanks)
    4. looks_4 NJAMC with 1 posts (1 thanks)
    1. trending_up 1,847 views
    2. thumb_up 5 thanks given
    3. group 2 followers
    1. forum 4 posts
    2. attach_file 1 attachments




 
Search this Thread

Why is my code using historical data?

  #1 (permalink)
 
ShruggedAtlas's Avatar
 ShruggedAtlas 
Bloomington
 
Experience: Beginner
Platform: NinjaTrader
Broker: Interactive Brokers
Trading: Stocks
Posts: 191 since Apr 2011
Thanks Given: 78
Thanks Received: 75

If anyone cares to take the time to look over my code and help me figure out why it is using an accumulation of historical data to make the 'final calculations' rather than recalculating everything tick by tick.

this code is fairly straightforward, it detects a few conditions for each time frame and then is supposed to evaluate market direction and price extreme and then keep track of a score. the score is supposed to represent the "likelyhood" of a change of direction. for example: if everything is bullish and price is at the extremes of the regression channel then the likelihood of success for a short position is higher ie. higher score. The scoring system assigns the following to each time frame:

1 for daily, 2 for hourly, 6 for ten minute and 18 for two minute. Therefore, if price is bullish and reaching upper extremes on all 4 time frames then the maximum score for short (ShortScore) is 27 for at any point in time. My output is showing numbers like 414 which means it's factoring previous ticks on all time frames. I just want one score number and it can never go above 27 for ShortScore and never below -27 for LongScore.

Here is the code:

public class MyCustomStrategy : Strategy
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
#endregion
int LongScore = 0; //keeps track of probablilities for Long success
int ShortScore = 0; //keeps track of probablilities for Short success



protected override void Initialize()
{
CalculateOnBarClose = false; //score must be calculated at each tick
Add(PeriodType.Minute, 10); //adding 10 minute data
Add(PeriodType.Minute, 60); //adding 60 minute data
Add(PeriodType.Day, 1); //adding daily data
}



protected override void OnBarUpdate()
{
if (CurrentBars[0] <= BarsRequired ||
CurrentBars[1] <= BarsRequired ||
CurrentBars[2] <= BarsRequired ||
CurrentBars[3] <= BarsRequired)
return;

int DailyScore = 1; int HourlyScore = 2; //score to be added for each time frame
int TenMinScore = 6; int TwoMinScore = 18; //score to be added for each time frame

for (int x = 0; x <= 3; x++)
{
if (BarsInProgress == x) //for each time frame
{
//local variables used to track indicator states
bool MaUp; //tracks if EMA is above SMA - bullish sign
bool SmaRising; //tracks if SMA is rising
bool PriceAboveMedian; //tracks if price is above the median of regression channel
int PriceToRcUp = 0; //tracks how far price is above upper regression channel
int PriceToRcDown = 0; //tracks how far price is above the lower regression channel
string LongOrShort; //used to track if the entry should be long or short
double[] ChannelSD = new double[] {2, 2.5, 3, 3.5};
//regression channels standard deviations

// check to see if price is above the median of channel - bullish
if (Close[0] > RegressionChannel(150, 2)[0])
{
PriceAboveMedian = true;
}
else PriceAboveMedian = false;

//check to see if longer term trendline is rising - bullish
if (Rising(SMA(25)))
{
SmaRising = true;
}
else SmaRising = false;

//check to see if EMA is above SMA - bullish
if (EMA(8)[0] > SMA(25)[0])
{
MaUp = true;
}
else MaUp = false;

/// for each standard deviation on the regression channels
/// add a number to PriceToRc so we can track where price is
/// relative to the regresion channel extrememes
foreach (double i in ChannelSD)
if(Close[0] > RegressionChannel(150, i).Upper[0])
{
PriceToRcUp = PriceToRcUp + 1;
}

foreach (double i in ChannelSD)
if(Close[0] < RegressionChannel(150, i).Lower[0])
{
PriceToRcDown = PriceToRcDown + 1;
}

//evaluate which direction trade should go - long or short?
if(PriceAboveMedian && SmaRising && MaUp && PriceToRcUp > 0)
{
LongOrShort = "SHORT"; //this line not needed - used for debugging
switch(x)
{
case 0:
LongScore = LongScore + DailyScore;
ShortScore = ShortScore - DailyScore;
break;
case 1:
LongScore = LongScore + HourlyScore;
ShortScore = ShortScore - HourlyScore;
break;
case 2:
LongScore = LongScore + TenMinScore;
ShortScore = ShortScore - TenMinScore;
break;
case 3:
LongScore = LongScore + TwoMinScore;
ShortScore = ShortScore - TwoMinScore;
break;
}
}
else
if(PriceAboveMedian == false && SmaRising == false && PriceToRcDown > 0)
{
LongOrShort = "LONG"; //this line not needed - used for debugging
switch (x)
{
case 0:
LongScore = LongScore - DailyScore;
ShortScore = ShortScore + DailyScore;
break;
case 1:
LongScore = LongScore - HourlyScore;
ShortScore = ShortScore + HourlyScore;
break;
case 2:
LongScore = LongScore - TenMinScore;
ShortScore = ShortScore + TenMinScore;
break;
case 3:
LongScore = LongScore - TwoMinScore;
ShortScore = ShortScore + TwoMinScore;
break;
}
}
else
LongOrShort = "NOTRADE"; //this line not needed - used for debugging
}
}
Print("LongScore is " + LongScore);
Print("ShortScore is " + ShortScore);
}

#region Properties
[Description("")]
[GridCategory("Parameters")]
public int MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion
}
}

"I've missed more than 9,000 shots in my career. I've lost almost 300 games. 26 times, I've been trusted to take the game-winning shot and missed. I've failed over and over and over again in my life. And that is why I succeed."
- Michael Jordan, 5-Time NBA Most Valuable Player, 6-Time NBA Champion
Attached Thumbnails
Click image for larger version

Name:	OUTPUT.JPG
Views:	163
Size:	79.9 KB
ID:	61498  
Visit my NexusFi Trade Journal Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Trade idea based off three indicators.
Traders Hideout
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Exit Strategy
NinjaTrader
How to apply profiles
Traders Hideout
PowerLanguage & EasyLanguage. How to get the platfor …
EasyLanguage Programming
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
29 thanks
Just another trading journal: PA, Wyckoff & Trends
25 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
21 thanks
GFIs1 1 DAX trade per day journal
16 thanks
  #3 (permalink)
 traderwerks   is a Vendor
 
Posts: 692 since Jun 2009
Thanks Given: 436
Thanks Received: 465


Move longScore and shortScore into onbarupdate or reset them to zero there.

Math. A gateway drug to reality.
Reply With Quote
Thanked by:
  #4 (permalink)
 
ShruggedAtlas's Avatar
 ShruggedAtlas 
Bloomington
 
Experience: Beginner
Platform: NinjaTrader
Broker: Interactive Brokers
Trading: Stocks
Posts: 191 since Apr 2011
Thanks Given: 78
Thanks Received: 75

Thx traderwerks!

I see my mistake. I was thinking that those variables needed to be outside the scope of the BarsInProgress but now I see that it needs to be reset after each bar update. Thx. The fix worked.

This is my first substantial programming project so your help was very much appreciated!

"I've missed more than 9,000 shots in my career. I've lost almost 300 games. 26 times, I've been trusted to take the game-winning shot and missed. I've failed over and over and over again in my life. And that is why I succeed."
- Michael Jordan, 5-Time NBA Most Valuable Player, 6-Time NBA Champion
Visit my NexusFi Trade Journal Started this thread Reply With Quote
Thanked by:
  #5 (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


ShruggedAtlas View Post
Thx traderwerks!

I see my mistake. I was thinking that those variables needed to be outside the scope of the BarsInProgress but now I see that it needs to be reset after each bar update. Thx. The fix worked.

This is my first substantial programming project so your help was very much appreciated!

Hi,

I don't know if this will help you in the future but there is a flag that NT sets when it is running Historical Data:
Historical

This can be used to filter bars in the OnBarUpdate() function.

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:




Last Updated on February 14, 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