Блог им. raxat
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // This strategy has been created for illustration purposes only and should not be relied upon as a basis for buying, selling, or holding any asset or security. // © Diamond //@version=5 strategy('SMA Golden Short Strategy', overlay=true, calc_on_every_tick=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value=0.04, commission_type=strategy.commission.percent) //Inputs smaFast = input.int(title='Fast SMA', defval=50, minval=1) smaSlow = input.int(title='Slow SMA', defval=200, minval=1)
//Options to configure backtest trade range startDate = input.time(title='Start Date', defval=timestamp('01 Jan 1998 00:00')) endDate = input.time(title='End Date', defval=timestamp('31 Dec 2070 23:59')) //Calculations fastSMA = ta.sma(close, smaFast) slowSMA = ta.sma(close, smaSlow)
//Plot plot(series=fastSMA, color=color.new(color.orange, 0), linewidth=2) plot(series=slowSMA, color=color.new(color.blue, 0), linewidth=3)
//Conditions inDateRange = time >= startDate and time < endDate longOpenCondition = ta.crossover(fastSMA, slowSMA) shortOpenCondition = ta.crossunder(fastSMA, slowSMA)
//Trading if longOpenCondition and inDateRange strategy.entry(id='long', direction=strategy.long, comment=' ') label.new(bar_index, na, text='Long', size=size.normal, style=label.style_label_up, color=color.blue, textcolor=color.white, yloc=yloc.belowbar) if shortOpenCondition and inDateRange strategy.entry(id='short', direction=strategy.short, comment=' ') label.new(bar_index, na, text='Short', size=size.normal, style=label.style_label_down, color=color.red, textcolor=color.white, yloc=yloc.abovebar)
//Stop Loss / Take Profit useStopLoss = input(false, title='Use Stop Loss & Take Profit') sl_inp = input(defval = 1.0, title='Stop Loss %')/100.0 tp_inp = input(defval = 1.5, title='Take Profit %')/100.0 stop_level = strategy.position_avg_price * (1 - sl_inp) take_level = strategy.position_avg_price * (1 + tp_inp) stop_level_short = strategy.position_avg_price * (1 + sl_inp) take_level_short = strategy.position_avg_price * (1 - tp_inp)
//if useStopLoss and strategy.position_size > 0 and strategy.openprofit > 0 // strategy.exit(id="long", comment = "Take Profit Long +" + str.tostring(math.round(strategy.openprofit)), limit = take_level) //if useStopLoss and strategy.position_size > 0 and strategy.openprofit < 0 // strategy.exit(id="long", comment = "Stop Loss Long -" + str.tostring(math.round(strategy.openprofit)), stop = stop_level) if useStopLoss and strategy.position_size < 0 and strategy.openprofit > 0 strategy.exit(id="short", comment = "Take Profit Short +" + str.tostring(math.round(strategy.openprofit)), limit = take_level_short) if useStopLoss and strategy.position_size < 0 and strategy.openprofit < 0 strategy.exit(id="short", comment = "Stop Loss Short -" + str.tostring(math.round(strategy.openprofit)), stop = stop_level_short)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // This strategy has been created for illustration purposes only and should not be relied upon as a basis for buying, selling, or holding any asset or security. // © Diamond //@version=5 strategy('SMA Golden Short Strategy', overlay=true, calc_on_every_tick=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value=0.04, commission_type=strategy.commission.percent) //Inputs smaFast = input.int(title='Fast SMA', defval=50, minval=1) smaSlow = input.int(title='Slow SMA', defval=200, minval=1) //Options to configure backtest trade range startDate = input.time(title='Start Date', defval=timestamp('01 Jan 1998 00:00')) endDate = input.time(title='End Date', defval=timestamp('31 Dec 2070 23:59')) //Calculations fastSMA = ta.sma(close, smaFast) slowSMA = ta.sma(close, smaSlow) //Plot plot(series=fastSMA, color=color.new(color.orange, 0), linewidth=2) plot(series=slowSMA, color=color.new(color.blue, 0), linewidth=3) //Conditions inDateRange = time >= startDate and time < endDate longOpenCondition = ta.crossover(fastSMA, slowSMA) shortOpenCondition = ta.crossunder(fastSMA, slowSMA) //Trading if longOpenCondition and inDateRange strategy.entry(id='long', direction=strategy.long, comment=' ') label.new(bar_index, na, text='Long', size=size.normal, style=label.style_label_up, color=color.blue, textcolor=color.white, yloc=yloc.belowbar) if shortOpenCondition and inDateRange strategy.entry(id='short', direction=strategy.short, comment=' ') label.new(bar_index, na, text='Short', size=size.normal, style=label.style_label_down, color=color.red, textcolor=color.white, yloc=yloc.abovebar) //Stop Loss / Take Profit useStopLoss = input(false, title='Use Stop Loss & Take Profit') sl_inp = input(defval = 1.0, title='Stop Loss %')/100.0 tp_inp = input(defval = 1.5, title='Take Profit %')/100.0 stop_level = strategy.position_avg_price * (1 - sl_inp) take_level = strategy.position_avg_price * (1 + tp_inp) stop_level_short = strategy.position_avg_price * (1 + sl_inp) take_level_short = strategy.position_avg_price * (1 - tp_inp) //if useStopLoss and strategy.position_size > 0 and strategy.openprofit > 0 // strategy.exit(id="long", comment = "Take Profit Long +" + str.tostring(math.round(strategy.openprofit)), limit = take_level) //if useStopLoss and strategy.position_size > 0 and strategy.openprofit < 0 // strategy.exit(id="long", comment = "Stop Loss Long -" + str.tostring(math.round(strategy.openprofit)), stop = stop_level) if useStopLoss and strategy.position_size < 0 and strategy.openprofit > 0 strategy.exit(id="short", comment = "Take Profit Short +" + str.tostring(math.round(strategy.openprofit)), limit = take_level_short) if useStopLoss and strategy.position_size < 0 and strategy.openprofit < 0 strategy.exit(id="short", comment = "Stop Loss Short -" + str.tostring(math.round(strategy.openprofit)), stop = stop_level_short)