Programming logic

 

Oscillator


Please i  need help with logic. I created an ea to buy sell but I'm finding it hard to get the logic right. 

my problem is when I use all conditions inside of if statement it doesn't work properly on chart 

To validate buy;

  1. Green line <= 32, 
  2. Green line must go below red line , 
  3. Green line below blue line (optional); 
  4. Yellow line <=50 (optional)

Execute buy trade when green line crosses above red line. 

To validate sell;

  1. Green line >= 68
  2. Green line must go above red line 
  3. Green line above upper blue line (optional)
  4. Yellow line >= 50 (optional)

Execute sell trade when green line crosses below red line 

I also want to have a switch or if else statement for the optional cases. 


The opposite goes for a sell but i think if i understand and get one side correct the other won't be much problem 

Please if source code needed I'll provide 

 
  • Usually people who cannot code do not receive free help on this forum, although it could happen if you are lucky. Be patient.
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community.
  • If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free).
  • Finally, you also have the option to hire a programmer in the Freelance section.
 
bool FlagBuy=false;
bool UseOptional1=false;
bool UseOptional2=false;

void OnTick()
{

if(!FlagBuy)
{
if(Green line <= 32 &&
Green line < red line &&
((UseOptional1 && Green line <blue line ) || !UseOptional1) &&
((UseOptional2 && Yellow line <=50) || !UseOptional2) )
FlagBuy=true;
}

if(FlagBuy &&  greenline(2)<redline(2) && greenline(1)>redline(1))
{
if(Buy())
FlagBuy=false;
}

}
 
Fernando Carreiro #:
  • Usually people who cannot code do not receive free help on this forum, although it could happen if you are lucky. Be patient.
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community.
  • If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free).
  • Finally, you also have the option to hire a programmer in the Freelance section.

I did write some code and was stuck here is what i did


  1. Keep track of latest 3 candlesticks for each buffer in the TDI indicator.
  2. Store it in separate variables
  3. Using different if statements check if 
  • Yellow line(0) <= 50 && green line(2) <=32 && blue line(2) <=32 && green line(2) < red line(2) && green line(0) > red line(0) 

The number in the brackets is candle index

I would do this for the optional case and the non-optional case but it did not work 

 double Rsi, Rsi1, Rsi2, Vbh, Vbh1, Vbh2, Vbl, Vbl1, Vbl2, Mbl, Tsl, Tsl1, Tsl2;  //variables to hold TDI Buffer values for each candle

         Rsi = RSI(0);  //the function is called and candle index passed as param to get current candle value on TDI chart
         Rsi1 = RSI(1);
         Rsi2 = RSI(2);

         Vbh = VBH(1);
         Vbh1 = VBH(1);
         Vbh2 = VBH(2);

         Tsl = TSL(0);
         Tsl1 = TSL(1);
         Tsl2 = TSL(2);

         Vbl = VBL(0);
         Vbl1 = VBL(1);
         Vbl2 = VBL(2);

         Mbl = MBL(0);
         
         //Cases for Buy crosses
         //rsi below 32, vbl, tsl and mbl <= 50

         if(Mbl <= 50 && Rsi2 <= 32 && Vbl2 <= 32 && Rsi2 < Tsl2 && Rsi > Tsl)
            {
               Comment("Strong Long Signal");
               trade.Buy(0.2, _Symbol, askPrice, 0, 0, "Buy by TDI_auto");
               Print("Case One");
            }
         //rsi below 32, vbl and tsl
         if(Mbl > 50 && Rsi2 <= 32 && Rsi > Tsl)
            {
               Comment("Medium long signal ");
               trade.Buy(0.2, _Symbol, askPrice, 0, 0, "Buy by TDI_auto");
               Print("Case Two");

            }
         //rsi below 32 and below tsl
         if(Rsi2 <= 32 && Rsi > Tsl)
            {
               Comment("Weak Long Signal");
               trade.Buy(0.2, _Symbol, askPrice, 0, 0, "Buy by TDI_auto");
               Print("Case three");
            }



         //Cases for Sell crosses

         // tsl rsi above 68, vbh, tsl and mbl >= 50
         if(Mbl >= 50 && Rsi2 >= 68 && Vbl2 >= 68 && Rsi2 > Tsl2 && Rsi < Tsl)
            {
               Comment("Strong Sell Signal");

               trade.Sell(0.2, _Symbol, bidPrice, 0, 0, "Sell by TDI_auto");
               Print("Case One");
            }
         //rsi above 68, vbh and tsl
         if(Mbl < 50 && Rsi2 >= 68 && Rsi < Tsl)
            {
               Comment("Medium Sell signal ");

               trade.Sell(0.2, _Symbol, bidPrice, 0, 0, "Sell by TDI_auto");
               Print("Case Two");
            }
         //rsi above 68 and above
         if(Rsi2 >= 68 && Rsi < Tsl)
            {
               Comment("Weak Sell Signal");

               trade.Sell(0.2, _Symbol, bidPrice, 0, 0, "Sell by TDI_auto");
               Print("Case Three");
            }

 
Daniel Cioca #:

Wow brilliant solution 

I implemented but it seems like the conditional is never true even when the crosses are valid

I've attached the source code and indicator file below 

  
         if(!flagBuy){ 
           if(Rsi <= 32 && Rsi < Tsl && ((useOptionalOne && Rsi < Vbl) || !useOptionalOne) && ((useOptionalTwo && Mbl <= 50) || !useOptionalTwo))
           flagBuy = true;
         }
           Print("Buy Flag is :",flagBuy);
         
         if(flagBuy && Rsi < Tsl && Rsi > Tsl){
           if(trade.Buy(0.2,_Symbol,askPrice,0,0,"TDI_auto")){
            flagBuy = false;
           }
         }
         
         //Cases for Sell Crosses
          
          if(!flagSell){ 
           if(Rsi >= 68 && Rsi > Tsl && ((useOptionalOne && Rsi > Vbh) || !useOptionalOne) && ((useOptionalTwo && Mbl >= 50) || !useOptionalTwo))
           flagSell = true;
         }
           Print("Sell Flag is :",flagSell);
         
         if(flagSell && Rsi > Tsl && Rsi < Tsl){
           if(trade.Sell(0.2,_Symbol,bidPrice,0,0,"TDI_auto")){
            flagSell = false;
           }
         } 
  
Files:
TDI_auto.mq5  13 kb
TDI.mq5  18 kb
 
Quantum Dev #:

Wow brilliant solution 

I implemented but it seems like the conditional is never true even when the crosses are valid

I've attached the source code and indicator file below 

Off course is never true

         if(flagSell && Rsi > Tsl && Rsi < Tsl){
           if(trade.Sell(0.2,_Symbol,bidPrice,0,0,"TDI_auto")){
            flagSell = false;
           }
         } 

And on the buy side the same. 

if(FlagBuy &&  greenline(2)<redline(2) && greenline(1)>redline(1))

greenline(2)<redline(2) // This is condition on candle 2

greenline(1)>redline(1) // This is condition for candle 1.

// It means that greenline crossed over red line
 
Daniel Cioca #:

Off course is never true

And on the buy side the same. 

Thank you so much I got it right 

Reason: