· 4 years ago · Aug 09, 2021, 12:48 PM
1//@version=4
2// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
3// © rajandran
4
5//@version=4
6strategy("Simple EMA Crossover Strategy",overlay=true)
7
8
9
10//input parameters (controls)
11len1 = input(defval=20,title="Length1", type = input.integer, minval=1, maxval=50,step=1)
12len2 = input(defval=50,title="Length2", type = input.integer, minval=1, maxval=50,step=1)
13iqty= input(defval=5.0,title="Length2", type = input.float, minval=1, maxval=10000,step=1)
14
15//Algomojo Controls - for Angel Broking
16//Block 1 : API Controls + Algomojo Input Controls
17
18
19//Enter Your Algomojo API Key and API Secret Key
20
21
22api_key = "f73f3e14795376d1df7bce0d62fb2309"
23api_secret = "d546fda249dc63b25d579a17db9c7525"
24
25
26stgy_name = input(title="Strategy Name",type=input.string,defval="Tradingview Strategy")
27variety = input(title="Variety",type=input.string,defval="NORMAL",options=["NORMAL","STOPLOSS","AMO"])
28tradingsymbol = input(title="Trading Symbol",type=input.string,defval="NIFTY26AUG21FUT")
29symboltoken = input(title="Symbol Token",type=input.string,defval="49939")
30exchange = input(title="Exchange",type=input.string,defval="NFO",options=["NSE","NFO","MCX","BSE"])
31ordertype = input(title="Order Type",type=input.string,defval="MARKET",options=["MARKET","LIMIT","STOPLOSS_LIMIT","STOPLOSS_MARKET"])
32producttype = input(title="Product Type",type=input.string,defval="CARRYFORWARD",options=["DELIVERY","CARRYFORWARD","MARGIN","INTRADAY","AMO_MARGIN","AMO_DELIVERY","AMO_CARRYFORWARD"])
33duration = "DAY"
34price = "0"
35squareoff = "0"
36stoploss = "0"
37quantity = input(title="Quantity", type=input.integer, defval=50)
38triggerprice = "0"
39trailingStopLoss = "0"
40disclosedquantity = "0"
41
42////////////////////////////////////////Block 1 Module Ends////////////////////////////////////////////////////////////////////////
43
44
45//Block 2 : Autotrading API data configuration - Angel Broking
46
47//BE - Buy Entry (1x qty), SX - Short Exit (1x qty), BSR - Buy Stop and Reverse (2x qty)
48
49BE = "{ \"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+"\" } }"
50SX = "{ \"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+"\" } }"
51BSR = "{ \"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+"\" } }"
52
53//SE - Short Entry (1x qty), Buy Exit - Short Exit (1x qty), SXR - Short Stop and Reveverse (2x qty)
54
55SE = "{ \"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+"\" } }"
56BX = "{ \"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+"\" } }"
57SSR = "{ \"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+"\" } }"
58
59
60////////////////////////////////////////Block 2 Module Ends////////////////////////////////////////////////////////////////////////
61
62
63//compute the ema values
64
65ema1 = ema(close,len1) //historical ema values will be stored
66ema2 = ema(close,len2)
67
68//plot the ema values
69
70
71plot(ema1,color=color.red,linewidth=2)
72plot(ema2,color=color.blue,linewidth=2)
73
74//trading logic - When to Enter , When to Exit the Positions
75
76buy = crossover(ema1,ema2) //positive ema crossover
77sell = crossunder(ema1,ema2) //negative ema crossover
78
79//plot buy and sell signals
80
81plotshape(buy,style=shape.labelup,location = location.belowbar,color=color.green, title="Buy", text="BuySignal",textcolor = color.white)
82plotshape(sell,style=shape.labeldown,location = location.abovebar,color=color.red, title="Sell", text="SellSignal",textcolor = color.white)
83
84
85////////////////////////////////////////Block 3 Module Ends////////////////////////////////////////////////////////////////////////
86//Block 3 : Backtesting Controls - Angel Broking
87
88
89FromMonth = input(defval = 9, title = "From Month", minval = 1, maxval = 12)
90FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
91FromYear = input(defval = 2018, title = "From Year", minval = 999)
92ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
93ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
94ToYear = input(defval = 9999, title = "To Year", minval = 999)
95start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
96finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
97window() => time >= start and time <= finish ? true : false
98
99
100////////////////////////////////////////Block 3 Module Ends////////////////////////////////////////////////////////////////////////
101
102
103longCondition = buy
104shortCondition = sell
105
106//Execution Logic - What order to send and when to send
107
108//Block 4 : Execution Controls
109
110if (longCondition and strategy.position_size==0)
111 strategy.entry("BUY", strategy.long,qty=1, when = window(),alert_message=BE) //Buy Entry
112if (longCondition and strategy.position_size<0)
113 strategy.entry("BUY", strategy.long,qty=1, when = window(),alert_message=BSR) //Buy Stop and Reverse
114if (shortCondition and strategy.position_size==0)
115 strategy.entry("SELL", strategy.short,qty=1, when = window(),alert_message=SE) //Short Entry
116if (shortCondition and strategy.position_size>0)
117 strategy.entry("SELL", strategy.short,qty=1, when = window(),alert_message=SSR) //Short Stop and Reverse
118
119////////////////////////////////////////Block 4 Module Ends////////////////////////////////////////////////////////////////////////
120
121