# CCIValue as Stop Loss or Take Profit

Hi,

I wrote an EA which uses the CCI Indicator.

When CCIValue > 120 then it opens a Buy Position and if it is <-120 it opens a sell Position

example code:

if(CCIValue> 120)

if(CCIValue< -120)

signal="sell";

--------------------------------------------------

if(signal=="sell" && PositionsTotal()<1)

------------------------------------------------

It is not the complete Code but i think you know what i mean. So now for example i would like to set the take profit point on the sell Position when CCIValue is >-120.

Is this possible?

I tried for example:

if(signal=="sell" && PositionsTotal()<1)

My idea of the EA is that he starts a position when the CCIValue goes in the Oversold or Overbuy Area and closes the trade when it goes back under the 100 values.

Thank you very much for your help

Extract profit down to the last pip
• www.mql5.com
The article describes an attempt to combine theory with practice in the algorithmic trading field. Most of discussions concerning the creation of Trading Systems is connected with the use of historic bars and various indicators applied thereon. This is the most well covered field and thus we will not consider it. Bars represent a very artificial entity; therefore we will work with something closer to proto-data, namely the price ticks.

norman Nething-wilhelm:

```if(...)//close condition

```#include<Trade\Trade.mqh>

string CurrentTime;

void OnTick()

{

double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

MqlRates PriceInfo[];

ArraySetAsSeries(PriceInfo,true);

int PriceData =CopyRates(_Symbol,_Period,0,3,PriceInfo);

string signal="";

double myPriceArray[];

int CCIDefinition =iCCI(_Symbol,_Period,14,PRICE_CLOSE);

ArraySetAsSeries(myPriceArray,true);

CopyBuffer(CCIDefinition,0,0,3,myPriceArray);

float CCIValue=(myPriceArray[0]);

datetime time=TimeLocal();

CurrentTime=TimeToString(time,TIME_MINUTES);

{

if(CCIValue> 120)

if(CCIValue< -120)

signal="sell";

static datetime lastTrade=0; datetime now=TimeCurrent();

if(now - lastTrade >= 0)

{

if(signal=="sell" && PositionsTotal()<1)

}

}

Comment("The current signal is: ",signal);

}

{

}```

Hi,

I did not get it to work. I dont know what i do wrong.

Here again is the code. I simply want to start a buy trade wenn the ccivalue is over 110 and a sell trade when the CCIValue is under -110 .

SL should be around 100 Points and TP for example of the BuyTrade when the CCIValue goes back under 120.

That was it.

PS: Another option was that when one trade is finished the EA should wait minimum 60 Minutes to start a new trade. But in my solution the EA only looks at every full hour if the conditions are fullfilled. What did i do wrong with the time? For example when a trade closes lets say 1510 . Then the EA should only start a new trade at 1610. But in my example the EA only looks at full hours.

norman Nething-wilhelm #:

EA only looks at full hours.

for me it is taking trades in minutes also. Not full hours. Please check your simulation type choice:

Yashar Seyyedin #:

for me it is taking trades in minutes also. Not full hours. Please check your simulation type choice:

```#include<Trade\Trade.mqh>

string CurrentTime;

void OnTick()

{

double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

MqlRates PriceInfo[];

ArraySetAsSeries(PriceInfo,true);

int PriceData =CopyRates(_Symbol,_Period,0,3,PriceInfo);

string signal="";

double myPriceArray[];

int CCIDefinition =iCCI(_Symbol,_Period,14,PRICE_CLOSE);

ArraySetAsSeries(myPriceArray,true);

CopyBuffer(CCIDefinition,0,0,3,myPriceArray);

float CCIValue=(myPriceArray[0]);

datetime time=TimeLocal();

CurrentTime=TimeToString(time,TIME_MINUTES);

{

if(CCIValue> 120)

if(CCIValue< -120)

signal="sell";

static datetime lastTrade=0; datetime now=TimeCurrent();

if(now - lastTrade >= 3600 )

{

if(signal=="sell" && PositionsTotal()<1)

}

}

Comment("The current signal is: ",signal);

}

{

}```

HI ,

First of all thanks for your help. I forgot to change a variable to the standard for 1 hour before i copied the code. Please check with the code above again.

Following statement was wrong for 1h

`if(now - lastTrade >= 3600 )`

I had it on 0 .

When you test it, you can see the code only looks at full hours if the condition is met.

But my Goal is when a trade is closed, the EA should look after the conditions from the beginning and not for example on 2 o clock, 3 o clock and so.. For example when

the conditions are met at 0145 . he doesn't start the trade before 0200 .

I hope you understand what i mean.

Here for example. only on 1600, 1000 , 1400 ... but my goal is that he should start a trade when the conditions like ccivalue above xy is. This should be priority and not the full hour. Now he looks at every full hour, when the ccivalue is above my config he starts a trade. This is false because he should start the trade when it starts to go above the 120 and not only check it on the full hour. I think it is easy to solve but i am hanging there 1 week.

I just want that after every trade the EA should make a "break" befor enter a trade again. So the CCI had a chance to cool down.

Files:
1perhour.JPG  306 kb

norman Nething-wilhelm #: . I dont know what i do wrong.
```   int CCIDefinition =iCCI(_Symbol,_Period,14,PRICE_CLOSE);

ArraySetAsSeries(myPriceArray,true);

CopyBuffer(CCIDefinition,0,0,3,myPriceArray);
```

Perhaps you should read the manual, especially the examples.
How To Ask Questions The Smart Way. (2004)
How To Interpret Answers.
RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
How to call indicators in MQL5 - MQL5 Articles (2010)

William Roeder #:

Perhaps you should read the manual, especially the examples.
How To Ask Questions The Smart Way. (2004)
How To Interpret Answers.
RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
How to call indicators in MQL5 - MQL5 Articles (2010)

Hi,

Thank you very much i will read and try to do it better. But in my example what is the solution with the time problem?

I just want that after a trade is closed, the EA should wait 1hour. After that hour the EA should start from beginning to look after the criteria to start the next trade (cci value) . In my example when a trade is finished for example at 14.45h ,the indicator should start looking after the criterias at 1545 . So when the criteria is met at lets say 1610 then it should open the next position. but in my example it only looks after the criterias at 1600, 1700, 1800 ... and starts the trades only on full hours. So i got many losses.

I think it must be a small correction in the code or not?

Thank you

norman Nething-wilhelm #: But in my example what is the solution with the time problem?

I think it must be a small correction in the code or not?

Creating an indicator per tick is a time problem. The fix is a small correction. Asked and answered.

William Roeder #:

Creating an indicator per tick is a time problem. The fix is a small correction. Asked and answered.

Hi again,

So i tried different ways to solve it but i can't get it work.

Could you help me how i can code it that the EA simply waits after a trade?

For example when a trade is closed, the EA should wait a specific time before opening another trade?

1. norman Nething-wilhelm #: So i tried different ways to solve it but i can't get it work.

What is “it?” Your question was about using CCI.

“Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
How To Ask Questions The Smart Way. (2004)
```void OnTick(){