Created
June 27, 2022 17:55
-
-
Save Wpkenpachi/84140c9f8f0eda0d7d15d5c57128105b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ | |
// © angelportinari17 | |
//@version=5 | |
strategy("Probabilistic Strategy", overlay=true, max_bars_back=5000, max_labels_count=500, margin_long=0.01, margin_short=0.01, initial_capital = 1000) | |
import Wpkenpachi/WpProbabilisticLib/1 as WpProb | |
// INPUTS | |
int days_before = input.int(1, "Qtd. Dias atrás", minval = 1, maxval = 50, step = 1) | |
int qtd_candles_before = input.int(89, "Qtd. Candles para trás", minval = 5, maxval = 500, step = 1) | |
opts() => ["NO", "AS_RED", "AS_GREEN"] | |
[opt_no, opt_as_red, opt_as_green] = opts() | |
string consider_dojis = input.string("NO", "Considerar Dojis", options=["NO", "AS_RED", "AS_GREEN"]) | |
float percent_diff_to_open_order = input.float(16, "Valor de %diff para entrar nas operações", minval=0, maxval=99, step=0.1) | |
float percent_to_break_pattern = input.float(9, "Valor de %diff para sair da operação", minval=0, maxval=100, step=0.1) | |
float qtd_tp_points_to_break_pattern = input.float(100, "Qtd em pontos de TAKE PROFIT", minval=1, step=1) | |
float qtd_sl_points_to_break_pattern = input.float(100, "Qtd em pontos de STOP LOSS", minval=1, step=1) | |
int qtd_ctt_default = input.int(1, "Quantidade de contratos padrão", minval=1, maxval=10000, step=1) | |
float cap_per_ctt = input.float(1000, "R$ para cada 1 Ctt", minval=100) | |
float cap_min_to_order = input.float(100, "R$ valor minimo para abrir uma ordem de 1Ctt na B3", minval=100) | |
float max_profit_percent_day = input.float(15, "Máximo % valor de alavancagem diária") | |
float max_drawdown = input.float(-800, "R$ valor de drawdown máximo do capital") | |
// === | |
// STRATEGY VAS | |
var bool reached_max_drawdown = float(0) | |
var bool reached_daily_profit = bool(na) | |
var float daily_profit_meta = float(0) | |
var float last_day_profit_value = float(0) | |
var float daily_profit_value = float(0) | |
// CONSTANTS | |
MS_IN_3H = (1000 * 60 * 60 * 3) | |
MS_IN_24H = (1000 * 60 * 60 * 24) | |
current_time = time - MS_IN_3H | |
// === | |
// TIME AND DATE FUNCTIONS AND CONSTS CHECKERS | |
IsSessionStart(sessionTime, sessionTimeZone=syminfo.timezone) => | |
inSess = not na(time(timeframe.period, sessionTime, sessionTimeZone)) | |
inSess and not inSess[1] | |
IsLastBarSession(sessionTime, sessionTimeZone=syminfo.timezone) => | |
var int lastBarHour = na | |
var int lastBarMinute = na | |
var int lastBarSecond = na | |
inSess = not na(time(timeframe.period, sessionTime, sessionTimeZone)) | |
if not inSess and inSess[1] | |
lastBarHour := hour[1] | |
lastBarMinute := minute[1] | |
lastBarSecond := second[1] | |
hour == lastBarHour and minute == lastBarMinute and second == lastBarSecond | |
TimeInRange(tf, session) => | |
time(tf, session) != 0 | |
IsBetween(period) => | |
TimeInRange(timeframe.period, period) | |
isToday = year(timenow) == year(time) and month(timenow) == month(time) and dayofmonth(timenow) == dayofmonth(time) + days_before ? true : false | |
isDaysBefore = year(timenow) == year(time) and month(timenow) == month(time) and dayofmonth(timenow - MS_IN_24H) == dayofmonth(time) + days_before ? true : false | |
isUnAvaliableTime = (IsBetween("0900-0916") or IsBetween("1000-1016") or IsBetween("1700-1802")) | |
// === | |
// VARS ABOUE TIME | |
var int first_bar_today = int(na) | |
var int last_bar_today = int(na) | |
if isToday and IsBetween("0900-0901") and barstate.isfirst | |
first_bar_today := bar_index | |
if isToday and barstate.islast | |
last_bar_today := bar_index | |
var first_bar_last_day = int(na) | |
var last_bar_last_day = int(na) | |
if isDaysBefore and IsBetween("0900-0901") | |
first_bar_last_day := bar_index | |
if isDaysBefore and IsLastBarSession("1754-1800") | |
last_bar_last_day := bar_index | |
// === | |
// Utils functions | |
// === | |
// Strategy Calcs and variables | |
get_daily_profit() => | |
math.round(strategy.netprofit - last_day_profit_value, 2) | |
get_daily_profit_percent() => | |
math.round(get_daily_profit() * 100 / strategy.equity, 2) | |
// | |
// RUN TIME | |
if isDaysBefore and IsBetween("0900-0901") | |
label.new(bar_index, y=na, text=str.tostring(bar_index), style=label.style_none, textcolor=color.white, yloc=yloc.abovebar, size=size.huge) | |
if isDaysBefore and IsLastBarSession("1754-1800") | |
label.new(bar_index, y=na, text=str.tostring(bar_index), style=label.style_none, textcolor=color.white, yloc=yloc.abovebar, size=size.huge) | |
if ta.change(time("1D")) | |
days_before += 1 | |
if IsBetween("0900-0903") and IsSessionStart("0900-0905") // Starting Day | |
reached_daily_profit := false | |
daily_profit_meta := math.round((strategy.equity * max_profit_percent_day) / 100, 2) | |
//string txt = "Profit Open: Cur[" + str.tostring(get_daily_profit()) + "] Meta[" + str.tostring(daily_profit_meta) + "]" | |
//label.new(bar_index, y=na, text=txt, style=label.style_none, textcolor=color.green, yloc=yloc.abovebar, size=size.normal) | |
if IsBetween("1754-1800") and IsLastBarSession("1754-1800") // Closing Day | |
daily_profit_value := 0 | |
//string txt = "Profit Close: Reached[" + str.tostring(get_daily_profit()) + " " + str.tostring(get_daily_profit_percent()) + "%] Meta[" + str.tostring(daily_profit_meta) + "]" | |
//label.new(bar_index, y=na, text=txt, style=label.style_none, textcolor=color.green, yloc=yloc.belowbar, size=size.normal) | |
last_day_profit_value := get_daily_profit() | |
if barstate.isnew | |
qtd_ctt_default := math.round(strategy.equity / cap_per_ctt) | |
reached_max_drawdown := strategy.equity <= max_drawdown | |
// Reached the daily profit meta | |
reached_daily_profit := get_daily_profit() >= daily_profit_meta | |
var float diff_p = int(0) | |
var int lower_p = int(0) | |
[d_p, l_p] = WpProb.CandleTypePercentDiff(open, close, qtd_candles_before, consider_dojis) | |
if ta.change(time("1")) | |
diff_p := d_p | |
lower_p := l_p | |
if barstate.isnew and not na(bar_index[qtd_candles_before]) | |
bool entry_condition = diff_p >= percent_diff_to_open_order and not strategy.opentrades | |
bool close_condition_diff_back = diff_p <= percent_to_break_pattern and strategy.opentrades | |
bool has_equity_to_open_order = (strategy.equity / cap_min_to_order) >= 1 | |
call_direction = lower_p == 1 ? strategy.long : strategy.short | |
string call_direction_name = lower_p == 1 ? "BUY" : "SELL" | |
if has_equity_to_open_order and not reached_max_drawdown and not reached_daily_profit | |
strategy.entry(call_direction_name, call_direction, qtd_ctt_default, when = entry_condition) | |
strategy.exit("[TAKE OR STOP]", call_direction_name, profit = qtd_tp_points_to_break_pattern, loss = qtd_sl_points_to_break_pattern) | |
strategy.close_all(when = close_condition_diff_back, comment = "[DIFF_BACK]") | |
strategy.close_all(when = reached_daily_profit, comment = "[DAILY_PROFIT_REACHED] " + str.tostring(get_daily_profit_percent()) + "%") | |
strategy.close_all(when = strategy.opentrades and IsBetween("1730-1800"), comment = "[MARKET_WILL_CLOSE]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment