Welcome to the BEST forum for traders

Did you know we're sharing tons of exclusive content here?
Our community is growing quickly. Take a look around and say "Hello".

  • Forums
  • Pinescript
  • Open long position at the breakout of the previous bar's high. Close the position at the current bar's close.

Open long position at the breakout of the previous bar's high. Close the position at the current bar's close.

Sep 7, 2022 - 12:44 PM

Viewed 576 times

https://best-trading-indicator.com/community/bti/forums/4180/topics/1459941 COPY
  • I'm a beginner at Pine Script, so sorry if the question seems trivial... I haven't been able to figure out how to code this simple concept: we open a long position at the breakout of the previous bar's high and close the position at the close of the current bar. Any ideas?

    Untitled.jpg

    0
  • Something like this

    //@version=5
    
    VERSION = "V"
    SCRIPT_NAME = "Script " + VERSION
    
    // # ========================================================================= #
    // #                   |   STRATEGY  |
    // # ========================================================================= #
    // These values are used both in the strategy() header and in the script's relevant inputs as default values so they match.
    // Unless these values match in the script's Inputs and the TV backtesting Properties, results between them cannot be compared.
    InitCapital = 1000000
    InitPosition = 100.0
    InitCommission = 0.075
    InitPyramidMax = 1
    CalcOnorderFills = false
    ProcessOrdersOnClose = true
    CalcOnEveryTick = false
    CloseEntriesRule = "FIFO"
    
    strategy(title=SCRIPT_NAME, shorttitle=SCRIPT_NAME, 
     overlay=true, pyramiding=InitPyramidMax, initial_capital=InitCapital, default_qty_type=strategy.fixed, process_orders_on_close=ProcessOrdersOnClose,
     default_qty_value=InitPosition, commission_type=strategy.commission.percent, commission_value=InitCommission, calc_on_order_fills=CalcOnorderFills, 
     calc_on_every_tick=CalcOnEveryTick,
     precision=9, max_lines_count=500, max_labels_count=500)
    
    
    buy_condition = close > high[1]
    if buy_condition
        strategy.entry("Long", strategy.long)
    
    if time == time_close
       strategy.close("Long")
    
    

    For more information on Pine and how to learn Pine: https://www.pinecoders.com/learning_pine_roadmap/

    0
CONTENTS