Super trend to Ninja trader?

A

adii800

Member
Hey guys!

So this simple strat made over 7k in under 24 hrs net of commissions with one contract on /ES at the time this was posted,

So, I stumbled upon Mobius's Supertrend and was blown away by its efficiency when paired with other indicators (such as MACD). And since TOS currently does not support Auto Trading :( and I have used NT in the past so I was wondering if I could configure my MACD + Supertrend strategy on NT.

I am aware that there is a supertrend indicator already on Ninja Trader (https://ninjatraderecosystem.com/us...complete-collection-of-supporting-indicators/) but I cross-referenced it using the same settings as the one by Mobius and It does not match.

My guess is that the problem is the "offset period" since it cannot be set to zero?

Here is my strategy where I combine super trend and MACD signals on TOS (used on 3 Range on /ES) that generated over 7k (on /ES with 1 contract) in profits in the last 24 hrs net of commissions:

Bullish Strategy:
Code:
# SuperTrend
input AtrMult = 1.0;
input nATR = 4;
input AvgType = AverageType.HULL;
input PaintBars = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST[1] then UP else DN;
plot SuperTrend = ST;
SuperTrend.AssignValueColor(if close < ST then Color.RED else Color.GREEN);
AssignPriceColor(if PaintBars and close < ST

                 then Color.RED

                 else if PaintBars and close > ST

                      then Color.GREEN

                      else Color.CURRENT);

# End Code SuperTrend

#MACD

#
# TD Ameritrade IP Company, Inc. (c) 2007-2020
#

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

#ORDERS

AddOrder(type = OrderType.BUY_TO_OPEN, ST < close and Diff > 0, tradeSize = 1, tickColor = Color.GREEN, arrowColor = Color.GREEN, name = "Long", price = CLOSE);
AddOrder(type = OrderType.SELL_TO_CLOSE, ST > close or Diff < 0, tradeSize = 1, name = "Close", tickColor = Color.GRAY, arrowColor = Color.GRAY, price = CLOSE);

Bearish Strategy:

Code:
# SuperTrend
input AtrMult = 1.0;
input nATR = 4;
input AvgType = AverageType.HULL;
input PaintBars = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST[1] then UP else DN;
plot SuperTrend = ST;
SuperTrend.AssignValueColor(if close < ST then Color.RED else Color.GREEN);
AssignPriceColor(if PaintBars and close < ST

                 then Color.RED

                 else if PaintBars and close > ST

                      then Color.GREEN

                      else Color.CURRENT);

# End Code SuperTrend

#MACD

#
# TD Ameritrade IP Company, Inc. (c) 2007-2020
#

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

#ORDERS

AddOrder(type = OrderType.SELL_TO_OPEN, ST > close and Diff < 0, tradeSize = 1, name = "Short", tickColor = Color.LIGHT_RED, arrowColor = Color.LIGHT_RED, price = CLOSE);
AddOrder(type = OrderType.BUY_TO_CLOSE, ST < close or Diff > 0, tradeSize = 1, tickColor = Color.GRAY, arrowColor = Color.GRAY, name = "Close", price = CLOSE);
Supertrend Code by Mobius:
Code:
# Mobius
# SuperTrend
# Chat Room Request
input AtrMult = 1.0;
input nATR = 4;
input AvgType = AverageType.HULL;
input PaintBars = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST[1] then UP else DN;
plot SuperTrend = ST;
SuperTrend.AssignValueColor(if close < ST then Color.RED else Color.GREEN);
AssignPriceColor(if PaintBars and close < ST

                 then Color.RED

                 else if PaintBars and close > ST

                      then Color.GREEN

                      else Color.CURRENT);

AddChartBubble(close crosses below ST, low[1], low[1], color.Dark_Gray);
AddChartBubble(close crosses above ST, high[1], high[1], color.Dark_Gray, no);
# End Code SuperTrend
Some results:


Please help either convert the strategies to Ninja Trader or the Supertrend (Mobius) indicator to NT or tweak the existing Supertrend indicator (link is above) to make it match the one on TOS on the 3 RANGE CHART. Or even, if you find a NT indicator that already emulates the one by Mobius please share it.

ANY help is greatly appreciated as I am trying to make this work ASAP, and I will do my best to answer any questions!

Thanks for all the help guys!

** To set up your 3 Range chart (on PC): go to chart settings (the gear icon @ the top left corner of your chart), click "Time Axis" (found in the top header), change aggregation type to Range, change the price range to 3, and the Range type to range bars.

PS: Sorry BenTen for posting in the wrong forum again :/ I have no idea how to move it to a different forum after I posted it.
 
Last edited:
I

irishgold

Member
I have been playing with your idea, it's a lot of trades on that 3 tick time frame. But it seems profitable. You have to be careful as TOS strategy report can give unrealistic results different than real world. I have been contemplating the same idea for Ninjatrader also. There are people who do this but not sure how expensive that would be to get it done and then the result may not pan out in real world, real time.
 
A

adii800

Member
I have been playing with your idea, it's a lot of trades on that 3 tick time frame. But it seems profitable. You have to be careful as TOS strategy report can give unrealistic results different than real world. I have been contemplating the same idea for Ninjatrader also. There are people who do this but not sure how expensive that would be to get it done and then the result may not pan out in real world, real time.
True, it is a lotta trades. Precisely why I want to get it automated. Also, yea TOS doesn't account for bull put spreads, slippage, volatility, etc. which affect trading in real-time so these results will definitely be on the higher side but tbh I'm fine with even half of what it's making because you could just scale contracts at that point. Also one could also use higher time frames like 8 Range too, which give pretty good results as well. Are you familiar w anyone who does this stuff, all I want is Supertrend to be converted to ninja trader, I could probably manage the rest.

Thanks :)
 
