FrizBugLibrary "FrizBug"
Debug Tools | Pinescript Debugging Tool Kit
All in one Debugger - the benefit of wrapper functions to simply wrap variables or outputs and have the code still execute the same. Perfect for Debugging on Pine
str(inp)
Overloaded tostring like Function for all type+including Object Variables will also do arrays and matricies of all Types
Parameters:
inp : All types
Returns: string
print_label(str, x_offset, y, barstate, style, color, textcolor, text_align, size)
Label Helper Function - only needs the Str input to work
Parameters:
str :
x_offset : offset from last bar + or -
y : price of label
barstate : barstate built in variable
style : label style settin7
color : color setting
textcolor : textcolor
text_align : text align setting
size : text_sise
Returns: label
init()
initializes the database arrays
Returns: tuple | 2 matrix (1 matrix is varip(live) the other is reagular var (Bar))
update(log, live, live_console, log_console, live_lbl, log_lbl)
Put at the very end of your code / This updates all of the consoles
Parameters:
log : This matrix is the one used for Bar updates
live : This matrix is the one used for Real Time updates
live_console : on_offs for the consoles and lbls - call in the update function
log_console : on_offs for the consoles and lbls - call in the update function
live_lbl : on_offs for the consoles and lbls - call in the update function
log_lbl : on_offs for the consoles and lbls - call in the update function
Returns: void
log(log, inp, str_label, off, rows, index_cols, bars_back)
Function Will push to the Console offset to the right of Current bar, This is the main Console - it has 2 Feeds left and right (changeable)"
Parameters:
log : Matrix - Log or Live
inp : All types
str_label : (optional) This input will label it on the feed
off : Useful for when you don't want to remove the function"
rows : when printing or logging a matrix this will shorten the output will show last # of rows"
index_cols : When printing or logging a array or matrix this will shorten the array or the columns of a matrix by the #"
bars_back : Adjustment for Bars Back - Default is 1 (0 for barstate.islast)"
Returns: inp - all types (The log and print functions can be used as wrapper functions see usage below for examples)
Print(log, str_label, off, bars_back)
Function can be used to send information to a label style Console, Can be used as a wrapper function, Similar to str.format use with str()
Parameters:
log :
str_label : (optional) Can be used to label Data sent to the Console
off : Useful for when you don't want to remove the function
bars_back : Adjustment for Bars Back - Default is 1 (0 for barstate.islast)
Returns: string
print(inp, str_label, off, bars_back)
This Function can be used to send information to a label style Console, Can be used as a wrapper function, Overload print function
Parameters:
inp : All types
str_label : string (optional) Can be used to label Data sent to the Console
off : Useful for when you don't want to remove the function
bars_back : Adjustment for Bars Back - Default is 1 (0 for barstate.islast)
Returns: inp - all types (The log and print functions can be used as wrapper functions see usage below for examples)
Credits:
@kaigouthro - for the font library
@RicardoSantos - for the concept I used to make this
Thanks!
Use cases at the bottom
Bug
Pinescript v4 - The Holy Grail (Trailing Stop)After studying several other scripts, I believe I have found the Holy Grail! (Or perhaps I've just found a bug with Tradingview's Pinescript v4 language) Anyhow, I'm publishing this script in the hope that someone smarter than myself could shed some light on the fact that adding a trailing stop to any strategy seems to make it miraculously...no that's an understatement...incredulously, stupendously, mind-bendingly profitable. I'm talking about INSANE profit factors, higher than 200x, with drawdowns of <10%. Sounds too good to be true? Maybe it is...or you could hook it up to your LIVE broker, and pray it doesn't explode. This is an upgraded version of my original Pin Bar Strategy.
Recommended Chart Settings:
Asset Class: Forex
Time Frame: H1
Long Entry Conditions:
a) Exponential Moving Average Fan up trend
b) Presence of a Bullish Pin Bar
c) Pin Bar pierces the Exponential Moving Average Fan
Short Entry Conditions:
a) Exponential Moving Average down trend
b) Presence of a Bearish Pin Bar
c) Pin Bar pierces the Exponential Moving Average Fan
Exit Conditions:
a) Trailing stop is hit
b) Moving Averages cross-back (optional)
c) It's the weekend
Default Robot Settings:
Equity Risk (%): 3 //how much account balance to risk per trade
Stop Loss (x*ATR, Float): 0.5 //stoploss = x * ATR, you can change x
Stop Loss Trail Points (Pips): 1 //the magic sauce, not sure how this works
Stop Loss Trail Offset (Pips): 1 //the magic sauce, not sure how this works
Slow SMA (Period): 50 //slow moving average period
Medium EMA (Period): 18 //medium exponential moving average period
Fast EMA (Period): 6 //fast exponential moving average period
ATR (Period): 14 // average true range period
Cancel Entry After X Bars (Period): 3 //cancel the order after x bars not triggered, you can change x
Backtest Results (2019 to 2020, H1, Default Settings):
AUDUSD - 1604% profit, 239.6 profit factor, 4.9% drawdown (INSANE)
NZDUSD - 1688.7% profit, 100.3 profit factor, 2.5% drawdown
GBPUSD - 1168.8% profit, 98.7 profit factor, 0% drawdown
USDJPY - 900.7% profit, 93.7 profit factor, 4.9% drawdown
USDCAD - 819% profit, 31.7 profit factor, 8.1% drawdown
EURUSD - 685.6% profit, 26.8 profit factor, 5.9% drawdown
USDCHF - 1008% profit, 18.7 profit factor, 8.6% drawdown
GBPJPY - 1173.4% profit, 16.1 profit factor, 7.9% drawdown
EURAUD - 613.3% profit, 14.4 profit factor, 9.8% drawdown
AUDJPY - 1619% profit, 11.26 profit factor, 9.1% drawdown
EURJPY - 897.2% profit, 6 profit factor, 13.8% drawdown
EURGBP - 608.9% profit, 5.3 profit factor, 9.8% drawdown (NOT TOO SHABBY)
As you can clearly see above, this forex robot is projected by the Tradingview backtester to be INSANELY profitable for all common forex pairs. So what was the difference between this strategy and my previous strategies? Check my code and look for "trail_points" and "trail_offset"; you can even look them up in the PineScript v4 documentation. They specify a trailing stop as the exit condition, which automatically closes the trade if price reverses against you.
I however suspect that the backtester is not properly calculating intra-bar price movement, and is using a simplified model. With this simplfied approach, the trailing stop code becomes some sort of "holy grail" generator, making every trade entered profitable.
Risk Warning:
This is a forex trading strategy that involves high risk of equity loss, and backtest performance will not equal future results. You agree to use this script at your own risk.
Hint:
To get more realistic results, and *maybe* overcome the intrabar simulation error, change the settings to: "Stop Loss Trail Points (pips)": 100
I am not sure if this eradicates the bug, but the entries and exits look more proper, and the profit factors are more believable.
Barssince Context TestThis is just published for visibility as a public service until the Pine devs are able to fix or document this behavior. The barssince() function returns different values when inside a conditional context. As long as it can be documented (and relied upon), this could be a pretty cool feature, but right now this is now how I read the help documentation to describe the function's intended behavior.
Anyway, in the script you'll see that test and test2 agree on red bars, but on green bars test gets a much lower value, which was pretty shocking to me until I traced down this particular cause within my larger script.
Function call bugThis is a code to showcase a function call bug in PineScript.
Problem description:
When a function is called from inside of if-then scope, this function may return an invalid result.
Code description:
Two identical code pieces are used to perform simple calculation. The only difference is that one piece has a part of its code wrapped into a function.
Expected result:
All green and red crosses should appear on the same candles. These crosses are calculated by identical code.
Actual result:
Some red crosses appear on the candles they should not appear on.
[RESEARCH] Percentrank BugI found a bug with built-in percentrank function. Sometimes it gives unexpected and incorrect results. You can see a one of them on the chart.
ALL scripts which use percentrank function are affected. No matter which version they use, no matter who is their author - ALL scripts which use this built-in function can work incorrectly.
If you want to avoid this bug use _percentrank function (the "shim" ) - you can find it in the script.
NOTE: Don't push on TradingView Support or Pine Core Team because they already know about this issue and work on the fix. I publish it to warn you.
Help Requested - "Time-Slot" Delay for Crossover StrategyHello Tradingview Community,
I've been playing around with some various volume-based indicators, and recently have been experiencing a delay in the entries for my strategy for 2 minutes in this scenario. If the time is adjusted, it also is delayed 2 bars.
In this example, the "Short" position should enter when the blue line Crosses under the upper green line, and visa vera for the "Long" position. A simplified Pine Script is available below as well.
I have Googled this issue and attempted to modify the code with => criteria instead, but too no avail. Would appreciate any help or anyone who could point me in the right direction on this Time Delay Issue. Thanks!
function bugFunctions with parameters do not work correctly: the returned value is sometimes different from the expected one.
The example script works correctly only when idx = 1 (instead of the current line 6).
strategy.direction.all() bug - work aroundthe way to work around the bug for this specific strategy, altho its not as efficient as it would if both orders activation was possible.