Profile Picture

HELP: Simple 200 Day SMA Long/Short Crossover System

Posted By rvs3 2 Years Ago
Message
Posted Wednesday September 23 2015
Hi all,

Just started using rightedge a few days ago, and my first goal is to develop a simple long/short 200 day sma crossover system:
- Close shorts and go long when price closes above 200 day SMA
- Close Longs and go Short when price closes below 200 day SMA

I am new to programming, and am having trouble incorporating the code that will close long/short positions once they have been opened.

Below is the system source code, I appreciate someone pointing me in the right direction and/or pointing out any errors or simple improvements that might be made.


-----------------------------------------------------------------------------------------------------------------------
#region Using statements
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using RightEdge.Common;
using RightEdge.Common.ChartObjects;
using RightEdge.Indicators;
#endregion

#region System class
public class MySystem : MySystemBase
{
public override void Startup()
{
// Perform initialization or set system wide options here

}
}
#endregion

public class MySymbolScript : MySymbolScriptBase
{

SMA SMA200Day;

public override void Startup()
{
// Perform initialization here.
SystemParameters param = SystemData.SystemParameters;

SMA200Day = new SMA((int)param["SMA Period"],Close);

}

public override void NewBar()
{
// Put your trading code here
if(!SystemData.InLeadBars)
{
IList<Position> pos = SystemData.PositionManager.GetOpenPositions(Symbol);
if(pos.Count==0)//No position is open in current symbol
{
if (Close.Current > SMA200Day.Current)// Close is higher than Current 200DaySMA Price
{
TradingSystem.OpenPosition(Symbol,PositionType.Long,OrderType.MarketOnOpen);//Open Long Position
}
if (Close.Current < SMA200Day.Current)//Close is Lower than Current 200DaySMA Price
{
TradingSystem.OpenPosition(Symbol,PositionType.Short,OrderType.MarketOnOpen);//Open Short Position
}
}

}
else //position already open
{
if (Close.Current < SMA200Day.Current)
{
foreach(Position p in pos)
{
p.CloseaAtMarket();
}
}
}

//How to close short positions without interfering with open longs?
}

public override void OrderFilled(Position position, Trade trade)
{
// This method is called when an order is filled

}

public override void OrderCancelled(Position position, Order order, string information)
{
// This method is called when an order is cancelled or rejected
   if (!order.CancelPending)
   {
    OutputWarning("Unexpected order cancel: " + order.ToString() + " " + information);
   }
}
}

---------------------------------------------------------------------------

I appreciate any input someone might have on how to finish up this program. Thank you

Edited: Wednesday September 23 2015 by rvs3
Posted Wednesday September 23 2015
Attached is a modified version of your code which should do what your are trying to do.

You may have been having trouble because the "position already open" else statement was attached to the wrong if statement (the one checking if the system was in lead bars, not the one checking if a position was open).

Thanks,
Daniel

Attachments
CrossoverSample.cs (227 views, 2.00 KB)
Posted Thursday September 24 2015
Great, thanks very much Daniel, that is exactly what I need.




Similar Topics


Reading This Topic


2005-2017 © RightEdge Systems