Please Help Me Out

 

I am NewBee trying to develop an Expert Advisor Based on RSI 

 i am struggling to code set TP ans SL

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>

int pos=0;
int myRSIDefinition;

CTrade trade; // To send orders and close positions
CPositionInfo positionInfo; // to get info about current positions
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
// Define the handler once at EA Init, not every tick
   myRSIDefinition=iRSI(Symbol(),Period(),14,PRICE_CLOSE);
// If is not posible to get indicator handler print some error information and return with error code
   if(myRSIDefinition==INVALID_HANDLE)
     {
      Print("There was an error creating RSI handler!");
      return(INIT_FAILED);
     }
   return INIT_SUCCEEDED;
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   string signal="";
   double TakeProfit;
   double StopLoss;
   double TakeProfitLevel;
   double StopLossLevel;
   double  Ask=NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_ASK),Digits());
   double  Bid=NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_BID),Digits());
   TakeProfit = 20;
   StopLoss = 60;
   TakeProfitLevel = Bid - TakeProfit*Point();   //0.0001 // Take Profit value defined
   StopLossLevel = Bid + StopLoss*Point(); // Stop loss value defined
   double myRSIArray[];
   ArraySetAsSeries(myRSIArray,true);
   CopyBuffer(myRSIDefinition,0,0,3,myRSIArray);
   double myRSIValue=NormalizeDouble(myRSIArray[0],2);
   if(myRSIValue>67)
      signal="buy";
   if(myRSIValue<33)
      signal="sell";
   if(signal=="buy" && PositionsTotal()<2)
     {
      //First close all Sell positions
      for(pos=0; pos<PositionsTotal(); pos++)
        {
         //Select the position to load info
         if(positionInfo.SelectByIndex(pos))
           {
            // Get the position type, if sell then close it
            if(positionInfo.PositionType()==POSITION_TYPE_SELL)
              {
               trade.PositionClose(positionInfo.Ticket());
              }
           }
        }
      trade.Buy(0.10,Symbol());
      Comment("The current signal is: ",signal);
     }
   if(signal=="sell" && PositionsTotal()<2)
     {
      //First close all Buy positions
      for(pos=0; pos<PositionsTotal(); pos++)
        {
         //Select the position to load info
         if(positionInfo.SelectByIndex(pos))
           {
            // Get the position type, if buy then close it
            if(positionInfo.PositionType()==POSITION_TYPE_BUY)
              {
               trade.PositionClose(positionInfo.Ticket());
              }
           }
        }
      trade.Sell(0.10,Symbol());
      Comment("The current signal is: ",signal);
     }
  }
//+------------------------------------------------------------------+

Files:
1.mq5  7 kb
 
aashiqsaw i am struggling to code set TP ans SL

No question asked. Until you can state your problem in concrete terms, no one cane help you.

 
aashiqsaw :

I am NewBee trying to develop an Expert Advisor Based on RSI 

 i am struggling to code set TP ans SL


If you want to close positions, then the cycle must go TO ZERO:

for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions