NexusFi: Find Your Edge


Home Menu

 





What's wrong with my code? Simple strategy


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one SellBlock1138 with 9 posts (0 thanks)
    2. looks_two gregid with 3 posts (3 thanks)
    3. looks_3 trendwaves with 1 posts (1 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 5,541 views
    2. thumb_up 6 thanks given
    3. group 4 followers
    1. forum 14 posts
    2. attach_file 1 attachments




 
Search this Thread

What's wrong with my code? Simple strategy

  #1 (permalink)
 SellBlock1138 
Cincinnati, OH
 
Experience: Beginner
Platform: NinjaTrader/Thinkorswim
Broker: Ninjatrader/FXCM/TD
Trading: All of them
Posts: 13 since Jan 2016
Thanks Given: 2
Thanks Received: 1

I'm learning how to code and I'm really new to it. Right now I'm just trying to make this simple strategy but it isn't going very well so if anyone out there is kind enough to take a look and tell me what I'm doing wrong I would be quite grateful seeing as how ninjatrader's support has been less than stellar.

The strategy is simple, first break the market up into equal risk reward chunks, if you were to visualize it it would most likely look like the brick bar type without the wicks - essentially a renko with a 1x reversal instead of 2x. Next just go long after each green bar unless you are already long then just stay that way and go short after each red bar or stay short if you already are just reversing when the bars reverse. I know this strategy won't be profitable as is and there are a lot of other things I want to do to it but you can't build a house without a foundation so please just code advice here, thanks.

So the pseudo code logic is this:
if flat then go long 1 and set entry price to baseprice
if long then create a reversal order at (baseprice - piplength) - also if long and current price is greater than or equal to (benchprice + piplength) then set benchprice to (benchprice + piplength)
if short do the opposite of whats in the if long

the problem with my actual code is when I go to backtest it and it shows that it goes long 1 but then never changes direction and never makes any other trades besides the first and I can't tell why. Are my order types wrong or in the wrong direction? Is it only reading the if flat part? If there is a totally different way to write all this that might work better I'm open to that too

#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.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
namespace NinjaTrader.NinjaScript.Strategies{
public class SecondTest : Strategy{
private double Baseprice;
protected override void OnStateChange(){
if (State == State.SetDefaults){
Description = @"En";
Name = "SecondTest";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
ExitOnSessionCloseSeconds = 60;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
OrderFillResolution = OrderFillResolution.High;
OrderFillResolutionType = BarsPeriodType.Minute;
OrderFillResolutionValue = 1;
Slippage = 0;
StartBehavior = StartBehavior.ImmediatelySubmit;
TimeInForce = TimeInForce.Gtc;
TraceOrders = true;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 0;
IsInstantiatedOnEachOptimizationIteration = true;
Piplength = 100;
Baseprice = 1;
}
else if (State == State.Configure){}
}
protected override void OnBarUpdate()
{
if (Position.MarketPosition == MarketPosition.Flat);{
EnterLong(1);
Baseprice = Position.AveragePrice;
}

if (Position.MarketPosition == MarketPosition.Long);{
if (Close[0] >= (Baseprice + Piplength));{
Baseprice += Piplength;
}

EnterShortStopMarket((Position.Quantity * 2), (Baseprice - Piplength));
}

if (Position.MarketPosition == MarketPosition.Short);{
if (Close[0] >= (Baseprice - Piplength));{
Baseprice -= Piplength;
}

EnterLongStopMarket((Position.Quantity * 2), (Baseprice + Piplength));
}
}
#region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name="Piplength", Order=1, GroupName="NinjaScriptStrategyParameters")]
public int Piplength
{ get; set; }
#endregion
}
}

Visit my NexusFi Trade Journal Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
Futures True Range Report
The Elite Circle
Deepmoney LLM
Elite Quantitative GenAI/LLM
ZombieSqueeze
Platforms and Indicators
Exit Strategy
NinjaTrader
 
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
39 thanks
NexusFi site changelog and issues/problem reporting
26 thanks
GFIs1 1 DAX trade per day journal
18 thanks
The Program
18 thanks
  #3 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 650 since Aug 2009
Thanks Given: 320
Thanks Received: 623



SellBlock1138 View Post
if (Close[0] >= (Baseprice + Piplength));{
Baseprice += Piplength;
}

1. You are comparing apples to oranges. When evaluated it looks something like this:
if (14.25 >= (14.00+100)) which evaluates to:
if (14.25 >= 114.00) which always returns false.
To fix it you want to use eg.:
int HowManyTicks = 10;
 
Code
if (Close[0] >= (Baseprice + (HowManyTicks * TickSize))
{
...
}
2. Notice in the above code there is no semicolon ; before {}. That's how it should be - semicolon is terminating the expression while curly braces {} enclose the code to be evaluated further.
3. Use Print to avoid mistakes like in 1. If you would do it you would quickly notice the comparison doesn't make any sense, eg.:
 
Code
Print("Close: " + Close[0] + " Price: " +  (Baseprice + Piplength));

Reply With Quote
Thanked by:
  #4 (permalink)
 SellBlock1138 
Cincinnati, OH
 
Experience: Beginner
Platform: NinjaTrader/Thinkorswim
Broker: Ninjatrader/FXCM/TD
Trading: All of them
Posts: 13 since Jan 2016
Thanks Given: 2
Thanks Received: 1

Wow duh how did I not think that I should just be using a double value for the piplength to begin with, thank you that helps quite a bit!!!

Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #5 (permalink)
 SellBlock1138 
Cincinnati, OH
 
Experience: Beginner
Platform: NinjaTrader/Thinkorswim
Broker: Ninjatrader/FXCM/TD
Trading: All of them
Posts: 13 since Jan 2016
Thanks Given: 2
Thanks Received: 1

now my strategy is actually opening and closing positions in a backtest, however they still aren't where I am trying to tell the computer to place them, further it was giving martingale results when i was trying to put the order in for double the position size but thats not what I wanted, I wanted to go 1 long to 1 short and vice versa but putting in an order for 1 should only close out the other and not get me in unless its double the size (in this case 2x1 should be 2 so an order for 2 short when you are 1 long should get you 1 net short) so it makes no sense to me why I had to change that but here is the new code

#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.Gui.Tools;
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 ThirdTest : Strategy
{
private double Baseprice;


protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"3";
Name = "ThirdTest";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
OrderFillResolution = OrderFillResolution.High;
OrderFillResolutionType = BarsPeriodType.Minute;
OrderFillResolutionValue = 1;
Slippage = 0;
StartBehavior = StartBehavior.ImmediatelySubmitSynchronizeAccount;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 0;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
Piplength = 0.1;
Baseprice = 1;
}
else if (State == State.Configure)
{
}
}

protected override void OnBarUpdate()
{
Print(Position.MarketPosition);
if (Position.MarketPosition == MarketPosition.Flat){
Print(Close[0]);
EnterLong(1);
Baseprice = Position.AveragePrice;
Print(Baseprice);
}

if (Position.MarketPosition == MarketPosition.Long){
Print(Close[0]);
if (Close[0] >= (Baseprice + Piplength)){
Baseprice += Piplength;
Print(Baseprice);
}

EnterShortStopMarket(1, (Baseprice - Piplength));
}

if (Position.MarketPosition == MarketPosition.Short){
Print(Close[0]);
if (Close[0] >= (Baseprice - Piplength)){
Baseprice -= Piplength;
Print(Baseprice);
}

EnterLongStopMarket(1, (Baseprice + Piplength));
}
}

#region Properties
[NinjaScriptProperty]
[Range(0.001, double.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name="Piplength", Order=1, GroupName="NinjaScriptStrategyParameters")]
public double Piplength
{ get; set; }
#endregion

}
}



