sl script works buys, not sells. - page 3

 
Michael Charles Schefe #:

You should read better the formula I gave you, you didn't apply it correctly.

               step+=NormalizeDouble(.35 * 1000 / ts * tv / ltv,dig);

Should be :

               step+=NormalizeDouble(.35 * 1000 / tv * ts / ltv,dig);
 
Alain Verleyen #:

You should read better the formula I gave you, you didn't apply it correctly.

Should be :

oops. sorry.

what about tv. i have TICK_VALUE_LOSS

but should it be just TICK_VALUE?

 
Michael Charles Schefe #:

oops. sorry.

what about tv. i have TICK_VALUE_LOSS

but should it be just TICK_VALUE?

It's a complicated matter. See an extended discussion here : https://www.mql5.com/en/forum/441959

My suggestion is to stay simple and just use TICK_VALUE.

SYMBOL_TRADE_TICK_VALUE_LOSS vs SYMBOL_TRADE_TICK_VALUE_PROFIT
SYMBOL_TRADE_TICK_VALUE_LOSS vs SYMBOL_TRADE_TICK_VALUE_PROFIT
  • 2023.02.18
  • www.mql5.com
*edit: when i posted the thread, i was confused about the correct way to calculate position size based on entry price and stop loss price, so don't take anything i say here as truth, unless someone else verifies it in the comments
 
Alain Verleyen #:

It's a complicated matter. See an extended discussion here : https://www.mql5.com/en/forum/441959

My suggestion is to stay simple and just use TICK_VALUE.


thanks -- ever so much!

...yeah thats why i asked. i had read that thread and couple other with arguements both for and against using loss/profit or just value.

threads like that make the issues less clear than the original question seemed. exactly when the saying "clear as mud" is for.

thanks again. off to bed now.

 

my code has worked now for a full week, and i added to the code -- to remove the swap fee from the initial 350 before converting that result into points. I also made it to "refresh" the sl every day at 01:00 when swap fees have been updated.

i know it is working because i have had 1 trade hit sl on at least half of my fx pairs on my broker, all within 15 cents of the $350.

 
The script derives the new SL from position.PriceOpen(), not the current Bid or Ask. That works fine right when a position opens. Once price has moved a meaningful distance from that entry, the same reference point can put the calculated SL on the wrong side of the current market, and MT5 rejects it as invalid stops.
 
Asanka Manikgama Arachchilage Don #:
The script derives the new SL from position.PriceOpen(), not the current Bid or Ask. That works fine right when a position opens. Once price has moved a meaningful distance from that entry, the same reference point can put the calculated SL on the wrong side of the current market, and MT5 rejects it as invalid stops.

not sure what you mean. i have been using it daily, hourly -- since my prev msg and i have not seen any "Invalid Stops" errors. This is probably because i have my sl 70 - 140% of the tradesize. The only small issue i have is with jpy pairs, the sl can be $15 from my intended dollar sl. All other pairs are within cents of my intended dollar sl.

EDIT: maybe the spread should be added to the calc, as you sugggested. @Alain Verleyen ?

 
Michael Charles Schefe #:

EDIT: maybe the spread should be added to the calc, as you sugggested. @Alain Verleyen ?

It's a matter of choice, in my opinion : no it should not.
 
Asanka Manikgama Arachchilage Don #:
The script derives the new SL from position.PriceOpen(), not the current Bid or Ask. That works fine right when a position opens. Once price has moved a meaningful distance from that entry, the same reference point can put the calculated SL on the wrong side of the current market, and MT5 rejects it as invalid stops.

Note that after i saw your msg i went back to see my code, and found i already had added swaps into the math. See following code. If anyone has any criticism or advice or suggestions for improvement, please make them below.

I call this function at end of M5 candle -- if a trade has opened. I also clear my sl 6 minutes before 0 hour and then call the function 6 minutes after 01:00 to recalculate with new swaps, and to replace the sl(s).

also note that i am using these at Global Enviro

   CTrade trade;
   CPositionInfo position;
   trade.SetAsyncMode(true);

@Alain Verleyen @Vinicius Pereira De Oliveira @Vladislav Boyko @Ryan L Johnson @Gerard William G J B M Dinh Sy

My "$ value to sl" function. working with no noticed Invalid stop errors or any other errors noticed.

void AutoA3m::ApplySL()
  {
//   CTrade trade;
//   CPositionInfo position;
//   trade.SetAsyncMode(true);
   string symb;
   static int start = PositionsTotal()-1, pos=0, digb;
   static double ts, tv, step, ltvb, swp;//, poi; //pft, ltv, cursl, maxsl;
   static double sumsl;
   sumsl = (iStartLots * 700);
//---
   for(int i = PositionsTotal()-1; i >= 0; i--)
     {
      if(position.SelectByIndex(i))
        {
         //         if(PositionGetDouble(POSITION_SL)>0)
         //            continue;
         symb=position.Symbol();
         ts=SymbolInfoDouble(symb,SYMBOL_TRADE_TICK_SIZE);
         swp=position.Swap(); 
         ltvb=position.Volume();
         digb=(int)SymbolInfoInteger(symb,SYMBOL_DIGITS);
         step=position.PriceOpen(); 
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
           {
            tv=SymbolInfoDouble(symb,SYMBOL_TRADE_TICK_VALUE);
            step-=NormalizeDouble((sumsl - swp) / tv * ts / ltvb,digb);  // should be / tv * ts
            if(fabs(PositionGetDouble(POSITION_SL)-step) > 35/pow(10,digb))
               if(trade.PositionModify(PositionGetInteger(POSITION_TICKET),step,NULL))
                  pos-=1;
           }
         else
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
              {
               tv=SymbolInfoDouble(symb,SYMBOL_TRADE_TICK_VALUE);
               step+=NormalizeDouble((sumsl - swp) / tv * ts / ltvb,digb); // should be / tv * ts
               if(fabs(PositionGetDouble(POSITION_SL)-step) > 35/pow(10,digb))
                  if(trade.PositionModify(PositionGetInteger(POSITION_TICKET),step,NULL))
                     pos-=1;
              }
        }
     }
  }