NexusFi: Find Your Edge


Home Menu

 





Avoiding trading loop in script


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one mcteague with 2 posts (0 thanks)
    2. looks_two Quick Summary with 1 posts (0 thanks)
    3. looks_3 stepseazy with 1 posts (0 thanks)
    4. looks_4 kandlekid with 1 posts (0 thanks)
    1. trending_up 1,934 views
    2. thumb_up 0 thanks given
    3. group 4 followers
    1. forum 5 posts
    2. attach_file 0 attachments




 
Search this Thread

Avoiding trading loop in script

  #1 (permalink)
 mcteague 
New York NY USA
 
Experience: Intermediate
Platform: esignal, thinkorswim,
Trading: Stocks
Posts: 122 since Oct 2012
Thanks Given: 63
Thanks Received: 35

This might seem like a silly and basic question. But
When you write a script to take a position when certain conditions occur,
("if this and this and this then do that") how do you make sure the script does not keep taking new positions?

So for example: if I wrote a script to execute a buy of one ES contract when the macd crosses above the signal line, how would I limit the actual number of trades? That particular event could happen a lot.

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Exit Strategy
NinjaTrader
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
MC PL editor upgrade
MultiCharts
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
ZombieSqueeze
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
30 thanks
Just another trading journal: PA, Wyckoff & Trends
26 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
23 thanks
GFIs1 1 DAX trade per day journal
18 thanks
  #3 (permalink)
 mcteague 
New York NY USA
 
Experience: Intermediate
Platform: esignal, thinkorswim,
Trading: Stocks
Posts: 122 since Oct 2012
Thanks Given: 63
Thanks Received: 35


I can't believe that I have not done this in so long that forgot how to do a while loop.
Which, in the clarity of a nights sleep, I realize is the answer.

Started this thread Reply With Quote
  #4 (permalink)
 
MrYou's Avatar
 MrYou 
NC, USA
 
Experience: None
Platform: None Yet
Trading: Guitar
Posts: 403 since Jun 2011
Thanks Given: 618
Thanks Received: 196

If NinjaTrader is managing your trades then it will not take another trade beyond "EntriesPerDirection".

Reference this post for an example on how I use the switch and case statements for keeping track of position.

Reply With Quote
  #5 (permalink)
 stepseazy 
Ithaca, NY
 
Experience: Beginner
Platform: Ninjatrader
Trading: CL
Posts: 7 since Oct 2011
Thanks Given: 1
Thanks Received: 1

If you set EntriesPerDirection to 1, that should stop it.

I believe UniqueEntries does something similar, but I've never used it. I think it stops the entry with the same tag from triggering twice, but I'm not sure...

Reply With Quote
  #6 (permalink)
kandlekid
College Point, NY (Queens)
 
Posts: 63 since Nov 2009
Thanks Given: 5
Thanks Received: 20


mcteague View Post
This might seem like a silly and basic question. But
When you write a script to take a position when certain conditions occur,
("if this and this and this then do that") how do you make sure the script does not keep taking new positions?

So for example: if I wrote a script to execute a buy of one ES contract when the macd crosses above the signal line, how would I limit the actual number of trades? That particular event could happen a lot.

It's simple. I use a state machine. I have three states: IAmNoPosition, IAmLong, IAmShort. When I have no position, the state is IAmNoPosition. When I enter a long position, the state changes to IAmLong, likewise for short. I can only enter a position when the state is IAmNoPosition, and I can only exit a position from IAmLong or IAmShort. If I exit a position, the state changes to IAmNoPosition.

So in your case, the MACD crossing would change the state from IAmNoPosition to IAmLong (after entering the order, and possibly checking the ack).

Reply With Quote




Last Updated on December 28, 2012


© 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