From 1d0dee8915a90060b3acaa0f9fdb905c9542ce00 Mon Sep 17 00:00:00 2001 From: Alberto Cuadra Lara Date: Sun, 24 Apr 2022 10:30:21 +0200 Subject: [PATCH] Add: include FLAG to analyze only the given tick (pair asset) --- weighted_strategy.pine | 82 +++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 37 deletions(-) diff --git a/weighted_strategy.pine b/weighted_strategy.pine index 2625399..52f8e74 100644 --- a/weighted_strategy.pine +++ b/weighted_strategy.pine @@ -1,5 +1,5 @@ // ************************************************************************************************************************************************************************************************************************************************************************* -// Acrypto - Weigthed Strategy v1.4.8 +// Acrypto - Weigthed Strategy v1.4.9 // Github: https://github.com/AlbertoCuadra/algo_trading_weighted_strategy // // @@ -8,10 +8,10 @@ // License: © accry - This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License // https://creativecommons.org/licenses/by-nc-sa/4.0/ // -// Last update: 23/04/2022 +// Last update: 24/04/2022 // ************************************************************************************************************************************************************************************************************************************************************************* //@version=5 -strategy(title='Acrypto - Weighted Strategy v1.4.8', overlay=true, precision=2, commission_value=0.075, commission_type=strategy.commission.percent, initial_capital=1000, currency=currency.USD, default_qty_type=strategy.percent_of_equity, default_qty_value=100, slippage=1) +strategy(title='Acrypto - Weighted Strategy v1.4.9', overlay=true, precision=2, commission_value=0.075, commission_type=strategy.commission.percent, initial_capital=1000, currency=currency.USD, default_qty_type=strategy.percent_of_equity, default_qty_value=100, slippage=1) // ************************************************************************************************************************************************************************************************************************************************************************* // COMMENTS // ************************************************************************************************************************************************************************************************************************************************************************* @@ -101,9 +101,11 @@ source_6_top = input.source(close, 'TP-TOP at previous?', group='Potential TOP/B source_6_bottom = input.source(close, 'TP-BOTTOM at previous?', group='Potential TOP/BOTTOM') long_trail_perc = input.float(150, 'Trail volume Long (%)', minval=0.0, step=1, group='Potential TOP/BOTTOM') * 0.01 short_trail_perc = input.float(150, 'Trail volume Short (%)', minval=0.0, step=1, group='Potential TOP/BOTTOM') * 0.01 -// * Flags +// * Miscellaneous FLAG_SIGNALS = input.bool(true, 'Show Buy/Sell Signals ?', group='Miscellaneous') FLAG_SHADOWS = input.bool(true, 'Show shadows satisfied strategies ?', group='Miscellaneous') +FLAG_ONEPAIR = input.bool(false, 'Analyze only one pair asset ?', group='Miscellaneous') +pair_asset = input.string('BINANCE:BTCUSDT', 'Pair asset to analyze', group='Miscellaneous') // * Alarms alarm_label_long = input.string('Buy', 'Label open long', group='Basic alarm system') alarm_label_short = input.string('Sell', 'Label open short', group='Basic alarm system') @@ -130,7 +132,13 @@ var profit_perc = 0. // Percentage to take profits since open position or last T var nextTP = 0. // Next target to take profits var since_entry = 0 // Number of bars since open last postion var since_close = 0 // Number of bars since close or TP/STOP last position - +var pair_asset_prime = syminfo.tickerid // Actual pair asset +var FLAG_VISIBLE = true +// * Check FLAG analyze only one pair asset +if FLAG_ONEPAIR + FLAG_VISIBLE := pair_asset == pair_asset_prime +else + FLAG_VISIBLE := true // * Compute profit quantity and profit percentage if strategy.position_size > 0 profit_qty := long_profit_qty @@ -348,7 +356,7 @@ condition_OS_several = condition_OS[1] and condition_OS[2] or condition_OS[1] an // ************************************************************************************************************************************************************************************************************************************************************************* // STRATEGY ENTRIES AND EXITS // ************************************************************************************************************************************************************************************************************************************************************************* -if time >= start and time <= end +if time >= start and time <= end and FLAG_VISIBLE // *************************************************************************************************************************************************************************** // * Set Entries // *************************************************************************************************************************************************************************** @@ -619,72 +627,72 @@ plotchar(strategy.wintrades/strategy.closedtrades*100, "Winrate [%]", "", locati // ************************************************************************************************************************************************************************************************************************************************************************* // * strategy 1: MACD // ************************************************************************************************************************************************************************************************************************************************************************* -plot(trend, 'Trend', style=plot.style_columns, color=MACD > signal ? color.new(color.teal, 30) : color.new(color.gray, 30), display=display.none) -plot(MACD, 'MACD', color=color.new(color.blue, 0), display=display.none) -plot(signal, 'Signal', color=color.new(color.orange, 0), display=display.none) -plotshape(long_signal1 and FLAG_SIGNALS ? up : na, 'Buy MACD', text='MACD', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0), display=display.none) -plotshape(short_signal1 and FLAG_SIGNALS ? dn : na, 'Sell MACD', text='MACD', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.gray, 0), textcolor=color.new(color.white, 0), display=display.none) +plot(FLAG_VISIBLE ? trend:na, 'Trend', style=plot.style_columns, color=MACD > signal ? color.new(color.teal, 30) : color.new(color.gray, 30), display=display.none) +plot(FLAG_VISIBLE ? MACD:na, 'MACD', color=color.new(color.blue, 0), display=display.none) +plot(FLAG_VISIBLE ? signal:na, 'Signal', color=color.new(color.orange, 0), display=display.none) +plotshape(long_signal1 and FLAG_SIGNALS and FLAG_VISIBLE ? up : na, 'Buy MACD', text='MACD', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0), display=display.none) +plotshape(short_signal1 and FLAG_SIGNALS and FLAG_VISIBLE ? dn : na, 'Sell MACD', text='MACD', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.gray, 0), textcolor=color.new(color.white, 0), display=display.none) // ************************************************************************************************************************************************************************************************************************************************************************* // * strategy 2: Stoch RSI // ************************************************************************************************************************************************************************************************************************************************************************* -plotshape(long_signal2 and FLAG_SIGNALS ? up : na, title='Buy Stoch RSI', text='SRSI', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0), display=display.none) -plotshape(short_signal2 and FLAG_SIGNALS ? dn : na, title='Sell Stoch RSI', text='SRSI', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.gray, 0), textcolor=color.new(color.white, 0), display=display.none) +plotshape(long_signal2 and FLAG_SIGNALS and FLAG_VISIBLE ? up : na, title='Buy Stoch RSI', text='SRSI', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0), display=display.none) +plotshape(short_signal2 and FLAG_SIGNALS and FLAG_VISIBLE ? dn : na, title='Sell Stoch RSI', text='SRSI', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.gray, 0), textcolor=color.new(color.white, 0), display=display.none) // ************************************************************************************************************************************************************************************************************************************************************************* // * strategy 3: RSI // ************************************************************************************************************************************************************************************************************************************************************************* -plotshape(long_signal3 and FLAG_SIGNALS ? up : na, title='Buy RSI', text='RSI', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0), display=display.none) -plotshape(short_signal3 and FLAG_SIGNALS ? dn : na, title='Sell RSI', text='RSI', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.gray, 0), textcolor=color.new(color.white, 0), display=display.none) +plotshape(long_signal3 and FLAG_SIGNALS and FLAG_VISIBLE ? up : na, title='Buy RSI', text='RSI', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0), display=display.none) +plotshape(short_signal3 and FLAG_SIGNALS and FLAG_VISIBLE ? dn : na, title='Sell RSI', text='RSI', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.gray, 0), textcolor=color.new(color.white, 0), display=display.none) // ************************************************************************************************************************************************************************************************************************************************************************* // * strategy 4: Supertrend // ************************************************************************************************************************************************************************************************************************************************************************* -plotshape(long_signal4 and FLAG_SIGNALS ? up : na, title='Buy Supertrend', text='Supertrend', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0), display=display.none) -plotshape(short_signal4 and FLAG_SIGNALS ? dn : na, title='Sell Supertrend', text='Supertrend', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.gray, 0), textcolor=color.new(color.white, 0), display=display.none) +plotshape(long_signal4 and FLAG_SIGNALS and FLAG_VISIBLE ? up : na, title='Buy Supertrend', text='Supertrend', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0), display=display.none) +plotshape(short_signal4 and FLAG_SIGNALS and FLAG_VISIBLE ? dn : na, title='Sell Supertrend', text='Supertrend', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.gray, 0), textcolor=color.new(color.white, 0), display=display.none) // ************************************************************************************************************************************************************************************************************************************************************************* // * strategy 5: MA CROSS // ************************************************************************************************************************************************************************************************************************************************************************* -plotshape(long_signal5 and FLAG_SIGNALS ? up : na, title='Buy MA CROSS', text='MA CROSS', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0), display=display.none) -plotshape(short_signal5 and FLAG_SIGNALS ? dn : na, title='Sell MA CROSS', text='MA CROSS', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.gray, 0), textcolor=color.new(color.white, 0), display=display.none) +plotshape(long_signal5 and FLAG_SIGNALS and FLAG_VISIBLE ? up : na, title='Buy MA CROSS', text='MA CROSS', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0), display=display.none) +plotshape(short_signal5 and FLAG_SIGNALS and FLAG_VISIBLE ? dn : na, title='Sell MA CROSS', text='MA CROSS', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.gray, 0), textcolor=color.new(color.white, 0), display=display.none) // ************************************************************************************************************************************************************************************************************************************************************************* // * STRATEGY 6: POTENTIAL TOP/BOTTOM // ************************************************************************************************************************************************************************************************************************************************************************* -plotshape(condition_OB_several ? dn : na, title='Top', text='T', location=location.abovebar, style=shape.labeldown, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0)) -plotshape(condition_OS_several ? up : na, title='Bottom', text='B', location=location.belowbar, style=shape.labelup, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0)) +plotshape(condition_OB_several and FLAG_VISIBLE ? dn : na, title='Top', text='T', location=location.abovebar, style=shape.labeldown, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0)) +plotshape(condition_OS_several and FLAG_VISIBLE ? up : na, title='Bottom', text='B', location=location.belowbar, style=shape.labelup, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0)) // ************************************************************************************************************************************************************************************************************************************************************************* // * Buy/Sell signals // ************************************************************************************************************************************************************************************************************************************************************************* w_total_long = weight_total(long_signal1, long_signal2, long_signal3, long_signal4, long_signal5) w_total_short = weight_total(short_signal1, short_signal2, short_signal3, short_signal4, short_signal5) -plotshape(w_total_long >= weight_trigger and FLAG_SIGNALS ? up : na, title='Buy Weigthed strategy', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0)) -plotshape(w_total_short >= weight_trigger and FLAG_SIGNALS ? dn : na, title='Sell Weigthed strategy', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.gray, 0), textcolor=color.new(color.white, 0)) +plotshape(w_total_long >= weight_trigger and FLAG_SIGNALS and FLAG_VISIBLE ? up : na, title='Buy Weigthed strategy', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.teal, 0), textcolor=color.new(color.white, 0)) +plotshape(w_total_short >= weight_trigger and FLAG_SIGNALS and FLAG_VISIBLE ? dn : na, title='Sell Weigthed strategy', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.gray, 0), textcolor=color.new(color.white, 0)) // ************************************************************************************************************************************************************************************************************************************************************************* // * Stop loss targets // ************************************************************************************************************************************************************************************************************************************************************************* -plot(series=(strategy.position_size > 0) ? price_stop_long : na, color=color.new(color.gray, 30), style=plot.style_cross, linewidth=2, title="Long Stop Loss") -plot(series=(strategy.position_size < 0) ? price_stop_short : na, color=color.new(color.gray, 30), style=plot.style_cross, linewidth=2, title="Short Stop Loss") +plot(series=(strategy.position_size > 0) and FLAG_VISIBLE ? price_stop_long : na, color=color.new(color.gray, 30), style=plot.style_cross, linewidth=2, title="Long Stop Loss") +plot(series=(strategy.position_size < 0) and FLAG_VISIBLE ? price_stop_short : na, color=color.new(color.gray, 30), style=plot.style_cross, linewidth=2, title="Short Stop Loss") // ************************************************************************************************************************************************************************************************************************************************************************* // * TP targets // ************************************************************************************************************************************************************************************************************************************************************************* -plot(strategy.position_size > 0 or strategy.position_size < 0 ? nextTP : na, color=color.new(color.aqua, 30), style=plot.style_cross, linewidth=2, title="Next TP") +plot(strategy.position_size > 0 or strategy.position_size < 0 and FLAG_VISIBLE ? nextTP : na, color=color.new(color.aqua, 30), style=plot.style_cross, linewidth=2, title="Next TP") // ************************************************************************************************************************************************************************************************************************************************************************* // * All strategies // ************************************************************************************************************************************************************************************************************************************************************************* -mPlot = plot(ohlc4, title='Price ohlc4', style=plot.style_circles, linewidth=0, display=display.none) -upPlot = plot((long_signal1 or long_signal2 or long_signal3 or long_signal4 or long_signal5) and (w_total_long > w_total_short) ? up : na, title='Up Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.aqua, 0), display=display.none) -dnPlot = plot((short_signal1 or short_signal2 or short_signal3 or short_signal4 or short_signal5) and (w_total_short > w_total_long) ? dn : na, title='Down Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.gray, 0), display=display.none) -plotchar(weight_trigger, "Trigger strategies", "", location.top, size = size.tiny, color=color.new(color.orange, 0)) -plotchar(w_total_long, "Satisfied Long strategies", "", location.top, size = size.tiny, color=w_total_long >= weight_trigger ? color.orange : color.gray) -plotchar(w_total_short, "Satisfied Short strategies", "", location.top, size = size.tiny, color=w_total_long >= weight_trigger ? color.orange : color.gray) -plotshape((long_signal1 or long_signal2 or long_signal3 or long_signal4 or long_signal5) and (w_total_long > w_total_short) ? up : na, title='UpTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.aqua, 0), display=display.none) -plotshape((short_signal1 or short_signal2 or short_signal3 or short_signal4 or short_signal5) and (w_total_short > w_total_long) ? dn : na, title='DownTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.gray, 0), display=display.none) -fill(mPlot, upPlot, title='UpTrend Highligter', color=colors('buy', FLAG_SHADOWS ? 80:100)) -fill(mPlot, dnPlot, title='DownTrend Highligter', color=colors('sell', FLAG_SHADOWS ? 80:100)) +mPlot = plot(FLAG_VISIBLE ? ohlc4:na, title='Price ohlc4', style=plot.style_circles, linewidth=0, display=display.none) +upPlot = plot((long_signal1 or long_signal2 or long_signal3 or long_signal4 or long_signal5) and (w_total_long > w_total_short) and FLAG_VISIBLE ? up : na, title='Up Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.aqua, 0), display=display.none) +dnPlot = plot((short_signal1 or short_signal2 or short_signal3 or short_signal4 or short_signal5) and (w_total_short > w_total_long) and FLAG_VISIBLE ? dn : na, title='Down Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.gray, 0), display=display.none) +plotchar(FLAG_VISIBLE ? weight_trigger:na, "Trigger strategies", "", location.top, size = size.tiny, color=color.new(color.orange, 0)) +plotchar(FLAG_VISIBLE ? w_total_long:na, "Satisfied Long strategies", "", location.top, size = size.tiny, color=w_total_long >= weight_trigger ? color.orange : color.gray) +plotchar(FLAG_VISIBLE ? w_total_short:na, "Satisfied Short strategies", "", location.top, size = size.tiny, color=w_total_long >= weight_trigger ? color.orange : color.gray) +plotshape((long_signal1 or long_signal2 or long_signal3 or long_signal4 or long_signal5) and (w_total_long > w_total_short) and FLAG_VISIBLE ? up : na, title='UpTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.aqua, 0), display=display.none) +plotshape((short_signal1 or short_signal2 or short_signal3 or short_signal4 or short_signal5) and (w_total_short > w_total_long) and FLAG_VISIBLE ? dn : na, title='DownTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.gray, 0), display=display.none) +fill(mPlot, upPlot, title='UpTrend Highligter', color=colors('buy', FLAG_SHADOWS and FLAG_VISIBLE ? 80:100)) +fill(mPlot, dnPlot, title='DownTrend Highligter', color=colors('sell', FLAG_SHADOWS and FLAG_VISIBLE ? 80:100)) // ************************************************************************************************************************************************************************************************************************************************************************* // MONTHLY TABLE PERFORMANCE - Developed by @QuantNomad // ************************************************************************************************************************************************************************************************************************************************************************* show_performance = input.bool(true, 'Show Monthly Performance ?', group='Performance - credits: @QuantNomad') prec = input(2, 'Return Precision', group='Performance - credits: @QuantNomad') -if show_performance +if show_performance and FLAG_VISIBLE new_month = month(time) != month(time[1]) new_year = year(time) != year(time[1])