and I've attached the output
it seems like its not getting the initial baseprice being set to 1 or the command after to set it to the average position price but instead seems like its starting it at 0 and increasing by the piplength amount every time instead of decreasing at least some of the time like I'm trying to tell it to in part of the code, not sure where I am messing up, thanks again to anyone and everyone helping, when I get better at all this I'll definitely pay it forward helping other coding newbies

Attached Files
Elite Membership required to download: NinjaScript Output 10_27_2016 2_53 PM.txt
Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #6 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 650 since Aug 2009
Thanks Given: 320
Thanks Received: 623

@SellBlock1138 I recommend you join the webinar by Scott that starts in an hour. Scott has been here from the beginning and you will definitely learn a few things regarding strategy writing in NT.

As to your strategy - try using more meaningful messages in your Print statements, eg.:
Print(Current Position: " + Position.MarketPosition + " Entering Long at price: " + (BasePrice));
Also: you can always use Exit... methods to make sure you are flat before entering a new trade.

Reply With Quote
  #7 (permalink)
 SellBlock1138 
Cincinnati, OH
 
Experience: Beginner
Platform: NinjaTrader/Thinkorswim
Broker: Ninjatrader/FXCM/TD
Trading: All of them
Posts: 13 since Jan 2016
Thanks Given: 2
Thanks Received: 1

I've registered, I'll go through that, change the prints for easy reading and read about the exit methods and see what I can figure out, thanks!

Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #8 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 650 since Aug 2009
Thanks Given: 320
Thanks Received: 623

Another thing helpful in Print statement is Time[0], this gives a timestamp of currently processed bar, eg.:
 
Code
Print(Time[0] + "blabla" + " rest of the message");
This gives you possibility to track the execution on the chart to check if it is doing what you wanted.

Reply With Quote
  #9 (permalink)
 SellBlock1138 
Cincinnati, OH
 
Experience: Beginner
Platform: NinjaTrader/Thinkorswim
Broker: Ninjatrader/FXCM/TD
Trading: All of them
Posts: 13 since Jan 2016
Thanks Given: 2
Thanks Received: 1

ok using the print it seems to me that the issue is that my double value named Baseprice is not actually getting set equal to the position entry price after the first entry, can anyone tell me what I'm missing that could be causing this? thanks

#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.Gui.Tools;
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 ThirdTest : Strategy
{
private double Baseprice;


protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"3";
Name = "ThirdTest";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
OrderFillResolution = OrderFillResolution.High;
OrderFillResolutionType = BarsPeriodType.Minute;
OrderFillResolutionValue = 1;
Slippage = 0;
StartBehavior = StartBehavior.ImmediatelySubmitSynchronizeAccount;
TimeInForce = TimeInForce.Gtc;
TraceOrders = true;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 0;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
Piplength = 0.1;
Baseprice = 1;
}
else if (State == State.Configure)
{
}
}

