crossover events

Created at 19 Mar 2014, 23:35
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
10

1000015

Joined 06.09.2013

crossover events
19 Mar 2014, 23:35


Hello,

I submitted a question a few days ago and I was impressed with how quick a response I received.  I am not a programmer so this is likely easy for everyone but I need the help.  I am looking to very simply perform a market buy or sell when a fast weighted moving average crosses over a slower weighted moving average.  I looked through sample cbots but didn't find anything that really fit this very simple trading technique.

Any help is appreciated.  Below is theoretically what I am trying to do but can't figure out the syntax

if ( fastWMA >  slow WMA)

{

 Buy;

}

if ( fastWMA < slowWMA) { Sell; }

 

Thank-you.


@1000015
Replies

Old Account
21 Mar 2014, 20:23

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter("Fast Periods", DefaultValue = 10)]
        public int F_Periods { get; set; }

        [Parameter("Slow Periods", DefaultValue = 20)]
        public int S_Periods { get; set; }

        [Parameter("Volume", DefaultValue = 1000)]
        public int Volume { get; set; }

        public WeightedMovingAverage F_WMA;
        public WeightedMovingAverage S_WMA;
        public bool TradeS;
        public bool TradeB;

        protected override void OnStart()
        {
            F_WMA = Indicators.WeightedMovingAverage(MarketSeries.Close, F_Periods);
            S_WMA = Indicators.WeightedMovingAverage(MarketSeries.Close, S_Periods);
        }

        protected override void OnTick()
        {
            bool isBuyPositionOpen = Positions.Count != 0 && LastResult.Position.TradeType == TradeType.Buy;
            bool isSellPositionOpen = Positions.Count != 0 && LastResult.Position.TradeType == TradeType.Sell;

            if (S_WMA.Result.LastValue > F_WMA.Result.LastValue && !isSellPositionOpen && TradeS)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "", 20, 20);
                TradeS = false;
            }

            if (S_WMA.Result.LastValue < F_WMA.Result.LastValue && !isBuyPositionOpen && TradeB)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "", 20, 20);
                TradeB = false;
            }

        }


        protected override void OnBar()
        {
            TradeS = true;
            TradeB = true;
        }
    }
}

 


@Old Account