Difference between these 2 lines

Created at 03 Aug 2022, 21:16
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
KU

kurtisnauss

Joined 08.06.2022

Difference between these 2 lines
03 Aug 2022, 21:16


Hi, 

I am new to writing my own codes and a little confused and hard to find any real answers when searching it online.

I would like to know what the difference between these 2 lines of code actually are and what each represents...

 

This is the preceding code that works with the "index" reference.

protected override void OnBar()
{
     int index = Bars.Count - 1;
     Entry(index);
     Exits(index);
}

 

These are the codes that follow and reference the lines above.

private void Entry(int index)
{

Example #1

Bars.ClosePrices[index] > slowMa.Result[index] && Bars.ClosePrices[index - 1] < slowMa.Result[index - 1]

 

Example #2

Bars.ClosePrices[index] > slowMa.Result[index]

 

When optimizing, example 1 produces very little results in terms of total trades and therefore reduces the net profit it produces. 

However, when using example 2, it produces very good results but uses up ALL of my memory and begins not responding to where I have to manually end task. 

 

Now finally when I originally had only this line in the bot it worked fine and never used up a lot of the memory.

fastMa.Result[index] > mediumMa.Result[index] && fastMa.Result[index - 1] < mediumMa.Result[index - 1]

 

I am very confused why it is doing this now that I added the new line.

The only real difference being the "Bars.ClosePrices" crossing over the moving average instead of having the ma crossover.

If someone could clear this up for me or present a solution to this problem it would GREATLY appreciated!! 

Thank you.


@kurtisnauss
Replies

firemyst
04 Aug 2022, 03:56

Bars.ClosePrices[index] > slowMa.Result[index]

This is going to happen on every bar where the close price is greater than the MA. If you have a long uptrend, and are on a small/fast time frame over a long period of back testing, this could happen thousands of times. 

For example:

The condition will evaluate to true for every single one of those bars shown above if your slow MA is the yellow line, because the close price is above the MA.

But that's only in theory since we don't have the whole of your code and don't know what else is happening in your bot.


@firemyst

kurtisnauss
04 Aug 2022, 04:19 ( Updated at: 21 Dec 2023, 09:22 )

RE:

firemyst said:

Bars.ClosePrices[index] > slowMa.Result[index]

This is going to happen on every bar where the close price is greater than the MA. If you have a long uptrend, and are on a small/fast time frame over a long period of back testing, this could happen thousands of times. 

For example:

The condition will evaluate to true for every single one of those bars shown above if your slow MA is the yellow line, because the close price is above the MA.

But that's only in theory since we don't have the whole of your code and don't know what else is happening in your bot.

Yes, that makes sense that would be reading and storing data for every bar that is printed.

Therefore, when writing it as in example #1, does that tell it to only read it one time on the initial cross and then waits until the next time we see it crossover?


@kurtisnauss

firemyst
04 Aug 2022, 04:30 ( Updated at: 21 Dec 2023, 09:22 )

RE: RE:

kurtisnauss said:

firemyst said:

Bars.ClosePrices[index] > slowMa.Result[index]

This is going to happen on every bar where the close price is greater than the MA. If you have a long uptrend, and are on a small/fast time frame over a long period of back testing, this could happen thousands of times. 

For example:

The condition will evaluate to true for every single one of those bars shown above if your slow MA is the yellow line, because the close price is above the MA.

But that's only in theory since we don't have the whole of your code and don't know what else is happening in your bot.

Yes, that makes sense that would be reading and storing data for every bar that is printed.

Therefore, when writing it as in example #1, does that tell it to only read it one time on the initial cross and then waits until the next time we see it crossover?

Yes.

In my above example, it wouldn't happen at all within the screen capture provided.

However, depending on the setting of your MA again, it could still happen quite a few times as shown here from the same chart (just a different MA period):

where you have a (any color) bar that closes below the line followed by a green bar that closes above the line. Those meet the conditions for Example #1.

In this screen capture, I see at least 11 times where that occurs.


@firemyst