Strategy · Pine Script v5 · On the chart

50 Pips A Day Strategy - Kaspricci

by hasnocool· code credited to Kaspricci

This strategy places buy-stop and sell-stop orders around the opening-session high and low, offset by a user-defined pip gap, and only allows those orders during a separate trading session.

View on GitHub

Repaints

GitHub stars106on the repository
Pine versionv5runs as is
Lines of code145
Settings21inputs
LicenceGPL-3.0
Last update2 years ago18 Sep 2024

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

145 lines of Pine Script v5 from 50 Pips A Day Strategy - Kaspricci.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

50 Pips A Day Strategy - Kaspricci.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Kaspricci
//@version=5
strategy("50 Pips A Day Strategy - Kaspricci", overlay=true, initial_capital = 1000, currency = currency.USD, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, margin_long = 0, margin_short = 0, calc_on_every_tick = true, calc_on_order_fills = false, slippage = 0, close_entries_rule = "ANY")
headlineEntry   = "Entry Seettings"
tooltipTF       = "Combined timeframe of London and New York Stock Exchange opening hours from 7:00 - 15:30 GMT and 13:30 - 20:00 GMT"
openingSession  = input.session(defval="0700-0800:23456", title="Opening Time", group=headlineEntry, tooltip="GMT timezone")
openingTime     = time(timeframe.period, openingSession, "GMT")
tradingSession  = input.session(defval="0800-2000:23456", title="Trading Time", group=headlineEntry, tooltip=tooltipTF)
tradingTime     = time(timeframe.period, tradingSession, "GMT")
buyStopSource   = input.source( defval = high, title = "Source for Buy Stop order", inline = "B", group=headlineEntry)
buyStopGap      = input.float(  defval = 2.0, minval = 0, step = 0.1, title = " + Pips:", inline = "B", group=headlineEntry)
sellStopSource  = input.source( defval = low, title = "Source for Sell Stop order", inline = "S", group=headlineEntry)
sellStopGap     = input.float(  defval = 2.0, minval = 0, step = 0.1, title = " - Pips:", inline = "S", group=headlineEntry)
closePending    = input.bool(   defval = true, group = headlineEntry, title = "Cancel pending order when new trade entered?", tooltip = "This will cancel the reamaining order, once the first trade has been entered.")
headlineTrade   = "Trade Seettings"
stopLossType    = input.string(defval = "FIX", title = "Stop Loss Type", options = ["ATR", "FIX"], group = headlineTrade)
atrLength       = input.int(defval = 14, title = "   ATR: Length        ",   inline = "ATR", group = headlineTrade, minval = 1)
atrFactor       = input.float(defval = 2.0, title = "Factor        ", inline = "ATR", group = headlineTrade, tooltip = "multiplier for ATR value", minval = 0, step = 0.05)
takeProfitRatio = input.float(defval = 2.0, title = "            TP Ration", group = headlineTrade, tooltip = "Multiplier for Take Profit calculation", minval = 0, step = 0.05)
fixStopLoss     = input.float(defval = 15.0, title = "   FIX: Stop Loss    ",   inline = "FIX", group = headlineTrade, minval = 0, step = 0.5) * 10 // need this in ticks
fixTakeProfit   = input.float(defval = 50.0, title = "Take Profit", inline = "FIX", group = headlineTrade, tooltip = "in pips", minval = 0, step = 0.5) * 10 // need this in ticks
useRiskMagmt    = input.bool(  title = "", defval = false, group = headlineTrade, inline = "RM")
riskPercent     = input.float( title = "Risk in %       ", defval = 1.0, minval = 0.0, step = 0.5, group = headlineTrade, inline = "RM", tooltip = "This will overwrite quantity from startegy settings and calculate the trade size based on stop loss and risk percent") / 100
rsikCapital     = input.string(defval = "Balance",                     group = headlineTrade, inline = "RM",    title = "",                          options = ["Balance", "Initial"], tooltip = "This will overwrite quantity from startegy settings and calculate the trade size based on stop loss and risk percent")
applyLotSizing  = input.bool(defval = true, title = "Apply Lot sizing", tooltip = "When enabled, the quantity will be rounded to the minimum lot size of 1000 (0.01 lots)")
stopLoss        = switch stopLossType
    "ATR" => math.round(ta.atr(atrLength) * atrFactor / syminfo.mintick, 0)
    "FIX" => fixStopLoss
