What's wrong with my code? Simple strategy - NinjaTrader Programming | futures.io
futures.io futures trading
 

Go Back   futures.io

> Futures Trading, News, Charts and Platforms > Platforms and Indicators > NinjaTrader > NinjaTrader Programming


What's wrong with my code? Simple strategy
Started:October 26th, 2016 (06:03 PM) by SellBlock1138 Views / Replies:403 / 14
Last Reply:November 7th, 2016 (01:24 PM) Attachments:1

Welcome to futures.io.

Welcome, Guest!

This forum was established to help traders (especially futures traders) by openly sharing indicators, strategies, methods, trading journals and discussing the psychology of trading.

We are fundamentally different than most other trading forums:
  • We work extremely hard to keep things positive on our forums.
  • We do not tolerate rude behavior, trolling, or vendor advertising in posts.
  • We firmly believe in openness and encourage sharing. The holy grail is within you, it is not something tangible you can download.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.


You'll need to register in order to view the content of the threads and start contributing to our community. It's free and simple, and we will never resell your private information.

-- Big Mike
     

Reply
 1  
 
Thread Tools Search this Thread

What's wrong with my code? Simple strategy

Old October 26th, 2016, 06:03 PM   #1 (permalink)
Elite Member
Cincinnati, OH
 
Futures Experience: None
Platform: NinjaTrader/Tradestation
Favorite Futures: FX, Options
 
Posts: 11 since Jan 2016
Thanks: 2 given, 0 received

What's wrong with my code? Simple strategy

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
}
}

Reply With Quote
     

Old October 26th, 2016, 06:03 PM   #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

     

Old October 26th, 2016, 07:58 PM   #3 (permalink)
Elite Member
London, UK
 
Futures Experience: Intermediate
Platform: NinjaTrader, Julia
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 644 since Aug 2009
Thanks: 314 given, 593 received



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
     
The following 2 users say Thank You to gregid for this post:
     

Old October 26th, 2016, 08:13 PM   #4 (permalink)
Elite Member
Cincinnati, OH
 
Futures Experience: None
Platform: NinjaTrader/Tradestation
Favorite Futures: FX, Options
 
Posts: 11 since Jan 2016
Thanks: 2 given, 0 received

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!!!

Reply With Quote
     

Old October 27th, 2016, 04:05 PM   #5 (permalink)
Elite Member
Cincinnati, OH
 
Futures Experience: None
Platform: NinjaTrader/Tradestation
Favorite Futures: FX, Options
 
Posts: 11 since Jan 2016
Thanks: 2 given, 0 received

Futures Edge on FIO
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
Register to download File Type: txt NinjaScript Output 10_27_2016 2_53 PM.txt (498.4 KB, 0 views)
Reply With Quote
     

Old October 27th, 2016, 04:27 PM   #6 (permalink)
Elite Member
London, UK
 
Futures Experience: Intermediate
Platform: NinjaTrader, Julia
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 644 since Aug 2009
Thanks: 314 given, 593 received

@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
     

Old October 27th, 2016, 04:30 PM   #7 (permalink)
Elite Member
Cincinnati, OH
 
Futures Experience: None
Platform: NinjaTrader/Tradestation
Favorite Futures: FX, Options
 
Posts: 11 since Jan 2016
Thanks: 2 given, 0 received

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!

Reply With Quote
     

Old October 27th, 2016, 04:36 PM   #8 (permalink)
Elite Member
London, UK
 
Futures Experience: Intermediate
Platform: NinjaTrader, Julia
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 644 since Aug 2009
Thanks: 314 given, 593 received

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
     

Old October 29th, 2016, 11:09 AM   #9 (permalink)
Elite Member
Cincinnati, OH
 
Futures Experience: None
Platform: NinjaTrader/Tradestation
Favorite Futures: FX, Options
 
Posts: 11 since Jan 2016
Thanks: 2 given, 0 received

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

Reply With Quote
     

Old October 29th, 2016, 04:05 PM   #10 (permalink)
Elite Member
Littleton, CO
 
Futures Experience: Intermediate
Platform: NT8, Jigsaw
Broker/Data: NinjaTrader Brokerage, CQG
Favorite Futures: ES, YM
 
Posts: 5 since Oct 2015
Thanks: 0 given, 1 received


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
     
The following 2 users say Thank You to poncho for this post:
     

Reply



futures.io > Futures Trading, News, Charts and Platforms > Platforms and Indicators > NinjaTrader > NinjaTrader Programming > What's wrong with my code? Simple strategy

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)
 

Al Brooks: Stop Losing when a Good Trade goes Bad, Correcting Mistakes

Elite only
 

Trading Technologies: Algo Design Lab hands-on

Dec 13

Normal webinar schedule resumes after the holidays

January
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is wrong with this code? dennisconn EasyLanguage Programming 4 August 18th, 2014 11:57 AM
Need simple code for StochRSI please TraderTom NinjaTrader Programming 5 May 6th, 2014 06:36 PM
what's wrong with my code??? zoudaodi ThinkOrSwim Programming 3 March 5th, 2014 03:01 PM
Need help to see what's wrong with my code kingfish88 ThinkOrSwim Programming 5 January 7th, 2014 04:04 AM
MC: Simple strategy code problem / Easy Language true EasyLanguage Programming 4 December 19th, 2010 09:48 AM


All times are GMT -4. The time now is 06:04 PM.

Copyright © 2016 by futures.io. 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.
 
no new posts
Page generated 2016-12-08 in 0.21 seconds with 20 queries on phoenix via your IP 54.159.129.152