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)
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
}
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
Can you help answer these questions from other members on NexusFi?
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