NexusFi: Find Your Edge


Home Menu

 





Need Help with Strategy Coding


Discussion in NinjaTrader

Updated
    1. trending_up 1,616 views
    2. thumb_up 3 thanks given
    3. group 3 followers
    1. forum 3 posts
    2. attach_file 0 attachments




 
Search this Thread

Need Help with Strategy Coding

  #1 (permalink)
 keepdchange 
California
 
Experience: Intermediate
Platform: Ninjatrader
Broker: Zen Fire
Trading: ES, Currency Futures (6E)
Posts: 157 since Sep 2009
Thanks Given: 78
Thanks Received: 10

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;

if (CurrentBars[0] < 1 || CurrentBars[1] < 1 || CurrentBars[2] < 1)
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);
}
}
}

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NexusFi Journal Challenge - April 2024
Feedback and Announcements
ZombieSqueeze
Platforms and Indicators
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
Deepmoney LLM
Elite Quantitative GenAI/LLM
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
38 thanks
NexusFi site changelog and issues/problem reporting
27 thanks
GFIs1 1 DAX trade per day journal
19 thanks
The Program
18 thanks
  #2 (permalink)
walter739
Caracas
 
Posts: 23 since Sep 2020
Thanks Given: 114
Thanks Received: 12

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.

Regards

Reply With Quote
Thanked by:
  #3 (permalink)
 
Sandpaddict's Avatar
 Sandpaddict 
Vancouver, Canada
 
Experience: Advanced
Platform: Ninjatrader, MT4
Broker: IB, Global Prime
Trading: Futures CFDs
Posts: 684 since Mar 2020
Thanks Given: 975
Thanks Received: 637



keepdchange View Post
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;

if (CurrentBars[0] < 1 || CurrentBars[1] < 1 || CurrentBars[2] < 1)
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);
}
}
}

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.



Sent using the NexusFi mobile app

Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #4 (permalink)
 
Sandpaddict's Avatar
 Sandpaddict 
Vancouver, Canada
 
Experience: Advanced
Platform: Ninjatrader, MT4
Broker: IB, Global Prime
Trading: Futures CFDs
Posts: 684 since Mar 2020
Thanks Given: 975
Thanks Received: 637


walter739 View Post
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.

Regards

Actually that's an even better answer. Build it in strategy builder (super easy) THEN go study the code.

Sent using the NexusFi mobile app

Visit my NexusFi Trade Journal Reply With Quote
Thanked by:




Last Updated on May 23, 2021


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts