Can not get SL and TP to set at desired points

 

I am working on building an EA to look for the candle to Close either above or below the BB, and ride it up or down. The additional parameters include:

  • Buy Conditions
    1. RSI is below upper limit (70)
    2. Slow SMA (45) is below Main line of the BB
    3. Candle closes above UpperBB
  • Sell Conditions
    1. RSI is above lower limit (30)
    2. Slow SMA (45) is above Main line of the BB
    3. Candle closes below LowerBB

I have been able to get it to find the trade setups, and enter both Buy and Sell. But I can not get the TP and SL to be set to the points I am wanting. I am using the ATR with a multiplier to set the SL based on the previous LowestLow within a range, and TP based on the risk reward ratio compared to the SL. I can not figure out how to get the R/R ratio input. And my current SL is getting put to a ridiculously far point, and do not know why. Any input to help with this would be greatly appreciated. Feel free to test it and manipulate it.

Files:
 
if(orderType%2==ORDER_TYPE_BUY)
            {
               price = Ask;
               tp = ATR*4;
               sl = Bid-ATR*2;

                  if(price>(entryPrice-stopsLevel))
                     {
                        entryPrice = price;
                        orderType = ORDER_TYPE_BUY;
                     }
  
               tpBuy = NormalizeDouble(entryPrice+tp,Digits());
               slBuy = NormalizeDouble(entryPrice-sl,Digits());

            }

I don't understand with this calculation. According to your code your slBuy would be Ask-Bid-ATR*2. If you want your SL to be ATR*2 difference using sl is already right you don't need to use ask price again.

And for your TP, BUY at the ASK and SELL at the BID (including SL and TP) you need to use Bid+ATR*4 instead of ATR*4.

hopefully it will help

 
Luandre Ezra:

I don't understand with this calculation. According to your code your slBuy would be Ask-Bid-ATR*2. If you want your SL to be ATR*2 difference using sl is already right you don't need to use ask price again.

And for your TP, BUY at the ASK and SELL at the BID (including SL and TP) you need to use Bid+ATR*4 instead of ATR*4.

hopefully it will help

Thank You. This is the first time doing this, and couldn't find an EA to do what I wanted, so I am making it myself. Those changes corrected the SL and TP points correctly. It now has SL set within 0.00200 pips and TP within 0.00400 pips using the 15M period. I will see how it works on the other periods. Thank you again.
Reason: