Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Simplified Algomojo Execution Controls for Tradingview Platform

15 min read

This tutorial explains how to configure trading pinescript trading strategies with Algomojo execution controls to send MKT orders to Algomojo supported Brokers in a very simple manner.

Supported Brokers : Aliceblue, Angel Broking, Tradejini, Upstox, Zebu

LCe1w8ED (1818×926)

Tradingview Supports strategies which contains rules in pinescript language which instructs the system when to Buy/Sell orders, Modify, Cancel Orders.

We know that Tradingview PRO account supports Webhook Alerts. Which can be used to transmit any information to 3rd Party Applications. Remember Webhook is more of one-way communication.

What is Webhook?
Webhook is a unique way of communicating in realtime from one application to another application. Webhooks are automatic. You don’t have to manually utilize them in order to make them work — once they’re set up, they run on their own.

TradingView Webhook

Tradingview Webhooks allow you to send a POST request to a certain URL every time the alert is triggered. This feature can be enabled when you create or edit an alert.

Algomojo Execution Controls

What Iam trying to achieve here is trying to provide readymade controls which can be used in your pinescript trading strategies to simplify your execution controls.

Algomojo Execution control comes with 3 different blocks

Block 1 – API Controls + Algomojo Input Controls

Sample Input Controls for Angel Broking. It creates input controls related to order parameters, leverage, trading symbol, quantity controls etc. Input controls differs for different brokers. checkout the supertrend pinescript code provided for different brokers below (pinescript is provided for Aliceblue, Tradejini, Zebu, Angel Broking, Aliceblue)

//Algomojo Controls - for Angel Broking
//Block 1 : API Controls + Algomojo Input Controls


//Enter Your Algomojo API Key and API Secret Key


api_key = "b48e4db30e47326722a388c03eb4ee26"
api_secret = "fc3ed44d0914b0e6466f9ff2e3ae003a"


stgy_name = input(title="Strategy Name",type=input.string,defval="Tradingview Strategy")
variety = input(title="Variety",type=input.string,defval="NORMAL",options=["NORMAL","STOPLOSS","AMO"])
tradingsymbol = input(title="Trading Symbol",type=input.string,defval="NIFTY29APR21FUT")
symboltoken = input(title="Symbol Token",type=input.string,defval="64422")
exchange =  input(title="Exchange",type=input.string,defval="NFO",options=["NSE","NFO","MCX","BSE"])
ordertype = input(title="Order Type",type=input.string,defval="MARKET",options=["MARKET","LIMIT","STOPLOSS_LIMIT","STOPLOSS_MARKET"])
producttype =  input(title="Product Type",type=input.string,defval="CARRYFORWARD",options=["DELIVERY","CARRYFORWARD","MARGIN","INTRADAY","AMO_MARGIN","AMO_DELIVERY","AMO_CARRYFORWARD"])
duration = "DAY"
price = "0"
squareoff = "0"
stoploss = "0"
quantity = input(title="Quantity", type=input.integer, defval=5)
triggerprice = "0"
trailingStopLoss =  "0"
disclosedquantity = "0"

////////////////////////////////////////Block 1 Module Ends////////////////////////////////////////////////////////////////////////


Block 2: Autotrading API data configuration

This creates variables containing the API data string information which needs to be configured in Block 3 Execution Module

API data messaging variables
BE – variables that control the api message for Buy Entry (Fresh Buy – 1x qty)
SX – variables that control the api message for Short Exit (Short Exit – 1x qty)
BSR -variables that control the api message for Buy Stop Reverse (2x qty)

SE – variables that control the api message for Short Entry (Fresh Shorts – 1x qty)
BX – variables that control the api message for Buy Exit (Buy Exit – 1x qty)
SSR -variables that control the api message for Short Stop Reverse (2x qty)

//Block 2 : Autotrading API data configuration - Angel Broking

//BE - Buy Entry (1x qty), SX - Short Exit (1x qty), BSR - Buy Stop and Reverse (2x qty)

BE =  "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {	\"stgy_name\": \""+stgy_name+"\",        \"variety\":\""+variety+"\",        \"tradingsymbol\":\""+tradingsymbol+"\",        \"symboltoken\":\""+symboltoken+"\",        \"transactiontype\":\"BUY\",        \"exchange\":\""+exchange+"\",        \"ordertype\":\""+ordertype+"\",        \"producttype\":\""+producttype+"\",        \"duration\":\""+duration+"\",        \"price\":\""+price+"\",        \"squareoff\":\""+squareoff+"\",        \"stoploss\":\""+stoploss+"\",        \"quantity\":\""+tostring(quantity)+"\",        \"triggerprice\": \""+triggerprice+"\",        \"trailingStopLoss\": \""+trailingStopLoss+"\",        \"disclosedquantity\":\""+disclosedquantity+"\"	  } }"
SX =  "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {	\"stgy_name\": \""+stgy_name+"\",        \"variety\":\""+variety+"\",        \"tradingsymbol\":\""+tradingsymbol+"\",        \"symboltoken\":\""+symboltoken+"\",        \"transactiontype\":\"BUY\",        \"exchange\":\""+exchange+"\",        \"ordertype\":\""+ordertype+"\",        \"producttype\":\""+producttype+"\",        \"duration\":\""+duration+"\",        \"price\":\""+price+"\",        \"squareoff\":\""+squareoff+"\",        \"stoploss\":\""+stoploss+"\",        \"quantity\":\""+tostring(quantity)+"\",        \"triggerprice\": \""+triggerprice+"\",        \"trailingStopLoss\": \""+trailingStopLoss+"\",        \"disclosedquantity\":\""+disclosedquantity+"\"	  } }"
BSR = "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {	\"stgy_name\": \""+stgy_name+"\",        \"variety\":\""+variety+"\",        \"tradingsymbol\":\""+tradingsymbol+"\",        \"symboltoken\":\""+symboltoken+"\",        \"transactiontype\":\"BUY\",        \"exchange\":\""+exchange+"\",        \"ordertype\":\""+ordertype+"\",        \"producttype\":\""+producttype+"\",        \"duration\":\""+duration+"\",        \"price\":\""+price+"\",        \"squareoff\":\""+squareoff+"\",        \"stoploss\":\""+stoploss+"\",        \"quantity\":\""+tostring(quantity*2)+"\",        \"triggerprice\": \""+triggerprice+"\",        \"trailingStopLoss\": \""+trailingStopLoss+"\",        \"disclosedquantity\":\""+disclosedquantity+"\"	  } }"

//SE - Short Entry (1x qty), Buy Exit - Short Exit (1x qty), SXR - Short Stop and Reveverse (2x qty)

SE =  "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {	\"stgy_name\": \""+stgy_name+"\",        \"variety\":\""+variety+"\",        \"tradingsymbol\":\""+tradingsymbol+"\",        \"symboltoken\":\""+symboltoken+"\",        \"transactiontype\":\"SELL\",        \"exchange\":\""+exchange+"\",        \"ordertype\":\""+ordertype+"\",        \"producttype\":\""+producttype+"\",        \"duration\":\""+duration+"\",        \"price\":\""+price+"\",        \"squareoff\":\""+squareoff+"\",        \"stoploss\":\""+stoploss+"\",        \"quantity\":\""+tostring(quantity)+"\",        \"triggerprice\": \""+triggerprice+"\",        \"trailingStopLoss\": \""+trailingStopLoss+"\",        \"disclosedquantity\":\""+disclosedquantity+"\"	  } }"
BX =  "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {	\"stgy_name\": \""+stgy_name+"\",        \"variety\":\""+variety+"\",        \"tradingsymbol\":\""+tradingsymbol+"\",        \"symboltoken\":\""+symboltoken+"\",        \"transactiontype\":\"SELL\",        \"exchange\":\""+exchange+"\",        \"ordertype\":\""+ordertype+"\",        \"producttype\":\""+producttype+"\",        \"duration\":\""+duration+"\",        \"price\":\""+price+"\",        \"squareoff\":\""+squareoff+"\",        \"stoploss\":\""+stoploss+"\",        \"quantity\":\""+tostring(quantity)+"\",        \"triggerprice\": \""+triggerprice+"\",        \"trailingStopLoss\": \""+trailingStopLoss+"\",        \"disclosedquantity\":\""+disclosedquantity+"\"	  } }"
SSR = "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {	\"stgy_name\": \""+stgy_name+"\",        \"variety\":\""+variety+"\",        \"tradingsymbol\":\""+tradingsymbol+"\",        \"symboltoken\":\""+symboltoken+"\",        \"transactiontype\":\"SELL\",        \"exchange\":\""+exchange+"\",        \"ordertype\":\""+ordertype+"\",        \"producttype\":\""+producttype+"\",        \"duration\":\""+duration+"\",        \"price\":\""+price+"\",        \"squareoff\":\""+squareoff+"\",        \"stoploss\":\""+stoploss+"\",        \"quantity\":\""+tostring(quantity*2)+"\",        \"triggerprice\": \""+triggerprice+"\",        \"trailingStopLoss\": \""+trailingStopLoss+"\",        \"disclosedquantity\":\""+disclosedquantity+"\"	  } }"


////////////////////////////////////////Block 2 Module Ends////////////////////////////////////////////////////////////////////////


Block3: Order Execution Module

Order execution module finally connects the signals with Alert Message containing the Block2 API data variables

//Block 3 : Execution Controls

if (longCondition and strategy.position_size==0)
    strategy.entry("BUY", strategy.long,qty=1, when = window(),alert_message=BE) //Buy Entry
if (longCondition and strategy.position_size<0)
    strategy.entry("BUY", strategy.long,qty=1, when = window(),alert_message=BSR) //Buy Stop and Reverse
