Strategy Template

Trying to include few basic things which is needed for strategy which can be used as template.

Few important components

Strategy parameters

Few important parameters include - initial_capital, default_qty_type, default_qty_value, commission_type, pyramiding and commission_value. All my strategies will have similar settings with initial captial set to 20000 to 100000. 100% of equity per trade with no pyramiding (set to 1) and minimal commission.

margin_long and margin_short can be used for leveraged trading. But, since we are not using pyramiding, it will make no effect.

Trade Limiting parameters

Two types of limiting is available in the scripts
  • Limiting trading direction : this is done through method strategy.risk.allow_entry_in and input parameter tradeDirection
  • Limiting trades to particular time window : This is achieved through adding start time and end time parameters of type input.time and check whether time is within this window

Custom Methods

  • customized security method to get higher timeframe data
  • customized moving average method to get moving average of any type

Custom Parameters

  • Moving average Type option list which I use quite often. Any strategy where there is need to use moving average, I try to scan through different moving average types and lengths to see which one is more appropriate for the given strategy. Hence, keeping this parameter in template to make it readily available when I start with new strategy
  • waitForCloseBeforeExit - this is used if trailing stop need to activated as soon as price hits the stop or only on close price. This is again something I switch quite often based on strategy. Hence, keeping this as part of the template.

Entry and Exit statements for long and short

These statements from line (57 to 62) can remain as is even with new strategy. Only thing to be set are variables - buyCondition, sellCondition, closeBuyCondition and closeSellCondition

Last but not the least

In pinescript, a long and short position cannot coexist in a strategy at any point of time. Any short positions created will automatically stop long positions and vice versa. Hence, it is important make short and long trades mutually exclusive. In this example, I have used 200 weekly moving average as trend bias. No short positions are taken when price is trading above 200 weekly moving average low/close and no long positions are taken when price is less than 200 weekly moving average high/close. Any rule built on top of this (In this case a simple supertrend rules) ensures that there are no conflicting signals and hence avoids confusing trades on the stratgy.

เอกสารเผยแพร่: Updated
  • strategy margin parameters. Set 90 as default
  • Qty set to 90 to avoid margin calls (Which I think is a bug)
  • Added groups to default strategy trade filters


ด้วยจิตวิญญาณของ TradingView อย่างแท้จริง ผู้เขียนสคริปต์นี้ได้เผยแพร่เป็นโอเพนซอร์ส เพื่อให้ผู้ค้าสามารถเข้าใจและตรวจสอบได้ ไชโยให้กับผู้เขียน! คุณสามารถใช้ได้ฟรี แต่การใช้รหัสนี้ซ้ำในสิ่งพิมพ์อยู่ภายใต้กฎของบ้าน คุณสามารถตั้งเป็นรายการโปรดเพื่อใช้บนชาร์ตได้


ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมที่ เงื่อนไขการใช้บริการ