Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Risk Aversion Switch

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

    #16
    Hello GTBrooks,

    I am stating that if you want to fetch Instrument Initial Margin in your script that is consistent with your broker's configs, you could take the same approach you are doing, but reference a Risk template that you create instead referencing the pre-made Risk template.

    I have submitted a feature request on your behalf to be able to get instrument margin and other risk settings straight from your broker. The ticket ID is SFT-3796.

    We collect interest in feature requests before determining if the feature should be implemented. For that reason we cannot offer an ETA or promise of fulfillment. Upon implementation, the number for the ticket ID can be publicly found in the Release Notes page of the help guide. I will provide a link below.

    Release Notes - https://ninjatrader.com/support/help...ease_notes.htm

    Please let us know if you have any questions.

    Comment


      #17
      Thank you for all your Help. I finished polishing the code. Do not use this code. It is NOT approved for use in NinjaTrader 8. Thank you again for all your help. I will follow the Ticket to see if Forex.com ever automate their Margin Requirements.

      Code:
      //This namespace holds Strategies in this folder and is required. Do not change it. 
      namespace NinjaTrader.NinjaScript.Strategies.AlanStrategies
      {
          public class StrategyNameHere : Strategy
          {
      
              private Account account;
      
              protected override void OnStateChange()
              {
      
                  if (State == State.SetDefaults)
                  {
                      ProfitTargetPips                = 1;
                      TrailingStopLossPips                = 1;
                      StopLossPips                    = 1;
                  }
                  else if (State == State.Configure)
                  {
      
                  }
                  else if (State == State.DataLoaded)
                  {
      
                      SetProfitTarget("", CalculationMode.Pips, (ProfitTargetPips + TrailingStopLossPips + StopLossPips));
                      SetTrailStop("", CalculationMode.Pips, (TrailingStopLossPips + StopLossPips), false);
                      SetStopLoss("", CalculationMode.Pips, StopLossPips, false);
      
              }
      
              protected override void OnBarUpdate()
              {
      
                  if(CurrentBar < 1) return;    
      
                  if (Close[0] > Close[1])
                  && ((Account.Get(AccountItem.CashValue, Currency.UsDollar) * 0.85) - (Account.Get(AccountItem.InitialMargin, Currency.UsDollar))) >= (Math.Max( (int) Math.Floor(( (Account.Get(AccountItem.CashValue, Currency.UsDollar) / 18) / (Risk.Get("GTB ForexDotCom").ByMasterInstrument[Instrument.MasterInstrument].InitialMargin) )), 0 ) * (Risk.Get("GTB ForexDotCom").ByMasterInstrument[Instrument.MasterInstrument].InitialMargin))
                  && (SystemPerformance.LongTrades.TradesPerformance.ProfitFactor >= 1)
                  && (PositionAccount.Quantity = Position.Quantity))    
                  {
                      EnterLong((Math.Max( (int) Math.Floor( (Account.Get(AccountItem.CashValue, Currency.UsDollar) / 18) / (Risk.Get("GTB ForexDotCom").ByMasterInstrument[Instrument.MasterInstrument].InitialMargin) ), 0 ) * 1000), "");
                  }
      
                  if (Close[0] <= Close[1]))    
                  {
                      ExitLong(Position.Quantity);
                  }
      
                  if (Close[0] < Close[1])
                  && ((Account.Get(AccountItem.CashValue, Currency.UsDollar) * 0.85) - (Account.Get(AccountItem.InitialMargin, Currency.UsDollar))) >= Math.Max( (int) Math.Floor(( (Account.Get(AccountItem.CashValue, Currency.UsDollar) / 18) / (Risk.Get("GTB ForexDotCom").ByMasterInstrument[Instrument.MasterInstrument].InitialMargin) )), 0 ) * (Risk.Get("GTB ForexDotCom").ByMasterInstrument[Instrument.MasterInstrument].InitialMargin)
                  && (SystemPerformance.ShortTrades.TradesPerformance.ProfitFactor >= 1)
                  && (PositionAccount.Quantity = Position.Quantity))    
                  {            
                      EnterShort((Math.Max( (int) Math.Floor( (Account.Get(AccountItem.CashValue, Currency.UsDollar) / 18) / (Risk.Get("GTB ForexDotCom").ByMasterInstrument[Instrument.MasterInstrument].InitialMargin) ), 0 ) * 1000), "");
                  }
      
                  if (Close[0] >= Close[1]))    
                  {
                      ExitShort(Position.Quantity);
                  }
      
              }
      
          }
      
              #region Properties
      
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="ProfitTargetPips", Description="ProfitTarget set in Pips", Order=0, GroupName="Parameters")]
              public int ProfitTargetPips
              { get; set; }
      
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="TrailingStopLossPips", Description="TrailingStopLoss set in Pips", Order=0, GroupName="Parameters")]
              public int TrailingStopLossPips
              { get; set; }
      
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="StopLossPips", Description="StopLoss set in Pips", Order=0, GroupName="Parameters")]
              public int StopLossPips
              { get; set; }
      
              #endregion
      
          }
      }
      Last edited by GTBrooks; 02-28-2019, 07:01 PM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      89 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      135 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      68 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      119 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      69 views
      0 likes
      Last Post PaulMohn  
      Working...
      X