NexusFi: Find Your Edge


Home Menu

 





if and else if - Ninjascript Trade logic


Discussion in NinjaTrader

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




 
Search this Thread

if and else if - Ninjascript Trade logic

  #1 (permalink)
 
ryangillespie's Avatar
 ryangillespie 
london, England
 
Experience: Intermediate
Platform: Ninjatrader
Trading: CL, ES
Posts: 41 since Nov 2011
Thanks Given: 10
Thanks Received: 2

Hi all,

I am learning to code in ninjascript, so if within startTradiinInHours in enters certain trades and then if within startTradingOutHours it enters certain trades.

However when I run the strategy it only trades the first 2 logic sets and dismisses the second two. Can someone point out what I have done wrong please.

 
Code
	if (Position.MarketPosition == MarketPosition.Flat)
				{
			/******************** ALL TRADE LOGIC GOES IN HERE   ***********************************/
					
					////////////////////////////////////// LONG CONDITIONS MARKET HOURS			                          
					
					if  //((BarsSinceExit() > 5 || BarsSinceExit() == -1)
						(ToTime(Time[0]) >= startTradingInTime
						&& ToTime(Time[0]) <= endTradingInTime
						&& XavTrend1 == 5
						&& XavTrend2 == 5
						&& ScalpTrader == 1
						&& adxlong_in > ADXlong 
						&& Close[0] > sma_in 
						//&& ema_in > sma_in
						&& entryOrder == null)	
						
						//EnterShort(lots, "DT+DT");
						EnterLong(lots, "UT+UT");
						if (printtolog == true) Print("LONG TRADE-----------RGAuto:" );  					       
						 
						//if (printtolog == true) Print("SHORT TRADE-----------RGAuto:");  

					}
					
					
					/////////////////////////////////////////////// SHORT CONDITION MARKET HOURS
				
				if (Position.MarketPosition == MarketPosition.Flat)
				{
									
					if  //(BarsSinceExit() > 5
						(ToTime(Time[0]) >= startTradingInTime
						&& ToTime(Time[0]) <= endTradingInTime
						&& XavTrend1 == -5
						&& XavTrend2 == -5
						&& ScalpTrader == 2
						&& adxshort_in > ADXshort
						&& Close[0] < sma_in 
						//&& ema_in < sma_in
						&& entryOrder == null)				
					
						//EnterLong(lots, "UT+UT");
						EnterShort(lots, "DT+DT"); 
						if (printtolog == true) Print("SHORT TRADE-----------RGAuto:");
						
						//if (printtolog == true) Print("LONG TRADE-----------RGAuto:" ); 
				 
				
					
				}
				
				if (Position.MarketPosition == MarketPosition.Flat)
				{
	
					
					////////////////////////////////////// LONG CONDITIONS NON MARKET HOURS			                          
					
					if  //((BarsSinceExit() > 5 || BarsSinceExit() == -1)
						(ToTime(Time[0]) >= startTradingOutTime
						&& ToTime(Time[0]) <= endTradingOutTime
						&& XavTrend1 == 5
						&& XavTrend2 == 5
						&& ScalpTrader == 1
						&& adxlong_in > ADXlong 
						&& Close[0] > sma_in 
						//&& ema_in > sma_in
						&& entryOrder == null)	
						
						EnterShort(lots, "DT+DT");
						//EnterLong(lots, "UT+UT");
						if (printtolog == true) Print("LONG TRADE-----------RGAuto:" );  					       
						 
						//if (printtolog == true) Print("SHORT TRADE-----------RGAuto:");  

				}
				
								if (Position.MarketPosition == MarketPosition.Flat)
				{
									
					if  //(BarsSinceExit() > 5
						(ToTime(Time[0]) >= startTradingOutTime
						&& ToTime(Time[0]) <= endTradingOutTime
						&& XavTrend1 == -5
						&& XavTrend2 == -5
						&& ScalpTrader == 2
						&& adxshort_in > ADXshort
						&& Close[0] < sma_in 
						//&& ema_in < sma_in
						&& entryOrder == null)				
					
						EnterLong(lots, "UT+UT");
						//EnterShort(lots, "DT+DT"); 
						if (printtolog == true) Print("SHORT TRADE-----------RGAuto:");
						
						//if (printtolog == true) Print("LONG TRADE-----------RGAuto:" ); 
				 
				
					
				}
				
			}

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
Online prop firm The Funded Trader (TFT) going under?
Traders Hideout
Deepmoney LLM
Elite Quantitative GenAI/LLM
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
Build trailing stop for micro index(s)
Psychology and Money Management
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
59 thanks
Funded Trader platforms
36 thanks
GFIs1 1 DAX trade per day journal
22 thanks
NexusFi site changelog and issues/problem reporting
22 thanks
The Program
20 thanks
  #2 (permalink)
 TigerStripes   is a Vendor
 
Posts: 109 since Mar 2021
Thanks Given: 33
Thanks Received: 56


ryangillespie View Post
Hi all,

I am learning to code in ninjascript, so if within startTradiinInHours in enters certain trades and then if within startTradingOutHours it enters certain trades.

However when I run the strategy it only trades the first 2 logic sets and dismisses the second two. Can someone point out what I have done wrong please.

 
Code
	if (Position.MarketPosition == MarketPosition.Flat)
				{
			/******************** ALL TRADE LOGIC GOES IN HERE   ***********************************/
					
					////////////////////////////////////// LONG CONDITIONS MARKET HOURS			                          
					
					if  //((BarsSinceExit() > 5 || BarsSinceExit() == -1)
						(ToTime(Time[0]) >= startTradingInTime
						&& ToTime(Time[0]) <= endTradingInTime
						&& XavTrend1 == 5
						&& XavTrend2 == 5
						&& ScalpTrader == 1
						&& adxlong_in > ADXlong 
						&& Close[0] > sma_in 
						//&& ema_in > sma_in
						&& entryOrder == null)	
						
						//EnterShort(lots, "DT+DT");
						EnterLong(lots, "UT+UT");
						if (printtolog == true) Print("LONG TRADE-----------RGAuto:" );  					       
						 
						//if (printtolog == true) Print("SHORT TRADE-----------RGAuto:");  

					}
					
					
					/////////////////////////////////////////////// SHORT CONDITION MARKET HOURS
				
				if (Position.MarketPosition == MarketPosition.Flat)
				{
									
					if  //(BarsSinceExit() > 5
						(ToTime(Time[0]) >= startTradingInTime
						&& ToTime(Time[0]) <= endTradingInTime
						&& XavTrend1 == -5
						&& XavTrend2 == -5
						&& ScalpTrader == 2
						&& adxshort_in > ADXshort
						&& Close[0] < sma_in 
						//&& ema_in < sma_in
						&& entryOrder == null)				
					
						//EnterLong(lots, "UT+UT");
						EnterShort(lots, "DT+DT"); 
						if (printtolog == true) Print("SHORT TRADE-----------RGAuto:");
						
						//if (printtolog == true) Print("LONG TRADE-----------RGAuto:" ); 
				 
				
					
				}
				
				if (Position.MarketPosition == MarketPosition.Flat)
				{
	
					
					////////////////////////////////////// LONG CONDITIONS NON MARKET HOURS			                          
					
					if  //((BarsSinceExit() > 5 || BarsSinceExit() == -1)
						(ToTime(Time[0]) >= startTradingOutTime
						&& ToTime(Time[0]) <= endTradingOutTime
						&& XavTrend1 == 5
						&& XavTrend2 == 5
						&& ScalpTrader == 1
						&& adxlong_in > ADXlong 
						&& Close[0] > sma_in 
						//&& ema_in > sma_in
						&& entryOrder == null)	
						
						EnterShort(lots, "DT+DT");
						//EnterLong(lots, "UT+UT");
						if (printtolog == true) Print("LONG TRADE-----------RGAuto:" );  					       
						 
						//if (printtolog == true) Print("SHORT TRADE-----------RGAuto:");  

				}
				
								if (Position.MarketPosition == MarketPosition.Flat)
				{
									
					if  //(BarsSinceExit() > 5
						(ToTime(Time[0]) >= startTradingOutTime
						&& ToTime(Time[0]) <= endTradingOutTime
						&& XavTrend1 == -5
						&& XavTrend2 == -5
						&& ScalpTrader == 2
						&& adxshort_in > ADXshort
						&& Close[0] < sma_in 
						//&& ema_in < sma_in
						&& entryOrder == null)				
					
						EnterLong(lots, "UT+UT");
						//EnterShort(lots, "DT+DT"); 
						if (printtolog == true) Print("SHORT TRADE-----------RGAuto:");
						
						//if (printtolog == true) Print("LONG TRADE-----------RGAuto:" ); 
				 
				
					
				}
				
			}

Initially you have some details and need to understand how to properly state an IF statement, if there is a IF statement and only ONE resultant, there is no need to add curley braces and you can indent place the resultant below the IF statement and the code will understand that one line is the only line to execute based on your IF statement.

example
if (EMA1.Diretion[0] < Closes[2][2] && RSI[0] > 30)
>>>>orderSignalEntry = enterySignal.long;

