Struggling to permanently change string/bool value

 

Hi guys, I'm having some trouble with my code.

At the moment I want to change my string value(also have tried using the bool function, but same problem persists) to "UpTrend" or "DownTrend"


, now the comments on the chart looks good with it's values(You can run on the Strategy Tester visual mode to see what I mean), showing a constant UpTrend/DownTrend comment


, but once I try to use the value I've changed if((Signal=="UpTrend")&&(MidCrossUp)) then it doesn't seem to register at all. Now I'm suspecting the string value reverts back to the NULL value I originally declared it as, but I really have no idea.

I'm new to coding so please bare with me.

Kind Regards

Arno

#include <Trade\Trade.mqh>
CTrade trade;

input int            MACD_Fast=12;
input int            MACD_Slow=26;
input int            MACD_Signal=1;

input string         DayTime_Begin="16:00"; //not included yet
input string         DayTime_End="21:00";

string Signal;             //the signal string in question

void OnTick()
  {
double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);


string Time = TimeToString(TimeCurrent(),TIME_MINUTES);


double MACDMainLine[];
double MACDSignalLine[];
double BBBase[];
double BBUpper[];
double BBLower[];
double ATRArray[];

ArraySetAsSeries(MACDMainLine, true);
ArraySetAsSeries(MACDSignalLine, true);
ArraySetAsSeries(BBBase, true);
ArraySetAsSeries(BBUpper, true);
ArraySetAsSeries(BBLower, true);
ArraySetAsSeries(ATRArray,true);

int MACDDefinition= iMACD(_Symbol,_Period,MACD_Fast,MACD_Slow,MACD_Signal,3);
int BandsDefinition= iBands(_Symbol,_Period,50,0,1.5,MACDDefinition);
int ATRDailyDefinition= iATR(_Symbol,PERIOD_D1,14);

CopyBuffer(MACDDefinition,0,0,3,MACDMainLine);
CopyBuffer(MACDDefinition,1,0,3,MACDSignalLine);
CopyBuffer(BandsDefinition,0,0,3,BBBase);
CopyBuffer(BandsDefinition,1,0,3,BBUpper);
CopyBuffer(BandsDefinition,2,0,3,BBLower);
CopyBuffer(ATRDailyDefinition,0,0,0,ATRArray);

double MACDMain1 = MACDMainLine[1];
double MACDMain2 = MACDMainLine[2];

double MACDSignal1 = MACDSignalLine[1];
double MACDSignal2 = MACDSignalLine[2];

double BBBase1 = BBBase[1];
double BBBase2 = BBBase[2];

double BBUpper1 = BBUpper[1];
double BBUpper2 = BBUpper[2];

double BBLower1 = BBLower[1];
double BBLower2 = BBLower[2];

double ATRValue1 = 500;

//crossovers
double MidCrossDown = (MACDSignal2>BBBase2)&&(MACDSignal1<BBBase1);
double MidCrossUp = (MACDSignal2<BBBase2)&&(MACDSignal1>BBBase1);

double UpperCrossDown = (MACDSignal2>BBUpper2)&&(MACDSignal1<BBUpper1);
double LowerCrossUp = (MACDSignal2<BBLower2)&&(MACDSignal1>BBLower1);




if(UpperCrossDown) //change of the string value
   {
   Signal = "UpTrend";
   };
if(MACDSignal1<BBLower1)   //change of the string value
   {
   Signal = "Downtrend";
   };
  
//buy trades
if((Signal=="UpTrend")&&(MidCrossUp))  //combination of changed string value which doesnt work
{
   trade.Buy(0.1,NULL,Ask,Ask-ATRValue1*_Point,Ask+ATRValue1*_Point,NULL);
}; 

if((Signal=="DownTrend")&&(MidCrossDown)) //combination of changed string value which doesnt work
{
   trade.Sell(0.1,NULL,Bid,Bid+ATRValue1*_Point,Bid-ATRValue1*_Point,NULL);
};  

Comment(MACDSignal1,"\n",
        MACDSignal2,"\n",
        BBBase1,"\n",
        BBBase2,"\n",
        "The Signal is: ",Signal);  
   
  }
 

If your string is correctly assigned and displayed in chart comment, the only reason is that you are using a strange combinations of rules that probably never met in the same time for opening orders.

 
Fabio Cavalloni:

If your string is correctly assigned and displayed in chart comment, the only reason is that you are using a strange combinations of rules that probably never met in the same time for opening orders.

Hi, thank you for the reply. Could you perhaps elaborate?

Regards


