foreach BarsInProgress? - NinjaTrader Programming | futures io social day trading
futures io futures trading


foreach BarsInProgress?
Updated: Views / Replies:1,793 / 11
Created: by ShruggedAtlas Attachments:0

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 
Thread Tools Search this Thread
 

foreach BarsInProgress?

  #1 (permalink)
Atlas
Bloomington
 
Futures Experience: Beginner
Platform: NinjaTrader
Broker/Data: Interactive Brokers
Favorite Futures: Stocks
 
ShruggedAtlas's Avatar
 
Posts: 191 since Apr 2011
Thanks: 78 given, 74 received

foreach BarsInProgress?

Is it possible to use something like a foreach loop on the BarsInProgress? I have a complicated procedure that i need to do for 4 different time frames. it's a lot of code that could be made simpler if I could apply the same code to each data stream.

Any ideas for how this might be done?

the basic idea behind the code is that I'm checking some conditions on each of the 4 time frames, then making an evaluation based on those conditions and then tracking a total score for the 4 different conditions as evaluated together. The part of the code that checks for conditions could be much tidier if it were compressed into a single code block.

any ideas?

"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
Reply With Quote
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Elite Member
Northern Germany
 
Futures Experience: Intermediate
Platform: NT
Favorite Futures: FDAX, CL
 
vvhg's Avatar
 
Posts: 1,583 since Mar 2011
Thanks: 1,016 given, 2,807 received


I think you could do that by just not specifying the barsinprogress, in that case the code fires for all barsinprogress....

Vvhg


Sent from my iPad using Tapatalk HD

Hic Rhodos, hic salta.
Reply With Quote
 
  #4 (permalink)
Atlas
Bloomington
 
Futures Experience: Beginner
Platform: NinjaTrader
Broker/Data: Interactive Brokers
Favorite Futures: Stocks
 
ShruggedAtlas's Avatar
 
Posts: 191 since Apr 2011
Thanks: 78 given, 74 received

Thx vvhg that might work if I work with BarsArray[] but it may end up being too complicated for me to work with. Still not sure. NT support on the NT forum gave me an option that uses a for loop to loop through each BarsInProgress. I couldn't get foreach to work with BarsInProgress so I assumed I couldn't use other loops. Apparently a for loop works just fine.

Code he gave as an example is shown below:


if(CurrentBar < 0 || CurrentBars[1] < 0 || CurrentBars[2] < 0) return;

for (int x = 0; x <= 2; x++)
{
if (BarsInProgress == x)
Print("Closing price for " + Instrument.FullName + ": " + Close[0]);
}


It looks like I'll probably work with this option since it's easy to work with will allow me to change my existing code the least.

Thx for your help!!

"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
Reply With Quote
 
  #5 (permalink)
Elite Member
Northern Germany
 
Futures Experience: Intermediate
Platform: NT
Favorite Futures: FDAX, CL
 
vvhg's Avatar
 
Posts: 1,583 since Mar 2011
Thanks: 1,016 given, 2,807 received


ShruggedAtlas View Post
Thx vvhg that might work if I work with BarsArray[] but it may end up being too complicated for me to work with. Still not sure. NT support on the NT forum gave me an option that uses a for loop to loop through each BarsInProgress. I couldn't get foreach to work with BarsInProgress so I assumed I couldn't use other loops. Apparently a for loop works just fine.

Code he gave as an example is shown below:


if(CurrentBar < 0 || CurrentBars[1] < 0 || CurrentBars[2] < 0) return;

for (int x = 0; x <= 2; x++)
{
if (BarsInProgress == x)
Print("Closing price for " + Instrument.FullName + ": " + Close[0]);
}


It looks like I'll probably work with this option since it's easy to work with will allow me to change my existing code the least.

Thx for your help!!

I'm not a good coder, so I would have to look it up, but I thought that if you don't specify the barsinprogress index, it would fire regardless of the bar series....But I might have that completely wrong.

As to loops, it is often a quick way to code sth, but I have no idea what the optimum would be in regards to syntax performance...but there can be huge performance differences.....
Other loops you could use here would be while loop or do loop with break. Disadvantage with do loops is that you better not forget to exit the loop at some point....

Vvhg


Sent from my iPad using Tapatalk HD

Hic Rhodos, hic salta.
Reply With Quote
 
  #6 (permalink)
Atlas
Bloomington
 
Futures Experience: Beginner
Platform: NinjaTrader
Broker/Data: Interactive Brokers
Favorite Futures: Stocks
 
ShruggedAtlas's Avatar
 
Posts: 191 since Apr 2011
Thanks: 78 given, 74 received

Here is what I have so far...seems to work - needs tweeks

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";
}
else
if(PriceAboveMedian == false && SmaRising == false && PriceToRcDown > 0)
{
LongOrShort = "LONG";
}
else LongOrShort = "NOTRADE";

