NexusFi: Find Your Edge


Home Menu

 





Code for Entering Orders with Profit Target and Stop Loss orders


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one shodson with 2 posts (2 thanks)
    2. looks_two aguison with 2 posts (0 thanks)
    3. looks_3 Quick Summary with 1 posts (0 thanks)
    4. looks_4 algodt with 1 posts (0 thanks)
    1. trending_up 10,066 views
    2. thumb_up 2 thanks given
    3. group 3 followers
    1. forum 4 posts
    2. attach_file 0 attachments




 
Search this Thread

Code for Entering Orders with Profit Target and Stop Loss orders

  #1 (permalink)
 aguison 
Long Beach CA
 
Posts: 3 since Jun 2013

Hello

I first did some diligence to see if there were any threads in the forum that could answer my question. Did not find any but I'll apologize in advance just in case I missed them.

I'm coding a strategy that places orders when a signal is triggered (not important how) and encountering an issue when I place my buy or sell limit orders using the EnterLongLimit() or EnterShortLimit() functions of Ninja, I saw that the software does not place the accompanying profit target or stop loss orders for my order that was just entered.

According to the Help guide, using the Managed Orders approach the above should occur automatically, however it is not.

The code for placing my orders is as follows:

EnterLongLimit(0, false, iOrderQty, dLimitPrice, GenerateSignalName("Buy Limit"));

EnterShortLimit(0, false, iOrderQty, dLimitPrice, GenerateSignalName("Sell Limit"));

The code for the profit target and stop loss is as follows:

SetProfitTarget("Profit Target", CalculationMode.Ticks, dProfitTargetTicks);

SetStopLoss("Stop Loss", CalculationMode.Ticks, dStopLossTicks, true);

I tried including the profit target and stop loss code in the Initialize section of the strategy (as per the manual) and when that didn't work, I moved them to the OnBarUpdate() section and that didn't work either.

Checked the Log tab in the Control Center and there are no entries. My entry orders are firing but they are not getting close out. The only thing that is closing them out is the system's Exit on Close automatic routines.

Hoping that someone can show how to resolve this issue or better yet send a code sample that works.

Thanks in advance,

Al G

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
ZombieSqueeze
Platforms and Indicators
NexusFi Journal Challenge - April 2024
Feedback and Announcements
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
Request for MACD with option to use different MAs for fa …
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Retail Trading As An Industry
67 thanks
Battlestations: Show us your trading desks!
48 thanks
NexusFi site changelog and issues/problem reporting
47 thanks
GFIs1 1 DAX trade per day journal
32 thanks
What percentage per day is possible? [Poll]
31 thanks

  #3 (permalink)
 
shodson's Avatar
 shodson 
OC, California, USA
Quantoholic
 
Experience: Advanced
Platform: IB/TWS, NinjaTrader, ToS
Broker: IB, ToS, Kinetick
Trading: stocks, options, futures, VIX
Posts: 1,976 since Jun 2009
Thanks Given: 533
Thanks Received: 3,709


Check your signal names for your entries and you stop/profit target. If they don't match then it won't work. Assign a variable to your signal name, then use the same variable in your entry orders and your stop loss and profit targets as well.

I also like to have my signal names be unique so they don't collide with potentially other orders, so I append CurrentBar. This is important if you are making multiple entries but you don't need it if you're only in one entry at a time.

 
Code
string signal;

if (buySignal)
{
   signal = GenerateSignalName("Buy Limit") + CurrentBar;
   EnterLongLimit(0, false, iOrderQty, dLimitPrice, signal);
}
else if (shortSignal)
{
   signal = GenerateSignalName("Sell Limit") + CurrentBar;
   EnterShortLimit(0, false, iOrderQty, dLimitPrice, signal);
}

SetProfitTarget(signal, CalculationMode.Ticks, dProfitTargetTicks);
SetStopLoss(signal, CalculationMode.Ticks, dStopLossTicks, true);

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
The following user says Thank You to shodson for this post:
  #4 (permalink)
 aguison 
Long Beach CA
 
Posts: 3 since Jun 2013

Hi Shodson

Good suggestion on using the CurrentBar as the signal name! I tried it and it works!

However in my code I am making calls to the SetProfitTarget and SetStopLoss methods RIGHT BEFORE placing my entry order because I want to make sure the CurrentBar value is updated.

In other words if I move the statements into the Initialize() section, it doesn't appear that it will work since Initialize is only invoked once. Is this correct?

What would you suggest is the proper code implementation for this?

Many thanks,

Al

Reply With Quote
  #5 (permalink)
 
shodson's Avatar
 shodson 
OC, California, USA
Quantoholic
 
Experience: Advanced
Platform: IB/TWS, NinjaTrader, ToS
Broker: IB, ToS, Kinetick
Trading: stocks, options, futures, VIX
Posts: 1,976 since Jun 2009
Thanks Given: 533
Thanks Received: 3,709


aguison View Post
Hi Shodson

Good suggestion on using the CurrentBar as the signal name! I tried it and it works!

However in my code I am making calls to the SetProfitTarget and SetStopLoss methods RIGHT BEFORE placing my entry order because I want to make sure the CurrentBar value is updated.

In other words if I move the statements into the Initialize() section, it doesn't appear that it will work since Initialize is only invoked once. Is this correct?

What would you suggest is the proper code implementation for this?

Many thanks,

Al

Then use the method signature of SetProfitTarget() and SetStopLoss() that doesn't take a signal name and it will be applied to all entries.

 
Code
SetProfitTarget(CalculationMode.Ticks, dProfitTargetTicks);
SetStopLoss(CalculationMode.Ticks, dStopLossTicks, true);

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
The following user says Thank You to shodson for this post:





Last Updated on June 24, 2013


© 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