The rules are:  if the Signal line crosses the BollingerBand Top line, it is an "UpTrend", then, while there is an "UpTrend" Signal, it wants to search for a crossover UP on the BollingerBand Mid line to buy. The same is true for the opposite Sell Signal.

I see I didnt fill the ATR array as I was supposed to, here is my updated code, but the same problem still persists. Please help me if you can :D


Regards


#include <Trade\Trade.mqh>
CTrade trade;

input int            MACD_Fast=12;
input int            MACD_Slow=26;
input int            MACD_Signal=1;

input string         DayTime_Begin="16:00"; //not included yet
input string         DayTime_End="21:00";

string Signal;             //the signal string in question

void OnTick()
  {
double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);


string Time = TimeToString(TimeCurrent(),TIME_MINUTES);


double MACDMainLine[];
double MACDSignalLine[];
double BBBase[];
double BBUpper[];
double BBLower[];
double ATRArray[];

ArraySetAsSeries(MACDMainLine, true);
ArraySetAsSeries(MACDSignalLine, true);
ArraySetAsSeries(BBBase, true);
ArraySetAsSeries(BBUpper, true);
ArraySetAsSeries(BBLower, true);
ArraySetAsSeries(ATRArray,true);

int MACDDefinition= iMACD(_Symbol,_Period,MACD_Fast,MACD_Slow,MACD_Signal,3);
int BandsDefinition= iBands(_Symbol,_Period,50,0,1.5,MACDDefinition);
int ATRDailyDefinition= iATR(_Symbol,PERIOD_D1,14);

CopyBuffer(MACDDefinition,0,0,3,MACDMainLine);
CopyBuffer(MACDDefinition,1,0,3,MACDSignalLine);
CopyBuffer(BandsDefinition,0,0,3,BBBase);
CopyBuffer(BandsDefinition,1,0,3,BBUpper);
CopyBuffer(BandsDefinition,2,0,3,BBLower);
CopyBuffer(ATRDailyDefinition,0,0,3,ATRArray);

double MACDMain1 = MACDMainLine[1];
double MACDMain2 = MACDMainLine[2];

double MACDSignal1 = MACDSignalLine[1];
double MACDSignal2 = MACDSignalLine[2];

double BBBase1 = BBBase[1];
double BBBase2 = BBBase[2];

double BBUpper1 = BBUpper[1];
double BBUpper2 = BBUpper[2];

double BBLower1 = BBLower[1];
double BBLower2 = BBLower[2];

double ATRValue1 = ATRArray[1];

//crossovers
double MidCrossDown = (MACDSignal2>BBBase2)&&(MACDSignal1<BBBase1);
double MidCrossUp = (MACDSignal2<BBBase2)&&(MACDSignal1>BBBase1);

double UpperCrossDown = (MACDSignal2>BBUpper2)&&(MACDSignal1<BBUpper1);
double LowerCrossUp = (MACDSignal2<BBLower2)&&(MACDSignal1>BBLower1);




if(UpperCrossDown) //change of the string value
   {
   Signal = "UpTrend";
   };
if(LowerCrossUp)   //change of the string value
   {
   Signal = "Downtrend";
   };
  
//buy trades
if((Signal=="UpTrend")&&(MidCrossUp))  //combination of changed string value which doesnt work
{
   trade.Buy(0.1,NULL,Ask,Ask-ATRValue1*_Point,Ask+ATRValue1*_Point,NULL);
}; 

if((Signal=="DownTrend")&&(MidCrossDown)) //combination of changed string value which doesnt work
{
   trade.Sell(0.1,NULL,Bid,Bid+ATRValue1*_Point,Bid-ATRValue1*_Point,NULL);
};  

Comment(MACDSignal1,"\n",
        MACDSignal2,"\n",
        BBBase1,"\n",
        BBBase2,"\n",
        "The Signal is: ",Signal);  
   
  }
 

You are not checking return value of trade.Buy and trade.Sell, it can be that order try to be opened but fails.

Check what that functions return, or add a Print("Open buy order") just before trade.Buy in order to be sure if rules are not met or order open fails.

 
Fabio Cavalloni:

You are not checking return value of trade.Buy and trade.Sell, it can be that order try to be opened but fails.

Check what that functions return, or add a Print("Open buy order") just before trade.Buy in order to be sure if rules are not met or order open fails.

Thanks a lot Fabio! Appreciate it, I saw everything was good except for my buy orders. I'll start making a habit of the Print function in my if functions. 

If you have any tips for bug control, please shoot a message! 

Have an amazing day! Cheers!