I

irishgold

Member
Hi,
I just stumbled across a YT video https://www.youtube.com/watch?v=0E3e4Ec30ro&t=145s
He gives a link to OneDrive with all the necessary zip files to import into Ninjatrader.
https://onedrive.live.com/?v=validatepermission&id=20255E9C7F1C9614!181571&challengeToken=!AIQQQm4RhEC_Ohc with DBT2 as password
That is a good starting point for an automated strategy and then you will have to tweak buy and sell signals to do what your strategy requires. I'll be working on it myself so if I'm able to make some modifications I'll let you know.
 
A

adii800

Member
Hi,
I just stumbled across a YT video https://www.youtube.com/watch?v=0E3e4Ec30ro&t=145s
He gives a link to OneDrive with all the necessary zip files to import into Ninjatrader.
https://onedrive.live.com/?v=validatepermission&id=20255E9C7F1C9614!181571&challengeToken=!AIQQQm4RhEC_Ohc with DBT2 as password
That is a good starting point for an automated strategy and then you will have to tweak buy and sell signals to do what your strategy requires. I'll be working on it myself so if I'm able to make some modifications I'll let you know.
Sorry for the late reply lol, I've been busy lately...
That looks interesting, I'll be sure to check it out and get back to you!
 
T

Tiredoflosing

New member
VIP
Hi,
I just stumbled across a YT video https://www.youtube.com/watch?v=0E3e4Ec30ro&t=145s
He gives a link to OneDrive with all the necessary zip files to import into Ninjatrader.
https://onedrive.live.com/?v=validatepermission&id=20255E9C7F1C9614!181571&challengeToken=!AIQQQm4RhEC_Ohc with DBT2 as password
That is a good starting point for an automated strategy and then you will have to tweak buy and sell signals to do what your strategy requires. I'll be working on it myself so if I'm able to make some modifications I'll let you know.
Password doesn't work bro :/

NVM- it works- I clicked the link from youtube video & it worked & accepted password
 
T

Tiredoflosing

New member
VIP
Hey guys!

So this simple strat made over 7k in under 24 hrs net of commissions with one contract on /ES at the time this was posted,

So, I stumbled upon Mobius's Supertrend and was blown away by its efficiency when paired with other indicators (such as MACD). And since TOS currently does not support Auto Trading :( and I have used NT in the past so I was wondering if I could configure my MACD + Supertrend strategy on NT.

