Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

liquidate all positions before the riskiest events - dates.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    liquidate all positions before the riskiest events - dates.





    regards to everyone.



    this is something i have been thinking about for a long time but would be impossible for me to code on my own. there are a number of economic and political events which have dates that are known well in advance and have very significant effects on the prices of financial assets. elections, referenda, releases of economic data and other events have very significant influence on the behavior of market manipulators both before and after they occur.



    i'm interested in adding code to my pure trend following strategies so that all positions would be liquidated before the close on every date in a list. and there are very different kinds of events (fomc meetings - minutes, elections, eia - api petroleum reports, unemployment reports, et cetera) that i would like to evaluate on their own or aggregated, so instead of hard coding this information into my strategies i'm interested in having code that would allow vectors (or lists, or arrays) of dates with very different numbers of components (from none to several tens of dates).



    this below is the list of federal open market committee meetings for the last two years. ¿how could i add code to my strategies so that in every one of those dates all positions would be liquidated starting at 15:30?




    November 05, 2020
    September 16, 2020
    August 27, 2020
    July 29, 2020
    June 10, 2020
    April 29, 2020
    March 31, 2020
    March 23, 2020
    March 19, 2020
    March 15, 2020
    March 3, 2020
    October 30, 2019
    September 18, 2019
    July 31, 2019
    December 19, 2018


    i have this very simple, alternating strategy to work on:

    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 tealternatingcurrentbar : Strategy
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"te alternating current bar.";
    Name = "tealternatingcurrentbar";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.UniqueEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 6;
    StartBehavior = StartBehavior.ImmediatelySubmitSynchronizeAccount;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelCloseIgnoreRejects ;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 1;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    Posi = 1;
    }
    else if (State == State.Configure)
    {
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 1)
    return;
    
    
    // Set 1
    if (CurrentBar % 2 == 0)
    {
    EnterShort(Convert.ToInt32(Posi), "");
    }
    
    // Set 2
    if (CurrentBar % 2 == 1)
    {
    EnterLong(Convert.ToInt32(Posi), "");
    }
    
    }
    
    #region Properties
    
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="Posi", Description="posi.", Order=1, GroupName="Parameters")]
    public int Posi
    { get; set; }
    
    #endregion
    }
    }

    i think that first it would be helpful to create a no trade condition with a structure like the following:

    if date = one of the elements in the vector and time >= 15:30 then do not trade = true.

    the idea for this fragment would be that no more positions would be opened after some cutoff time i could determine.


    and to liquidate all positions what i have in mind would be similar:

    if date = one of the elements in the vector and time >= 15:30 and market position = short then exitshort.

    this and an analog command for long positions would help to make sure that the strategy would not hold any positions over the dates i determined.



    i could use help with everything, from the format that the dates must have, to how to input a vector (or vectors) into a strategy (¿could i just list them all separated with commas?), to the logic so that every date would be processed and evaluated individually, and so forth.



    very well, hopefully this could also be an interesting coding challenge to the most proficient members in these fora. thanks in advance to everyone, regards.
    Last edited by rtwave; 12-14-2020, 09:46 AM.

    #2
    Hello rtwave,

    Thanks for your post.

    I would suggest creating a CSV type file with the list of dates and use a (C#) StreamReader to compare the current bar date to the date(s) in the list. The CSV file can be created and maintained with a simple windows application like NotePad. Using such a file you could also expand (eventually) beyond just the dates and could for example include a label of the event and perhaps a "significant factor" value.

    Here are links to an example of using a streamreader and working with dates.





    In terms of a trigger to read the list or check the date, you could check the DateTime of the current bar and compare it to the DateTime of the previous bar and if the day is different (which would occur at Midnight) then check the list.

    if (Time[0].Date != Time[1].Date)
    {
    Print ("new date: "+Time[0].Date);
    // check the list of dates
    }


    Paul H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by alifarahani, Today, 09:40 AM
    6 responses
    34 views
    0 likes
    Last Post alifarahani  
    Started by Waxavi, Today, 02:10 AM
    1 response
    17 views
    0 likes
    Last Post NinjaTrader_LuisH  
    Started by Kaledus, Today, 01:29 PM
    5 responses
    14 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by Waxavi, Today, 02:00 AM
    1 response
    12 views
    0 likes
    Last Post NinjaTrader_LuisH  
    Started by gentlebenthebear, Today, 01:30 AM
    3 responses
    17 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Working...
    X