// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // @algomojo // Webhook Url : https://amapi.algomojo.com/v1/PlaceOrder // Algomojo Trading Strategy Carry Forward Strategy/Intraday Strategy with Target/Stoploss/Trailing Stoploss // Supertrend Version 2.0 //////////////////////////////////////////////////////////////////////////////////////////////////////////// //Trading Block Description //Block 1 : API Controls + Algomojo Input Controls //Block 2 : Autotrading API data configuration //Block 3 : Backtesting Controls & Target and Stoploss Controls //Block 4 : Trading Strategy and Controls (write your trading strategy in the block //Block 5 : Intraday Function and Buy and Sell Signal Mapping (Signal Mapping is required) /////////////////////////////////////////////////////////////////////////////////////////////////////////// //@version=5 strategy('SuperTrend with EMA Filter - Algomojo', shorttitle='Supertrend + EMA Filter Algomojo', overlay=true,process_orders_on_close=true) import algomojo/automation/22 //Block 1 : API Controls + Algomojo Input Controls //Enter Your Algomojo API Key and API Secret Key am_apikey = input.string(title='API Key', defval='xxxxx', group='AlgoControls') am_apisecret = input.string(title='API Secret Key', defval='xxxxx', group='AlgoControls') am_broker = input.string(title='Broker', defval='ALICEBLUE', options=['ALICEBLUE', 'ANGELONE', 'FIRSTOCK', 'FYERS','GOODWILL','PAYTM','SAMCO','TRADEJINI','UPSTOX','ZEBU','ZERODHA'], group='AlgoControls') am_strategy = input.string(title='Strategy Name', defval='Supertrend Strategy', group='AlgoControls') am_symbol = input.string(title='Trading Symbol', defval='RELIANCE-EQ', group='AlgoControls') am_exchange = input.string(title='Exchange', defval='NSE', options=['NSE', 'NFO', 'MCX', 'BSE'], group='AlgoControls') am_quantity = input.int(title='Quantity', defval=1, group='AlgoControls') am_pricetype = input.string(title='Order Type', defval='MARKET', group='AlgoControls') am_product = input.string(title='Product', defval='MIS', options=['CNC', 'MIS', 'NRML'], group='AlgoControls') am_splitorder = input.string(title='Split Order?', defval='NO', options=['NO', 'YES'], group='AlgoControls') am_split_quantity = input.int(title='Split Quantity', defval=1, group='AlgoControls') am_Mode = input.string(title='Algo Mode', defval='ENABLE', options=['ENABLE', 'LONGONLY', 'SHORTONLY'], group='AlgoControls') ////////////////////////////////////////Block 1 Module Ends//////////////////////////////////////////////////////////////////////// //Block 2 : Autotrading API data configuration [BE,SX,BSR,SE,BX,SSR] = automation.algomodule(am_apikey, am_apisecret, am_broker, am_strategy, am_symbol, am_exchange,am_quantity, am_pricetype, am_product, am_splitorder, am_split_quantity) ////////////////////////////////////////Block 2 Module Ends//////////////////////////////////////////////////////////////////////// //Block 3 : Backtesting Controls & Live Automation Purpose FromDay = input.int(defval=1, title='From Day', minval=1, maxval=31, group='Backtesting') FromMonth = input.int(defval=1, title='From Month', minval=1, maxval=12, group='Backtesting') FromYear = input.int(defval=2018, title='From Year', minval=999, group='Backtesting') ToDay = input.int(defval=1, title='To Day', minval=1, maxval=31, group='Backtesting') ToMonth = input.int(defval=1, title='To Month', minval=1, maxval=12, group='Backtesting') ToYear = input.int(defval=9999, title='To Year', minval=999, group='Backtesting') start = timestamp(FromYear, FromMonth, FromDay, 00, 00) finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) window() => time >= start and time <= finish ? true : false highlighting = input.bool(title='Highlighter On/Off ?', defval=true, group='Intraday Controls') barcoloring = input.bool(title='Bar Coloring On/Off ?', defval=true, group='Intraday Controls') intraday = input.bool(title='Intraday On/Off ?', defval=false, group='Intraday Controls') marketSession = input.session(title='Market session', defval='0915-1500', confirm=false, group='Intraday Controls') risk = input.bool(title='Stoploss/Target On/Off', defval=false, group='Stoploss/Target Controls') type = input.string(title='Type', defval='FIXED', options=['FIXED', 'PERCENTAGE', 'VOLATILITY'], group='Stoploss/Target Controls') stoploss = input.float(defval=10.0, title='Stoploss', group='Stoploss/Target Controls') target = input.float(defval=20.0, title='Target', group='Stoploss/Target Controls') TickSz = input.float(defval=0.05, title='TickSize', group='Stoploss/Target Controls') ATRMultiplier = input.float(title='ATR Multiplier', step=0.1, defval=1.5, group='Stoploss/Target Controls') ATRLength = input.int(title='ATR Period', defval=20, group='Stoploss/Target Controls') iATR = ta.atr(ATRLength) trail = input.bool(title='Trailing Stoploss On/Off', defval=false, group='Trailstop Controls') longTrailPerc = input.float(title='Trail Long Stop (%)', minval=0.01, maxval=50.0, step=0.01, defval=1, group='Trailstop Controls') * 0.01 shortTrailPerc = input.float(title='Trail Short Stop (%)', minval=0.01, maxval=50.0, step=0.01, defval=1, group='Trailstop Controls') * 0.01 var longCondition = false var shortCondition = false ////////////////////////////////////////Block 3 Module Ends//////////////////////////////////////////////////////////////////////// //Block 4 : Trading Strategy //input controls factor = input.float(2.5,"Factor",1.0,10.0,0.5) length = input.int(10,"ATR Length",1,100,1) [supertrend,direction] = ta.supertrend(factor,length) ema200 = ta.ema(close,200) plot(ema200,"EMA 200",color.yellow,linewidth = 2) //direction variables contains series of +1 and -1 //supertrend variable contains the supertrend line supcolor = direction == -1 ? color.green : color.red plot(supertrend,"Supertrend",supcolor,linewidth = 2) //Trading Logic buySignal = direction == -1 and close > ema200 sellSignal = direction == 1 and direction[1] == -1 shortSignal = direction == 1 and close < ema200 coverSignal = direction == -1 and direction[1] == 1 ////////////////////////////////////////Block 4 Module Ends//////////////////////////////////////////////////////////////////////// //Block 5 : Intraday Function and Buy and Sell Signal Mapping //Remove the comments to do the long/short signal mapping //buySignal = tbuy //sellSignal = tsell //shortSignal = tshort //coverSignal = tcover barInSession(marketSession) => time(timeframe.period, marketSession) != 0 bool intradaySession = barInSession(marketSession) buy = buySignal sell = sellSignal short = shortSignal cover = coverSignal buy1 = buy[1] sell1 = sell[1] short1 = short[1] cover1 = cover[1] //assign signals if(not intraday) buy := buySignal short := shortSignal if(intraday) buy := buy and intradaySession short := short and intradaySession //Block 4 : Execution Controls if(am_Mode=="ENABLE") //Fresh Long Entry if buy and not cover and strategy.position_size == 0 and window() strategy.entry('buy',strategy.long,comment='BUY',alert_message=BE) if buy and cover and strategy.position_size == 0 and window() strategy.entry('buy',strategy.long,comment='BUY',alert_message=BE) //Stop and Reverse to Buy if buy and cover and strategy.position_size < 0 and window() strategy.entry('buy',strategy.long,comment='BUY',alert_message=BSR) //Long Exit if sell and not short and strategy.position_size > 0 and window() strategy.close('buy',comment='SELL',alert_message=BX) //Fresh Short Entry if short and not sell and strategy.position_size == 0 and window() strategy.entry('short',strategy.short,comment='SHORT',alert_message=SE) if short and sell and strategy.position_size == 0 and window() strategy.entry('short',strategy.short,comment='SHORT',alert_message=SE) //Stop and Reverse to Buy if short and sell and strategy.position_size > 0 and window() strategy.entry('short',strategy.short,comment='SHORT',alert_message=SSR) //Short Exit if cover and not buy and strategy.position_size < 0 and window() strategy.close('short',comment='COVER',alert_message=SX) if(am_Mode=="LONGONLY") //Fresh Long Entry if buy and not cover and strategy.position_size == 0 and window() strategy.entry('buy',strategy.long,comment='BUY',alert_message=BE) if buy and cover and strategy.position_size == 0 and window() strategy.entry('buy',strategy.long,comment='BUY',alert_message=BE) //Stop and Reverse to Buy if buy and cover and strategy.position_size < 0 and window() strategy.entry('buy',strategy.long,comment='BUY',alert_message=BSR) //Long Exit if sell and not short and strategy.position_size > 0 and window() strategy.close('buy',comment='SELL',alert_message=BX) if(am_Mode=="SHORTONLY") //Fresh Short Entry if short and not sell and strategy.position_size == 0 and window() strategy.entry('short',strategy.short,comment='SHORT',alert_message=SE) if short and sell and strategy.position_size == 0 and window() strategy.entry('short',strategy.short,comment='SHORT',alert_message=SE) //Stop and Reverse to Buy if short and sell and strategy.position_size > 0 and window() strategy.entry('short',strategy.short,comment='SHORT',alert_message=SSR) //Short Exit if cover and not buy and strategy.position_size > 0 and window() strategy.close('short',comment='COVER',alert_message=SX) if(intraday) longsquareOff = not intradaySession and strategy.position_size > 0 if(longsquareOff) strategy.close(id='BUY', comment='Square-off',alert_message=BX) shortsquareOff = not intradaySession and strategy.position_size < 0 if(shortsquareOff) strategy.close(id='SELL', comment='Square-off',alert_message=SX) ////////////////////////////////////////Block 4 Module Ends//////////////////////////////////////////////////////////////////////// buycount = ta.barssince(buySignal) shortcount = ta.barssince(shortSignal) color1 = buycount[1] < shortcount[1] ? color.green : buycount[1] > shortcount[1] ? color.red : na barcolor(barcoloring ? color1 : na) long_stop_level = ta.valuewhen(buy1, open - stoploss, 0) long_profit_level = ta.valuewhen(buy1, open + target, 0) short_stop_level = ta.valuewhen(short1, open + stoploss, 0) short_profit_level = ta.valuewhen(short1, open - target, 0) if(type=="PERCENTAGE") long_stop_level := ta.valuewhen(buy1, open, 0) * (100-stoploss)/100 long_profit_level := ta.valuewhen(buy1, open, 0) * (100+target)/100 long_stop_level := TickSz * math.round(long_stop_level/TickSz) long_profit_level := TickSz * math.round(long_profit_level/TickSz) short_stop_level := ta.valuewhen(short1, open, 0) * (100+stoploss)/100 short_profit_level := ta.valuewhen(short1, open, 0) * (100-target)/100 short_stop_level := TickSz * math.round(short_stop_level/TickSz) short_profit_level := TickSz * math.round(short_profit_level/TickSz) if(type=="VOLATILITY") long_stop_level := ta.valuewhen(buy1, open - iATR*ATRMultiplier, 0) long_profit_level := ta.valuewhen(buy1, open + iATR*ATRMultiplier, 0) short_stop_level := ta.valuewhen(short1, open + iATR*ATRMultiplier, 0) short_profit_level := ta.valuewhen(short1, open - iATR*ATRMultiplier, 0) //Sending Target/Stoploss Orders if(risk) if(strategy.position_size>0) strategy.exit('TP/SL', 'BUY', stop=long_stop_level, limit=long_profit_level,alert_message=BX) if(strategy.position_size<0) strategy.exit('TP/SL', 'SELL', stop=short_stop_level, limit=short_profit_level,alert_message=SX) plot(strategy.position_size <= 0 or not risk ? na : long_stop_level, color=color.new(color.red, 0), style=plot.style_circles, linewidth=2) plot(strategy.position_size <= 0 or not risk ? na : long_profit_level, color=color.new(color.green, 0), style=plot.style_circles, linewidth=2) plot(strategy.position_size >= 0 or not risk ? na : short_stop_level, color=color.new(color.red, 0), style=plot.style_circles, linewidth=2) plot(strategy.position_size >= 0 or not risk ? na : short_profit_level, color=color.new(color.green, 0), style=plot.style_circles, linewidth=2) //Initialization of Long Stop Price and Short Stop Price longStopPrice = 0.0 shortStopPrice = 0.0 //Sending Trailing Stoploss Orders if(trail) // Determine trail stop loss prices longStopPrice := if strategy.position_size > 0 stopValue = close * (1 - longTrailPerc) math.max(stopValue, longStopPrice[1]) else 0 shortStopPrice := if strategy.position_size < 0 stopValue = close * (1 + shortTrailPerc) math.min(stopValue, shortStopPrice[1]) else 999999 // Submit exit orders for trail stop loss price if strategy.position_size > 0 strategy.exit(id='TRAIL HIT', stop=longStopPrice,alert_message=BX) if strategy.position_size < 0 strategy.exit(id='TRAIL HIT', stop=shortStopPrice,alert_message=SX) // Plot stop loss values for confirmation plot(series=strategy.position_size > 0 and trail ? longStopPrice : na, color=color.new(color.fuchsia, 0), style=plot.style_cross, linewidth=2, title='Long Trail Stop') plot(series=strategy.position_size < 0 and trail ? shortStopPrice : na, color=color.new(color.fuchsia, 0), style=plot.style_cross, linewidth=2, title='Short Trail Stop')