I am aware that there is a supertrend indicator already on Ninja Trader (https://ninjatraderecosystem.com/us...complete-collection-of-supporting-indicators/) but I cross-referenced it using the same settings as the one by Mobius and It does not match.

My guess is that the problem is the "offset period" since it cannot be set to zero?

Here is my strategy where I combine super trend and MACD signals on TOS (used on 3 Range on /ES) that generated over 7k (on /ES with 1 contract) in profits in the last 24 hrs net of commissions:

Bullish Strategy:
Code:
# SuperTrend
input AtrMult = 1.0;
input nATR = 4;
input AvgType = AverageType.HULL;
input PaintBars = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST[1] then UP else DN;
plot SuperTrend = ST;
SuperTrend.AssignValueColor(if close < ST then Color.RED else Color.GREEN);
AssignPriceColor(if PaintBars and close < ST

                 then Color.RED

                 else if PaintBars and close > ST

                      then Color.GREEN

                      else Color.CURRENT);

# End Code SuperTrend

#MACD

#
# TD Ameritrade IP Company, Inc. (c) 2007-2020
#

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

#ORDERS

AddOrder(type = OrderType.BUY_TO_OPEN, ST < close and Diff > 0, tradeSize = 1, tickColor = Color.GREEN, arrowColor = Color.GREEN, name = "Long", price = CLOSE);
AddOrder(type = OrderType.SELL_TO_CLOSE, ST > close or Diff < 0, tradeSize = 1, name = "Close", tickColor = Color.GRAY, arrowColor = Color.GRAY, price = CLOSE);

Bearish Strategy:

Code:
# SuperTrend
input AtrMult = 1.0;
input nATR = 4;
input AvgType = AverageType.HULL;
input PaintBars = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST[1] then UP else DN;
plot SuperTrend = ST;
SuperTrend.AssignValueColor(if close < ST then Color.RED else Color.GREEN);
AssignPriceColor(if PaintBars and close < ST

                 then Color.RED

                 else if PaintBars and close > ST

                      then Color.GREEN

                      else Color.CURRENT);

# End Code SuperTrend

#MACD

#
# TD Ameritrade IP Company, Inc. (c) 2007-2020
#

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

#ORDERS

AddOrder(type = OrderType.SELL_TO_OPEN, ST > close and Diff < 0, tradeSize = 1, name = "Short", tickColor = Color.LIGHT_RED, arrowColor = Color.LIGHT_RED, price = CLOSE);
AddOrder(type = OrderType.BUY_TO_CLOSE, ST < close or Diff > 0, tradeSize = 1, tickColor = Color.GRAY, arrowColor = Color.GRAY, name = "Close", price = CLOSE);
Supertrend Code by Mobius:
Code:
# Mobius
# SuperTrend
# Chat Room Request
input AtrMult = 1.0;
input nATR = 4;
input AvgType = AverageType.HULL;
input PaintBars = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST[1] then UP else DN;
plot SuperTrend = ST;
SuperTrend.AssignValueColor(if close < ST then Color.RED else Color.GREEN);
AssignPriceColor(if PaintBars and close < ST

                 then Color.RED

                 else if PaintBars and close > ST

                      then Color.GREEN

                      else Color.CURRENT);

AddChartBubble(close crosses below ST, low[1], low[1], color.Dark_Gray);
AddChartBubble(close crosses above ST, high[1], high[1], color.Dark_Gray, no);
# End Code SuperTrend
Some results:


Please help either convert the strategies to Ninja Trader or the Supertrend (Mobius) indicator to NT or tweak the existing Supertrend indicator (link is above) to make it match the one on TOS on the 3 RANGE CHART. Or even, if you find a NT indicator that already emulates the one by Mobius please share it.

ANY help is greatly appreciated as I am trying to make this work ASAP, and I will do my best to answer any questions!

Thanks for all the help guys!

** To set up your 3 Range chart (on PC): go to chart settings (the gear icon @ the top left corner of your chart), click "Time Axis" (found in the top header), change aggregation type to Range, change the price range to 3, and the Range type to range bars.

PS: Sorry BenTen for posting in the wrong forum again :/ I have no idea how to move it to a different forum after I posted it.
For some reason, my Floating P/L is showing up black
 
A

adii800

Member
For some reason, my Floating P/L is showing up black
Wdym, like the bars themselves, are showing in black?

Uhh I may have misunderstood you but you can change the colors by double clicking the study in Edit Studies and Strategies.
 
T

Tiredoflosing

New member
VIP
Wdym, like the bars themselves, are showing in black?

Uhh I may have misunderstood you but you can change the colors by double clicking the study in Edit Studies and Strategies.
nvm- the issue was with TOS.
How has the script performed for u? any luck with moving to NT? i would be very interested as well
 
I

irishgold

Member
Just to update. I have been digging into code of NinjaTrader and I think this will be doable. I'm actually working on using HelkinAshi and Trend state from CSA study which I have found in TOS to be pretty good at predicting a buy and sell signal although not 100% in terms of repainting.
The above code in post 1 I only found profitable at an extremely low tick rate which with the number of trades I don't even know if that would be feasible.
 
I

irishgold

Member
As I said previously I do not think the above code is particularly profitable. But on another note I have converted a strategy I have pulled from various posts here on usethinkscript. It is pretty good in TOS and I just converted it to NinjaTrader script so if you are interested in it. The in the Ninjatrader Forum link you can drop into Documents/Ninjatrader 8/Bin/Custom/Strategies folder, then compile it in the Ninjascript editor, run it in the Strategy Analyzer.
I have been using the NQ 09-20 instrument with minute 2. On NinjaTrader forum I included a screenshot of the worst period from Feb20 to March 24 and just trading 1 contract. Here is the link: TOS to Ninjatrader conversion script
 
T

Tiredoflosing

New member
VIP
As I said previously I do not think the above code is particularly profitable. But on another note I have converted a strategy I have pulled from various posts here on usethinkscript. It is pretty good in TOS and I just converted it to NinjaTrader script so if you are interested in it. The in the Ninjatrader Forum link you can drop into Documents/Ninjatrader 8/Bin/Custom/Strategies folder, then compile it in the Ninjascript editor, run it in the Strategy Analyzer.
I have been using the NQ 09-20 instrument with minute 2. On NinjaTrader forum I included a screenshot of the worst period from Feb20 to March 24 and just trading 1 contract. Here is the link: TOS to Ninjatrader conversion script
for some reason, this strategy is coming as blank when i upload it & try to backtest it. is it working for u? I followed the upload instructions u sent in this post.
 
I

irishgold

Member
You must have active connection try Jul 17 to Jul23. I am having issues with it now that I try to use it in the Playback mode. It is a learning experience for me too. Sometimes what you hope for is just a pipe dream, too good to be true. I hope not but that's how it's looking. I have found that different dates don't always result in a good test. I just haven't figured out all this Ninja stuff.
 
T

Tiredoflosing

New member
VIP
You must have active connection try Jul 17 to Jul23. I am having issues with it now that I try to use it in the Playback mode. It is a learning experience for me too. Sometimes what you hope for is just a pipe dream, too good to be true. I hope not but that's how it's looking. I have found that different dates don't always result in a good test. I just haven't figured out all this Ninja stuff.
i do have an active connection, doesn't seem to load for me.
 
T

tradegeek

Member
2019 Donor
VIP
@irishgold Very interesting. Thanks for sharing the strategy. One problem I've found is this...

Let's say I'm short 1 contract then when the strategy changes to long this is what happens...

It triggers 1 close position, 1 CloseShort, and 1 LongEntry which results in net 2 contract long.

So instead of 1 lot long, you're now 2 lots. Do you know how to fix this to get rid of that extra close position trigger?
 
Last edited:
I

irishgold

Member
@tradegeek I have had little success in making Ninjatrader work for me. I am so frustrated, mainly because I really don't know how to make the code function the way I see the TOS strategy work. So I did notice trading long by itself and then trading short by itself seems to have better results. The strategy analyzer will show great success and then trying to use the Market replay it is not very good. Right now I have no real success story. Just like TOS strategy seems so awesome but not in real trading.
 
T

tradegeek

Member
2019 Donor
VIP
@irishgold Would you mind sharing the TOS version of the strategy? I would like to know the parameters of the entries and exits. I don't know any ninjascript and just learning thinkscript from code snippets and this forum but I like tinkering.
 
I

irishgold

Member
Code:
#VERSION
# 2020.07.11 V1.0 @irishgold -  Trade Strategy
# Strategy  Trend Code on two timeframes 2min 5 min

input Contracts =1;
input TradeLong = 1;
input TradeShort = 1;
def FPL = FPL();
AddLabel(yes,
         text = "Total Profit: " + AsDollars(FPL),
         color = if FPL > 0
                 then Color.GREEN
                 else Color.RED
        );
AddLabel(yes, "Contracts: " + Contracts, Color.WHITE);

# Trend Study reference code by usethinkscript member @henry1224 which is his version TTM Trend
input agg = AggregationPeriod.TWO_MIN;
def o1 = open( Period = agg);
def C1 = close( Period = agg);
def H1 = high( Period = agg);
def L1 = low( Period = agg);
def Adata = Sum((o1 + H1 + L1 + C1) / 4, 6) / 6;
#AddLabel(yes, Adata, Color.White);
def ATRend1 = if C1 >= Adata then 1 else 0;
def ATRend2 = if C1 >= Adata[1] then 1 else 0;
def ATRend3 = if C1 >= Adata[2] then 1 else 0;
def ATRend4 = if C1 >= Adata[3] then 1 else 0;
def ATRend5 = if C1 >= Adata[4] then 1 else 0;
def TotalT = (ATRend1 + ATRend2 + ATRend3 + ATRend4 + ATRend5);
def Trend_sBearish1 = if 2.5 > TotalT then 1 else 0;
AddLabel(yes, "T_2min: " + TotalT, if Trend_sBearish1 == 1 then Color.RED else Color.GREEN);
#Aggregation 2
input agg2 = AggregationPeriod.FIVE_MIN;
def o2 = open( Period = agg2);
def C2 = close( Period = agg2);
def H2 = high( Period = agg2);
def L2 = low( Period = agg2);
def Bdata = Sum((o2 + H2 + L2 + C2) / 4, 6) / 6;
#AddLabel(yes, Bdata, Color.White);
def BTRend1 = if C2 >= Bdata then 1 else 0;
def BTRend2 = if C2 >= Bdata[1] then 1 else 0;
def BTRend3 = if C2 >= Bdata[2] then 1 else 0;
def BTRend4 = if C2 >= Bdata[3] then 1 else 0;
def BTRend5 = if C2 >= Bdata[4] then 1 else 0;
def TotalT2 = (BTRend1 + BTRend2 + BTRend3 + BTRend4 + BTRend5);
def Trend_sBearish2 = if 3.5 > TotalT2 then 1 else 0;
def Trend_sBearish2Buy = if 2.5 > TotalT2 then 1 else 0;
AddLabel(yes, "T_5min: " + TotalT2, if Trend_sBearish2 == 1 then Color.RED else Color.GREEN);

# Close all trades Friday
def isEndofWeek = if GetDayOfWeek(GetYYYYMMDD()) == 5 then yes else no;
def EOD_Friday = if SecondsTillTime(1558) == 0 and SecondsFromTime(1558) == 0 and isEndofWeek then 1 else 0;
def noTrades = if  isEndofWeek == yes and GetTime() > RegularTradingEnd(GetYYYYMMDD()) then 1 else 0;
# Define Buy and Sell Signals
def buySignal =   TradeLong and !Trend_sBearish1 and !Trend_sBearish2Buy  and !noTrades;
def CloseLongSignal =     Trend_sBearish1 and Trend_sBearish2;
def sellSignal =   TradeShort and Trend_sBearish1 and Trend_sBearish2  and !noTrades;
def CloseShortSignal =   !Trend_sBearish1 and !Trend_sBearish2;


def orderPrice = (open[-1]+close[-1])/2;
AddLabel(yes, if buySignal then "LONG" else if sellSignal then "SHORT" else "" , Color.Blue);
# Long Trades
AddOrder(type = OrderType.BUY_TO_OPEN, buySignal, price =orderPrice, tradeSize = Contracts, name = "Long", tickcolor = Color.GREEN);
AddOrder(type = OrderType.SELL_TO_CLOSE, CloseLongSignal, price = open[-1], tradeSize = Contracts, name = "Close");

#Short Orders
AddOrder(type = OrderType.SELL_TO_OPEN, sellSignal, price = orderPrice, tradeSize = Contracts, name = "Short", tickcolor = Color.GREEN);
AddOrder(type = OrderType.BUY_TO_CLOSE,CloseShortSignal, price = open[-1], tradeSize = Contracts, name = "CloseShort");


# Close all trades Friday

AddOrder(type = OrderType.SELL_TO_CLOSE, EOD_Friday, price = open[-1], tradeSize = Contracts, name = "noTradesClose");
Here is my TOS code which I usually test on 2 min /NQ it is unbelievable in Strategy but not so reliable in real time. Turning on the TradeLong and TradeShort separately have higher profit value than trading both turned on.
 
I

irishgold

Member
BTW, I did update the Ninjascript code see the link. And it seems to function now in strategy analyzer and market replay. It's roughly based on the code I just post in previous post.My Ninjascript Code. Trading on 2 min /NQ did pretty profitable.
 
T

tradegeek

Member
2019 Donor
VIP
@irishgold Thank you very much! I will test it out and let you know if I find any improvements. Keep me updated!
 
Top