if (shortCondition and strategy.position_size==0)
    strategy.entry("SELL", strategy.short,qty=1, when = window(),alert_message=SE) //Short Entry
if (shortCondition and strategy.position_size>0)
    strategy.entry("SELL", strategy.short,qty=1, when = window(),alert_message=SSR) //Short Stop and Reverse
    
////////////////////////////////////////Block 3 Module Ends////////////////////////////////////////////////////////////////////////

Once all the three blocks are properly configured in the your pinescript trading strategy configuring the Alerts are likely to be a very minimal effort.

Now Create an Alert with proper webhook and placeholder message as shown below (different broker different webhook url needs to be used as instructed below the supertrend AFL code)

Alert Message Section

Alert Message Section is simplified to the end user to configure only the placeholder {{strategy.order.alert_message}} in the Message Box so that end user need not to know about the complexity of configuring the API dat in JSON format. All the complexities are handled at pinescript coding level.

Here are the sample Supertrend Code for Different Brokers using above 3 blocks. One can configure these three blocks in their tradingview strategy to simplify their Tradingview Configuration with Execution Controls.

Algomojo Supertrend Code & Controls & Alert Configuration for Aliceblue, Tradejini, Zebu

Input Controls

Supertrend Pinescript Code for Aliceblue, Tradejini, Zebu


// Algomojo Trading Strategy


//@version=4
strategy("SuperTrend Algomojo Trading Strategy", overlay=true)



//inputs
Periods = input(title="ATR Period", type=input.integer, defval=10)
src = input(hl2, title="Source")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=true)
highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
barcoloring = input(title="Bar Coloring On/Off ?", type=input.bool, defval=true)

//Block 1 : API Controls + Algomojo Input Controls


//Enter Your Algomojo API Key and API Secret Key


api_key = "86cbef19e7e61ccee91e497690d5814e"
api_secret = "e2d51c1ea698fdff1b2b2223889612e9"


strg_name = input(title="Strategy Name",type=input.string,defval="Tradingview Strategy")
s_prdt_ali = "BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML"
Tsym = input(title="Trading Symbol",type=input.string,defval="RELIANCE-EQ")
exch = input(title="Exchange",type=input.string,defval="NSE",options=["NSE","NFO","MCX","BSE"])
Ret = "DAY"
qty = input(title="Quantity", type=input.integer, defval=1)
prctyp =input(title="Order Type",type=input.string,defval="MKT",options=["MKT","LMT","SL","SL-M"])
discqty = "0"
MktPro ="NA"
Price = "0"
TrigPrice = "0"
Pcode = input(title="PCode",type=input.string,defval="NRML",options=["CNC","NRML","MIS","CO"])
AMO = "NO"

////////////////////////////////////////Block 1 Module Ends////////////////////////////////////////////////////////////////////////



//Block 2 : Autotrading API data configuration

//BE - Buy Entry (1x qty), SX - Short Exit (1x qty), BSR - Buy Stop and Reverse (2x qty)

BE = "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":       {   \"strg_name\":\""+strg_name+"\",         \"s_prdt_ali\":\"BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML\",         \"Tsym\":\""+Tsym+"\",         \"exch\":\""+exch+"\",         \"Ttranstype\":\"B\",         \"Ret\":\"DAY\",         \"prctyp\":\"MKT\",     \"qty\":\""+tostring(qty)+"\",       \"discqty\":\"0\",         \"MktPro\":\"NA\",         \"Price\":\"0\",         \"TrigPrice\":\"0\",         \"Pcode\":\"NRML\",         \"AMO\":\"NO\"       } }"
SX = "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":       {   \"strg_name\":\""+strg_name+"\",         \"s_prdt_ali\":\"BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML\",         \"Tsym\":\""+Tsym+"\",         \"exch\":\""+exch+"\",         \"Ttranstype\":\"B\",         \"Ret\":\"DAY\",         \"prctyp\":\"MKT\",     \"qty\":\""+tostring(qty)+"\",       \"discqty\":\"0\",         \"MktPro\":\"NA\",         \"Price\":\"0\",         \"TrigPrice\":\"0\",         \"Pcode\":\"NRML\",         \"AMO\":\"NO\"       } }"
BSR = "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":       {   \"strg_name\":\""+strg_name+"\",         \"s_prdt_ali\":\"BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML\",         \"Tsym\":\""+Tsym+"\",         \"exch\":\""+exch+"\",         \"Ttranstype\":\"B\",         \"Ret\":\"DAY\",         \"prctyp\":\"MKT\",     \"qty\":\""+tostring(qty*2)+"\",       \"discqty\":\"0\",         \"MktPro\":\"NA\",         \"Price\":\"0\",         \"TrigPrice\":\"0\",         \"Pcode\":\"NRML\",         \"AMO\":\"NO\"       } }" 

//SE - Short Entry (1x qty), Buy Exit - Short Exit (1x qty), SXR - Short Stop and Reveverse (2x qty)

SE = "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":       {   \"strg_name\":\""+strg_name+"\",         \"s_prdt_ali\":\"BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML\",         \"Tsym\":\""+Tsym+"\",         \"exch\":\""+exch+"\",         \"Ttranstype\":\"S\",         \"Ret\":\"DAY\",         \"prctyp\":\"MKT\",     \"qty\":\""+tostring(qty)+"\",       \"discqty\":\"0\",         \"MktPro\":\"NA\",         \"Price\":\"0\",         \"TrigPrice\":\"0\",         \"Pcode\":\"NRML\",         \"AMO\":\"NO\"       } }"
BX = "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":       {   \"strg_name\":\""+strg_name+"\",         \"s_prdt_ali\":\"BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML\",         \"Tsym\":\""+Tsym+"\",         \"exch\":\""+exch+"\",         \"Ttranstype\":\"S\",         \"Ret\":\"DAY\",         \"prctyp\":\"MKT\",     \"qty\":\""+tostring(qty)+"\",       \"discqty\":\"0\",         \"MktPro\":\"NA\",         \"Price\":\"0\",         \"TrigPrice\":\"0\",         \"Pcode\":\"NRML\",         \"AMO\":\"NO\"       } }"
SSR = "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":       {   \"strg_name\":\""+strg_name+"\",         \"s_prdt_ali\":\"BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML\",         \"Tsym\":\""+Tsym+"\",         \"exch\":\""+exch+"\",         \"Ttranstype\":\"S\",         \"Ret\":\"DAY\",         \"prctyp\":\"MKT\",     \"qty\":\""+tostring(qty*2)+"\",       \"discqty\":\"0\",         \"MktPro\":\"NA\",         \"Price\":\"0\",         \"TrigPrice\":\"0\",         \"Pcode\":\"NRML\",         \"AMO\":\"NO\"       } }" 

////////////////////////////////////////Block 2 Module Ends////////////////////////////////////////////////////////////////////////

atr2 = sma(tr, Periods)
atr= changeATR ? atr(Periods) : atr2
up=src-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
dn=src+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend


//Plots
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(sellSignal and showsignals ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)

longFillColor = highlighting ? (trend == 1 ? color.green : color.white) : color.white
shortFillColor = highlighting ? (trend == -1 ? color.red : color.white) : color.white
fill(mPlot, upPlot, title="UpTrend Highligter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)



//Backtesting Controls

FromMonth = input(defval = 9, title = "From Month", minval = 1, maxval = 12)
FromDay   = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear  = input(defval = 2018, title = "From Year", minval = 999)
ToMonth   = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay     = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear    = input(defval = 9999, title = "To Year", minval = 999)
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)  
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)       
window()  => time >= start and time <= finish ? true : false


//Alerts
alertcondition(buySignal, title="SuperTrend Buy", message="SuperTrend Buy!")
alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")




longCondition = buySignal
shortCondition = sellSignal

//Block 3 : Execution Controls

if (longCondition and strategy.position_size==0)
    strategy.entry("BUY", strategy.long,qty=1, when = window(),alert_message=BE)
if (longCondition and strategy.position_size<0)
    strategy.entry("BUY", strategy.long,qty=1, when = window(),alert_message=BSR)
if (shortCondition and strategy.position_size==0)
    strategy.entry("SELL", strategy.short,qty=1, when = window(),alert_message=SE)
if (shortCondition and strategy.position_size>0)
    strategy.entry("SELL", strategy.short,qty=1, when = window(),alert_message=SSR)
    
////////////////////////////////////////Block 3 Module Ends////////////////////////////////////////////////////////////////////////
    
buy1= barssince(buySignal)
sell1 = barssince(sellSignal)
color1 = buy1[1] < sell1[1] ? color.green : buy1[1] > sell1[1] ? color.red : na
barcolor(barcoloring ? color1 : na)

Webhook Endpoint URL : Aliceblue, Tradejini, Zebu

Broker - Aliceblue 
https://abapi.algomojo.com/1.0/PlaceOrder 

Broker - Tradejini
https://tjapi.algomojo.com/1.0/PlaceOrder 

Broker - Zebu
https://zbapi.algomojo.com/1.0/PlaceOrder

Alert Configuration

Angel Broking – Supertrend Pinescript Code, Inputs and Alert Configuration

Supertrend Pinescript Code for Angel Broking


// Algomojo Trading Strategy for Angel Broking


//@version=4
strategy("SuperTrend Algomojo Trading Strategy", shorttitle="Supertrend Algomojo - Angel", overlay=true)



//inputs
Periods = input(title="ATR Period", type=input.integer, defval=10)
src = input(hl2, title="Source")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=true)
highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
barcoloring = input(title="Bar Coloring On/Off ?", type=input.bool, defval=true)

//Algomojo Controls
//Block 1 : API Controls + Algomojo Input Controls


//Enter Your Algomojo API Key and API Secret Key


api_key = "b48e4db30e47326722a388c03eb4ee26"
api_secret = "fc3ed44d0914b0e6466f9ff2e3ae003a"


stgy_name = input(title="Strategy Name",type=input.string,defval="Tradingview Strategy")
variety = input(title="Variety",type=input.string,defval="NORMAL",options=["NORMAL","STOPLOSS","AMO"])
tradingsymbol = input(title="Trading Symbol",type=input.string,defval="NIFTY29APR21FUT")
symboltoken = input(title="Symbol Token",type=input.string,defval="64422")
exchange =  input(title="Exchange",type=input.string,defval="NFO",options=["NSE","NFO","MCX","BSE"])
ordertype = input(title="Order Type",type=input.string,defval="MARKET",options=["MARKET","LIMIT","STOPLOSS_LIMIT","STOPLOSS_MARKET"])
producttype =  input(title="Product Type",type=input.string,defval="CARRYFORWARD",options=["DELIVERY","CARRYFORWARD","MARGIN","INTRADAY","AMO_MARGIN","AMO_DELIVERY","AMO_CARRYFORWARD"])
duration = "DAY"
price = "0"
squareoff = "0"
stoploss = "0"
quantity = input(title="Quantity", type=input.integer, defval=5)
triggerprice = "0"
trailingStopLoss =  "0"
disclosedquantity = "0"

////////////////////////////////////////Block 1 Module Ends////////////////////////////////////////////////////////////////////////


//Block 2 : Autotrading API data configuration

//BE - Buy Entry (1x qty), SX - Short Exit (1x qty), BSR - Buy Stop and Reverse (2x qty)

BE =  "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {	\"stgy_name\": \""+stgy_name+"\",        \"variety\":\""+variety+"\",        \"tradingsymbol\":\""+tradingsymbol+"\",        \"symboltoken\":\""+symboltoken+"\",        \"transactiontype\":\"BUY\",        \"exchange\":\""+exchange+"\",        \"ordertype\":\""+ordertype+"\",        \"producttype\":\""+producttype+"\",        \"duration\":\""+duration+"\",        \"price\":\""+price+"\",        \"squareoff\":\""+squareoff+"\",        \"stoploss\":\""+stoploss+"\",        \"quantity\":\""+tostring(quantity)+"\",        \"triggerprice\": \""+triggerprice+"\",        \"trailingStopLoss\": \""+trailingStopLoss+"\",        \"disclosedquantity\":\""+disclosedquantity+"\"	  } }"
SX =  "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {	\"stgy_name\": \""+stgy_name+"\",        \"variety\":\""+variety+"\",        \"tradingsymbol\":\""+tradingsymbol+"\",        \"symboltoken\":\""+symboltoken+"\",        \"transactiontype\":\"BUY\",        \"exchange\":\""+exchange+"\",        \"ordertype\":\""+ordertype+"\",        \"producttype\":\""+producttype+"\",        \"duration\":\""+duration+"\",        \"price\":\""+price+"\",        \"squareoff\":\""+squareoff+"\",        \"stoploss\":\""+stoploss+"\",        \"quantity\":\""+tostring(quantity)+"\",        \"triggerprice\": \""+triggerprice+"\",        \"trailingStopLoss\": \""+trailingStopLoss+"\",        \"disclosedquantity\":\""+disclosedquantity+"\"	  } }"
BSR = "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {	\"stgy_name\": \""+stgy_name+"\",        \"variety\":\""+variety+"\",        \"tradingsymbol\":\""+tradingsymbol+"\",        \"symboltoken\":\""+symboltoken+"\",        \"transactiontype\":\"BUY\",        \"exchange\":\""+exchange+"\",        \"ordertype\":\""+ordertype+"\",        \"producttype\":\""+producttype+"\",        \"duration\":\""+duration+"\",        \"price\":\""+price+"\",        \"squareoff\":\""+squareoff+"\",        \"stoploss\":\""+stoploss+"\",        \"quantity\":\""+tostring(quantity*2)+"\",        \"triggerprice\": \""+triggerprice+"\",        \"trailingStopLoss\": \""+trailingStopLoss+"\",        \"disclosedquantity\":\""+disclosedquantity+"\"	  } }"

//SE - Short Entry (1x qty), Buy Exit - Short Exit (1x qty), SXR - Short Stop and Reveverse (2x qty)

SE =  "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {	\"stgy_name\": \""+stgy_name+"\",        \"variety\":\""+variety+"\",        \"tradingsymbol\":\""+tradingsymbol+"\",        \"symboltoken\":\""+symboltoken+"\",        \"transactiontype\":\"SELL\",        \"exchange\":\""+exchange+"\",        \"ordertype\":\""+ordertype+"\",        \"producttype\":\""+producttype+"\",        \"duration\":\""+duration+"\",        \"price\":\""+price+"\",        \"squareoff\":\""+squareoff+"\",        \"stoploss\":\""+stoploss+"\",        \"quantity\":\""+tostring(quantity)+"\",        \"triggerprice\": \""+triggerprice+"\",        \"trailingStopLoss\": \""+trailingStopLoss+"\",        \"disclosedquantity\":\""+disclosedquantity+"\"	  } }"
BX =  "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {	\"stgy_name\": \""+stgy_name+"\",        \"variety\":\""+variety+"\",        \"tradingsymbol\":\""+tradingsymbol+"\",        \"symboltoken\":\""+symboltoken+"\",        \"transactiontype\":\"SELL\",        \"exchange\":\""+exchange+"\",        \"ordertype\":\""+ordertype+"\",        \"producttype\":\""+producttype+"\",        \"duration\":\""+duration+"\",        \"price\":\""+price+"\",        \"squareoff\":\""+squareoff+"\",        \"stoploss\":\""+stoploss+"\",        \"quantity\":\""+tostring(quantity)+"\",        \"triggerprice\": \""+triggerprice+"\",        \"trailingStopLoss\": \""+trailingStopLoss+"\",        \"disclosedquantity\":\""+disclosedquantity+"\"	  } }"
SSR = "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {	\"stgy_name\": \""+stgy_name+"\",        \"variety\":\""+variety+"\",        \"tradingsymbol\":\""+tradingsymbol+"\",        \"symboltoken\":\""+symboltoken+"\",        \"transactiontype\":\"SELL\",        \"exchange\":\""+exchange+"\",        \"ordertype\":\""+ordertype+"\",        \"producttype\":\""+producttype+"\",        \"duration\":\""+duration+"\",        \"price\":\""+price+"\",        \"squareoff\":\""+squareoff+"\",        \"stoploss\":\""+stoploss+"\",        \"quantity\":\""+tostring(quantity*2)+"\",        \"triggerprice\": \""+triggerprice+"\",        \"trailingStopLoss\": \""+trailingStopLoss+"\",        \"disclosedquantity\":\""+disclosedquantity+"\"	  } }"


////////////////////////////////////////Block 2 Module Ends////////////////////////////////////////////////////////////////////////


atr2 = sma(tr, Periods)
atr= changeATR ? atr(Periods) : atr2
up=src-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
dn=src+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend


//Plots
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(sellSignal and showsignals ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)

longFillColor = highlighting ? (trend == 1 ? color.green : color.white) : color.white
shortFillColor = highlighting ? (trend == -1 ? color.red : color.white) : color.white
fill(mPlot, upPlot, title="UpTrend Highligter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)

//Backtesting Controls

FromMonth = input(defval = 9, title = "From Month", minval = 1, maxval = 12)
FromDay   = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear  = input(defval = 2018, title = "From Year", minval = 999)
ToMonth   = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay     = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear    = input(defval = 9999, title = "To Year", minval = 999)
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)  
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)       
window()  => time >= start and time <= finish ? true : false


//Alerts
alertcondition(buySignal, title="SuperTrend Buy", message="SuperTrend Buy!")
alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")




longCondition = buySignal
shortCondition = sellSignal

//Block 3 : Execution Controls

if (longCondition and strategy.position_size==0)
    strategy.entry("BUY", strategy.long,qty=1, when = window(),alert_message=BE)
if (longCondition and strategy.position_size<0)
    strategy.entry("BUY", strategy.long,qty=1, when = window(),alert_message=BSR)
if (shortCondition and strategy.position_size==0)
    strategy.entry("SELL", strategy.short,qty=1, when = window(),alert_message=SE)
if (shortCondition and strategy.position_size>0)
    strategy.entry("SELL", strategy.short,qty=1, when = window(),alert_message=SSR)
    
////////////////////////////////////////Block 3 Module Ends////////////////////////////////////////////////////////////////////////
    
buy1= barssince(buySignal)
sell1 = barssince(sellSignal)
color1 = buy1[1] < sell1[1] ? color.green : buy1[1] > sell1[1] ? color.red : na
barcolor(barcoloring ? color1 : na)

Webhook Endpoint URL : Angel Broking

Broker - Angel
https://anapi.algomojo.com/1.0/PlaceOrder

Supertrend Alert Configuration – Angel Broking

Algomojo Supertrend Code & Controls & Alert Configuration for Upstox

Supertrend Pinescript Code for Upstox


// Algomojo Trading Strategy for Upstox


//@version=4
strategy("SuperTrend Algomojo Trading Strategy", shorttitle="Supertrend Algomojo - Upstox", overlay=true)



//inputs
Periods = input(title="ATR Period", type=input.integer, defval=10)
src = input(hl2, title="Source")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=true)
highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
barcoloring = input(title="Bar Coloring On/Off ?", type=input.bool, defval=true)


//Block 1 : API Controls + Algomojo Input Controls


//Enter Your Algomojo API Key and API Secret Key


api_key = "781981bf5cc2662152dab1ed64e8ef30"
api_secret = "02aa902dbe344f7a88af59e0aa7e5d01"

//Algomojo Input Parameters


stgy_name = input(title="Strategy Name",type=input.string,defval="Tradingview Strategy")
symbol = input(title="Trading Symbol",type=input.string,defval="RELIANCE")
exchange = input(title="Exchange",type=input.string,defval="nse_eq",options=["nse_eq","bse_eq","nse_fo","mcx_fo","ncd_fo","bcd_fo"])
duration = "DAY"
order_type = input(title="Order Type",type=input.string,defval="MKT",options=["MKT","LMT","SL","SL-M"])
quantity = input(title="Quantity", type=input.integer, defval=5)
disclosed_quantity = "0"
MktPro = "NA"
price  = "0"
trigger_price = "0"
product = input(title="Product Type",type=input.string,defval="MIS",options=["CNC","MIS","NRML","CO"])
is_amo = "NO"

////////////////////////////////////////Block 1 Module Ends////////////////////////////////////////////////////////////////////////


//Block 2 : Autotrading API data configuration

//BE - Buy Entry (1x qty), SX - Short Exit (1x qty), BSR - Buy Stop and Reverse (2x qty)

BE =  "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {    \"stgy_name\":\""+stgy_name+"\",         \"symbol\":\""+symbol+"\",         \"exchange\":\""+exchange+"\",         \"transaction_type\":\"B\",        \"duration\":\""+duration+"\",        \"order_type\":\""+order_type+"\",        \"quantity\":\""+tostring(quantity)+"\",        \"disclosed_quantity\":\""+disclosed_quantity+"\",        \"MktPro\":\""+MktPro+"\",        \"price\":\""+price+"\",        \"trigger_price\":\""+trigger_price+"\",        \"product\":\""+product+"\",        \"is_amo\":\""+is_amo+"\"      } }"
SX =  "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {    \"stgy_name\":\""+stgy_name+"\",         \"symbol\":\""+symbol+"\",         \"exchange\":\""+exchange+"\",         \"transaction_type\":\"B\",        \"duration\":\""+duration+"\",        \"order_type\":\""+order_type+"\",        \"quantity\":\""+tostring(quantity)+"\",        \"disclosed_quantity\":\""+disclosed_quantity+"\",        \"MktPro\":\""+MktPro+"\",        \"price\":\""+price+"\",        \"trigger_price\":\""+trigger_price+"\",        \"product\":\""+product+"\",        \"is_amo\":\""+is_amo+"\"      } }"
BSR = "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {    \"stgy_name\":\""+stgy_name+"\",         \"symbol\":\""+symbol+"\",         \"exchange\":\""+exchange+"\",         \"transaction_type\":\"B\",        \"duration\":\""+duration+"\",        \"order_type\":\""+order_type+"\",        \"quantity\":\""+tostring(quantity*2)+"\",        \"disclosed_quantity\":\""+disclosed_quantity+"\",        \"MktPro\":\""+MktPro+"\",        \"price\":\""+price+"\",        \"trigger_price\":\""+trigger_price+"\",        \"product\":\""+product+"\",        \"is_amo\":\""+is_amo+"\"      } }"

//SE - Short Entry (1x qty), Buy Exit - Short Exit (1x qty), SXR - Short Stop and Reveverse (2x qty)

SE =  "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {    \"stgy_name\":\""+stgy_name+"\",         \"symbol\":\""+symbol+"\",         \"exchange\":\""+exchange+"\",         \"transaction_type\":\"S\",        \"duration\":\""+duration+"\",        \"order_type\":\""+order_type+"\",        \"quantity\":\""+tostring(quantity)+"\",        \"disclosed_quantity\":\""+disclosed_quantity+"\",        \"MktPro\":\""+MktPro+"\",        \"price\":\""+price+"\",        \"trigger_price\":\""+trigger_price+"\",        \"product\":\""+product+"\",        \"is_amo\":\""+is_amo+"\"      } }"
BX =  "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {    \"stgy_name\":\""+stgy_name+"\",         \"symbol\":\""+symbol+"\",         \"exchange\":\""+exchange+"\",         \"transaction_type\":\"S\",        \"duration\":\""+duration+"\",        \"order_type\":\""+order_type+"\",        \"quantity\":\""+tostring(quantity)+"\",        \"disclosed_quantity\":\""+disclosed_quantity+"\",        \"MktPro\":\""+MktPro+"\",        \"price\":\""+price+"\",        \"trigger_price\":\""+trigger_price+"\",        \"product\":\""+product+"\",        \"is_amo\":\""+is_amo+"\"      } }"
SSR = "{   \"api_key\":\""+api_key+"\", \"api_secret\":\""+api_secret+"\",     \"data\":      {    \"stgy_name\":\""+stgy_name+"\",         \"symbol\":\""+symbol+"\",         \"exchange\":\""+exchange+"\",         \"transaction_type\":\"S\",        \"duration\":\""+duration+"\",        \"order_type\":\""+order_type+"\",        \"quantity\":\""+tostring(quantity*2)+"\",        \"disclosed_quantity\":\""+disclosed_quantity+"\",        \"MktPro\":\""+MktPro+"\",        \"price\":\""+price+"\",        \"trigger_price\":\""+trigger_price+"\",        \"product\":\""+product+"\",        \"is_amo\":\""+is_amo+"\"      } }"

////////////////////////////////////////Block 2 Module Ends////////////////////////////////////////////////////////////////////////

atr2 = sma(tr, Periods)
atr= changeATR ? atr(Periods) : atr2
up=src-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
dn=src+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend



//Plots
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(sellSignal and showsignals ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)


longFillColor = highlighting ? (trend == 1 ? color.green : color.white) : color.white
shortFillColor = highlighting ? (trend == -1 ? color.red : color.white) : color.white
fill(mPlot, upPlot, title="UpTrend Highligter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)

//Backtesting Controls

FromMonth = input(defval = 9, title = "From Month", minval = 1, maxval = 12)
FromDay   = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear  = input(defval = 2018, title = "From Year", minval = 999)
ToMonth   = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay     = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear    = input(defval = 9999, title = "To Year", minval = 999)
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)  
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)       
window()  => time >= start and time <= finish ? true : false


//Alerts
alertcondition(buySignal, title="SuperTrend Buy", message="SuperTrend Buy!")
alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")




longCondition = buySignal
shortCondition = sellSignal


//Block 3 : Execution Controls

if (longCondition and strategy.position_size==0)
    strategy.entry("BUY", strategy.long,qty=1, when = window(),alert_message=BE) //Buy Entry
if (longCondition and strategy.position_size<0)
    strategy.entry("BUY", strategy.long,qty=1, when = window(),alert_message=BSR) //Buy Stop and Reverse
if (shortCondition and strategy.position_size==0)
    strategy.entry("SELL", strategy.short,qty=1, when = window(),alert_message=SE) //Short Entry
if (shortCondition and strategy.position_size>0)
    strategy.entry("SELL", strategy.short,qty=1, when = window(),alert_message=SSR) //Short Stop and Reverse
    
////////////////////////////////////////Block 3 Module Ends////////////////////////////////////////////////////////////////////////
    
buy1= barssince(buySignal)
sell1 = barssince(sellSignal)
color1 = buy1[1] < sell1[1] ? color.green : buy1[1] > sell1[1] ? color.red : na
barcolor(barcoloring ? color1 : na)

Webhook Endpoint URL : Upstox

Broker - Upstox
https://upapi.algomojo.com/1.0/PlaceOrder

Supertrend Alert Configuration – Upstox

If in case you have any concerns, questions or feedback do sent your questions to support@algomojo.com

Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Simplified Algomojo Execution Controls for Tradingview Platform

This tutorial explains how to configure trading pinescript trading strategies with Algomojo execution controls to send MKT orders to Algomojo supported Brokers in a...
Rajandran R
15 min read

How to Send Cover Orders from Tradingview to Algomojo…

This tutorial helps you to understand how to send cover orders from the Tradingview platform using the webhook method to Algomojo Trading Platform.
Rajandran R
4 min read

Creating and Backtesting Tradingview Pinescript Trading Strategy

This basic tutorial helps you to explore creating your own first trading system in Tradingview platform using pinescript programming language. If you are searching...
Rajandran R
1 min read

2 Replies to “Simplified Algomojo Execution Controls for Tradingview Platform”

  1. The above script is buying and selling 1 time. Stop and Reverses is not working.
    For example: Buying 10 and Selling 10.

    Is it possible like below example:
    When buy signal generates it should buy for instance of 10 quantity. When sell signal generates, it should close previous buy 10 quantity and start new sell 10 quantity and it should keep on doing for every buy and sell.

    For exampe:
    1st Buy Signal = Buy 10quantity.
    1st Sell Signal = Sell 1st Buy Signal 10quantity and add new sell position of 10 quantity
    2 Buy Signal = Sell 1st Sell Signal 10 quantity and add new buy position of 10 quantity
    2 Sell Signal = Sell 2nd Buy Signal 10 quantity and add new sell position of 10 quantity.

    I had used strategy.close(), for closing any open position, but not working and executing same as just 1 buy and 1 sell.
    if (longCondition)
    strategy.entry(“BUY”, strategy.long,qty=1, when = window(),alert_message=BE) //Long Entry

    if (shortCondition)
    strategy.close(“BUY”, qty=1, when = window(),alert_message=BSR)

    if (shortCondition)
    strategy.entry(“SELL”, strategy.short,qty=1, when = window(),alert_message=SE) //Short Entry

    if (longCondition)
    strategy.close(“SELL”, qty=1, when = window(),alert_message=SSR)

    1. Hi,

      To implement the stop and reverse one have to set the start date as todays date so that first trade will be taken 1x of qty and next reversal will be 2x times and thereby stop and reverse.
      Controls are available under indicator settings.

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More