/// <summary>
 /// Create a trading rule from AST to the model
 /// </summary>
 /// <param name="tradingStrategyAstNode"></param>
 /// <param name="tradingStrategyModel"></param>
 private void AddTradingRuleToModel(TradingStrategyAstNode tradingStrategyAstNode, TradingStrategy tradingStrategyModel)
 {
     foreach (var tradingRuleAst in tradingStrategyAstNode.TradingRules)
     {
         if (tradingRuleAst is ConditionalRuleAstNode)
         {
             var stopLossAstRule = tradingRuleAst as ConditionalRuleAstNode;
             tradingStrategyModel.TradingRules.Add(new StopLossRule(
                 stopLossAstRule.Name,
                 ConvertExecutionFrequency(stopLossAstRule.ExecuteFrequency),
                 new Variable("position"),
                 ConvertStatementAst(stopLossAstRule.Statements),
                 (BooleanExpression)ConvertExpressionAst(stopLossAstRule.Condition),
                 CreateVariable(stopLossAstRule.PositionSet.PositionSetName)));
         }
         else if (tradingRuleAst is GeneralRuleAstNode)
         {
             tradingStrategyModel.TradingRules.Add(new TradingRule(
                 tradingRuleAst.Name,
                 ConvertExecutionFrequency(tradingRuleAst.ExecuteFrequency),
                 ConvertStatementAst(tradingRuleAst.Statements))
                 );
         }
        
     }
 }
 /// <summary>
 /// Handles global identifier
 /// </summary>
 /// <param name="tradingStrategyAstNode"></param>
 /// <param name="tradingStrategyModel"></param>
 private void AddGlobalIdentifierToModel(TradingStrategyAstNode tradingStrategyAstNode, TradingStrategy tradingStrategyModel)
 {
     foreach (var globalVariable in tradingStrategyAstNode.GlobalIdentifierDef)
     {
         var exprAst = globalVariable.ExpressionAstNode;
         tradingStrategyModel.ConstantVariableDefinitions.Add(
             new GlobalIdentifier()
             {
                 Variable = CreateVariable(globalVariable.VariableName, exprAst.GetType()),
                 Constant = ConvertExpressionAst(exprAst)
             }
             );
     }
 }
        /// <summary>
        /// Start transform an abstract syntax tree into the model
        /// </summary>
        /// <param name="tradingStrategyAstNode">the node on abstract syntax represents trading strategy</param>
        /// <returns>model of a trading strategy</returns>
        public TradingStrategy Transform(TradingStrategyAstNode tradingStrategyAstNode)
        {
            if (tradingStrategyAstNode == null)
                throw new ArgumentException("tradingStrategyAstNode cannot be null.");

            TradingStrategy tradingStrategyModel = new TradingStrategy();

            AddGlobalIdentifierToModel(tradingStrategyAstNode, tradingStrategyModel);

            AddPositionSetDefToModel(tradingStrategyAstNode, tradingStrategyModel);

            AddPortfolioParamToModel(tradingStrategyAstNode, tradingStrategyModel);

            AddTradingRuleToModel(tradingStrategyAstNode, tradingStrategyModel);

            return tradingStrategyModel;
        }
        /// <summary>
        /// Handles parameters in the trading strategy AST node
        /// </summary>
        /// <param name="tradingStrategyAstNode"></param>
        /// <param name="tradingStrategyModel"></param>
        private void AddPortfolioParamToModel(TradingStrategyAstNode tradingStrategyAstNode, TradingStrategy tradingStrategyModel)
        {

            foreach (var parameter in tradingStrategyAstNode.StrategyParameters)
            {
                Type type = null;
                object value = null;
                string parameterName = null;

                if (parameter.Value is StringAstNode)
                {
                    var strAst = parameter.Value as StringAstNode;
                    type = typeof(string);
                    value = strAst.Value;
                    parameterName = parameter.Name;
                }

                if (parameterName != null)
                {
                    if (parameterName.ToLower() == "homecurrency")
                        tradingStrategyModel.Portfolio.HomeCurrency = (string)value;
                    else
                    {
                        throw new Exception("Unknown strategy parameter: " + parameterName);
                    }
                }
            }
        }
 /// <summary>
 /// Handles transformation of position set
 /// </summary>
 /// <param name="tradingStrategyAstNode"></param>
 /// <param name="tradingStrategyModel"></param>
 private void AddPositionSetDefToModel(TradingStrategyAstNode tradingStrategyAstNode, TradingStrategy tradingStrategyModel)
 {
     foreach (var positionSetDef in tradingStrategyAstNode.PositionSets)
     {
         CreateVariable(positionSetDef.Name, typeof(PositionSet));
         tradingStrategyModel.Portfolio.PositionSets.Add(new PositionSet()
         {
             Name = positionSetDef.Name,
          Number = ConvertExpressionAst(positionSetDef.Number),
             PositionType = ConvertPositionTypeAst(positionSetDef.PositionType)
         });
     }
 }