Search code examples
pine-scriptpine-script-v5

Pinescript code lines that gives one decimal for each order


enter image description here

Is there any lines or code that can make my order entries with one decimal instead of 3, because i am using bybit and it only allows orders with one decimal. I would like to backtest it with one decimal to have more precise results.

I tried using math floor with order qty but it didn't work. are there any suggestions on what i should try?


Solution

  • Just truncate the number by multiplying by 10, rounding and dividing by 10 again to get the quantity with one decimal.

    See this example:

    //@version=5
    strategy("My strategy", overlay=true, process_orders_on_close=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, precision=6)
    
    in_one_dec = input.bool(false, "Use one decimal?")
    
    entry_qty = (strategy.equity / close)
    entry_qty_adj = in_one_dec ? int(entry_qty * 10) / 10 : entry_qty
    
    plotchar(entry_qty, "entry_qty", "")
    plotchar(entry_qty_adj, "entry_qty_adj", "")
    
    longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
    if (longCondition)
        strategy.entry("My Long Entry Id", strategy.long, qty=entry_qty_adj)
    
    shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
    if (shortCondition)
        strategy.entry("My Short Entry Id", strategy.short, qty=entry_qty_adj)