How to convert a users Dollar amount into pips for stoploss or takeprofit

 

Can someone help me?

If a user inputs $10

extern double takeProfit=10.00;

extern double lotSize=0.01;

how can you convert this so you can add set the take profit to $10.00 above the ask.

Thank you

 

Her's a script that I did some time ago that modifies open orders according to SL and TP in account currency.

It may not be exactly what you want, but can probably be modified.

//+------------------------------------------------------------------+
//|                                                        order.mq4 |
//|                                                           GumRai |
//|                                                             none |
//+------------------------------------------------------------------+
#property copyright "GumRai"
#property link      "none"
#property version   "1.00"
#property strict
#property script_show_inputs
//--- input parameters
input double SL_in_Currency=10;
input double TP_in_Currency=10;
input bool   Chart_Trades_Only=false;

#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---

   for(int x=OrdersTotal()-1;x>=0;x--)
     {
      if(!OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
        {
         Print("Error with OrderSelect-",ErrorDescription(GetLastError()));
         continue;
        }
      if(!(OrderType()==OP_BUY || OrderType()==OP_SELL))
         continue;
      if(Chart_Trades_Only && OrderSymbol()!=Symbol())
         continue;

      double tick_size=SymbolInfoDouble(OrderSymbol(),SYMBOL_TRADE_TICK_SIZE);
      double pnt=SymbolInfoDouble(OrderSymbol(),SYMBOL_POINT);
      double minfromcurrentprice=SymbolInfoInteger(OrderSymbol(),SYMBOL_TRADE_STOPS_LEVEL)*pnt;
      double loss_for_1_lot=SymbolInfoDouble(OrderSymbol(),SYMBOL_TRADE_TICK_VALUE);  //Loss/Gain for a 1 tick move with 1 lot
      double loss_for_lot_size_this_trade=loss_for_1_lot*OrderLots();    //Loss/Gain for a 1 tick move with trade lot size
      double SL_ticks=SL_in_Currency/loss_for_lot_size_this_trade;
      double SL_points=MathFloor(SL_ticks*tick_size/pnt)*pnt;
      double TP_ticks=TP_in_Currency/loss_for_lot_size_this_trade;
      double TP_points=MathFloor(TP_ticks*tick_size/pnt)*pnt;

      if(OrderType()==OP_SELL)
        {
         SL_points= (-SL_points);
         TP_points= (-TP_points);
        }

      double new_sl=OrderOpenPrice()-SL_points;
      if(MathAbs(new_sl-OrderClosePrice()) -minfromcurrentprice<pnt)
        {
         Alert(OrderSymbol()," Order ",OrderTicket()," SL too close to current price");
         new_sl=OrderStopLoss();
        }
      double new_tp=OrderOpenPrice()+TP_points;
      if(MathAbs(new_tp-OrderClosePrice()) -minfromcurrentprice<pnt)
        {
         Alert(OrderSymbol()," Order ",OrderTicket()," TP too close to current price");
         new_tp=OrderTakeProfit();
        }
      if(MathAbs(new_sl-OrderStopLoss())>=pnt || MathAbs(new_tp-OrderTakeProfit())>=pnt)
         OrderModify(OrderTicket(),OrderOpenPrice(),new_sl,new_tp,0,0);

     }
  }
//+------------------------------------------------------------------+
 
Right on thank you. I will have a loo k in the morning and test it out.
 
I just tested it out and that is exactly what I was trying to do thank you so much I will have to go through your code so that I understand it better.
 

Hi, I was receiving Error#130 by running the  above code so i fixed it in the following way

replace this code

      if(MathAbs(new_sl-OrderClosePrice()) -minfromcurrentprice<pnt)

        {

         Alert(OrderSymbol()," Order ",OrderTicket()," SL too close to current price");

         new_sl=OrderStopLoss();

        }

      if(MathAbs(new_tp-OrderClosePrice()) -minfromcurrentprice<pnt)
        {
         Alert(OrderSymbol()," Order ",OrderTicket()," TP too close to current price");
         new_tp=OrderTakeProfit();
        }


By this

      if(OrderType() == OP_BUY)

        {

         if(OrderClosePrice() - new_sl - minfromcurrentprice < pnt)

           {

            Alert(OrderSymbol()," Order ",OrderTicket()," SL too close to current price");

            new_sl = OrderStopLoss();

           }

         if(new_tp - OrderClosePrice() - minfromcurrentprice < pnt)

           {

            Alert(OrderSymbol()," Order ",OrderTicket()," TP too close to current price");

            new_tp = OrderTakeProfit();

           }

        }

      if(OrderType() == OP_SELL)

        {

         if(new_sl - OrderClosePrice() - minfromcurrentprice < pnt)

           {

            Alert(OrderSymbol()," Order ",OrderTicket()," SL too close to current price");

            new_sl = OrderStopLoss();

           }

         if(OrderClosePrice() - new_tp - minfromcurrentprice < pnt)

           {

            Alert(OrderSymbol()," Order ",OrderTicket()," TP too close to current price");

            new_tp = OrderTakeProfit();

           }

        }



 

You've got it backwards. You don't convert money to SL/TP, you control your risk.

Never risk more than a small percentage of your account, certainly less than 2% per trade, 6% total to the account.

Risk depends on your initial stop loss, lot size, and the value of the pair. It does not depend on margin and leverage. No SL means you have infinite risk.

  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.

Reason: