Announcement

Collapse
No announcement yet.

How to trade only during certain times with a trading strategy?

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

  • How to trade only during certain times with a trading strategy?

    Situation
    You only want to trade a strategy during certain time periods, for example a day session from 9:00 till 15:00 or an overnight session from 22:00 till 6:00.

    Programming code


    Programming code

    using System;
    using System.Drawing;
    using System.Linq;
    using PowerLanguage.Function;
    using ATCenterProxy.interop;

    namespace PowerLanguage.Strategy
    {
    public class Example_TradeDuringCertainTimes : SignalObject
    {
    private IOrderMarket buyOrder, sellOrder;
    private TimeSpan startTime, endTime;

    [Input]
    public string StartTime { get; set; }

    [Input]
    public string EndTime { get; set; }

    public Example_TradeDuringCertainTimes(object _ctx) : base(_ctx) { }

    protected override void Create()
    {
    // Assign a default value
    StartTime = "8:00";
    EndTime = "20:00";

    buyOrder = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default,
    EOrderAction.Buy));
    sellOrder = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default,
    EOrderAction.Sell));
    }

    protected override void StartCalc()
    {
    // Convert the input string times to TimeSpan format
    startTime = ConvertToTime(StartTime);
    endTime = ConvertToTime(EndTime);
    }

    protected override void CalcBar()
    {
    // If the current bar time falls outside the time range, return control
    if (!TimeToTrade())
    {
    // Close any position that is still open
    if (StrategyInfo.MarketPosition > 0)
    sellOrder.Send();

    return;
    }

    // Enter a long position
    if ((StrategyInfo.MarketPosition == 0) && (Bars.CurrentBar % 2 == 0))
    buyOrder.Send();

    // Exit long position
    if ((StrategyInfo.MarketPosition > 0) && (Bars.CurrentBar % 10 == 0))
    sellOrder.Send();
    }

    // The ConvertToTime() method converts a string to a TimeSpan structure.
    private TimeSpan ConvertToTime(string timeToConvert)
    {
    DateTime dt = DateTime.ParseExact(timeToConvert, "H:mm",
    System.Globalization.CultureInfo.InvariantCulture) ;

    return dt.TimeOfDay;
    }

    // The TimeToTrade() method returns 'true' if the current bar
    // time falls within the trading hours range, or else returns 'false'.
    private bool TimeToTrade()
    {
    TimeSpan barTime = Bars.TimeValue.TimeOfDay;

    // If the range is on the same day, both expressions must be true
    if (startTime < endTime)
    return (barTime >= startTime) && (barTime <= endTime);

    // Otherwise the range crosses midnight, and only one of
    // the expressions need to be true
    return (barTime > startTime) || (barTime < endTime);
    }
    }
    }

Disclaimer: There is a risk of loss in trading futures, forex and options. Futures, forex and options trading are not appropriate for all investors. Only risk capital should be used when trading futures. All information is for educational use only and is not investment advice. Past performance is not indicative of future results.

This website is hosted and operated by AMP Global Clearing, LLC ("AMP"), which provides brokerage services to traders of futures and foreign exchange products. This website is intended for customer support, educational and informational purposes only and should NOT be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by AMP and the information made available on this Web site is NOT an offer or solicitation of any kind. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of AMP.

All Users and Contributors along with their websites, products and services, are independent persons or companies that are in no manner affiliated with AMP. AMP is not responsible for, do not approve, recommend or endorse any User and/or Contributor Content referenced on this website and it’s your sole responsibility to evaluate All Content. Please be aware that any performance information provided by a user and/or contributor should be considered hypothetical.

HYPOTHETICAL PERFORMANCE RESULTS HAVE MANY INHERENT LIMITATIONS, SOME OF WHICH ARE DESCRIBED BELOW. NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT WILL OR IS LIKELY TO ACHIEVE PROFITS OR LOSSES SIMILAR TO THOSE SHOWN. IN FACT, THERE ARE FREQUENTLY SHARP DIFFERENCES BETWEEN HYPOTHETICAL PERFORMANCE RESULTS AND THE ACTUAL RESULTS SUBSEQUENTLY ACHIEVED BY ANY PARTICULAR TRADING PROGRAM.

ONE OF THE LIMITATIONS OF HYPOTHETICAL PERFORMANCE RESULTS IS THAT THEY ARE GENERALLY PREPARED WITH THE BENEFIT OF HINDSIGHT. IN ADDITION, HYPOTHETICAL TRADING DOES NOT INVOLVE FINANCIAL RISK, AND NO HYPOTHETICAL TRADING RECORD CAN COMPLETELY ACCOUNT FOR THE IMPACT OF FINANCIAL RISK IN ACTUAL TRADING. FOR EXAMPLE, THE ABILITY TO WITHSTAND LOSSES OR TO ADHERE TO A PARTICULAR TRADING PROGRAM IN SPITE OF TRADING LOSSES ARE MATERIAL POINTS WHICH CAN ALSO ADVERSELY AFFECT ACTUAL TRADING RESULTS. THERE ARE NUMEROUS OTHER FACTORS RELATED TO THE MARKETS IN GENERAL OR TO THE IMPLEMENTATION OF ANY SPECIFIC TRADING PROGRAM WHICH CANNOT BE FULLY ACCOUNTED FOR IN THE PREPARATION OF HYPOTHETICAL PERFORMANCE RESULTS AND ALL OF WHICH CAN ADVERSELY AFFECT ACTUAL TRADING RESULTS.

AMP has NOT endorsed or recommended the use any product, services offered by any 3rd party User or Contributor on this forum. No person employed by, or associated with, AMP is authorized to provide any information about any 3rd party User or Contributor content. All specific brokerage related questions should be directed towards AMP.

Copyright © 2017. All rights reserved.

Working...
X