When there are multiple execution resultants, you will need to use the curley braces after the IF statement to enclose the Block of code that will run when the IF statement has been met.

example
if (EMA1.Diretion[0] < Closes[2][2] && RSI[0] > 30)
{
>>>>orderSignalEntry = enterySignal.long;
>>>>entryConMetSignal = 1;
}

Make sure you properly use the curley braces to enclose your main IF statement and that block contain the statements and resultants which will be executed when that is met. Create a secondary if statement to a set of code which will executer on other conditions.

A second note you have commented out your Second and third Secondary IF statement blocks which will not execute when commented. Set up the blocks using correct syntax which will allow you to execute both directions and a print statement when those conditions are met.

example.
 
Code
	protected override void OnBarUpdate()
		{
			/******************** ALL TRADE LOGIC GOES IN HERE   ***********************************/
			
			if ((Position.MarketPosition == MarketPosition.Flat)
                        && (ToTime(Time[0]) <= endTradingInTime) 
                        && (ToTime(Time[0]) >= startTradingInTime)
                        && (RTHHours == (true))) //create valid true condition inside trading hours
			{
                                 /////////////////////////////////// LONG CONDITIONS MARKET HOURS
				if  ((BarsSinceExit() > 5 || BarsSinceExit() == -1)
				&& (XavTrend1 == 5)
				&& (XavTrend2 == 5)
				&& (ScalpTrader == 1)
				&& (adxlong_in > ADXlong)
				&& (Close[0] > sma_in)
				&& (ema_in > sma_in))
				{
					//EnterShort(lots, "DT+DT");
					EnterLong(lots, "UT+UT");
					if (printtolog == true) Print("LONG TRADE-----------RGAuto:" );  					       
				 
					//if (printtolog == true) Print("SHORT TRADE-----------RGAuto:");  
				}

			        ///////////////////////////////////// SHORT CONDITION MARKET HOURS
			        if  ((BarsSinceExit() > 5 || BarsSinceExit() == -1)
				&& (XavTrend1 == -5)
				&& (XavTrend2 == -5)
				&& (ScalpTrader == 2)
				&& (adxshort_in > ADXshort)
				&& (Close[0] < sma_in)
				&& (ema_in < sma_in))
				{
					//EnterLong(lots, "UT+UT");
					EnterShort(lots, "DT+DT"); 
					if (printtolog == true) Print("SHORT TRADE-----------RGAuto:");
				
					//if (printtolog == true) Print("LONG TRADE-----------RGAuto:" ); 
				}
                       }
	
			////////////////////////////////////// LONG CONDITIONS NON MARKET HOURS			                          	
                        if ((Position.MarketPosition == MarketPosition.Flat)
                        && (ToTime(Time[0]) <= endTradingInTime) 
                        && (ToTime(Time[0]) >= startTradingInTime)
                        && (NonRTHHours == (true))) //create valid true condition outside trading hours
                        {
				if  ((BarsSinceExit() > 5 || BarsSinceExit() == -1)
				&& (XavTrend1 == 5)
				&& (XavTrend2 == 5)
				&& (ScalpTrader == 1)
				&& (adxlong_in > ADXlong)
				&& (Close[0] > sma_in) 
				&& (ema_in > sma_in))	
				{
					EnterShort(lots, "DT+DT");
					//EnterLong(lots, "UT+UT");
					if (printtolog == true) Print("LONG TRADE-----------RGAuto:" );  					       
				 
					//if (printtolog == true) Print("SHORT TRADE-----------RGAuto:");
				}
			
			        ////////////////////////////// SHORT CONDITIONS NON MARKET HOURS
			        if ((BarsSinceExit() > 5 || BarsSinceExit() == -1)
				&& (XavTrend1 == -5)
				&& (XavTrend2 == -5)
				&& (ScalpTrader == 2)
				&& (adxshort_in > ADXshort)
				&& (Close[0] < sma_in)
				&& (ema_in < sma_in))			
				{
					EnterLong(lots, "UT+UT");
					//EnterShort(lots, "DT+DT"); 
					if (printtolog == true) Print("SHORT TRADE-----------RGAuto:");
				
					//if (printtolog == true) Print("LONG TRADE-----------RGAuto:" ); 
				}
			 }
				
		}

Reply With Quote
  #3 (permalink)
 
ryangillespie's Avatar
 ryangillespie 
london, England
 
Experience: Intermediate
Platform: Ninjatrader
Trading: CL, ES
Posts: 41 since Nov 2011
Thanks Given: 10
Thanks Received: 2


@TigerStripes, Perfect, this helped. Thank you for taking the time to explain. Very much appreciated.

Started this thread Reply With Quote




Last Updated on July 22, 2022


© 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