My stoploss code is not working

 

 

 THE CODE I WRITTEN TO STOP LOSS is not working My purpose of using price close is to make transactions according to the last price of the bar.


if((CountOpenedPositions("buy") !=0 &&

               (((InitialBalance)-(PRICE_CLOSE) ))/InitialBalance   >= ( CloseLossPercent)/100 ||
              ((InitialBalance)-(PRICE_CLOSE) )/InitialBalance >= ( CloseLossPercent)/100)) || 

            (CountOpenedPositions("sell") !=0 &&
             
               
              (((PRICE_CLOSE)-(InitialBalance))/InitialBalance   <= ( CloseLossPercent)/100 ||
              ( (PRICE_CLOSE)-(InitialBalance) )/InitialBalance  <= ( CloseLossPercent)/100))
 

Hello,


You really need to post more of your code. There could be many issues with it, no one would know if it's one or more unless you show all the parts using those variables.


For example, I can't even see what data types they are. Does it show an error when you compile it?


Just looking at it, I don't know if you're purposely missing bits (I hope you are tbh) because it looks very buggy. Where is the end and beginning of your if statement?


You need to use { and } when you start and end a function, or an if statement.

 
Kutluhan Yesilkaya:

Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.


Sort out your brackets. You are using too many where they are not needed and they are not in the correct places.

I can't see that your snippet of code could possibly compile.

 
 (((InitialBalance)-(PRICE_CLOSE) ))/InitialBalance   >= ( CloseLossPercent)/100 ||
PRICE_CLOSE is the constant zero. Makes no sense. Money minus a price also makes no sense.
Kutluhan Yesilkaya: THE CODE I WRITTEN TO STOP LOSS is not working

The code you wrote has nothing to do with a Stop Loss.

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk. Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

  1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.

  2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)

  3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
              MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
              Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19

  4. You must normalize lots properly and check against min and max.

  5. You must also check FreeMargin to avoid stop out

Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

 
William Roeder : PRICE_CLOSE, sabit sıfırdır. Anlamı yok. Para eksi fiyat da bir anlam ifade etmiyor.

Yazdığınız kodun Kaybı Durdur ile ilgisi yok.

The code is giving an error. I want to calculate the stop loss based on the following closing price. I also want to calculate a rate on the entry price, not in pips.

 
Kutluhan Yesilkaya:

The code is giving an error. I want to calculate the stop loss based on the following closing price. I also want to calculate a rate on the entry price, not in pips.

Did you read my Post????

Keith Watford:

Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.


Sort out your brackets. You are using too many where they are not needed and they are not in the correct places.

I can't see that your snippet of code could possibly compile.

 
little.trader:

Merhaba,


Gerçekten kodunuzun daha fazlasını göndermeniz gerekiyor. Bununla ilgili birçok sorun olabilir, bu değişkenleri kullanarak tüm parçaları göstermediğiniz sürece kimse bir veya daha fazla olup olmadığını bilemez.


Örneğin, hangi veri türleri olduklarını bile göremiyorum. Derlerken bir hata mı gösteriyor?


Sadece ona bakınca, kasıtlı olarak bitleri mi kaçırdığınızı bilmiyorum (umarım tbh'sinizdir) çünkü çok hatalı görünüyor. İf ifadenizin sonu ve başlangıcı nerede?


Bir işlevi veya bir if ifadesini başlatıp bitirdiğinizde {ve} kullanmanız gerekir .

Hello, if I ask, could you write this code? in selling position

(closing price of the bar-position opening price) / if the position opening price is> 2%, then close the position

 

Although you keep ignoring peoples advice, here would be a way to do it:

/* Select ticket */
PositionSelectByTicket(1234);

/* Get values */
const double open_price = PositionGetDouble(POSITION_PRICE_OPEN);
const double current_close = iClose(Symbol(), Period(), NULL);		// This BTW is the same as the current bid-price!
const double two_percent_of_open_price = open_price / 50.0;

/* Check state */
if((open_price - two_percent_of_open_price) > current_close)
{
        /* call some closing function */
}

Figure out the rest on your own.

 
Dominik Egert :

İnsanların öğütlerini görmezden gelseniz de, işte bunu yapmanın bir yolu:

Gerisini kendi başına çöz.

I did what people said. not happening. I tried the codes you wrote, it gives an error. I tried a lot. this job should not be hard to this land. All I want is the code of the bar's closing price.

 
Kutluhan Yesilkaya:

All I want is the code of the bar's closing price.

You know the bar's closing price only when a new bar has started. Therefore you need to use a new bar check, from there you query the last bar's close.

bool isNewBar=IsNewBar(); // find it in the forum
if(isNewBar)
  {
   double closePrice=iClose(NULL,0,1); // last bar's close
   // calculate loss of any open position here
  }
 
lippmaje:

You know the bar's closing price only when a new bar has started. Therefore you need to use a new bar check, from there you query the last bar's close.

three errors giving
Reason: