Strategy · Pine Script v5 · On the chart

그리드현물매매전략

by tvextbot

This strategy builds a spot grid on the price chart from a user-set upper price and a percentage spacing.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code318
Settings18inputs
LicenceNone statedfile: MPL-2.0
Last update3 years ago7 Sep 2023

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

318 lines of Pine Script v5 from [TVExtBot]그리드현물매매전략.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

[TVExtBot]그리드현물매매전략.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TVExtBot

//@version=5
strategy(title='그리드현물매매전략', overlay=true,
     initial_capital=1000, commission_type = strategy.commission.percent, commission_value = 0.06, pyramiding = 10, 
     default_qty_value=10, default_qty_type=strategy.percent_of_equity,
     process_orders_on_close=true, close_entries_rule='ANY')


//---------------------------------------백테스트기간 설정--------------------------------------{

// Make input options that configure backtest date range
startYear = input.int(title="시작 년",
     defval=2020, minval=1800, maxval=2100, inline="1", group = '백테스트기간')
startMonth = input.int(title="월",
     defval=1, minval=1, maxval=12, inline="1", group = '백테스트기간')
startDate = input.int(title="일",
     defval=1, minval=1, maxval=31, inline="1", group = '백테스트기간')

endYear = input.int(title="종료 년",
     defval=2050, minval=1800, maxval=2100, inline="2", group = '백테스트기간')
endMonth = input.int(title="월",
     defval=12, minval=1, maxval=12, inline="2", group = '백테스트기간')
endDate = input.int(title="일",
     defval=31, minval=1, maxval=31, inline="2", group = '백테스트기간')

// Look if the close time of the current bar
// Falls inside the date range
inDateRange = (time >= timestamp(syminfo.timezone, startYear,
         startMonth, startDate, 0, 0)) and
     (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))

//--------------------------------------------------------------------------------}

//---------------------------------------주문정보설정--------------------------------------{
token = input.string(title = "인증키", defval = "", inline="3", group = "주문정보", tooltip = "TVExtBot 인증키")
buyOrderid = input.string(title = "매수주문ID", defval = "", group = "주문정보",   inline = "4")
buyMemo = input.string(title = " 메모", defval = "", group = "주문정보", inline = "4")
sellOrderid = input.string(title = "매도주문ID", defval = "", group = "주문정보",   inline = "5")
sellMemo = input.string(title = " 메모", defval = "", group = "주문정보", inline = "5")
closeOrderid = input.string(title = "손절주문ID", defval = "", group = "주문정보",   inline = "6")
closeMemo = input.string(title = " 메모", defval = "", group = "주문정보", inline = "6")

//---------------------------------------그리드설정--------------------------------------{

PercentProfit = input(0.50, "간격(%)", group = "그리드 간격 설정")

x1 = (100 - (PercentProfit * 1)) / 100
x2 = (100 - (PercentProfit * 2)) / 100
x3 = (100 - (PercentProfit * 3)) / 100
x4 = (100 - (PercentProfit * 4)) / 100

x5 = (100 - (PercentProfit * 5)) / 100

x6 = (100 - (PercentProfit * 6)) / 100
x7 = (100 - (PercentProfit * 7)) / 100
x8 = (100 - (PercentProfit * 8)) / 100
x9 = (100 - (PercentProfit * 9)) / 100

x10 = (100 - (PercentProfit * 10)) / 100

UpperLimit = input.price(0.00, '상단 그리드 가격', group = "", confirm=true)
plot(UpperLimit, color = color.purple)

LowerLimit = UpperLimit * x10
plot(LowerLimit, color = color.purple)

//-----------------------------------------------------------------------------------------}

//---------------------------------------그리드 라인 설정--------------------------------------{

Linea_A = UpperLimit * x1
plot(Linea_A, color =  color.blue)

Linea_B = UpperLimit * x2
plot(Linea_B, color =  color.blue)

Linea_C = UpperLimit * x3
plot(Linea_C, color =  color.blue)

