Dark Theme
Light Theme
Trading Articles
Article Categories
Article Tools
Welcome to NexusFi: the best trading community on the planet, with over 150,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 -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
if and else if - Ninjascript Trade logic
Updated July 22, 2022
trending_up
959 views
thumb_up
0 thanks given
group
2 followers
forum
2 posts
attach_file
0 attachments
if and else if - Ninjascript Trade logic
July 20th, 2022, 06:59 AM
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:" );
}
}
Can you help answer these questions from other members on NexusFi?
Best Threads (Most Thanked) in the last 7 days on NexusFi
July 20th, 2022, 12:16 PM
Posts: 109 since Mar 2021
Thanks Given: 33
Thanks Received: 56
ryangillespie
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:" );
}
}
}
July 22nd, 2022, 08:36 AM
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.
Last Updated on July 22, 2022