Coding
// Error - comparing a double value with an object
close > HMA
you just refer to an object - an indicator instance https://protrader.org/ptmc-api/netscript/indicator . In your case, you have to specify it like:
// gets HMA's first line value with zero-bar offsetdouble
HMA_value = HMA.GetValue(0,0);
to get an indicator value GetValue() from HMA instance
Good luck!
Hello, Kolomiets!
I do not understand very well, because I am a beginner.
But, my idea was to create a simple EA for me to learn to program, my idea was if the candle close below the mean HMA then make a sale, and close it up then make a purchase. See if EA is correct now?
Should the closing of the previous candle be done by NextBar ()? How can I do it? Because he does not understand what he is. Thank you!
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using PTLRuntime.NETScript;
using PTLRuntime.NETScript.Indicators;
namespace RECO_RECO
{
///
/// RECO_RECO
///
///
public class RECO_RECO : NETStrategy
{
public RECO_RECO()
: base()
{
#region Initialization
base.Author = "Gustavo Rosso";
base.Comments = "";
base.Company = "";
base.Copyrights = "";
base.DateOfCreation = "21.07.2017";
base.ExpirationDate = 0;
base.Version = "1.0";
base.Password = "66b4a6416f59370e942d353f08a9ae36";
base.ProjectName = "RECO_RECO";
#endregion
}
[InputParameter("Média HMA", 0)]
public int hma = 50;
[InputParameter("Nº Lotes", 1, 1, 100, 1, 1)]
public double lots = 1;
[InputParameter("SL", 2, 0, 500)]
public double SL = 100;
[InputParameter("TP", 3, 0, 500)]
public double TP = 100;
[InputParameter("Magic number", 0, 1, 9999)]
public int MagicNumber = 13;
Indicator HMA;
///
/// This function will be called after creating
///
public override void Init()
{
HMA = Indicators.iCustom("HMA", CurrentData, hma, MAMode.LWMA,PriceType.Close);
}
///
/// Entry point. This function is called when new quote comes
///
public override void OnQuote()
{
double HMA_value = HMA.GetValue(0,1);
double close = CurrentData.GetPrice(PriceType.Close, 1);
string orderId;
if (Positions.Count < 1)
{
if (close > HMA_value)
{
NewOrderRequest request = new NewOrderRequest
{
Instrument = Instruments.Current,
Account = Accounts.Current,
MagicNumber = MagicNumber,
Type = OrdersType.Market,
Side = Operation.Buy,
Amount = lots,
TakeProfitOffset = TP * Point,
StopLossOffset = SL * Point,
Price = Instruments.Current.LastQuote.Ask
};
orderId = Orders.Send(request);
}
if (close < HMA_value)
{
NewOrderRequest request = new NewOrderRequest
{
Instrument = Instruments.Current,
Account = Accounts.Current,
MagicNumber = MagicNumber,
Type = OrdersType.Market,
Side = Operation.Sell,
Amount = lots,
TakeProfitOffset = TP * Point,
StopLossOffset = SL * Point,
Price = Instruments.Current.LastQuote.Bid
};
orderId = Orders.Send(request);
}
}
else
{
Position[] pos = Positions.GetPositions();
foreach (Position p in pos)
{
if (p.Instrument == Instruments.Current)
{
if (p.Side == Operation.Buy)
{
if (close < HMA_value)
{
p.Close();
}
}
if (p.Side == Operation.Sell)
{
if (close > HMA_value)
{
p.Close();
}
}
}
}
}
}
///
/// This function will be called before removing
///
public override void Complete()
{
}
}
}
I friendly recommend you to start you programming study with more simple examples (indicators, macros, custom columns) - strategy is an automated indicator(or them conjunction) tied by specific logic and which can trade. Thereby it has the highest level of script's difficulty ladder.
Sorry, your code isn't correct. My recommendations to make it work are next:
1. To check some signals on the crossing you have to check all start/end points for both lines/signals (it's a pure math question).
2. The crossing must be checked on next bar appearing. Then you will check already happened crossing (just logic question).
3. You have to define what reaction you want and after that - place trade logic in NextBar() or OnQuote() method (design question).
Following next:
"I do not understand very well, because I am a beginner...to learn to program...How can I do it? Because he does not understand what he is"
The working code will be some kind of disservice on your way. You will gain more practice and programming skills by exploring Holt's EMA EA example from our code base.
Good luck, Gustavo!
You are welcome!
Of course, I have it. Just click on my Name...Profile tab))Also, you are free to visit next sources to find some help:
Skype chat - PTMC C# API https://join.skype.com/mqyuzWNCdXSO
Custom script request - Developers list - https://protrader.org/forum/algostudio/custom-script-request/list-ptmc-script-developers