// set probability of success score based on evaluation
if(LongOrShort == "LONG")
{
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(LongOrShort == "SHORT")
{
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;
}

}

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

Last edited by ShruggedAtlas; February 3rd, 2012 at 05:39 PM.
Reply With Quote
 
  #7 (permalink)
Atlas
Bloomington
 
Futures Experience: Beginner
Platform: NinjaTrader
Broker/Data: Interactive Brokers
Favorite Futures: Stocks
 
ShruggedAtlas's Avatar
 
Posts: 191 since Apr 2011
Thanks: 78 given, 74 received

just imagine the code in the last post...only about 4 times longer

"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
Reply With Quote
 
  #8 (permalink)
 Vendor: www.sharkindicators.com 
Calgary, AB
 
Futures Experience: Intermediate
Platform: NinjaTrader
Favorite Futures: futures, forex
 
Posts: 11 since Aug 2010
Thanks: 1 given, 39 received

BarsInProgress Issue

Hi ShruggedAtlas,

In the code you've provided, the for loop is redundant... so you can get the exact same effect with it removed.

My question is do you:

1) Want to perform your code on ALL time frames all the time (ie: whenever ANY of the timeframes are updated)

or

2) Only perform your code for the timeframe that is currently being updated

If you fall into category 1, then you need a for loop, and you'll have to reference all the closing prices, highs and lows etc by: Closes[x][0], Highs[x][0] etc. (note the plural).

Otherwise, if you're category 2 you can omit the for loop and access your data using Close[0], High[0] (etc) <-- no plural.

OnBarUpdate is called for each BarsInProgress in turn, and the Close, High (etc) DataSeries are changed to reflect the current BarsInProgress.

I assume you're more likely category 2, because you are referencing SMA, EMA and regressionChannel on the main timeframe (BarsInProgres == 0), am I right? Or do you intend to check the regressionChannel on every timeframe?

Jeremy

Reply With Quote
The following user says Thank You to jeremytang for this post:
 
  #9 (permalink)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,653 since Mar 2010
Thanks: 4,226 given, 25,601 received
Forum Reputation: Legendary

Some comments on your code:

You want to calculate your score with each tick by setting CalculateOnBarClose = false. Your score uses relatively complex calculations such as a regression channel.

Are you aware that with this code you recalculate your daily regression channel with each incoming tick producing billions of calculations for a value that only needs to be recalculated once per day?

If you want a trendfilter that calculates the trend from 4 different timeframes, you need either do this in CalculateOnBarClose = true or you need to pulse the calculations, that is

-> execute the trend calculations only every minute
-> but trigger the alerts intra-bar in case that price exceeds one of the channels

But now to your original question. OnBarUpdate() is executed for each BarsInProgress. That means that the code

 
Code
for (int x = 0; x <= 3; x++)
{
       if (BarsInProgress == x)    //for each time frame
       {
           .....
       }
}
does not add anything.

It is simply redundant. If you delete those lines, the code section in brackets will be executed with each BarsInProgress.


Other suggestions:

-> daily bars are not always added at the proper location that is start at the trading day
-> daily data will not be available at the moment when the new session starts, because it has not been compiled then by the daily data provider

So you should probably emulate daily bars by using intraday data.

Reply With Quote
The following user says Thank You to Fat Tails for this post:
 
  #10 (permalink)
Elite Member
Calcutta, India
 
Futures Experience: Intermediate
Platform: ArthaChitra
 
Posts: 278 since Jun 2009
Thanks: 161 given, 259 received



ShruggedAtlas View Post
if(CurrentBar < 0 || CurrentBars[1] < 0 || CurrentBars[2] < 0) return;

for (int x = 0; x <= 2; x++)
{
if (BarsInProgress == x)
Print("Closing price for " + Instrument.FullName + ": " + Close[0]);
}



the above code is same as
if(CurrentBar < 0 || CurrentBars[1] < 0 || CurrentBars[2] < 0) return;
Print("Closing price for " + Instrument.FullName + ": " + Close[0]);


to get the close of the other series just uses the below code"
Print(Closes[1][0]);
Print(Closes[2][0]);

Reply With Quote
The following user says Thank You to bukkan for this post:

Reply



futures io > > > > > foreach BarsInProgress?

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Jigsaw Trading: TBA

Elite only

FuturesTrader71: TBA

Elite only

NinjaTrader: TBA

Jan 18

RandBots: TBA

Jan 23

GFF Brokers & CME Group: Futures & Bitcoin

Elite only

Adam Grimes: TBA

Elite only

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
foreach loop problem gomad NinjaTrader Programming 14 May 17th, 2011 08:40 PM


All times are GMT -4. The time now is 09:28 AM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts
Page generated 2017-12-14 in 0.21 seconds with 19 queries on phoenix via your IP 54.82.56.95