Linea_D = UpperLimit * x4
plot(Linea_D, color =  color.blue)

Linea_50Percent = UpperLimit * x5
plot(Linea_50Percent, color =  color.purple)

Linea_E = UpperLimit *  x6
plot(Linea_E, color =  color.blue)

Linea_F = UpperLimit * x7
plot(Linea_F, color =  color.blue)

Linea_G = UpperLimit  * x8
plot(Linea_G, color =  color.blue)

Linea_H = UpperLimit * x9
plot(Linea_H, color =  color.blue)

//----------------------------------------------------------------------------------------}

//---------------------------------------매수 주문 설정--------------------------------------{

FloorLimit = close > LowerLimit 
Cond1 = close < Linea_A
Cd1 = FloorLimit and Cond1
LongA = Cd1 and inDateRange

isLong1 = if (LongA and barstate.isconfirmed)
    1
else 
    0 

FloorLimit2 = close > LowerLimit 
Cond2 = close < Linea_B
Cd2 = FloorLimit2 and Cond2
LongB = Cd2 and inDateRange

isLong2 = if (LongB and barstate.isconfirmed)
    1
else 
    0

FloorLimit3 = close > LowerLimit 
Cond3 = close < Linea_C
Cd3 = FloorLimit and Cond3
LongC = Cd3 and inDateRange 

isLong3 = if (LongC and barstate.isconfirmed)
    1
else 
    0

FloorLimit4 = close > LowerLimit 
Cond4 = close < Linea_D
Cd4 = FloorLimit and Cond4
LongD = Cd4 and inDateRange 

isLong4 = if (LongD and barstate.isconfirmed)
    1
else 
    0 
    
FloorLimit5 = close > LowerLimit 
Cond5 = close < Linea_50Percent
Cd5 = FloorLimit and Cond5
Long_E = Cd5 and inDateRange 

isLong5 = if (Long_E and barstate.isconfirmed)
    1
else 
    0 
    
FloorLimit6 = close > LowerLimit 
Cond6 = close < Linea_E
Cd6 = FloorLimit and Cond6
Long_F = Cd6 and inDateRange 

isLong6 = if (Long_F and barstate.isconfirmed)
    1
else 
    0 
    
FloorLimit7 = close > LowerLimit 
Cond7 = close < Linea_F
Cd7 = FloorLimit and Cond7
Long_G = Cd7 and inDateRange 

isLong7 = if (Long_G and barstate.isconfirmed)
    1
else 
    0 
    
FloorLimit8 = close > LowerLimit 
Cond8 = close < Linea_G
Cd8 = FloorLimit and Cond8
Long_H = Cd8 and inDateRange 

isLong8 = if (Long_H and barstate.isconfirmed)
    1
else 
    0 
    
FloorLimit9 = close > LowerLimit 
Cond9 = close < Linea_H
Cd9 = FloorLimit and Cond9
LongH = Cd9 and inDateRange 

isLong9 = if (Long_H and barstate.isconfirmed)
    1
else 
    0 

Cond10 = close < LowerLimit
Long_I = Cond10 and inDateRange 

isLong10 = if (Long_I and barstate.isconfirmed)
    1
else 
    0 



//----------------------------------------------------------------------------------------}

//---------------------------------------매도 주문 설정--------------------------------------{

isExit1 = if (close > UpperLimit and barstate.isconfirmed)
    close
    
isExit2 = if (close > Linea_A and barstate.isconfirmed)
    close
    
isExit3 = if (close > Linea_B and barstate.isconfirmed)
    close

isExit4 = if (close > Linea_C and barstate.isconfirmed)
    close
    
isExit5 = if (close > Linea_D and barstate.isconfirmed)
    close
    
isExit6 = if (close > Linea_50Percent and barstate.isconfirmed)
    close
    
isExit7 = if (close > Linea_E and barstate.isconfirmed)
    close

isExit8 = if (close > Linea_F and barstate.isconfirmed)
    close
    