takeProfit      = switch stopLossType
    "ATR" => math.round(stopLoss * takeProfitRatio, 0)
    "FIX" => fixTakeProfit
// calculate quantity
baseCapital = switch rsikCapital
    "Balance" => strategy.initial_capital + strategy.netprofit
    "Initial" => strategy.initial_capital
riskAmount      = strategy.convert_to_symbol(baseCapital * riskPercent)
tickValue       = syminfo.pointvalue * syminfo.mintick
riskBasedQty    = riskAmount / stopLoss / tickValue
riskBasedQty    := applyLotSizing ? math.round(riskBasedQty / 100000, 2) * 100000 : riskBasedQty
quantity        = useRiskMagmt ? riskBasedQty : na // if NaN the default from strategy settings will be used
inTradingTime   = not na(tradingTime)
inOpeningTime   = not na(openingTime)
var float buyStopPrice  = na
var float sellStopPrice = na
plot(inTradingTime ? buyStopPrice  : na, title = "Buy Stop Price",  color = color.green, style = plot.style_linebr)
plot(inTradingTime ? sellStopPrice : na, title = "Sell Stop Price", color = color.red,   style = plot.style_linebr)
///////////////////////////////////////////////////////////////////////////////
// api bot values
longSL          = buyStopPrice - stopLoss * syminfo.mintick
longTP1         = buyStopPrice + takeProfit * syminfo.mintick * 0.5 // 50% of total take profit
longTP2         = buyStopPrice + takeProfit * syminfo.mintick // same as full
longTPFull      = buyStopPrice + takeProfit * syminfo.mintick
shortSL         = sellStopPrice + stopLoss * syminfo.mintick
shortTP1        = sellStopPrice - takeProfit * syminfo.mintick * 0.5 // 50% of total take profit
shortTP2        = sellStopPrice - takeProfit * syminfo.mintick
shortTPFull     = sellStopPrice - takeProfit * syminfo.mintick
takeAmount1     = riskPercent / 2
takeAmount2     = riskPercent
takeAmountFull  = riskPercent
headlineAlerts  = "Alert Settings"
i_apiEnterLong  = input.text_area('Enter Long - 3commas,alerteron etc', 'API Entry - Long',   group = headlineAlerts)
i_apiExitLong   = input.text_area('Exit Long - 3commas,alerteron etc', 'API Exit - Long',     group = headlineAlerts)
i_apiEnterShort = input.text_area('Enter Short - 3commas,alerteron etc', 'API Entry - Short', group = headlineAlerts)
i_apiExitShort  = input.text_area('Exit Short - 3commas,alerteron etc', 'API Exit - Short',   group = headlineAlerts)
// #nl# => New line
// #side# => Buy or Sell (Long or Short)
// #symbol# => The ticker e.g. BTCUSDT
f_replaceKeys (_str) =>
    _value = _str
    _value := str.replace_all(_value, "#nl#", "\n")
//    _value := str.replace_all(_value, "#side#", "\n") // does not make sense to me...
    _value := str.replace_all(_value, "#symbol#", syminfo.ticker)
// #LongSL# => Long Stop loss
// #LongTP1# => Long Take Profit 1
// #LongTP2# => Long Take Profit 2
// #LongTPFULL# => Long Take Profit in FULL
// #TakeAmount1# => Percent to remove from trade on hitting TP1
// #TakeAmount2# => Percent to remove from trade on hitting TP2
// #TakeAmountFULL# => Percent to remove from trade on hitting TP3 (should be 100%)
f_replaceLongTokens (_str, _sl, _tp1, _tp2, _tpFull, _ta1, _ta2, _taFull) =>
    _value = _str
    _value := str.replace_all(_value, "#LongSL#", str.tostring(_sl))
    _value := str.replace_all(_value, "#LongTP1#", str.tostring(_tp1))
    _value := str.replace_all(_value, "#LongTP2#", str.tostring(_tp2))
    _value := str.replace_all(_value, "#LongTPFULL#", str.tostring(_tpFull))
    _value := str.replace_all(_value, "#TakeAmount1#", str.tostring(_ta1))
    _value := str.replace_all(_value, "#TakeAmount2#", str.tostring(_ta2))
    _value := str.replace_all(_value, "#TakeAmountFULL#", str.tostring(_taFull))
// #ShortSL# => Short Stop loss
// #ShortTP1# => Short Take Profit 1
// #ShortTP2# => Short Take Profit 2
// #ShortTPFULL# => Short Take Profit in FULL
// #TakeAmount1# => Percent to remove from trade on hitting TP1
// #TakeAmount2# => Percent to remove from trade on hitting TP2
// #TakeAmountFULL# => Percent to remove from trade on hitting TP3 (should be 100%)
f_replaceShortTokens (_str, _sl, _tp1, _tp2, _tpFull, _ta1, _ta2, _taFull) =>
    _value = _str
    _value := str.replace_all(_value, "#ShortSL#", str.tostring(_sl))
    _value := str.replace_all(_value, "#ShortTP1#", str.tostring(_tp1))
    _value := str.replace_all(_value, "#ShortTP2#", str.tostring(_tp2))
    _value := str.replace_all(_value, "#ShortTPFULL#", str.tostring(_tpFull))
    _value := str.replace_all(_value, "#TakeAmount1#", str.tostring(_ta1))
    _value := str.replace_all(_value, "#TakeAmount2#", str.tostring(_ta2))
    _value := str.replace_all(_value, "#TakeAmountFULL#", str.tostring(_taFull))
i_apiEnterLong  := f_replaceKeys (i_apiEnterLong)
i_apiEnterLong  := f_replaceLongTokens (i_apiEnterLong, longSL, longTP1, longTP2, longTPFull, takeAmount1, takeAmount2, takeAmountFull)
i_apiExitLong   := f_replaceKeys (i_apiExitLong)
i_apiExitLong   := f_replaceLongTokens (i_apiExitLong, longSL, longTP1, longTP2, longTPFull, takeAmount1, takeAmount2, takeAmountFull)
i_apiEnterShort := f_replaceKeys (i_apiEnterShort)
i_apiEnterShort := f_replaceShortTokens (i_apiEnterShort, shortSL, shortTP1, shortTP2, shortTPFull, takeAmount1, takeAmount2, takeAmountFull)
i_apiExitShort  := f_replaceKeys (i_apiExitShort)
i_apiExitShort  := f_replaceShortTokens (i_apiExitShort, shortSL, shortTP1, shortTP2, shortTPFull, takeAmount1, takeAmount2, takeAmountFull)
// ----- handline trade entries and exits --------------------------------------------------------------------------------------------------------------------------
var float openingHigh = na
var float openingLow  = na
// determine high and low druing opening session
if barstate.isconfirmed and inOpeningTime
    openingHigh     := math.max(buyStopSource,  nz(openingHigh[1], buyStopSource))
    openingLow      := math.min(sellStopSource, nz(openingLow[1],  sellStopSource))
    buyStopPrice  := openingHigh + buyStopGap * 10 * syminfo.mintick
    sellStopPrice := openingLow - sellStopGap * 10 * syminfo.mintick
// as soon as the trading session starts calculate stop prices based on high / low of first closing candle, create pending orders
if barstate.isconfirmed and inTradingTime and not inTradingTime[1]
    tradeID = str.tostring(strategy.closedtrades + strategy.opentrades + 1)
    strategy.entry(tradeID + "L", strategy.long, qty = quantity, stop = buyStopPrice, comment = "Long: " + tradeID, alert_message = i_apiEnterLong)
    strategy.exit(tradeID + "SL", tradeID + "L", profit = takeProfit, loss = stopLoss, comment_loss = "SL", comment_profit = "TP", alert_message = i_apiExitLong)
    strategy.entry(tradeID + "S", strategy.short, qty = quantity, stop = sellStopPrice, comment = "Short: " + tradeID, alert_message = i_apiEnterShort)
    strategy.exit(tradeID + "SL", tradeID + "S", profit = takeProfit, loss = stopLoss, comment_loss = "SL", comment_profit = "TP", alert_message = i_apiExitShort)
// once the session is gone, reset prices and cancel all remaining pending orders, close open orders
if barstate.isconfirmed and not inTradingTime and inTradingTime[1]
    buyStopPrice    := na
    sellStopPrice   := na
    openingHigh     := na
    openingLow      := na
    strategy.cancel_all()
    currentTradeID = str.tostring(strategy.closedtrades + strategy.opentrades)
    strategy.close(currentTradeID + "L", immediately = true, comment = "L: session end", alert_message = i_apiExitLong)
    strategy.close(currentTradeID + "S", immediately = true, comment = "S: session end", alert_message = i_apiExitShort)
