using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
namespace WealthLab.Strategies
{
public class MyStrategy : WealthScript
{
//StrategyParameter Stop;
StrategyParameter Step;
StrategyParameter PeakRange;
StrategyParameter DeltaStop;
StrategyParameter Mode;
public MyStrategy()
{
//Stop = CreateParameter("Stop", 3000, 1000, 5000, 200);
Step = CreateParameter("Step", 3200, 100, 3500, 100);
DeltaStop = CreateParameter("DeltaStop", 0, 100, 3500, 100);
PeakRange = CreateParameter("PeakRange", 2700, 900, 3000, 100);
Mode = CreateParameter("Mode", 0, -1, 1, 1);
}
protected override void Execute()
{
//double stoploss = Stop.Value;
double stoploss = Step.Value + DeltaStop.Value;
double StopPrice = 0;
double TakePrice = 0;
double price,LowestLow,HighestHigh;
double delta;
DataSeries Peaks = Peak.Series( High, PeakRange.ValueInt, PeakTroughMode.Value);
DataSeries Troughs = Trough.Series( Low, PeakRange.ValueInt, PeakTroughMode.Value);
//DataSeries HighestSeries = Highest.Series(High,Period.ValueInt);
//DataSeries LowestSeries = Lowest.Series(Low,Period.ValueInt);
double LastPeakSignalPrice = 0;
double WorkingPeak = 0;
double WorkingHigh = 0;
double PeakSignalPrice = 0;
bool SetShortOrderMode = false;
double LastTroughSignalPrice = 0;
double WorkingTrough = 0;
double WorkingLow = 0;
double TroughSignalPrice = 0;
bool SetBuyOrderMode = false;
double trailingStop;
DataSeries PeakSignalPriceSeries = new DataSeries(Bars,"PeakSignalPriceSeries");
DataSeries TroughSignalPriceSeries = new DataSeries(Bars,"PeakSignalPriceSeries");
for(int bar = 20; bar < Bars.Count; bar++)
{
PeakSignalPriceSeries[bar] = Close[bar];
TroughSignalPriceSeries[bar] = Close[bar];
foreach( Position p in Positions )
{
if ( p.Active )
{
if (p.PositionType == PositionType.Short)
{
trailingStop = p.TrailingStop;
if (Peaks[bar]!= WorkingPeak)
if (p.TrailingStop > Peaks[bar]) trailingStop = Peaks[bar];
if (!Bars.IsLastBarOfDay(bar)) ExitAtTrailingStop(bar+1,p,trailingStop,"ExitAtTrailingStop");
}
if (p.PositionType == PositionType.Long)
{
trailingStop = p.TrailingStop;
if (Troughs[bar]!= WorkingTrough)
if (p.TrailingStop < Troughs[bar]) trailingStop = Troughs[bar];
if (!Bars.IsLastBarOfDay(bar)) ExitAtTrailingStop(bar+1,p,trailingStop,"ExitAtTrailingStop");
}
}
}
if (Mode.Value != 1)
if (High[bar-1]>Peaks[bar-1])
{
WorkingPeak = Peaks[bar-1];
if (WorkingHigh<High[bar-1]) WorkingHigh=High[bar-1];
PeakSignalPrice = WorkingPeak - Step.Value;
SetShortOrderMode = true;
}
if ((SetShortOrderMode)&&(LastPeakSignalPrice != PeakSignalPrice))
{
Position p = null;
if (!Bars.IsLastBarOfDay(bar-1))
{
p = ShortAtStop(bar, PeakSignalPrice);
PeakSignalPriceSeries[bar] = PeakSignalPrice;
}
if (p != null)
{
SetShortOrderMode = false;
LastPeakSignalPrice = PeakSignalPrice;
trailingStop = p.EntryPrice + stoploss;
ExitAtTrailingStop(bar+1,p,trailingStop,"ExitAtTrailingStop");
}
}
if (Mode.Value != -1)
if (Low[bar-1]<Troughs[bar-1])
{
WorkingTrough = Troughs[bar-1];
if (WorkingLow>Low[bar-1]) WorkingLow=Low[bar-1];
TroughSignalPrice = WorkingTrough + Step.Value;
SetBuyOrderMode = true;
}
if ((SetBuyOrderMode)&&(LastTroughSignalPrice != TroughSignalPrice))
{
Position p = null;
if (!Bars.IsLastBarOfDay(bar-1))
{
p = BuyAtStop(bar, TroughSignalPrice);
TroughSignalPriceSeries[bar] = TroughSignalPrice;
}
if (p != null)
{
SetBuyOrderMode = false;
LastTroughSignalPrice = TroughSignalPrice;
trailingStop = p.EntryPrice - stoploss;
ExitAtTrailingStop(bar+1,p,trailingStop,"ExitAtTrailingStop");
}
}
}//for
PlotSeries( PricePane, Peaks, Color.Green, LineStyle.Dots, 3 );
PlotSeries( PricePane, Troughs, Color.Red, LineStyle.Dots, 3 );
PlotSeries( PricePane, PeakSignalPriceSeries, Color.Green, LineStyle.Solid, 1 );
PlotSeries( PricePane, TroughSignalPriceSeries, Color.Red, LineStyle.Solid, 1 );
}
}
}