NexusFi: Find Your Edge


Home Menu

 





Strategy: two fix different stop times a day


Discussion in NinjaTrader

Updated
    1. trending_up 897 views
    2. thumb_up 8 thanks given
    3. group 3 followers
    1. forum 16 posts
    2. attach_file 9 attachments




 
Search this Thread

Strategy: two fix different stop times a day

  #1 (permalink)
 
Renkotrader's Avatar
 Renkotrader 
Frankfurt, Hessen, Germany
 
Experience: Advanced
Platform: NinjaTrader 8
Broker: APEX & NinjaTrader Brokerage
Trading: 6E, 6J, CL, ES, FDAX, FGBL, GC, HG, NQ, RTY, SI, YM
Posts: 547 since May 2012
Thanks Given: 1,419
Thanks Received: 227

Hello strategy programmers,

what I am looking for, is a simple thing:

I want to implement in my strategy 2 different stop times a day. Lets say for example 11:30 and 17:45 o'clock.

This means: each running trade, no matter of its open time, will quit exactly to the next of the both fixed exit times.

a) Please: I am not searching a starting period like this example from the NT-forum:
if (ToTime(Time[0]) >= 74500 && ToTime(Time[0]) <= 134500)

b) And: this both fix-time-trade-end-conditions must works in timeless charts like Renko, too.

Two fix-single-independent-of-each-other-trade-ending-times. I cant believe, that this is so difficult or complex to realize - but I am failing in its.

What I need is an working example or an working example code.

Thank you for your help!

Best regards,
Renkotrader

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Futures True Range Report
The Elite Circle
Better Renko Gaps
The Elite Circle
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
Exit Strategy
NinjaTrader
Build trailing stop for micro index(s)
Psychology and Money Management
 
  #2 (permalink)
 
Renkotrader's Avatar
 Renkotrader 
Frankfurt, Hessen, Germany
 
Experience: Advanced
Platform: NinjaTrader 8
Broker: APEX & NinjaTrader Brokerage
Trading: 6E, 6J, CL, ES, FDAX, FGBL, GC, HG, NQ, RTY, SI, YM
Posts: 547 since May 2012
Thanks Given: 1,419
Thanks Received: 227

PS: here is a example strategy script for using. This makes things much easier

Attached Files
Elite Membership required to download: Example2FixTimeStops.cs
Started this thread Reply With Quote
  #3 (permalink)
 
trendisyourfriend's Avatar
 trendisyourfriend 
Quebec Canada
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: AMP/CQG
Trading: ES, NQ, YM
Frequency: Daily
Duration: Minutes
Posts: 4,527 since Oct 2009
Thanks Given: 4,171
Thanks Received: 6,018


 
Code
private DateTime lastEntryTime = DateTime.MinValue;

protected override void OnBarUpdate()
{
    if (CurrentBar < BarsRequiredToTrade)
        return;

    // Define your time variables for the closing periods
    TimeSpan closingTime1 = new TimeSpan(11, 30, 0);   // 11:30 AM
    TimeSpan closingTime2 = new TimeSpan(17, 45, 0);   // 5:45 PM

    // Check if a trade is open
    if (Position.MarketPosition == MarketPosition.Long || Position.MarketPosition == MarketPosition.Short)
    {
        // Check if the current time is after the first closing time
        if (ToTime(Time[0]) >= closingTime1)
        {
            // Check if the trade was opened before the first closing time
            if (lastEntryTime < closingTime1)
            {
                // Close the trade
                ExitLong();  // Close a long position, NT Help > this method is ignored if a long position does not exist
                ExitShort(); // Close a short position, NT Help > this method is ignored if a short position does not exist
            }
        }
        // Check if the current time is after the second closing time
        else if (ToTime(Time[0]) >= closingTime2)
        {
            // Check if the trade was opened before the second closing time
            if (lastEntryTime < closingTime2)
            {
                // Close the trade
                ExitLong();  // Close a long position
                ExitShort(); // Close a short position
            }
        }
    }

    // crossover strategy logic to open a trade
    if (CrossAbove(smaFast, smaSlow, 1))
    {
        EnterLong();
        lastEntryTime = Time[0]; // record entry time
    }
    else if (CrossBelow(smaFast, smaSlow, 1))
    {
        EnterShort();
        lastEntryTime = Time[0]; // record entry time
    }
}

Reply With Quote
Thanked by:
  #4 (permalink)
 
Renkotrader's Avatar
 Renkotrader 
Frankfurt, Hessen, Germany
 
Experience: Advanced
Platform: NinjaTrader 8
Broker: APEX & NinjaTrader Brokerage
Trading: 6E, 6J, CL, ES, FDAX, FGBL, GC, HG, NQ, RTY, SI, YM
Posts: 547 since May 2012
Thanks Given: 1,419
Thanks Received: 227

Hello trendisyourfriend,

its a great job - thank you very much, but on my computer it does not works.

a) The '>=' operator cannot be applied to operands of type 'int' and 'System.TimeSpan'. CS0019
b) The '<' operator cannot be applied to operands of type 'System.DateTime' and 'System.TimeSpan'. CS0019
c) The '>=' operator cannot be applied to operands of type 'int' and 'System.TimeSpan'. CS0019
d) The '<' operator cannot be applied to operands of type 'System.DateTime' and 'System.TimeSpan'. CS0019

So I had the idea, to put "closingTime1" (for example) in this convert-construct:
(Convert.ToInt32(closingTime1))

The result is: no compiling-errors, but no trades, too.
So my idea was not the best...

Best regards,
Renkotrader

Attached Files
Elite Membership required to download: Example2FixTimeStops.cs
Started this thread Reply With Quote
  #5 (permalink)
 
trendisyourfriend's Avatar
 trendisyourfriend 
Quebec Canada
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: AMP/CQG
Trading: ES, NQ, YM
Frequency: Daily
Duration: Minutes
Posts: 4,527 since Oct 2009
Thanks Given: 4,171
Thanks Received: 6,018

It seems that NinjaTrader handles time comparisons differently from standard C# DateTime comparisons. To address this, i have modified the code to compare DateTime TimeSpan object with Tanother TimeSpan type object. This code should work.

Attached Files
Elite Membership required to download: crossOverWithExitOnTime.cs
Reply With Quote
Thanked by:
  #6 (permalink)
 
Renkotrader's Avatar
 Renkotrader 
Frankfurt, Hessen, Germany
 
Experience: Advanced
Platform: NinjaTrader 8
Broker: APEX & NinjaTrader Brokerage
Trading: 6E, 6J, CL, ES, FDAX, FGBL, GC, HG, NQ, RTY, SI, YM
Posts: 547 since May 2012
Thanks Given: 1,419
Thanks Received: 227

Hi trendisyourfriend,

in my tests I can see, that its works for the forst exit time (11:30) - but not for the second one (17:45).
Something does not work or calculate correct, or?

If I take a look at cour code, so the logical structure looks like correct for me...

Sorry for the "bad" news

Best regards,
Renkotrader

Started this thread Reply With Quote
  #7 (permalink)
 
trendisyourfriend's Avatar
 trendisyourfriend 
Quebec Canada
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: AMP/CQG
Trading: ES, NQ, YM
Frequency: Daily
Duration: Minutes
Posts: 4,527 since Oct 2009
Thanks Given: 4,171
Thanks Received: 6,018


Renkotrader View Post
Hi trendisyourfriend,

in my tests I can see, that its works for the forst exit time (11:30) - but not for the second one (17:45).
Something does not work or calculate correct, or?

If I take a look at cour code, so the logical structure looks like correct for me...

Sorry for the "bad" news

Best regards,
Renkotrader

I will leave it up to you to find the solution. Just use print statements to find what does not work as expected and you should be able to solve this problem. That's what coding is all about after all.

Reply With Quote
Thanked by:
  #8 (permalink)
 
Renkotrader's Avatar
 Renkotrader 
Frankfurt, Hessen, Germany
 
Experience: Advanced
Platform: NinjaTrader 8
Broker: APEX & NinjaTrader Brokerage
Trading: 6E, 6J, CL, ES, FDAX, FGBL, GC, HG, NQ, RTY, SI, YM
Posts: 547 since May 2012
Thanks Given: 1,419
Thanks Received: 227

Hi trendisyourfriend,

I will do its - and then I will giving you feedback.

Thanks again!

Best regards,
Renkotrader

Started this thread Reply With Quote
  #9 (permalink)
 
trendisyourfriend's Avatar
 trendisyourfriend 
Quebec Canada
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: AMP/CQG
Trading: ES, NQ, YM
Frequency: Daily
Duration: Minutes
Posts: 4,527 since Oct 2009
Thanks Given: 4,171
Thanks Received: 6,018


Renkotrader View Post
Hi trendisyourfriend,

I will do its - and then I will giving you feedback.

Thanks again!

Best regards,
Renkotrader

I could not resist and reworked my example to use the ToTime function which is easier to understand and use.

In this scenario when we open a trade that is where we set the closing time:


Attached Files
Elite Membership required to download: crossOverWithExitOnTime.cs
Reply With Quote
Thanked by:
  #10 (permalink)
 
Renkotrader's Avatar
 Renkotrader 
Frankfurt, Hessen, Germany
 
Experience: Advanced
Platform: NinjaTrader 8
Broker: APEX & NinjaTrader Brokerage
Trading: 6E, 6J, CL, ES, FDAX, FGBL, GC, HG, NQ, RTY, SI, YM
Posts: 547 since May 2012
Thanks Given: 1,419
Thanks Received: 227


Good morning trendisyourfriend,

thank you, I compared both versions and the are very different.

Your new one works and that is fine

Two things I have to ask to this solution:

1) It is possible to change the hard coded times to variables, so that I can use its in the parameter list? I tried its the morning in different ways, but no one has worked.

2) I did, what you said about debugging for learning. To work with the Print() is a real good thing. I defined a Print-Line, but there the next problem was finding me. I would love to get the execution close time from the order - not only the start-time. For me it is important in comparing the times from a time chart with a renko chart. In the helping system I dont find another method then "Time[0]". So I thought, I can use its after the line "ExitLong();", but this is not working, too. Is there a special trick for getting the close time?

Best regards,
Renkotrader

Attached Thumbnails
Click image for larger version

Name:	script output.PNG
Views:	23
Size:	36.0 KB
ID:	334644  
Attached Files
Elite Membership required to download: crossOverWithExitOnTime.cs
Started this thread Reply With Quote




Last Updated on September 9, 2023


© 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