protected override void OnBarUpdate()
{
Print("Time: " + Time[0] + " Direction: " + Position.MarketPosition + " Entry price: " + Position.AveragePrice + " Current tick price: " + Close[0] + " Baseprice: " + Baseprice);
if (Position.MarketPosition == MarketPosition.Flat){
EnterLong(1);
Baseprice = (Position.AveragePrice);
}

if (Position.MarketPosition == MarketPosition.Long){
if (Close[0] >= (Baseprice + Piplength)){
Baseprice += Piplength;
}

EnterShortStopMarket(1, (Baseprice - Piplength));
}

if (Position.MarketPosition == MarketPosition.Short){
if (Close[0] >= (Baseprice - Piplength)){
Baseprice -= Piplength;
}

EnterLongStopMarket(1, (Baseprice + Piplength));
}
}

#region Properties
[NinjaScriptProperty]
[Range(0.001, double.MaxValue)]
[Display(ResourceType = typeof(Custom.Resource), Name="Piplength", Order=1, GroupName="NinjaScriptStrategyParameters")]
public double Piplength
{ get; set; }
#endregion

}
}


it won't let me attach the whole output so here are the first few lines so that everyone can see what I see

Time: 12/31/2015 5:01:00 PM Direction: Flat Entry price: 0 Current tick price: 120.18 Baseprice: 1
12/31/2015 5:01:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 12/31/2015 5:01:00 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
Time: 1/3/2016 5:01:00 PM Direction: Long Entry price: 120.17 Current tick price: 120.2 Baseprice: 0
1/3/2016 5:01:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/3/2016 5:01:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
Time: 1/3/2016 5:03:00 PM Direction: Long Entry price: 120.17 Current tick price: 120.2 Baseprice: 0.1
1/3/2016 5:03:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/3/2016 5:03:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.10'0 SignalName='' FromEntrySignal=''
1/3/2016 5:03:00 PM Strategy 'ThirdTest/-1': Amended open order at 1/3/2016 5:03:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.10'0 SignalName='Sell short' FromEntrySignal=''
Time: 1/3/2016 5:05:00 PM Direction: Long Entry price: 120.17 Current tick price: 120.21 Baseprice: 0.2
1/3/2016 5:05:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/3/2016 5:05:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.20'0 SignalName='' FromEntrySignal=''
1/3/2016 5:05:00 PM Strategy 'ThirdTest/-1': Amended open order at 1/3/2016 5:05:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.20'0 SignalName='Sell short' FromEntrySignal=''
Time: 1/3/2016 5:06:00 PM Direction: Long Entry price: 120.17 Current tick price: 120.2 Baseprice: 0.3
1/3/2016 5:06:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/3/2016 5:06:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.30'0 SignalName='' FromEntrySignal=''
1/3/2016 5:06:00 PM Strategy 'ThirdTest/-1': Amended open order at 1/3/2016 5:06:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.30'0 SignalName='Sell short' FromEntrySignal=''
Time: 1/3/2016 5:07:00 PM Direction: Long Entry price: 120.17 Current tick price: 120.21 Baseprice: 0.4
1/3/2016 5:07:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/3/2016 5:07:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.40'0 SignalName='' FromEntrySignal=''
1/3/2016 5:07:00 PM Strategy 'ThirdTest/-1': Amended open order at 1/3/2016 5:07:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.40'0 SignalName='Sell short' FromEntrySignal=''
Time: 1/3/2016 5:08:00 PM Direction: Long Entry price: 120.17 Current tick price: 120.21 Baseprice: 0.5
1/3/2016 5:08:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/3/2016 5:08:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.50'0 SignalName='' FromEntrySignal=''
1/3/2016 5:08:00 PM Strategy 'ThirdTest/-1': Amended open order at 1/3/2016 5:08:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.50'0 SignalName='Sell short' FromEntrySignal=''
Time: 1/3/2016 5:09:00 PM Direction: Long Entry price: 120.17 Current tick price: 120.22 Baseprice: 0.6
1/3/2016 5:09:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/3/2016 5:09:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.60'0 SignalName='' FromEntrySignal=''
1/3/2016 5:09:00 PM Strategy 'ThirdTest/-1': Amended open order at 1/3/2016 5:09:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.60'0 SignalName='Sell short' FromEntrySignal=''
Time: 1/3/2016 5:10:00 PM Direction: Long Entry price: 120.17 Current tick price: 120.21 Baseprice: 0.7
1/3/2016 5:10:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/3/2016 5:10:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.70'0 SignalName='' FromEntrySignal=''
1/3/2016 5:10:00 PM Strategy 'ThirdTest/-1': Amended open order at 1/3/2016 5:10:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.70'0 SignalName='Sell short' FromEntrySignal=''
Time: 1/3/2016 5:11:00 PM Direction: Long Entry price: 120.17 Current tick price: 120.21 Baseprice: 0.8
1/3/2016 5:11:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/3/2016 5:11:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.80'0 SignalName='' FromEntrySignal=''
1/3/2016 5:11:00 PM Strategy 'ThirdTest/-1': Amended open order at 1/3/2016 5:11:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.80'0 SignalName='Sell short' FromEntrySignal=''
Time: 1/3/2016 5:12:00 PM Direction: Long Entry price: 120.17 Current tick price: 120.22 Baseprice: 0.9
1/3/2016 5:12:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/3/2016 5:12:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.90'0 SignalName='' FromEntrySignal=''
1/3/2016 5:12:00 PM Strategy 'ThirdTest/-1': Amended open order at 1/3/2016 5:12:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=0.90'0 SignalName='Sell short' FromEntrySignal=''
Time: 1/3/2016 5:13:00 PM Direction: Long Entry price: 120.17 Current tick price: 120.22 Baseprice: 1
1/3/2016 5:13:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/3/2016 5:13:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=1.00'0 SignalName='' FromEntrySignal=''
1/3/2016 5:13:00 PM Strategy 'ThirdTest/-1': Amended open order at 1/3/2016 5:13:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=1.00'0 SignalName='Sell short' FromEntrySignal=''
Time: 1/3/2016 5:14:00 PM Direction: Long Entry price: 120.17 Current tick price: 120.2 Baseprice: 1.1
1/3/2016 5:14:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/3/2016 5:14:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=1.10'0 SignalName='' FromEntrySignal=''
1/3/2016 5:14:00 PM Strategy 'ThirdTest/-1': Amended open order at 1/3/2016 5:14:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=1.10'0 SignalName='Sell short' FromEntrySignal=''
Time: 1/3/2016 5:15:00 PM Direction: Long Entry price: 120.17 Current tick price: 120.21 Baseprice: 1.2
1/3/2016 5:15:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/3/2016 5:15:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=1.20'0 SignalName='' FromEntrySignal=''
1/3/2016 5:15:00 PM Strategy 'ThirdTest/-1': Amended open order at 1/3/2016 5:15:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=1.20'0 SignalName='Sell short' FromEntrySignal=''
Time: 1/3/2016 5:16:00 PM Direction: Long Entry price: 120.17 Current tick price: 120.22 Baseprice: 1.3

Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #10 (permalink)
 poncho 
Littleton, CO
 
Experience: Intermediate
Platform: Jigsaw (on NT8)
Broker: IB, IQFeed
Trading: CL
Posts: 12 since Oct 2015
Thanks Given: 13
Thanks Received: 14


Without testing your code, I can't say for sure, but my guess is that when you call:

 
Code
EnterLong(1);
Baseprice = (Position.AveragePrice);
You are expecting that the order has been filled immediately after the EnterLong(1) method is called and the Position object has been populated. Due to multi-threading in NT8, this may not be the case. The only way you can know if an order has been filled is when the OnExecutionUpdate function has been called by the engine. As soon as that function is called, the engine then calls OnPositionUpdate where the Position object is populated.

You can see this in your code because when you execute Baseprice = (Position.AveragePrice), Position.AveragePrice is 0 because the Position object has not been populated.

I would recommend reading about how the Managed Approach works for strategies in the NT8 help guide to see what functions are called by engine and in what order they are called when an order entry is executed. This in the Ninjascript->Language Reference->Strategy->Order Methods section of the help guide.

Reply With Quote
Thanked by:




Last Updated on November 7, 2016


© 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