Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Yo PaulH I need help again...
Collapse
X
-
Yo PaulH I need help again...
I was wondering how to reverse a position when a stop-loss is hit. (in ninjascript editor (code)). Say I have entered a long position and the stop loss is hit. I want to reverse to a short position. Could you please give me an outline or idea of how this could be written in the editor? ThanksTags: None
-
Hello wowza,
If your target is hit that would close the position so you wouldn't be able to reverse that specific position. You could at that point check if you were flat and then just enter into another position in the opposite direction. To "reverse" a position or to close the existing position and enter into the opposite you would just call the opposite entry without the stop being hit first. If you called EnterLong and were 1 Long, you could call EnterShort at that point and it would close the long and enter short.
Please let me know if I may be of further assistance.
JesseNinjaTrader Customer Service
-
Do one of you fine upstanding Ninja Reps know if there is already a sample somewhere of what I am trying to accomplish? What I am trying to accomplish is use only one entry condition for a strategy. The strategy would enter a long position the very first time my entry condition it met. I am not using stop losses or targets I only am using mechanical exits. Whether that matters of not I don't know. What I would like to do is when any trade results in a loss the next trade would still use the same entry condition but it would keep taking trades in the opposite direction. So for example say my first three trades all stay long entries as long as each trade resulted in a win. As soon as there is a loss we keep the same entry condition but start taking short trades consecutively till there is another loss and keep switching back and for from long entries to short entries only switching direction when there is a loss. If you don't have a sample strategy you know of, maybe you can point me in the direction of whether I this can be done with a boolean or do I need to use a case switch in my code. I appreciate any ideas.
Thank Quantismo
Comment
-
Hello quantismo,
I am not aware of a specific sample that does what you are asking but what you described is likely possible using code. It would involve using bools but also performance information as you want to know if the trade was a winning or losing trade.
You can search through the user uploaded strategies on the ecosystem: https://ninjatraderecosystem.com/
You can find a sample of getting the last trade here: https://ninjatrader.com/support/help...collection.htm
Please let me know if I can be of further assistance.
The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
JesseNinjaTrader Customer Service
Comment
-
Hi, thanks Jesse for answering me before, I put together this simple strategy with simple entry and exit logic that enters lots of trades to more easily visualize when I got it working. My c# skills are poor but usually I can figure out a workaround to most problems. This problem should be easy I would think but for some reason its kicking my @#$. All I am trying to accomplish is every time a last trade results in a loss (using mechanical exits only) I want to make the next trade only when its entry criteria is met to take the next trade in the opposite direction. For example, every time the last trade was long position and resulted in a loss the next trade when its entry criteria is met would be a short and basically keep reversing direction after one loss. I will paste up the code here of what I created so far that does not work yet. If anyone can tell me what I might be doing wrong to get it working I would appreciate it.
Thanks in advance
quantismo
HTML Code:[HTML]public class BOOLway1 : Strategy { private int myTradeCount; private bool LONGok; private bool SHORTok; private bool PNLgain; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Strategy here."; Name = "BOOLway1"; Calculate = Calculate.OnBarClose; EntriesPerDirection = 1; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = true; ExitOnSessionCloseSeconds = 30; IsFillLimitOnTouch = false; MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; OrderFillResolution = OrderFillResolution.Standard; Slippage = 0; StartBehavior = StartBehavior.WaitUntilFlat; TimeInForce = TimeInForce.Gtc; TraceOrders = false; RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; StopTargetHandling = StopTargetHandling.PerEntryExecution; BarsRequiredToTrade = 20; IsInstantiatedOnEachOptimizationIteration = true; myTradeCount = 0; LONGok = false; SHORTok = false; PNLgain = false; } } protected override void OnBarUpdate() { if (BarsInProgress != 0) return; if (CurrentBars[0] < 1) return; { ///////////////////////////////////////////////////////////// if (Position.MarketPosition == MarketPosition.Short) if (Close[0] < Open[0]) { ExitShort(); /// close short } //////////////////////////////////////////////////////////// if (SHORTok == true) if (Close[0] < Open[0]) if (Position.MarketPosition == MarketPosition.Flat) { /// enter short EnterShort(); } //////////////////////////////////////////////////////////// if (Position.MarketPosition == MarketPosition.Long) if (Close[0] < Open[0]) { ///// close long ExitLong(); } ///////////////////////////////////////////////////////////// if (LONGok == false) if (Close[0] < Open[0]) if (Position.MarketPosition == MarketPosition.Flat) { ///// enter long EnterLong(); } /////////////////////////calulating wins and losses below//////////////////////////////// if (SystemPerformance.AllTrades.Count > 1) { Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1]; double lastProfitCurrency = lastTrade.ProfitCurrency; /////////////////////////calulating wins and losses above//////////////////////////////// ////////////////////checking for last loss below//////////// if (LONGok == false && lastTrade.ProfitCurrency < 0) { LONGok = true; SHORTok = true; } else if (SHORTok == true && lastTrade.ProfitCurrency < 0) { LONGok = false; SHORTok = false; } ////////////////////checking for last loss above///////////// } } } } }
Comment
-
Hello quantismo,
From looking at the code I don't see what the problem may be. This would be a good situation to use Prints to see how the conditions are being evaluated. I would suggest try and add prints into your conditions or also just in OnBarUpdate to output the variable values:
Code:Print("LONGok " + LONGok );
Please let me know if I can be of further assistance.
JesseNinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Haiasi, Today, 06:53 PM
|
1 response
4 views
0 likes
|
Last Post Today, 07:05 PM | ||
Started by ScottWalsh, Today, 06:52 PM
|
1 response
11 views
0 likes
|
Last Post Today, 07:02 PM | ||
Started by ScottW, Today, 06:09 PM
|
1 response
5 views
0 likes
|
Last Post Today, 06:53 PM | ||
Started by ftsc2022, 10-25-2022, 12:03 PM
|
5 responses
256 views
0 likes
|
Last Post Today, 06:48 PM | ||
Started by Board game geek, 10-29-2023, 12:00 PM
|
14 responses
244 views
0 likes
|
Last Post
by DJ888
Today, 05:38 PM
|
Comment