Welcome to NexusFi: the best trading community on the planet, with over 200,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- discounts are available after registering.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Hi. I am new to coding. Trying to practice with this sample strategy from Ninjatrader. Seems like it only takes Long trades. Can anyone please add the couple of lines for the strategy to also take short trades? I tried to copy the last few lines and duplicate with reversing the conditions, but not sure where other characters such as } need to go as I get errors.
Appreciate any help.. Here is the script:
//
// Copyright (C) 2021, NinjaTrader LLC <www.ninjatrader.com>.
// NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
//
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
//This namespace holds strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class SampleMultiTimeFrame : Strategy
{
private SMA sma50B0;
private SMA sma50B1;
private SMA sma50B2;
private SMA sma5B0;
private SMA sma5B1;
private SMA sma5B2;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = NinjaTrader.Custom.Resource.NinjaScriptStrategyDescriptionSampleMultiTimeFrame;
Name = NinjaTrader.Custom.Resource.NinjaScriptStrategyNameSampleMultiTimeFrame;
// This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = false;
}
else if (State == State.Configure)
{
// Add a 5 minute Bars object to the strategy
AddDataSeries(Data.BarsPeriodType.Minute, 5);
// Add a 15 minute Bars object to the strategy
AddDataSeries(Data.BarsPeriodType.Minute, 15);
}
else if (State == State.DataLoaded)
{
sma50B0 = SMA(50);
sma5B0 = SMA(5);
// Add simple moving averages to the chart for display
// This only displays the SMA's for the primary Bars object on the chart
AddChartIndicator(sma5B0);
AddChartIndicator(sma50B0);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < BarsRequiredToTrade)
return;
if (sma50B1 == null || sma50B2 == null || sma5B1 == null || sma5B2 == null)
{
// Note: Bars are added to the BarsArray and can be accessed via an index value
// E.G. BarsArray[1] ---> Accesses the 5 minute Bars object added above
sma50B1 = SMA(BarsArray[1], 50);
sma50B2 = SMA(BarsArray[2], 50);
sma5B1 = SMA(BarsArray[1], 5);
sma5B2 = SMA(BarsArray[2], 5);
}
// OnBarUpdate() will be called on incoming tick events on all Bars objects added to the strategy
// We only want to process events on our primary Bars object (index = 0) which is set when adding
// the strategy to a chart
if (BarsInProgress != 0)
return;
// Checks if the 5 period SMA is above the 50 period SMA on both the 5 and 15 minute time frames
if (sma5B1[0] > sma50B1[0] && sma5B2[0] > sma50B2[0])
{
// Checks for a cross above condition of the 5 and 50 period SMA on the primary Bars object and enters long
if (CrossAbove(sma5B0, sma50B0, 1))
{
EnterLong(1000, "SMA");
}
}
// Checks for a cross below condition of the 5 and 15 period SMA on the 15 minute time frame and exits long
if (CrossBelow(sma5B2, sma50B2, 1))
ExitLong(1000);
}
}
}
Can you help answer these questions from other members on NexusFi?
Hello I use deleveloped in strategybuilder and see the code for help me . And too for more complicated strategys donwload of appshare or forum and see the codes. Maybe if you do that can help.
Sure it's easy enough but it's pretty useless as well. You will GAIN far more by trying to figure out HOW to do it yourself.
To give you an idea what you have to do though is. Set the signal. Right now it's a SMA crossover. So copy the code and figure out where to insert it and change the the code from SMAfast > SMAslow to SMAfast < SMAslow.
Then once that condition is met copy "long" code and and figure out how to paste and change code to represent short.
Now to learn how to do this simply open another strategy with short code in it and examine and copy appropriate parts.
Paste. Compile. Look for errors. Repeat.
Someone just giving you the code will not help you because you will immediately find out you want to try something else as that didn't work.
I'm no programmer but if you have any more questions I can try to help but you have to understand what the code is doing if your going to manipulate it.