Time Trigger sample

The source for TimeTrigger

Time Trigger description

Starting programming topics by showing a simple time trigger strategy to illustrate how same trading system code can be used in backtesting and real broker trading.

In backtesting mode our system communicates with database and simulator. Database is giving us price information and simulator is executing our orders against price data in database.

In real trading mode our system communicates with broker API to get real time price data and send orders and receive order confirmations.

This communication is illustrated as message sequence chart in attached image.



Then I have attached two code samples. We are receiving ticks in NewTick function and when time condition (1800 GMT) is true we send order to open the position. Eventually our order is filled and in OrderFilled function we set our profit target for 6 pips and stop loss for 45 pips.

My code samples are standard C language and do not have any platform dependencies.


#region NewTick
public override void NewTick(BarData partialBar, TickData tick)
{
    IList pos = SymbolScript.SystemData.PositionManager.GetOpenPositions(Symbol);
    IList pend = SymbolScript.SystemData.PositionManager.GetPendingPositions(Symbol);
    if ( pos.Count == 0 && pend.Count == 0 )
    {
        if ( tick.time.Hour == 18 && tick.time.Minute == 0 ) //GMT time
            OpenPosition(tick);
    }
}
#endregion

#region OrderFilled
public override void OrderFilled(Position position, Trade trade)
{		
    if ( trade.TradeType == TradeType.OpenPosition )
    {
        int _rounding = Symbol.SymbolInformation.DecimalPlaces;
        if ( _rounding == 5 )
            _rounding = 4;
        if ( position.Type == PositionType.Long )
        {
            position.SetProfitTarget(Math.Round((position.EntryPrice.SymbolPrice + 
                6 * _tickSize),_rounding),TargetPriceType.AbsolutePrice);
            position.SetStopLoss(Math.Round((position.EntryPrice.SymbolPrice - 
                45 * _tickSize),_rounding),TargetPriceType.AbsolutePrice);
        }
        else
        {
            position.SetProfitTarget(Math.Round((position.EntryPrice.SymbolPrice - 
                6 * _tickSize),_rounding),TargetPriceType.AbsolutePrice);
            position.SetStopLoss(Math.Round((position.EntryPrice.SymbolPrice + 
                45 * _tickSize),_rounding),TargetPriceType.AbsolutePrice);
        }
    }
}
#endregion