// as soon as the first pending order has been entered the remaing pending order shall be cancelled 
if strategy.opentrades > 0 and closePending
    currentTradeID = str.tostring(strategy.closedtrades + strategy.opentrades)
    strategy.cancel(currentTradeID + "L")
    strategy.cancel(currentTradeID + "S")
Expand (196 lines)

Overview

50 Pips A Day Strategy - Kaspricci exits with a fixed or ATR-based stop loss and a matching take-profit distance or ratio. Optional settings size the position from account risk and round quantity to lots. It draws on the price chart. Calculation on every tick can change signals on the live bar.

Details

Published by

hasnocool on GitHub

Code credit

Kaspricci, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Sessions & time, Breakout, Volatility & bands

Markets

Forex

Timeframes

M15-H1

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Topics

analysisbtcusdtcryptocurrencydatapandaspythonsimulationtradingtradingview

Settings

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

SettingTypeDefaultVariable
Opening Timesession0700-0800:23456openingSession
Trading Timesession0800-2000:23456tradingSession
Source for Buy Stop ordersourcehighbuyStopSource
+ Pips:float2.0buyStopGap
Source for Sell Stop ordersourcelowsellStopSource
- Pips:float2.0sellStopGap
Cancel pending order when new trade entered?booltrueclosePending
Stop Loss TypestringFIXstopLossType
ATR: Length int14atrLength
Factor float2.0atrFactor
TP Rationfloat2.0takeProfitRatio
FIX: Stop Loss float15.0fixStopLoss
Show all 21 settings
SettingTypeDefaultVariable
Take Profitfloat50.0fixTakeProfit
useRiskMagmtboolfalseuseRiskMagmt
Risk in % float1.0riskPercent
rsikCapitalstringBalancersikCapital
Apply Lot sizingbooltrueapplyLotSizing
API Entry - LongtextEnter Long - 3commas,alerteron etci_apiEnterLong
API Exit - LongtextExit Long - 3commas,alerteron etci_apiExitLong
API Entry - ShorttextEnter Short - 3commas,alerteron etci_apiEnterShort
API Exit - ShorttextExit Short - 3commas,alerteron etci_apiExitShort
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

Turtle MACD Time-Zone Filter

by skywalker0803r
42 stars

This strategy enters long when price closes above the prior Donchian high and short when it closes below the prior Donchian low, only while flat and inside a Taipei-hour window. It exits on a MACD histogram reversal or through an ATR stop and a percentage take

Strategy v5 BreakoutOscillators & momentum
Updated 2 months ago · None stated GitHub repository of Turtle MACD Time-Zone Filter

ORION: Hybrid Strategy

by eddiebelaval
7 stars

This strategy identifies volatility breakout opportunities by constructing dynamic ATR-based price boxes and detecting volatility squeezes. Trades are filtered using an exponential moving average trend condition and designated session hours. Entries trigger up

Strategy v5 Volatility & bandsBreakout
Updated 3 months ago · None stated GitHub repository of ORION: Hybrid Strategy

This overlay strategy offers two selectable entry modes: a Bollinger squeeze on typical price and a simple moving-average cross. It filters trades by weekday, session, date range, and an optional news window, and can restrict direction or invert the side. Risk

Strategy v5 Volatility & bandsMoving averages
Updated today · None stated GitHub repository of Boilerplate Configurable Strategy [Yosiet]
257 stars

This strategy plots VWMA-based Bollinger bands on the 1-minute ES futures price chart for volatility analysis and potential breakout identification. It calculates dynamic long and short stop loss and take profit levels using percentage inputs relative to entry

Strategy v6 Volatility & bandsSupport & resistance
Updated 6 weeks ago · None stated GitHub repository of V1.6 ES - Futures 1M No Repaint
218 stars

This strategy detects price breakouts above rolling highest highs or below lowest lows over a customizable lookback period. It includes optional volume spike and ATR volatility threshold filters, along with bar confirmation rules. Positions are taken upon conf

Strategy v6 BreakoutVolatility & bands
Updated 3 months ago · None stated GitHub repository of Breakout Strategy Template

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