Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
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
Can you help answer these questions from other members on NexusFi?
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.
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
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....
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
}
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;
}
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
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
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?
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
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.