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


What's wrong with my code? Simple strategy
Updated: Views / Replies:816 / 14
Created: by SellBlock1138 Attachments:1

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • 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.

-- Big Mike, Site Administrator

Reply
 1  
 
Thread Tools Search this Thread
 

What's wrong with my code? Simple strategy

  #1 (permalink)
Elite Member
Cincinnati, OH
 
Futures Experience: Beginner
Platform: NinjaTrader/Thinkorswim
Broker/Data: Ninjatrader/FXCM/TD
Favorite Futures: All of them
 
Posts: 13 since Jan 2016
Thanks: 2 given, 1 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
 
  #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.

 
  #3 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 603 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:
 
  #4 (permalink)
Elite Member
Cincinnati, OH
 
Futures Experience: Beginner
Platform: NinjaTrader/Thinkorswim
Broker/Data: Ninjatrader/FXCM/TD
Favorite Futures: All of them
 
Posts: 13 since Jan 2016
Thanks: 2 given, 1 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
 
  #5 (permalink)
Elite Member
Cincinnati, OH
 
Futures Experience: Beginner
Platform: NinjaTrader/Thinkorswim
Broker/Data: Ninjatrader/FXCM/TD
Favorite Futures: All of them
 
Posts: 13 since Jan 2016
Thanks: 2 given, 1 received

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
 
  #6 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 603 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
 
  #7 (permalink)
Elite Member
Cincinnati, OH
 
Futures Experience: Beginner
Platform: NinjaTrader/Thinkorswim
Broker/Data: Ninjatrader/FXCM/TD
Favorite Futures: All of them
 
Posts: 13 since Jan 2016
Thanks: 2 given, 1 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
 
  #8 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 603 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
 
  #9 (permalink)
Elite Member
Cincinnati, OH
 
Futures Experience: Beginner
Platform: NinjaTrader/Thinkorswim
Broker/Data: Ninjatrader/FXCM/TD
Favorite Futures: All of them
 
Posts: 13 since Jan 2016
Thanks: 2 given, 1 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
 
  #10 (permalink)
Elite Member
Littleton, CO
 
Futures Experience: Intermediate
Platform: Jigsaw (on NT8)
Broker/Data: IB, IQFeed
Favorite Futures: CL
 
Posts: 10 since Oct 2015
Thanks: 6 given, 8 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 > > > > > 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)

July Journal Challenge w/$1100 in prizes from TopstepTrader

July

John @ No BS Day Trading (TBA)

Elite only

An Afternoon with FIO member Softsoap (being rescheduled)

Elite only
     

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


All times are GMT -4. The time now is 08:25 AM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@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 2017-06-28 in 0.20 seconds with 20 queries on phoenix via your IP 54.224.247.75