How to calculate an SL price, based on OrderCalcProfit() ?

 

I know OrderCalcProfit() returns the profit/loss of a hypothetical position based on open price, close price, volume etc.

how can one do the reverse calculation ?
I need it co calculate the price for trailing SL.

for an open position (which Open Price, Volume, and Symbol are known) how can I calculate a price such that if position is closed at that specific price, it will gain a pre-determined profit ? (let's say Balance * 1.01)

 
Apply the following example, but instead of adjusting the volume, do the equivalent to adjust the stop size.

Use a unit size for the stop size, and then adjust according to ratio of the loss reported by the function, in the a similar way I did for the volume.

Forum on trading, automated trading systems and testing trading strategies

SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) sometimes zero

Fernando Carreiro, 2022.08.23 17:41

You can! These are the steps I take. I supply the function with a lot size equal to the “Max Lot Size” allowed for the symbol in question, then calculate the ratio needed to achieve the fractional risk that I wish to apply, to get the correct volume for the order. I then align that with the “Lot Step” and finally check it against both the maximum and minimum allowed lots for the symbol.

The reason I use the “maximum” lots instead of just “1.0” lots as a reference value is because there is no guarantee that the value of 1.0 is within the minimum and maximum values allowed. Given that using 1.0, or the maximum, gives equivalent results anyway (by using the ratio method), I choose to use the “max lots” as the reference point which also offers the most precision for the calculation.

Something like this ...

// This code will not compile. It is only a example reference

if( OrderCalcProfit( eOrderType, _Symbol, dbLotsMax, dbPriceOpen, dbPriceStopLoss, dbProfit ) )
{
   dbOrderLots = fmin( fmax( round( dbRiskMax * dbLotsMax / ( -dbProfit * dbLotsStep ) )
               * dbLotsStep, dbLotsMin ), dbLotsMax ); 
      
   // the rest of the code ...
};

Forum on trading, automated trading systems and testing trading strategies

Tick size vs Point(), can be a little tricky in Multicurrency EA

Fernando Carreiro, 2022.03.09 12:11

Tick Size and Point Size can be very different especially on stocks and other symbols besides forex.

Always use Tick Size to adjust and align your prices, not the point size. In essence, make sure that your price quotes, are properly aligned to the Tick size (see following examples).

...
double tickSize = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
...
double normalised_price = round( price / tick_size ) * tick_size;
...
// Or use a function
double Round2Ticksize( double price )
{
   double tick_size = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
   return( round( price / tick_size ) * tick_size );
};
 
Something like this pseudocode for a long positions ?:
double GainFrom100Point = OrderCalcProfit(...BUY, Vol, OpenPrice, OpenPrice+100*_Point , etc...);
double new_SL = OpenPrice + (Predetermined_Profit / GainFrom100Point)*100*_Point;


(error checkups and tick size adjustments are omitted for now)