isExit9 = if (close > Linea_G and barstate.isconfirmed)
    close

isExit10 = if (close > Linea_H and barstate.isconfirmed)
    close
    
//----------------------------------------------------------------------------------------}
    
//---------------------------------------매수 매도 주문--------------------------------------{

strategy.entry(id = "BUY1",  direction = strategy.long, qty = 10, when = (isLong1 and strategy.position_size == 0), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY2",  direction = strategy.long, qty = 10, when = (isLong2 and strategy.position_size == 10), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY3",  direction = strategy.long, qty = 10, when = (isLong3 and strategy.position_size == 20), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY4",  direction = strategy.long, qty = 10, when = (isLong4 and strategy.position_size == 30), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY5",  direction = strategy.long, qty = 10, when = (isLong5 and strategy.position_size == 40), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY6",  direction = strategy.long, qty = 10, when = (isLong6 and strategy.position_size == 50), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY7",  direction = strategy.long, qty = 10, when = (isLong7 and strategy.position_size == 60), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY8",  direction = strategy.long, qty = 10, when = (isLong8 and strategy.position_size == 70), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY9",  direction = strategy.long, qty = 10, when = (isLong9 and strategy.position_size == 80), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY10", direction = strategy.long, qty = 10, when = (isLong10 and strategy.position_size == 90), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')


strategy.exit(id = "EXIT1",     from_entry = "BUY1",    qty = 100, stop = isExit1, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT2",     from_entry = "BUY2",    qty = 100, stop = isExit2, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT3",     from_entry = "BUY3",    qty = 100, stop = isExit3, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT4",     from_entry = "BUY4",    qty = 100, stop = isExit4, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT5",     from_entry = "BUY5",    qty = 100, stop = isExit5, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT6",     from_entry = "BUY6",    qty = 100, stop = isExit6, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT7",     from_entry = "BUY7",    qty = 100, stop = isExit7, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT8",     from_entry = "BUY8",    qty = 100, stop = isExit8, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT9",     from_entry = "BUY9",    qty = 100, stop = isExit9, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT10",    from_entry = "BUY10",   qty = 100, stop = isExit10, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')

//--------------------------------------------------------------------------------------}

//---------------------------------------스탑로스 설정--------------------------------------{

ExitConditionOn = input.string("NO", title="사용여부", options=["NO", "YES"], group = "스탑로스 설정") == "YES"

StopLoss = input(0.00, "손절비율(%)", group = "스탑로스 설정")

x = (100 - StopLoss) / 100

StopLossLevel = LowerLimit * x

GridDestroyed = close < StopLossLevel and barstate.isconfirmed

if ExitConditionOn
    strategy.close_all(when = GridDestroyed, comment = "손절", alert_message='TVM:{"orderid":"' + closeOrderid +'","memo":"' + closeMemo + '","token":"' + token + '"}:MVT')

//--------------------------------------------------------------------------------------------}


//---------------------------------------그리드가격 테이블설정--------------------------------------{

i_position = input.string(defval = "Bottom Right", title = "테이블위치", options = ["Top Right", "Middle Right", "Bottom Right"], group = "테이블설정")
position = i_position == "Top Right" ? position.top_right : i_position == "Middle Right" ? position.middle_right : position.bottom_right
i_w = 12
i_h = 2
text_size = size.normal

var table perfTable = table.new(position, 2, 11, frame_color = color.blue, frame_width = 2, border_width = 1)

table.cell(perfTable, 0, 0, "상단 그리드 가격", bgcolor = color.new(color.purple, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 0, text = str.tostring(UpperLimit), bgcolor = color.new(color.purple, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 1,  "그리드가격 1", bgcolor = color.new(color.blue, 74),  text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 2, "그리드가격 2", bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 3, "그리드가격 3", bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 4, "그리드가격 4", bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 1, text = str.tostring(Linea_A), bgcolor = color.new(color.blue, 74),  text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 2, text = str.tostring(Linea_B), bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 3, text = str.tostring(Linea_C), bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 4, text = str.tostring(Linea_D), bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 5, "중간 그리드 가격", bgcolor = color.new(color.purple, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 5, text = str.tostring(Linea_50Percent), bgcolor = color.new(color.purple, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 6,  "그리드가격 5", bgcolor = color.new(color.blue, 74),  text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 7, "그리드가격 6", bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 8, "그리드가격 7", bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 9, "그리드가격 8", bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 6,  text = str.tostring(Linea_E), bgcolor = color.new(color.blue, 74),  text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 7, text = str.tostring(Linea_F), bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 8, text = str.tostring(Linea_G), bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 9, text = str.tostring(Linea_H), bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 10, "하단 그리드 가격(손절)", bgcolor = color.new(color.purple, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 10, text = str.tostring(StopLossLevel), bgcolor = color.new(color.purple, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)

//--------------------------------------------------------------------------------}

Overview

그리드현물매매전략 calculates ten descending levels, plots the upper and lower bounds, and allows up to ten stacked entries sized as a percentage of equity. Orders are processed on bar close inside a configurable backtest date window. External order identifiers and memos support TVExtBot automation. The exit switch and stop-loss ratio default off, so no stop is active unless the user enables them. The provided excerpt does not show complete entry and exit logic.

Details

Published by

tvextbot on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Support & resistance, Mean reversion

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy can add several entries in the same direction
  • The strategy enters trades without a stop loss

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
시작 년int2020startYear
월int1startMonth
일int1startDate
종료 년int2050endYear
월int12endMonth
일int31endDate
인증키stringtoken
매수주문IDstringbuyOrderid
메모stringbuyMemo
매도주문IDstringsellOrderid
메모stringsellMemo
손절주문IDstringcloseOrderid
Show all 18 settings
SettingTypeDefaultVariable
메모stringcloseMemo
간격(%)float0.50PercentProfit
상단 그리드 가격price0.00UpperLimit
사용여부stringNOExitConditionOn
손절비율(%)float0.00StopLoss
테이블위치stringBottom Righti_position
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

330 stars

This strategy divides a fixed price range between an upper and a lower limit into equally spaced grid levels and plots them on the price chart. It marks a buy when price crosses down through a grid and a sell when price crosses up through a grid, using either

Strategy v5 Support & resistanceMean reversion Repaints
Updated 2 months ago · None stated GitHub repository of Grid Bot Simulator [Alorse]

3C QFL Mean Reversal

by hasnocool
106 stars

3C QFL Mean Reversal is a strategy based on the Quickfinger Luc (QFL) trading methodology. It identifies breakdown buying opportunities below established price base levels and enters mean-reversion trades. The strategy includes multi-safety order DCA scaling,

Strategy v5 Mean reversionSupport & resistance Repaints
Updated 2 years ago · GPL-3.0 GitHub repository of 3C QFL Mean Reversal

This strategy builds a spot grid between user-set high and low prices and draws it on the price chart. It splits that range into ten levels and places up to five long limit orders at the lower levels, sizing each from current equity. Orders already below price

Strategy v6 Support & resistanceMean reversion Repaints
Updated 6 weeks ago · None stated GitHub repository of Grid Spot Trading Algorithm [The Quant Science]

FairValueGap E003

by ruckard
13 stars

Calculates the distance between the current price and the opposite end of the latest fair value gap, entering trades on the expectation that price will partially fill the gap. It incorporates risk management with customizable stop loss, take profit based on ri

Strategy v5 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · None stated GitHub repository of FairValueGap E003

Wyckoff Phase

by muammeryldrm42
7 stars

This strategy identifies Wyckoff consolidation ranges directly on the price chart to trade potential spring and upthrust events. It calculates highest highs and lowest lows over a customizable lookback period, filtering setups by average volume spikes and ATR

Strategy v5 SMC / ICT / price actionSupport & resistance
Updated 3 months ago · MIT GitHub repository of Wyckoff Phase

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers