CCIValue as Stop Loss or Take Profit - page 2

 
William Roeder #:
  1. 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)
              When asking about code
              Be precise and informative about your problem

    Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.

    We can't see your broken code.

Hi William Roeder,


Sorry for the late answer. the last two weeks i had covid. Now i feel a little bit better. Here is my new code. Could you help me were i must put in your suggestion code were the EA should stop?

#include<Trade\Trade.mqh>

CTrade trade;

input string StartTradingTime="06:00";
input string StopTradingTime="10:00";

string CurrentTime;

bool TradingIsAllowed=false;


void OnTick()
  {
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);   
  
   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,20,PRICE_CLOSE);
   
   ArraySetAsSeries(myPriceArray,true);
   
   CopyBuffer(CCIDefinition,0,0,3,myPriceArray);
   
   float CCIValue=(myPriceArray[0]);
   
   datetime time=TimeLocal();
   
   CurrentTime=TimeToString(time,TIME_MINUTES); 
   
   if(CheckTradingTime()==true)
   {
   
   if(CCIValue> 120)
   signal="buy";
   
   if(CCIValue< -120)
   signal="sell";
   
   
   
   if(signal=="sell" && PositionsTotal()<1)
   trade.Sell(1.10,NULL,Bid,(Bid+80*_Point),(Bid-110 * _Point),NULL);
    
   if(signal=="buy" && PositionsTotal()<1)
   trade.Buy(1.10,NULL,Ask,(Ask-80*_Point),(Ask+110 * _Point),NULL);
   
   }
   
      
   
   Comment("The current signal is: ",signal);
   
  }
  
bool CheckTradingTime()
   {
   if(StringSubstr(CurrentTime,0,5)==StartTradingTime)
   TradingIsAllowed=true;
   if(StringSubstr(CurrentTime,0,5)==StopTradingTime)
   TradingIsAllowed=false;
   return TradingIsAllowed;
   }
 
  1. Please use the Styler (Ctrl + , or Extra => Styler) to organize your code: easier to read, faster to change and correct!
  2. Bear in mind there's virtually nothing that hasn't already been programmed for MT4/MT5 and is ready for you.
    A search for cci automated trading will give you turnkey EAs (not just one).
    It's a lot faster then trying every beginner mistake at least once.
  3. If your program compiles without any error but doesn't do what it should use the debugger. It tells you where and why:
    Code debugging:  https://www.metatrader5.com/en/metaeditor/help/development/debug
    Error Handling and Logging in MQL5:  https://www.mql5.com/en/articles/2041
    Tracing, Debugging and Structural Analysis of Source Code, scroll down to: "Launching and Debuggin": https://www.mql5.com/en/articles/272
You gonna be faster and more successful with theses hints - I am sure.
 
  1.    int CCIDefinition =iCCI(_Symbol,_Period,20,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)

  2.    trade.Sell(1.10,NULL,Bid,(Bid+80*_Point),(Bid-110 * _Point),NULL);
        
       trade.Buy(1.10,NULL,Ask,(Ask-80*_Point),(Ask+110 * _Point),NULL);

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  3. input string StartTradingTime="06:00";
    input string StopTradingTime="10:00";

    When dealing with time, a lot of people use strings; they can not be optimized. Using (int) seconds or (double) hours and fraction can be inputs.

    See also Dealing with Time (Part 1): The Basics - MQL5 Articles (2021.10.01)
    Dealing with Time (Part 2): The Functions - MQL5 Articles (2021.10.08)
    MQL5 Programming Basics: Time - MQL5 Articles (2013.04.26)

    Remember to handle the case where start > end (e.g. 2100 … 0200

Reason: