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,766 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

  #11 (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

you must be right! I added this extra line for myself
if (Position.MarketPosition == MarketPosition.Flat){
EnterLong(1);
Print("RIGHT HERE NERD: " + Position.AveragePrice);
Baseprice = Position.AveragePrice;

and sure enough it showed up 0 haha, I'll read that section of the help you suggested and post back what I figure out, thanks!

Visit my NexusFi Trade Journal Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
How to apply profiles
Traders Hideout
Better Renko Gaps
The Elite Circle
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
REcommedations for programming help
Sierra Chart
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
29 thanks
Just another trading journal: PA, Wyckoff & Trends
25 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
22 thanks
GFIs1 1 DAX trade per day journal
17 thanks
  #12 (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

Thanks! Adding these extra lines fixed my code!

protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{

if (execution.Order.OrderState == OrderState.Filled)
Baseprice = Position.AveragePrice;;
}

Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #13 (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 am now having another issue - I was backtesting on the usdjpy pair and specifically on jan 7th 2016 around 8:16pm it hit a sort of error when the market was moving really fast where the logic in the code doesn't seem to be followed, but in the tick by tick play by play in the output I don't understand why its doing what it is if another set of eyes could point me in the right direction that would be awesome, thanks

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;
IsUnmanaged = false;
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 = 120.00;
}
else if (State == State.Configure)
{
}
}

protected override void OnBarUpdate()
{
Print("!!!!! TIME: " + Time[0] + " DIR: " + Position.MarketPosition + " SIZE: " + Position.Quantity + " PRICE: " + Position.AveragePrice + " CURPRICE: " + Close[0] + " BASEPRICE: " + Baseprice);
if (Position.MarketPosition == MarketPosition.Flat){

EnterLong(1000, "Enter");
}

if (Position.MarketPosition == MarketPosition.Long){

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

EnterShortStopMarket(1000, Baseprice - Piplength);

}

if (Position.MarketPosition == MarketPosition.Short){

if (Close[0] <= Baseprice - Piplength){
Baseprice -= Piplength;
}

EnterLongStopMarket(1000, Baseprice + Piplength);

}
}

protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{

if (execution.Order.OrderState == OrderState.Filled)
Baseprice = Position.AveragePrice;
}



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

}
}



output in question


!!!!! TIME: 1/7/2016 8:12:00 PM DIR: Long SIZE: 1000 PRICE: 117.8 CURPRICE: 117.87 BASEPRICE: 117.8
1/7/2016 8:12:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/7/2016 8:12:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.70'0 SignalName='' FromEntrySignal=''
1/7/2016 8:12:00 PM Strategy 'ThirdTest/-1': Ignored SubmitOrderManaged() method at 1/7/2016 8:12:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.70'0 SignalName='Sell short' FromEntrySignal='' Reason='Order already has this stop price/limit price/quantity'
!!!!! TIME: 1/7/2016 8:13:00 PM DIR: Long SIZE: 1000 PRICE: 117.8 CURPRICE: 117.85 BASEPRICE: 117.8
1/7/2016 8:13:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/7/2016 8:13:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.70'0 SignalName='' FromEntrySignal=''
1/7/2016 8:13:00 PM Strategy 'ThirdTest/-1': Ignored SubmitOrderManaged() method at 1/7/2016 8:13:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.70'0 SignalName='Sell short' FromEntrySignal='' Reason='Order already has this stop price/limit price/quantity'
!!!!! TIME: 1/7/2016 8:14:00 PM DIR: Long SIZE: 1000 PRICE: 117.8 CURPRICE: 117.9 BASEPRICE: 117.8
1/7/2016 8:14:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/7/2016 8:14:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.80'0 SignalName='' FromEntrySignal=''
1/7/2016 8:14:00 PM Strategy 'ThirdTest/-1': Amended open order at 1/7/2016 8:14:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.80'0 SignalName='Sell short' FromEntrySignal=''
!!!!! TIME: 1/7/2016 8:15:00 PM DIR: Long SIZE: 1000 PRICE: 117.8 CURPRICE: 117.91 BASEPRICE: 117.9
1/7/2016 8:15:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/7/2016 8:15:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.80'0 SignalName='' FromEntrySignal=''
1/7/2016 8:15:00 PM Strategy 'ThirdTest/-1': Ignored SubmitOrderManaged() method at 1/7/2016 8:15:00 PM: BarsInProgress=0 Action=SellShort OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.80'0 SignalName='Sell short' FromEntrySignal='' Reason='Order already has this stop price/limit price/quantity'
!!!!! TIME: 1/7/2016 8:16:00 PM DIR: Short SIZE: 1000 PRICE: 117.8 CURPRICE: 118.35 BASEPRICE: 117.8
1/7/2016 8:16:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/7/2016 8:16:00 PM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.90'0 SignalName='' FromEntrySignal=''
Strategy 'ThirdTest/-1': A Buy order placed at '1/7/2016 8:16:00 PM' has been ignored since the stop price is less than or equal to the close price of the current bar. This is an invalid order and subsequent orders may also be ignored. Please fix your strategy.
1/7/2016 8:16:00 PM Strategy 'ThirdTest/-1': Ignored SubmitOrderManaged() method at 1/7/2016 8:16:00 PM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.90'0 SignalName='Buy' FromEntrySignal='' Reason='Invalid order price, please see log tab'
!!!!! TIME: 1/7/2016 8:17:00 PM DIR: Short SIZE: 1000 PRICE: 117.8 CURPRICE: 118.21 BASEPRICE: 117.8
1/7/2016 8:17:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/7/2016 8:17:00 PM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.90'0 SignalName='' FromEntrySignal=''
1/7/2016 8:17:00 PM Strategy 'ThirdTest/-1': Ignored SubmitOrderManaged() method at 1/7/2016 8:17:00 PM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.90'0 SignalName='Buy' FromEntrySignal='' Reason='Invalid order price, please see log tab'
!!!!! TIME: 1/7/2016 8:18:00 PM DIR: Short SIZE: 1000 PRICE: 117.8 CURPRICE: 118.12 BASEPRICE: 117.8
1/7/2016 8:18:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/7/2016 8:18:00 PM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.90'0 SignalName='' FromEntrySignal=''
1/7/2016 8:18:00 PM Strategy 'ThirdTest/-1': Ignored SubmitOrderManaged() method at 1/7/2016 8:18:00 PM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.90'0 SignalName='Buy' FromEntrySignal='' Reason='Invalid order price, please see log tab'
!!!!! TIME: 1/7/2016 8:19:00 PM DIR: Short SIZE: 1000 PRICE: 117.8 CURPRICE: 118.11 BASEPRICE: 117.8
1/7/2016 8:19:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/7/2016 8:19:00 PM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.90'0 SignalName='' FromEntrySignal=''
1/7/2016 8:19:00 PM Strategy 'ThirdTest/-1': Ignored SubmitOrderManaged() method at 1/7/2016 8:19:00 PM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.90'0 SignalName='Buy' FromEntrySignal='' Reason='Invalid order price, please see log tab'
!!!!! TIME: 1/7/2016 8:20:00 PM DIR: Short SIZE: 1000 PRICE: 117.8 CURPRICE: 118.11 BASEPRICE: 117.8
1/7/2016 8:20:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/7/2016 8:20:00 PM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.90'0 SignalName='' FromEntrySignal=''
1/7/2016 8:20:00 PM Strategy 'ThirdTest/-1': Ignored SubmitOrderManaged() method at 1/7/2016 8:20:00 PM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.90'0 SignalName='Buy' FromEntrySignal='' Reason='Invalid order price, please see log tab'
!!!!! TIME: 1/7/2016 8:21:00 PM DIR: Short SIZE: 1000 PRICE: 117.8 CURPRICE: 118.21 BASEPRICE: 117.8
1/7/2016 8:21:00 PM Strategy 'ThirdTest/-1': Entered internal SubmitOrderManaged() method at 1/7/2016 8:21:00 PM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.90'0 SignalName='' FromEntrySignal=''
1/7/2016 8:21:00 PM Strategy 'ThirdTest/-1': Ignored SubmitOrderManaged() method at 1/7/2016 8:21:00 PM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1,000 LimitPrice=0 StopPrice=117.90'0 SignalName='Buy' FromEntrySignal='' Reason='Invalid order price, please see log tab'
!!!!! TIME: 1/7/2016 8:22:00 PM DIR: Short SIZE: 1000 PRICE: 117.8 CURPRICE: 118.21 BASEPRICE: 117.8

Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #14 (permalink)
 
trendwaves's Avatar
 trendwaves 
Florida
Legendary Market Wizard
 
Experience: Advanced
Platform: NinjaTrader 8
Trading: ES, NQ, CL
Posts: 703 since Dec 2012
Thanks Given: 2,898
Thanks Received: 2,525

the strategy is generating invalid orders which are being rejected.

what is the intended purpose of this line of code ?

 
Code
EnterShortStopMarket(1000, Baseprice - Piplength);

Be Patient and Trade Smart
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #15 (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


trendwaves View Post
the strategy is generating invalid orders which are being rejected.

what is the intended purpose of this line of code ?

 
Code
EnterShortStopMarket(1000, Baseprice - Piplength);


I see that but I don't understand why the orders are invalid/being rejected. The goal of that line is to enter or modify the stop order to reverse my position, it is supposed to trail behind the position using the baseprice piplength calculation, instead of trailing with every pip/tenthpip I want it to make 10 pip jumps but only when the price gets 10 pips further away from the order, thanks

Visit my NexusFi Trade Journal Started this thread Reply With Quote




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